Commit 76cb82f4 authored by Janez's avatar Janez

Merge branch 'external-packages' of /home/git/repositories/kt/mothra

parents 8dea4323 e3dcc489
......@@ -43,7 +43,12 @@ INSTALLED_APPS_WORKFLOWS_SUB = (
#'workflows.vipercharts',
#'workflows.MUSE',
#'workflows.hbp',
)
)
INSTALLED_APPS_EXTERNAL_PACKAGES = (
#'rdm.db',
#'rdm.wrappers'
)
BROKER_URL = 'django://'
......
......@@ -178,6 +178,7 @@ INSTALLED_APPS_DEFAULT = (
)
INSTALLED_APPS_WORKFLOWS_SUB = ()
INSTALLED_APPS_EXTERNAL_PACKAGES = ()
TEST_RUNNER = 'discover_runner.DiscoverRunner'
......@@ -225,4 +226,5 @@ except NameError:
INSTALLED_APPS = \
INSTALLED_APPS_DEFAULT +\
INSTALLED_APPS_WORKFLOWS_SUB
INSTALLED_APPS_WORKFLOWS_SUB +\
INSTALLED_APPS_EXTERNAL_PACKAGES
......@@ -7,6 +7,7 @@ import uuid
import os
import sys
from django.conf import settings
from django.core.management.color import color_style
import json
def add_category(category,categories):
......@@ -71,6 +72,11 @@ def serialize_category(c):
return data
def export_package(package_name,writer):
style = color_style()
if package_name in settings.INSTALLED_APPS_EXTERNAL_PACKAGES:
raise CommandError("You cannot export external packages.")
if 'workflows.'+package_name not in settings.INSTALLED_APPS:
raise CommandError("Package not found in INSTALLED_APPS.")
......@@ -89,6 +95,7 @@ def export_package(package_name,writer):
package_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),'../../'+package_name+"/package_data/")
ensure_dir(package_directory)
widgets_directory = os.path.join(package_directory,"widgets")
deprecated_widgets_directory = os.path.join(package_directory,"deprecated_widgets")
ensure_dir(widgets_directory)
categories_directory = os.path.join(package_directory,"categories")
ensure_dir(categories_directory)
......@@ -102,6 +109,9 @@ def export_package(package_name,writer):
for aw in aws:
aw.update_uid()
if os.path.isfile(os.path.join(deprecated_widgets_directory,aw.uid+'.json')):
writer.write(style.ERROR(" - Deprecated widget "+str(aw)+" found! Please import package to remove it. This widget has NOT been exported.\n"))
continue
add_category(aw.category,categories)
serialized_widget = serialize_widget(aw)
......
......@@ -16,11 +16,19 @@ class Command(BaseCommand):
def handle(self, *args, **options):
packages = []
extern_packages = []
for app in settings.INSTALLED_APPS:
if 'workflows.' in app:
packages.append(app)
package_name = app.split('workflows.')[1]
packages.append(package_name)
elif app in settings.INSTALLED_APPS_EXTERNAL_PACKAGES:
print app
extern_packages.append(app)
for package in packages:
package_name = package.split('workflows.')[1]
self.stdout.write("Importing package "+package_name+"\n")
import_package(package_name,self.stdout)
\ No newline at end of file
self.stdout.write("Importing package "+package+"\n")
import_package(package,self.stdout)
for package in extern_packages:
self.stdout.write("Importing external package "+package+"\n")
import_package(package,self.stdout, external=True)
from unicodedata import category
from django.core.management.base import BaseCommand, CommandError
from workflows.models import Category, AbstractWidget, AbstractInput, AbstractOutput, AbstractOption
from workflows.models import Category, AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Widget
from django.core import serializers
from optparse import make_option
import uuid
import os
import sys
import inspect
from django.conf import settings
import json
from .export_package import serialize_category, serialize_widget
from django.core.management.color import color_style
def parsewidgetdata(widget_data):
widget = None
......@@ -28,14 +30,27 @@ def parsewidgetdata(widget_data):
raise CommandError("Wrong data in widget files!")
return widget, inputs, outputs, options
def import_package(package_name,writer):
package_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),'../../'+package_name+"/package_data/")
def import_package(package_name,writer,external=False):
style = color_style()
if external:
module = __import__(package_name)
if '.' in package_name:
cf_module = package_name.split('.')[-1]
module = getattr(module, cf_module)
package_directory = os.path.join(os.path.dirname(inspect.getfile(module)), 'package_data/')
else:
package_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),'../../'+package_name+"/package_data/")
widgets_directory = os.path.join(package_directory,"widgets")
deprecated_widgets_directory = os.path.join(package_directory,"deprecated_widgets")
categories_directory = os.path.join(package_directory,"categories")
if not os.path.exists(package_directory) or not os.path.exists(widgets_directory) or not os.path.exists(categories_directory):
raise CommandError("Cannot find package data. Are you sure this package has been exported already?")
try:
deprecated_widgets_files = os.listdir(deprecated_widgets_directory)
except:
deprecated_widgets_files = []
widget_files = os.listdir(widgets_directory)
category_files = os.listdir(categories_directory)
......@@ -153,6 +168,24 @@ def import_package(package_name,writer):
stale_os.delete()
writer.write(" - Removing stale options\n")
if deprecated_widgets_files:
for deprecated_widget_file in deprecated_widgets_files:
wfilepath = os.path.join(deprecated_widgets_directory,deprecated_widget_file)
w_file = open(wfilepath,'r')
w_data = json.loads(w_file.read())
w_file.close()
widget, inputs, outputs, options = parsewidgetdata(w_data)
created = False
try:
aw = AbstractWidget.objects.get(uid=widget['fields']['uid'],package=package_name)
if Widget.objects.filter(abstract_widget=aw).count()==0:
writer.write(' - Removing widget '+str(widget['fields']['name'])+'\n')
aw.delete()
else:
writer.write(style.ERROR(' - The widget '+str(widget['fields']['name'])+' is still used in workflows. It was not removed, but it is deprecated!\n'))
except AbstractWidget.DoesNotExist:
pass
if not global_change:
#writer.write(" No changes detected in the widgets.\n")
pass
......@@ -168,12 +201,13 @@ class Command(BaseCommand):
raise CommandError('Argument "package_name" is required.')
package_name = args[0]
external = package_name in settings.INSTALLED_APPS_EXTERNAL_PACKAGES
if 'workflows.'+package_name not in settings.INSTALLED_APPS:
if 'workflows.'+package_name not in settings.INSTALLED_APPS and not external:
raise CommandError("Package not found in INSTALLED_APPS.")
writer = self.stdout
import_package(package_name,writer)
import_package(package_name,writer,external=external)
writer.write('Thanks for using the new import command. You rock.\n')
from mothra.settings import INSTALLED_APPS
from mothra.settings import INSTALLED_APPS, INSTALLED_APPS_EXTERNAL_PACKAGES
appName = 'workflows'
def get_installed_apps():
return list(get_local_installed_apps()) + list(get_extern_installed_apps())
def get_local_installed_apps():
return [name[len(appName)+1:] for name in INSTALLED_APPS if name.startswith(appName+'.') and len(name)>len(appName)+1]
def get_extern_installed_apps():
return INSTALLED_APPS_EXTERNAL_PACKAGES
#Following functions deal with imports of libraries as dicts
def import_all_packages_libs_as_dict(libName):
pckLibs = {}
......@@ -13,7 +19,10 @@ def import_all_packages_libs_as_dict(libName):
return pckLibs
def import_package_lib_as_dict(packageName, libName):
return dynamic_import_globals_as_dict(appName+"."+packageName + "." + libName, packageName)
if packageName in get_local_installed_apps():
return dynamic_import_globals_as_dict(appName+"."+packageName + "." + libName, packageName)
else:
return dynamic_import_globals_as_dict(packageName + "." + libName, packageName)
def dynamic_import_globals_as_dict(name, package):
try:
......@@ -30,7 +39,11 @@ def import_all_packages_libs(libName, localSetAttrFunc):
import_package_lib(pck, libName, localSetAttrFunc)
def import_package_lib(packageName, libName, localSetAttrFunc):
dynamic_import_globals(appName+"."+packageName + "." + libName, packageName, localSetAttrFunc)
if packageName in get_local_installed_apps():
dynamic_import_globals(appName+"."+packageName + "." + libName, packageName, localSetAttrFunc)
else:
# External CF package
dynamic_import_globals(packageName + "." + libName, packageName, localSetAttrFunc)
def dynamic_import_globals(name, package, localSetAttrFunc):
m = None
......@@ -49,5 +62,4 @@ def dynamic_import_globals(name, package, localSetAttrFunc):
g = globals()
for name in all_names:
#g[name] = m.__dict__.get(name)
#print name
localSetAttrFunc(name, m.__dict__.get(name), package)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment