コード例 #1
0
ファイル: org_migrator.py プロジェクト: candlepin/candlepin
def main():
    (options, args) = parse_options()

    if options.debug:
        log.setLevel(logging.DEBUG)
        cp.set_log_level(logging.DEBUG)

    if options.trace:
        log.setLevel(LOGLVL_TRACE)
        cp.set_log_level(LOGLVL_TRACE)

    with cp.get_db_connector(options.dbtype)(options.host, options.port, options.username, options.password, options.db) as db:
        log.info('Using database: %s @ %s', db.backend(), options.host)

        if options.act_import:
            # Use the provided argument if the --file is still default
            if options.file == 'export.zip' and len(args) > 0:
                options.file = args[0]

            # Verify the file exists (as a file) and can be read so we can kick out a cleaner
            # error message than a spooky exception
            if not os.access(options.file, os.R_OK) or not os.path.isfile(options.file):
                log.error("File does not exist or cannot be read: %s", options.file)
                return

            importer = OrgImporter(db, options.file, None, options.ignore_dupes)

            log.info('Importing data from file: %s', options.file)
            with (db.start_transaction(readonly=False)) as transaction:
                if importer.execute():
                    transaction.commit()
                    log.info('Import from file \'%s\' completed successfully', options.file)
                else:
                    transaction.rollback()
                    log.error("Import task failed.")

        elif options.act_export:
            org_key = args[0]
            org_id = resolve_org(db, org_key)

            if org_id is not None:
                log.info('Resolved org "%s" to org ID: %s', org_key, org_id)

                exporter = OrgExporter(db, options.file, org_id, False)

                log.info('Exporting data to file: %s', options.file)
                with(db.start_transaction(readonly=True)) as transaction:
                    if exporter.execute():
                        transaction.commit()
                        log.info('Data for organization \'%s\' successfully exported to file: %s', org_key, options.file)
                    else:
                        transaction.rollback()
                        log.error("Export task failed.")
            else:
                log.error("Export failed; No such org: %s", org_key)

        elif options.act_list:
            print("Available orgs: %s" % (list_orgs(db)))
コード例 #2
0
ファイル: org_migrator.py プロジェクト: kahowell/candlepin
def main():
    (options, args) = parse_options()

    if options.debug:
        log.setLevel(logging.DEBUG)
        cp.set_log_level(logging.DEBUG)

    db = None

    try:
        db = cp.get_db_connector(options.dbtype, options.host, options.port,
                                 options.username, options.password,
                                 options.db)
        log.info('Using database: %s @ %s', db.backend(), options.host)

        if options.act_import:
            # Use the provided argument if the --file is still default
            if options.file == 'export.zip' and len(args) > 0:
                options.file = args[0]

            # Verify the file exists (as a file) and can be read so we can kick out a cleaner
            # error message than a spooky exception
            if not os.access(options.file, os.R_OK) or not os.path.isfile(
                    options.file):
                log.error("File does not exist or cannot be read: %s",
                          options.file)
                return

            importer = OrgImporter(db, options.file)

            log.info('Importing data from file: %s', options.file)
            with (db.start_transaction(readonly=False)) as transaction:
                if importer.execute():
                    transaction.commit()
                    log.info('Task complete! Shutting down...')
                else:
                    transaction.rollback()
                    log.error("Import task failed. Shutting down...")

        elif options.act_export:
            org_id = resolve_org(db, args[0])

            if org_id is not None:
                log.info('Resolved org "%s" to org ID: %s', args[0], org_id)

                exporter = OrgExporter(db, options.file, org_id)

                log.info('Exporting data to file: %s', options.file)
                with (db.start_transaction(readonly=True)) as transaction:
                    if exporter.execute():
                        transaction.commit()
                        log.info('Task complete! Shutting down...')
                    else:
                        transaction.rollback()
                        log.error("Import task failed. Shutting down...")
            else:
                log.error("No such org: %s", args[0])

        elif options.act_list:
            print "Available orgs: %s" % (list_orgs(db))

    finally:
        if db is not None:
            db.close()