Пример #1
0
    if args.label_folder == '':
        args.label_folder = None

    if args.label_folder is not None and not args.label_folder.endswith(
            os.sep):
        args.label_folder += os.sep

    currentDT = datetime.datetime.now()
    currentDT = '{}-{}-{} {}:{}:{}'.format(currentDT.year, currentDT.month,
                                           currentDT.day, currentDT.hour,
                                           currentDT.minute, currentDT.second)

    config = Config()
    dbConn = Database(config)
    if not dbConn.canConnect():
        raise Exception('Error connecting to database.')

    # check if running on file server
    imgBaseDir = config.getProperty('FileServer', 'staticfiles_dir')
    if not os.path.isdir(imgBaseDir):
        raise Exception(
            '"{}" is not a valid directory on this machine. Are you running the script from the file server?'
            .format(imgBaseDir))

    if args.label_folder is not None and not os.path.isdir(args.label_folder):
        raise Exception(
            '"{}" is not a valid directory on this machine.'.format(
                args.label_folder))

    if not imgBaseDir.endswith(os.sep):
Пример #2
0
def migrate_aide(forceMigrate=False):
    from modules import Database, UserHandling
    from util.configDef import Config
    
    config = Config()
    dbConn = Database(config)
    if not dbConn.canConnect():
        raise Exception('Error connecting to database.')
    
    warnings = []
    errors = []

    # skip if not forced and if database has same version
    doMigrate = True
    
    # check if DB has version already implemented
    dbVersion = None
    hasVersion = dbConn.execute('''
        SELECT EXISTS (
            SELECT FROM information_schema.tables 
            WHERE  table_schema = 'aide_admin'
            AND    table_name   = 'version'
        ) AS hasVersion;
    ''', None, 1)
    if hasVersion[0]['hasversion']:
        # check DB version
        dbVersion = dbConn.execute('SELECT version FROM aide_admin.version;', None, 1)
        if dbVersion is not None and len(dbVersion):
            dbVersion = dbVersion[0]['version']
            needsUpdate = version.compare_versions(version.AIDE_VERSION, dbVersion)
            if needsUpdate is not None:
                if needsUpdate < 0:
                    # running an older version of AIDE with a newer DB version
                    warnings.append(f'WARNING: local AIDE version ({version.AIDE_VERSION}) is older than the one in the database ({dbVersion}); please update your installation.')
                elif needsUpdate == 0:
                    doMigrate = False
                else:
                    doMigrate = True

    if not doMigrate and not forceMigrate:
        return warnings, errors

    # bring all projects up-to-date (if registered within AIDE)
    projects = dbConn.execute('SELECT shortname FROM aide_admin.project;', None, 'all')
    if projects is not None and len(projects):

        # get all schemata and check if project still exists
        schemata = dbConn.execute('SELECT schema_name FROM information_schema.schemata', None, 'all')
        if schemata is not None and len(schemata):
            schemata = set([s['schema_name'].lower() for s in schemata])
            for p in projects:
                try:
                    pName = p['shortname']

                    # check if project still exists
                    if not pName.lower() in schemata:
                        warnings.append(f'WARNING: project "{pName}" is registered but does not exist in database.')
                        #TODO: option to auto-remove?
                        continue

                    # special modification for CNN-to-labelclass map: drop only dep. on version (remove ancient tests)
                    if version.compare_versions(version.AIDE_VERSION, dbVersion) in (-1, None):
                        dbConn.execute(sql.SQL('DROP TABLE IF EXISTS {};').format(
                            sql.Identifier(pName, 'cnn_labelclass')
                        ), None)

                    # make modifications one at a time
                    for mod in MODIFICATIONS_sql:
                        dbConn.execute(mod.format(schema=pName), None, None)

                    # pre-official 2.0: mark existing CNN states as "labelclass_autoupdate" (as this was the default behavior)
                    if version.compare_versions(dbVersion, '2.0.210514') == -1:
                        dbConn.execute(sql.SQL('''
                            UPDATE {}
                            SET labelclass_autoupdate = TRUE;
                        ''').format(sql.Identifier(pName, 'cnnstate')), None)

                except Exception as e:
                    errors.append(str(e))
        else:
            warnings.append('WARNING: no project schemata found within database.')
    else:
        warnings.append('WARNING: no project registered within AIDE.')

    # update DB version accordingly
    dbConn.execute('''
        DELETE FROM aide_admin.version;
        INSERT INTO aide_admin.version (version)
        VALUES (%s);
    ''', (version.AIDE_VERSION, ))

    return warnings, errors