Commit 85c8b347 authored by matjaz's avatar matjaz

Command to create new package from template packet.

parent 5a73c18c
from django.core.management.base import BaseCommand, CommandError
from optparse import make_option
import uuid
from distutils import dir_util
import os
import sys
from mothra.settings import PROJECT_DIR
class Command(BaseCommand):
args = 'new_package_name'
help = 'Creates new package based on package_template. It generates new uids for template database objects, renames functions, renames files and registers package in INSTALLED_APPS.'
def handle(self, *args, **options):
if (len(args) < 1):
raise CommandError('Argument "new_package_name" is required')
self.stdout.write('Starting package initiation from template.\n')
if (int(options['verbosity'])<2):
self.stdout.write('Tip: use higher verbosity option number to see what is going on in detail.\n')
new_package_from_template(self.stdout.write, args[0], int(options['verbosity']))
self.stdout.write('Creating new package successfully finished. You might want to use import_package command now.\n')
def replace_in_files(files, topPath, fromStr, toStr, verbosity, writeFunc):
if verbosity > 1:
writeFunc("Replacing '%s' with '%s' in:\n" % (fromStr, toStr))
for f in files:
content = open(f, 'r').read()
contentNew = content.replace(fromStr, toStr)
if content != contentNew:
open(f, 'w').write(contentNew)
if verbosity > 2:
writeFunc(" .%s\n" % (f[len(topPath):],))
def new_package_from_template(writeFunc, package, verbosity):
pckTmpName = "package_template"
pckTmpTitle = "Package Template"
pckTmpPrefix = "pcktmp_"
pckTmpUid = "uid_to_replace"
pckPrefix = package + "_"
subappPlaceholder = " #WORKFLOWS_SUBAPP_PLACEHOLDER"
subapp = " \'workflows.%s\',\n"%package
settFile = os.path.join(PROJECT_DIR,'settings.py')
wfDir = os.path.abspath(os.path.join(PROJECT_DIR,"..","workflows"))
templateDir = os.path.join(wfDir,pckTmpName)
packageDir = os.path.join(wfDir, package)
if os.path.exists(packageDir):
raise Exception('Directory "%s" for package of given name already exists! Terminating procedure of creating new package.'%packageDir)
copied = dir_util.copy_tree(templateDir, packageDir)
if verbosity>1:
writeFunc("Creating objects:\n")
for f in copied:
if verbosity>2:
writeFunc(" .%s\n"%f[len(wfDir):])
replace_in_files(copied, wfDir, pckTmpPrefix, pckPrefix, verbosity, writeFunc)
replace_in_files(copied, wfDir, pckTmpTitle, package[:1].upper()+package[1:], verbosity, writeFunc)
replace_in_files(copied, wfDir, pckTmpName, package, verbosity, writeFunc)
if verbosity>1:
writeFunc("Replacing temporary uids in:\n")
for f in copied:
content = open(f, 'r').read()
contentNew = content
while contentNew.count(pckTmpUid)>0:
contentNew = contentNew.replace(pckTmpUid, str(uuid.uuid4()), 1)
if content != contentNew:
open(f, 'w').write(contentNew)
if verbosity>2:
writeFunc(" .%s\n"%(f[len(wfDir):],))
if verbosity>1:
writeFunc("Renaming files:\n")
for f in copied:
if f[len(wfDir):].count(pckTmpPrefix)>0:
fNew = f.replace(pckTmpPrefix, pckPrefix)
os.rename(f,fNew)
if verbosity>2:
writeFunc(" .%s => .%s\n"%(f[len(wfDir):],fNew[len(wfDir):]))
if verbosity>1:
writeFunc("Renaming directories:\n")
for f in [x[0] for x in os.walk(packageDir)]:
if f.endswith(pckTmpName):
fNew = f.replace(pckTmpName, package)
os.rename(f,fNew)
if verbosity>2:
writeFunc(" .%s => .%s\n"%(f[len(wfDir):],fNew[len(wfDir):]))
if verbosity>1:
writeFunc("Adding package to INSTALLED_APPS variable in .%s\n"%settFile[len(PROJECT_DIR):])
content = open(settFile, 'r').read()
place = content.find(subappPlaceholder)
contentNew = content[:place] + subapp + content[place:]
open(settFile, 'w').write(contentNew)
\ No newline at end of file
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
"pk": 20, "pk": 20,
"model": "workflows.category", "model": "workflows.category",
"fields": { "fields": {
"uid": "to_replace_0", "uid": "uid_to_replace",
"parent": null, "parent": null,
"workflow": null, "workflow": null,
"user": null, "user": null,
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
"treeview_image": "", "treeview_image": "",
"name": "Create Integer List", "name": "Create Integer List",
"is_streaming": false, "is_streaming": false,
"uid": "to_replace_1", "uid": "uid_to_replace",
"interaction_view": "", "interaction_view": "",
"image": "", "image": "",
"package": "package_template", "package": "package_template",
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
"widget": 98, "widget": 98,
"name": "Integer List String", "name": "Integer List String",
"short_name": "str", "short_name": "str",
"uid": "to_replace_1_1", "uid": "uid_to_replace",
"default": "3\r\n2\r\n1\r\n4", "default": "3\r\n2\r\n1\r\n4",
"required": false, "required": false,
"multi": false, "multi": false,
...@@ -61,7 +61,7 @@ ...@@ -61,7 +61,7 @@
"widget": 98, "widget": 98,
"name": "Sort list", "name": "Sort list",
"short_name": "bol", "short_name": "bol",
"uid": "to_replace_1_2", "uid": "uid_to_replace",
"default": "true", "default": "true",
"required": true, "required": true,
"multi": false, "multi": false,
...@@ -80,7 +80,7 @@ ...@@ -80,7 +80,7 @@
"name": "Integer List", "name": "Integer List",
"short_name": "lst", "short_name": "lst",
"variable": "intList", "variable": "intList",
"uid": "to_replace_1_2", "uid": "uid_to_replace",
"order": 1, "order": 1,
"description": "List of integers" "description": "List of integers"
} }
...@@ -93,7 +93,7 @@ ...@@ -93,7 +93,7 @@
"treeview_image": "", "treeview_image": "",
"name": "Filter Integers", "name": "Filter Integers",
"is_streaming": false, "is_streaming": false,
"uid": "to_replace_2", "uid": "uid_to_replace",
"interaction_view": "pcktmp_filter_integers", "interaction_view": "pcktmp_filter_integers",
"image": "", "image": "",
"package": "package_template", "package": "package_template",
...@@ -117,7 +117,7 @@ ...@@ -117,7 +117,7 @@
"widget": 101, "widget": 101,
"name": "Integer List", "name": "Integer List",
"short_name": "lst", "short_name": "lst",
"uid": "to_replace_2_1", "uid": "uid_to_replace",
"default": "", "default": "",
"required": true, "required": true,
"multi": false, "multi": false,
...@@ -136,7 +136,7 @@ ...@@ -136,7 +136,7 @@
"name": "Filtered Integer List", "name": "Filtered Integer List",
"short_name": "lst", "short_name": "lst",
"variable": "intList", "variable": "intList",
"uid": "to_replace_2_2", "uid": "uid_to_replace",
"order": 1, "order": 1,
"description": "Filtered list of integers" "description": "Filtered list of integers"
} }
...@@ -149,7 +149,7 @@ ...@@ -149,7 +149,7 @@
"treeview_image": "", "treeview_image": "",
"name": "Sum Integers", "name": "Sum Integers",
"is_streaming": false, "is_streaming": false,
"uid": "to_replace_3", "uid": "uid_to_replace",
"interaction_view": "", "interaction_view": "",
"image": "", "image": "",
"package": "package_template", "package": "package_template",
...@@ -173,7 +173,7 @@ ...@@ -173,7 +173,7 @@
"widget": 99, "widget": 99,
"name": "Integer List", "name": "Integer List",
"short_name": "lst", "short_name": "lst",
"uid": "to_replace_3_1", "uid": "uid_to_replace",
"default": "", "default": "",
"required": true, "required": true,
"multi": false, "multi": false,
...@@ -192,7 +192,7 @@ ...@@ -192,7 +192,7 @@
"name": "Sum", "name": "Sum",
"short_name": "int", "short_name": "int",
"variable": "sum", "variable": "sum",
"uid": "to_replace_3_2", "uid": "uid_to_replace",
"order": 1, "order": 1,
"description": "Sum of integer list" "description": "Sum of integer list"
} }
...@@ -205,7 +205,7 @@ ...@@ -205,7 +205,7 @@
"treeview_image": "", "treeview_image": "",
"name": "Display Summation", "name": "Display Summation",
"is_streaming": false, "is_streaming": false,
"uid": "to_replace_4", "uid": "uid_to_replace",
"interaction_view": "", "interaction_view": "",
"image": "", "image": "",
"package": "package_template", "package": "package_template",
...@@ -229,7 +229,7 @@ ...@@ -229,7 +229,7 @@
"widget": 100, "widget": 100,
"name": "Integer List", "name": "Integer List",
"short_name": "lst", "short_name": "lst",
"uid": "to_replace_4_1", "uid": "uid_to_replace",
"default": "", "default": "",
"required": false, "required": false,
"multi": false, "multi": false,
...@@ -247,7 +247,7 @@ ...@@ -247,7 +247,7 @@
"widget": 100, "widget": 100,
"name": "Sum", "name": "Sum",
"short_name": "int", "short_name": "int",
"uid": "to_replace_4_2", "uid": "uid_to_replace",
"default": "", "default": "",
"required": false, "required": false,
"multi": false, "multi": false,
......
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