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 = ( ...@@ -43,7 +43,12 @@ INSTALLED_APPS_WORKFLOWS_SUB = (
#'workflows.vipercharts', #'workflows.vipercharts',
#'workflows.MUSE', #'workflows.MUSE',
#'workflows.hbp', #'workflows.hbp',
) )
INSTALLED_APPS_EXTERNAL_PACKAGES = (
#'rdm.db',
#'rdm.wrappers'
)
BROKER_URL = 'django://' BROKER_URL = 'django://'
......
...@@ -178,6 +178,7 @@ INSTALLED_APPS_DEFAULT = ( ...@@ -178,6 +178,7 @@ INSTALLED_APPS_DEFAULT = (
) )
INSTALLED_APPS_WORKFLOWS_SUB = () INSTALLED_APPS_WORKFLOWS_SUB = ()
INSTALLED_APPS_EXTERNAL_PACKAGES = ()
TEST_RUNNER = 'discover_runner.DiscoverRunner' TEST_RUNNER = 'discover_runner.DiscoverRunner'
...@@ -225,4 +226,5 @@ except NameError: ...@@ -225,4 +226,5 @@ except NameError:
INSTALLED_APPS = \ INSTALLED_APPS = \
INSTALLED_APPS_DEFAULT +\ INSTALLED_APPS_DEFAULT +\
INSTALLED_APPS_WORKFLOWS_SUB INSTALLED_APPS_WORKFLOWS_SUB +\
INSTALLED_APPS_EXTERNAL_PACKAGES
...@@ -7,6 +7,7 @@ import uuid ...@@ -7,6 +7,7 @@ import uuid
import os import os
import sys import sys
from django.conf import settings from django.conf import settings
from django.core.management.color import color_style
import json import json
def add_category(category,categories): def add_category(category,categories):
...@@ -71,6 +72,11 @@ def serialize_category(c): ...@@ -71,6 +72,11 @@ def serialize_category(c):
return data return data
def export_package(package_name,writer): 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: if 'workflows.'+package_name not in settings.INSTALLED_APPS:
raise CommandError("Package not found in INSTALLED_APPS.") raise CommandError("Package not found in INSTALLED_APPS.")
...@@ -89,6 +95,7 @@ def export_package(package_name,writer): ...@@ -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/") package_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),'../../'+package_name+"/package_data/")
ensure_dir(package_directory) ensure_dir(package_directory)
widgets_directory = os.path.join(package_directory,"widgets") widgets_directory = os.path.join(package_directory,"widgets")
deprecated_widgets_directory = os.path.join(package_directory,"deprecated_widgets")
ensure_dir(widgets_directory) ensure_dir(widgets_directory)
categories_directory = os.path.join(package_directory,"categories") categories_directory = os.path.join(package_directory,"categories")
ensure_dir(categories_directory) ensure_dir(categories_directory)
...@@ -102,6 +109,9 @@ def export_package(package_name,writer): ...@@ -102,6 +109,9 @@ def export_package(package_name,writer):
for aw in aws: for aw in aws:
aw.update_uid() 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) add_category(aw.category,categories)
serialized_widget = serialize_widget(aw) serialized_widget = serialize_widget(aw)
......
...@@ -16,11 +16,19 @@ class Command(BaseCommand): ...@@ -16,11 +16,19 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
packages = [] packages = []
extern_packages = []
for app in settings.INSTALLED_APPS: for app in settings.INSTALLED_APPS:
if 'workflows.' in app: 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: for package in packages:
package_name = package.split('workflows.')[1] self.stdout.write("Importing package "+package+"\n")
self.stdout.write("Importing package "+package_name+"\n") import_package(package,self.stdout)
import_package(package_name,self.stdout)
\ No newline at end of file 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 unicodedata import category
from django.core.management.base import BaseCommand, CommandError 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 django.core import serializers
from optparse import make_option from optparse import make_option
import uuid import uuid
import os import os
import sys import sys
import inspect
from django.conf import settings from django.conf import settings
import json import json
from .export_package import serialize_category, serialize_widget from .export_package import serialize_category, serialize_widget
from django.core.management.color import color_style
def parsewidgetdata(widget_data): def parsewidgetdata(widget_data):
widget = None widget = None
...@@ -28,14 +30,27 @@ def parsewidgetdata(widget_data): ...@@ -28,14 +30,27 @@ def parsewidgetdata(widget_data):
raise CommandError("Wrong data in widget files!") raise CommandError("Wrong data in widget files!")
return widget, inputs, outputs, options return widget, inputs, outputs, options
def import_package(package_name,writer): def import_package(package_name,writer,external=False):
package_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),'../../'+package_name+"/package_data/") 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") 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") 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): 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?") 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) widget_files = os.listdir(widgets_directory)
category_files = os.listdir(categories_directory) category_files = os.listdir(categories_directory)
...@@ -153,6 +168,24 @@ def import_package(package_name,writer): ...@@ -153,6 +168,24 @@ def import_package(package_name,writer):
stale_os.delete() stale_os.delete()
writer.write(" - Removing stale options\n") 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: if not global_change:
#writer.write(" No changes detected in the widgets.\n") #writer.write(" No changes detected in the widgets.\n")
pass pass
...@@ -168,12 +201,13 @@ class Command(BaseCommand): ...@@ -168,12 +201,13 @@ class Command(BaseCommand):
raise CommandError('Argument "package_name" is required.') raise CommandError('Argument "package_name" is required.')
package_name = args[0] 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.") raise CommandError("Package not found in INSTALLED_APPS.")
writer = self.stdout 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') 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' appName = 'workflows'
def get_installed_apps(): 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] 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 #Following functions deal with imports of libraries as dicts
def import_all_packages_libs_as_dict(libName): def import_all_packages_libs_as_dict(libName):
pckLibs = {} pckLibs = {}
...@@ -13,7 +19,10 @@ def import_all_packages_libs_as_dict(libName): ...@@ -13,7 +19,10 @@ def import_all_packages_libs_as_dict(libName):
return pckLibs return pckLibs
def import_package_lib_as_dict(packageName, libName): 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): def dynamic_import_globals_as_dict(name, package):
try: try:
...@@ -30,7 +39,11 @@ def import_all_packages_libs(libName, localSetAttrFunc): ...@@ -30,7 +39,11 @@ def import_all_packages_libs(libName, localSetAttrFunc):
import_package_lib(pck, libName, localSetAttrFunc) import_package_lib(pck, libName, localSetAttrFunc)
def import_package_lib(packageName, 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): def dynamic_import_globals(name, package, localSetAttrFunc):
m = None m = None
...@@ -49,5 +62,4 @@ def dynamic_import_globals(name, package, localSetAttrFunc): ...@@ -49,5 +62,4 @@ def dynamic_import_globals(name, package, localSetAttrFunc):
g = globals() g = globals()
for name in all_names: for name in all_names:
#g[name] = m.__dict__.get(name) #g[name] = m.__dict__.get(name)
#print name
localSetAttrFunc(name, m.__dict__.get(name), package) 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