auto_import_packages.py 4.63 KB
Newer Older
matjaz's avatar
matjaz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
from datetime import datetime
import os
from django.core.management.base import BaseCommand, CommandError
from workflows import module_importer
from workflows.management.commands import export_package
from workflows.management.commands import import_package
from optparse import make_option

class Command(BaseCommand):
    args = 'file_name'
    help = 'Automatically iterates through all installed workflows sub-applications/projects/packages and imports their database entires. ' \
           'Note: Installed workflows packages are defined in mothra/settings.py via variable INSTALLED_APPS and begin with the string "workflows.". ' \
           'Auto import procedure does the following:\n' \
           '    - Creates database export of all definition objects using export_package command.\n'\
           '    - Export file goes to folder specified in mothra/settings.py/BACKUP_DIR and is timestamped\n'\
           '    For each installed package:\n' \
           '    - Loads package settings from "workflows/<package_name>/settings.py\n' \
           '    - If settings do not exist or settings.py/AUTO_IMPORT_DB == False then exit\n' \
           '    - Else tries to import all the files specified in settings.py/AUTO_IMPORT_DB_FILES list\n' \
           '    - If some files are missing skip them.\n' \
           '    - Imports are done using import_package command using -r option if settings.py/AUTO_IMPORT_DB_REPLACE_OPTION == True'

    option_list = BaseCommand.option_list + (
        make_option('-n', '--nobackup',
            action="store_true",
            dest='nobackup',
            default=False,
            help='No backup is created prior starting the import process.'
        ),
    )

    def handle(self, *args, **options):
        auto_import_all_packages(self.stdout.write, options['nobackup'])
        self.stdout.write('Auto import procedure finished.\n')

def auto_import_all_packages(writeFunc, nobackup):
    if nobackup:
        writeFunc('No backup will be created due to "--nobackup" option.\n')
    else:
        try:
            from mothra.settings import BACKUP_DIR
        except:
            raise CommandError('Do not know where to backup existing database: BACKUP_DIR variable not found in mothra/settings.py. Consider using "--nobackup" option.')
        if not os.path.exists(BACKUP_DIR): os.makedirs(BACKUP_DIR)
        timeStamp = datetime.now().strftime('_%Y%m%d_%H%M%S.json')
        backupDir = os.path.join(BACKUP_DIR,"db_backup"+timeStamp)

        writeFunc('Exporting to backup...\n')
        result = export_package.export_package_string(lambda text: writeFunc('    '+text), ('all',), False, True, 1)
        try:
            f = open(backupDir, 'w')
            f.write(result.encode('utf-8'))
            f.close()
            writeFunc('Backup successfully written.\n')
        except Exception as e:
            raise CommandError('There was a problem with writing to the given backup file "%s". Problem: %s'%(backupDir, e))
        writeFunc('Export procedure successfully finished. Results written to the file "%s".\n' %backupDir)

    #get all relevant package settings:
    packageSetts = module_importer.import_all_packages_libs_as_dict("settings")
    for pckSett in packageSetts:
        writeFunc('--------------------------------------------------------------------------------\n')
63
        writeFunc('Auto importing package "%s":\n'%pckSett)
matjaz's avatar
matjaz committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

        sett = packageSetts[pckSett]
        if sett is None:
            writeFunc('    No settings found for this package.\n')
            continue

        try:
            imp = sett.AUTO_IMPORT_DB
            files = sett.AUTO_IMPORT_DB_FILES
        except:
            writeFunc('    Either AUTO_IMPORT_DB or AUTO_IMPORT_DB_FILES not found in package\'s settings.\n')
            continue

        replace = False
        try:
            replace = sett.AUTO_IMPORT_DB_REPLACE_OPTION
        except:
            pass

        if not imp:
            writeFunc('    AUTO_IMPORT_DB set to false in package\'s settings.\n')
            continue

        for fileName in files:
            writeFunc('   Importing file "%s":\n' % fileName)
            try:
                fileContent = open(fileName, 'r').read()
            except:
                writeFunc('        Cannot open or read given package data file.\n')
            else:
                import_package.import_package_string(lambda text: writeFunc('        '+text), fileContent, replace)
            writeFunc('   Done with file "%s":\n' % fileName)

    writeFunc('--------------------------------------------------------------------------------\n')
    return