Пример #1
0
def cmd_upgrade(upgrader=None):
    """Command for applying upgrades."""
    from flask import current_app
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user

    logfilename = os.path.join(current_app.config['CFG_LOGDIR'],
                               'invenio_upgrader.log')
    if not upgrader:
        upgrader = InvenioUpgrader()
    logger = upgrader.get_logger(logfilename=logfilename)

    try:
        upgrades = upgrader.get_upgrades()

        if not upgrades:
            logger.info("All upgrades have been applied.")
            return

        logger.info("Following upgrade(s) will be applied:")

        for u in upgrades:
            title = u['__doc__']
            if title:
                logger.info(" * %s (%s)" % (u['id'], title))
            else:
                logger.info(" * %s" % u['id'])

        logger.info("Running pre-upgrade checks...")
        upgrader.pre_upgrade_checks(upgrades)

        logger.info("Calculating estimated upgrade time...")
        estimate = upgrader.human_estimate(upgrades)

        wait_for_user(wrap_text_in_a_box(
            "WARNING: You are going to upgrade your installation "
            "(estimated time: %s)!" % estimate))

        for u in upgrades:
            title = u['__doc__']
            if title:
                logger.info("Applying %s (%s)" % (u['id'], title))
            else:
                logger.info("Applying %s" % u['id'])
            upgrader.apply_upgrade(u)

        logger.info("Running post-upgrade checks...")
        upgrader.post_upgrade_checks(upgrades)

        if upgrader.has_warnings():
            logger.warning("Upgrade completed with %s warnings - please check "
                           "log-file for further information:\nless %s"
                           % (upgrader.get_warnings_count(), logfilename))
        else:
            logger.info("Upgrade completed successfully.")
    except RuntimeError as e:
        for msg in e.args:
            logger.error(unicode(msg))
        logger.info("Please check log file for further information:\n"
                    "less %s" % logfilename)
        sys.exit(1)
Пример #2
0
def cmd_upgrade(upgrader=None):
    """Command for applying upgrades."""
    from invenio.config import CFG_LOGDIR
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user

    logfilename = os.path.join(CFG_LOGDIR, 'invenio_upgrader.log')
    if not upgrader:
        upgrader = InvenioUpgrader()
    logger = upgrader.get_logger(logfilename=logfilename)

    try:
        upgrades = upgrader.get_upgrades()

        if not upgrades:
            logger.info("All upgrades have been applied.")
            return

        logger.info("Following upgrade(s) will be applied:")

        for u in upgrades:
            title = u['__doc__']
            if title:
                logger.info(" * %s (%s)" % (u['id'], title))
            else:
                logger.info(" * %s" % u['id'])

        logger.info("Running pre-upgrade checks...")
        upgrader.pre_upgrade_checks(upgrades)

        logger.info("Calculating estimated upgrade time...")
        estimate = upgrader.human_estimate(upgrades)

        wait_for_user(
            wrap_text_in_a_box(
                "WARNING: You are going to upgrade your installation "
                "(estimated time: %s)!" % estimate))

        for u in upgrades:
            title = u['__doc__']
            if title:
                logger.info("Applying %s (%s)" % (u['id'], title))
            else:
                logger.info("Applying %s" % u['id'])
            upgrader.apply_upgrade(u)

        logger.info("Running post-upgrade checks...")
        upgrader.post_upgrade_checks(upgrades)

        if upgrader.has_warnings():
            logger.warning("Upgrade completed with %s warnings - please check "
                           "log-file for further information:\nless %s" %
                           (upgrader.get_warnings_count(), logfilename))
        else:
            logger.info("Upgrade completed successfully.")
    except RuntimeError as e:
        for msg in e.args:
            logger.error(unicode(msg))
        logger.info("Please check log file for further information:\n"
                    "less %s" % logfilename)
        sys.exit(1)
Пример #3
0
def populate(yes_i_know=False):
    """Load CDS general demorecords."""
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user
    from invenio.config import CFG_PREFIX
    from invenio.modules.scheduler.models import SchTASK

    wait_for_user(
        wrap_text_in_a_box(
            "WARNING: You are going to override data in tables!"))

    print(">>> Going to load demo records...")
    xml_data = pkg_resources.resource_filename(
        'cds', os.path.join('demosite', 'data', 'cds-demobibdata.xml'))

    job_id = SchTASK.query.count()

    for cmd in [
            "bin/bibupload -u admin -i %s" % (xml_data, ),
            "bin/bibupload %d" % (job_id + 1, ),
            "bin/bibindex -u admin",
            "bin/bibindex %d" % (job_id + 2, ),
            "bin/bibindex -u admin -w global",
            "bin/bibindex %d" % (job_id + 3, ),
            "bin/bibreformat -u admin -o HB",
            "bin/bibreformat %d" % (job_id + 4, ),
            "bin/webcoll -u admin",
            "bin/webcoll %d" % (job_id + 5, ),
    ]:
        cmd = os.path.join(CFG_PREFIX, cmd)
        if os.system(cmd):
            print("ERROR: failed execution of", cmd)
            sys.exit(1)
    print(">>> CDS Demo records loaded successfully.")
Пример #4
0
def drop(yes_i_know=False, quiet=False):
    """Drop database tables."""
    print(">>> Going to drop tables and related data on filesystem ...")

    from invenio.utils.date import get_time_estimator
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user
    from invenio.ext.sqlalchemy.utils import test_sqla_connection, \
        test_sqla_utf8_chain
    from invenio.ext.sqlalchemy import db, models
    from invenio.modules.jsonalchemy.wrappers import StorageEngine

    # Step 0: confirm deletion
    wait_for_user(
        wrap_text_in_a_box(
            "WARNING: You are going to destroy your database tables and related "
            "data on filesystem!"))

    # Step 1: test database connection
    test_sqla_connection()
    test_sqla_utf8_chain()
    list(models)

    # Step 2: destroy associated data
    tables = list(reversed(db.metadata.sorted_tables))

    def _dropper(items, prefix, dropper):
        N = len(items)
        prefix = prefix.format(N)
        e = get_time_estimator(N)
        dropped = 0
        if quiet:
            print(prefix)

        for i, table in enumerate(items):
            try:
                if not quiet:
                    print_progress(1.0 * (i + 1) / N,
                                   prefix=prefix,
                                   suffix=str(
                                       datetime.timedelta(seconds=e()[0])))
                dropper(table)
                dropped += 1
            except Exception:
                print('\r>>> problem with dropping {0}'.format(table))
                current_app.logger.exception(table)

        if dropped == N:
            print(">>> Everything has been dropped successfully.")
        else:
            print("ERROR: not all items were properly dropped.")
            print(">>> Dropped", dropped, 'out of', N)

    _dropper(StorageEngine.__storage_engine_registry__,
             '>>> Dropping {0} storage engines ...',
             lambda api: api.storage_engine.drop())

    _dropper(tables, '>>> Dropping {0} tables ...',
             lambda table: table.drop(bind=db.engine, checkfirst=True))
Пример #5
0
def drop(yes_i_know=False, quiet=False):
    """Drop database tables."""
    print(">>> Going to drop tables and related data on filesystem ...")

    from invenio.utils.date import get_time_estimator
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user
    from invenio.ext.sqlalchemy.utils import test_sqla_connection, \
        test_sqla_utf8_chain
    from invenio.ext.sqlalchemy import db, models
    from invenio.modules.jsonalchemy.wrappers import StorageEngine

    # Step 0: confirm deletion
    wait_for_user(wrap_text_in_a_box(
        "WARNING: You are going to destroy your database tables and related "
        "data on filesystem!"))

    # Step 1: test database connection
    test_sqla_connection()
    test_sqla_utf8_chain()
    list(models)

    # Step 2: destroy associated data
    tables = list(reversed(db.metadata.sorted_tables))

    def _dropper(items, prefix, dropper):
        N = len(items)
        prefix = prefix.format(N)
        e = get_time_estimator(N)
        dropped = 0
        if quiet:
            print(prefix)

        for i, table in enumerate(items):
            try:
                if not quiet:
                    print_progress(
                        1.0 * (i + 1) / N, prefix=prefix,
                        suffix=str(datetime.timedelta(seconds=e()[0])))
                dropper(table)
                dropped += 1
            except Exception:
                print('\r>>> problem with dropping {0}'.format(table))
                current_app.logger.exception(table)

        if dropped == N:
            print(">>> Everything has been dropped successfully.")
        else:
            print("ERROR: not all items were properly dropped.")
            print(">>> Dropped", dropped, 'out of', N)

    _dropper(StorageEngine.__storage_engine_registry__,
             '>>> Dropping {0} storage engines ...',
             lambda api: api.storage_engine.drop())

    _dropper(tables, '>>> Dropping {0} tables ...',
             lambda table: table.drop(bind=db.engine, checkfirst=True))
def pre_upgrade():
    """
    Run pre-upgrade check conditions to ensure the upgrade can be applied
    without problems.
    """
    logger = logging.getLogger('invenio_upgrader')

    global DB_VERSION  # Needed because we assign to it

    DB_VERSION = _invenio_schema_version_guesser()

    if DB_VERSION == 'unknown':
        raise RuntimeError(
            "Your Invenio database schema version could not be"
            " determined. Please upgrade to Invenio v1.0.0 first.")

    if DB_VERSION == 'pre-0.99.0':
        raise RuntimeError("Upgrading from Invenio versions prior to 0.99 is"
                           " not supported. Please upgrade to 0.99.0 first.")

    if DB_VERSION == '0.99.0':
        raise RuntimeError(
            "It seems like you are running Invenio version "
            "0.99.0. Please run the upgrade in the following special way:\n"
            "make install\ninveniocfg --update-all\n"
            "make update-v0.99.0-tables\nmake update-v0.99.6-tables\n"
            "inveniocfg --upgrade\n\nNote: Most warnings printed during "
            "inveniocfg --upgrade can safely be ignored when upgrading from"
            " Invenio 0.99.0.")

    if DB_VERSION in ['0.99.x', '0.99.x-1.0.0']:
        raise RuntimeError(
            "It seems like you are running Invenio version "
            "v0.99.1-v0.99.x. Please run the upgrade in the following special"
            " way:\nmake install\ninveniocfg --update-all\n"
            "make update-v0.99.6-tables\n"
            "inveniocfg --upgrade\n\nNote: Most warnings printed during "
            "inveniocfg --upgrade can safely be ignored when upgrading from"
            " Invenio v0.99.1-0.99.x.")

    if DB_VERSION == 'master':
        warnings.warn("Invenio database schema is on a development version"
                      " between 1.0.x and 1.1.0")

        # Run import here, since we are on 1.0-1.1 we know the import will work
        from invenio.utils.text import wait_for_user
        try:
            wait_for_user("\nUPGRADING TO 1.1.0 FROM A DEVELOPMENT VERSION"
                          " WILL LEAD TO MANY WARNINGS! Please thoroughly"
                          " test the upgrade on non-production systems first,"
                          " and pay close attention to warnings.\n")
        except SystemExit:
            raise RuntimeError("Upgrade aborted by user.")
    else:
        logger.info("Invenio version v%s detected." % DB_VERSION)
def pre_upgrade():
    """
    Run pre-upgrade check conditions to ensure the upgrade can be applied
    without problems.
    """
    logger = logging.getLogger('invenio_upgrader')

    global DB_VERSION  # Needed because we assign to it

    DB_VERSION = _invenio_schema_version_guesser()

    if DB_VERSION == 'unknown':
        raise RuntimeError(
            "Your Invenio database schema version could not be"
            " determined. Please upgrade to Invenio v1.0.0 first.")

    if DB_VERSION == 'pre-0.99.0':
        raise RuntimeError("Upgrading from Invenio versions prior to 0.99 is"
                           " not supported. Please upgrade to 0.99.0 first.")

    if DB_VERSION == '0.99.0':
        raise RuntimeError(
            "It seems like you are running Invenio version "
            "0.99.0. Please run the upgrade in the following special way:\n"
            "make install\ninveniocfg --update-all\n"
            "make update-v0.99.0-tables\nmake update-v0.99.6-tables\n"
            "inveniocfg --upgrade\n\nNote: Most warnings printed during "
            "inveniocfg --upgrade can safely be ignored when upgrading from"
            " Invenio 0.99.0.")

    if DB_VERSION in ['0.99.x', '0.99.x-1.0.0']:
        raise RuntimeError(
            "It seems like you are running Invenio version "
            "v0.99.1-v0.99.x. Please run the upgrade in the following special"
            " way:\nmake install\ninveniocfg --update-all\n"
            "make update-v0.99.6-tables\n"
            "inveniocfg --upgrade\n\nNote: Most warnings printed during "
            "inveniocfg --upgrade can safely be ignored when upgrading from"
            " Invenio v0.99.1-0.99.x.")

    if DB_VERSION == 'master':
        warnings.warn("Invenio database schema is on a development version"
                      " between 1.0.x and 1.1.0")

        # Run import here, since we are on 1.0-1.1 we know the import will work
        from invenio.utils.text import wait_for_user
        try:
            wait_for_user("\nUPGRADING TO 1.1.0 FROM A DEVELOPMENT VERSION"
                          " WILL LEAD TO MANY WARNINGS! Please thoroughly"
                          " test the upgrade on non-production systems first,"
                          " and pay close attention to warnings.\n")
        except SystemExit:
            raise RuntimeError("Upgrade aborted by user.")
    else:
        logger.info("Invenio version v%s detected." % DB_VERSION)
Пример #8
0
def init(user='******', password='', yes_i_know=False):
    """Initialize database and user."""
    from invenio.ext.sqlalchemy import db
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user

    # Step 0: confirm deletion
    wait_for_user(
        wrap_text_in_a_box(
            "WARNING: You are going to destroy your database tables! Run first"
            " `inveniomanage database drop`."))

    # Step 1: drop database and recreate it
    if db.engine.name == 'mysql':
        # FIXME improve escaping
        args = dict((k, str(v).replace('$', '\$'))
                    for (k, v) in iteritems(current_app.config)
                    if k.startswith('CFG_DATABASE'))
        args = dict(zip(args, map(quote, args.values())))
        prefix = ('{cmd} -u {user} --password={password} '
                  '-h {CFG_DATABASE_HOST} -P {CFG_DATABASE_PORT} ')
        cmd_prefix = prefix.format(cmd='mysql',
                                   user=user,
                                   password=password,
                                   **args)
        cmd_admin_prefix = prefix.format(cmd='mysqladmin',
                                         user=user,
                                         password=password,
                                         **args)
        cmds = [
            cmd_prefix + '-e "DROP DATABASE IF EXISTS {CFG_DATABASE_NAME}"',
            (cmd_prefix + '-e "CREATE DATABASE IF NOT EXISTS '
             '{CFG_DATABASE_NAME} DEFAULT CHARACTER SET utf8 '
             'COLLATE utf8_general_ci"'),
            # Create user and grant access to database.
            (cmd_prefix + '-e "GRANT ALL PRIVILEGES ON '
             '{CFG_DATABASE_NAME}.* TO {CFG_DATABASE_USER}@localhost '
             'IDENTIFIED BY {CFG_DATABASE_PASS}"'),
            (cmd_prefix + '-e "GRANT ALL PRIVILEGES ON '
             '{CFG_DATABASE_NAME}.* TO {CFG_DATABASE_USER}@\'%\' '
             'IDENTIFIED BY {CFG_DATABASE_PASS}"'),
            cmd_admin_prefix + 'flush-privileges'
        ]
        for cmd in cmds:
            cmd = cmd.format(**args)
            print(cmd)
            if os.system(cmd):
                print("ERROR: failed execution of", cmd, file=sys.stderr)
                sys.exit(1)
        print('>>> Database has been installed.')
Пример #9
0
def init(user='******', password='', yes_i_know=False):
    """Initialize database and user."""
    from invenio.ext.sqlalchemy.utils import initialize_database_user
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user

    from sqlalchemy_utils.functions import database_exists, create_database, \
        drop_database

    from sqlalchemy.engine.url import URL
    from sqlalchemy import create_engine

    # Step 0: confirm deletion
    wait_for_user(wrap_text_in_a_box(
        "WARNING: You are going to destroy your database tables! Run first"
        " `inveniomanage database drop`."
    ))

    # Step 1: create URI to connect admin user
    cfg = current_app.config
    SQLALCHEMY_DATABASE_URI = URL(
        cfg.get('CFG_DATABASE_TYPE', 'mysql'),
        username=user,
        password=password,
        host=cfg.get('CFG_DATABASE_HOST'),
        database=cfg.get('CFG_DATABASE_NAME'),
        port=cfg.get('CFG_DATABASE_PORT'),
    )

    # Step 2: drop the database if already exists
    if database_exists(SQLALCHEMY_DATABASE_URI):
        drop_database(SQLALCHEMY_DATABASE_URI)
        print('>>> Database has been dropped.')

    # Step 3: create the database
    create_database(SQLALCHEMY_DATABASE_URI, encoding='utf8')
    print('>>> Database has been created.')

    # Step 4: setup connection with special user
    engine = create_engine(SQLALCHEMY_DATABASE_URI)
    engine.connect()

    # Step 5: grant privileges for the user
    initialize_database_user(
        engine=engine,
        database_name=current_app.config['CFG_DATABASE_NAME'],
        database_user=current_app.config['CFG_DATABASE_USER'],
        database_pass=current_app.config['CFG_DATABASE_PASS'],
    )
    print('>>> Database user has been initialized.')
Пример #10
0
def init(user='******', password='', yes_i_know=False):
    """Initialize database and user."""
    from invenio.ext.sqlalchemy import db
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user

    # Step 0: confirm deletion
    wait_for_user(wrap_text_in_a_box(
        "WARNING: You are going to destroy your database tables! Run first"
        " `inveniomanage database drop`."
    ))

    # Step 1: drop database and recreate it
    if db.engine.name == 'mysql':
        # FIXME improve escaping
        args = dict((k, str(v).replace('$', '\$'))
                    for (k, v) in iteritems(current_app.config)
                    if k.startswith('CFG_DATABASE'))
        args = dict(zip(args, map(quote, args.values())))
        prefix = ('{cmd} -u {user} --password={password} '
                  '-h {CFG_DATABASE_HOST} -P {CFG_DATABASE_PORT} ')
        cmd_prefix = prefix.format(cmd='mysql', user=user, password=password,
                                   **args)
        cmd_admin_prefix = prefix.format(cmd='mysqladmin', user=user,
                                         password=password,
                                         **args)
        cmds = [
            cmd_prefix + '-e "DROP DATABASE IF EXISTS {CFG_DATABASE_NAME}"',
            (cmd_prefix + '-e "CREATE DATABASE IF NOT EXISTS '
             '{CFG_DATABASE_NAME} DEFAULT CHARACTER SET utf8 '
             'COLLATE utf8_general_ci"'),
            # Create user and grant access to database.
            (cmd_prefix + '-e "GRANT ALL PRIVILEGES ON '
             '{CFG_DATABASE_NAME}.* TO {CFG_DATABASE_USER}@localhost '
             'IDENTIFIED BY {CFG_DATABASE_PASS}"'),
            (cmd_prefix + '-e "GRANT ALL PRIVILEGES ON '
             '{CFG_DATABASE_NAME}.* TO {CFG_DATABASE_USER}@\'%\' '
             'IDENTIFIED BY {CFG_DATABASE_PASS}"'),
            cmd_admin_prefix + 'flush-privileges'
        ]
        for cmd in cmds:
            cmd = cmd.format(**args)
            print(cmd)
            if os.system(cmd):
                print("ERROR: failed execution of", cmd, file=sys.stderr)
                sys.exit(1)
        print('>>> Database has been installed.')
Пример #11
0
def populate(packages=[],
             default_data=True,
             files=None,
             job_id=0,
             extra_info=None,
             yes_i_know=False):
    """Load demo records.  Useful for testing purposes."""
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user

    # Load cli interfaces for tools that we are going to need
    from invenio_records.manage import main as rmanager

    # Step 0: confirm deletion
    wait_for_user(
        wrap_text_in_a_box(
            "WARNING: You are going to override data in tables!"))

    if not default_data:
        print('>>> Default data has been skiped (--no-data).')
        return
    if not packages:
        packages = ['invenio_demosite.base']

    from werkzeug.utils import import_string
    map(import_string, packages)

    print(">>> Going to load demo records...")

    if files is None:
        files = [
            pkg_resources.resource_filename(
                'invenio',
                os.path.join('testsuite', 'data', 'demo_record_marc_data.xml'))
        ]

    # upload demo site files:
    bibupload_flags = '-t marcxml'
    for f in files:
        job_id += 1
        for cmd in ((rmanager,
                     "records create %s %s" % (bibupload_flags, f)), ):
            if run_py_func(*cmd, passthrough=True).exit_code:
                print("ERROR: failed execution of", *cmd)
                sys.exit(1)

    print(">>> Demo records loaded successfully.")
Пример #12
0
def upload_to_site(marcxml, yes_i_know):
    """
    makes the appropriate calls to bibupload to get the MARCXML record onto
    the site.

    @param: marcxml (string): the absolute location of the MARCXML that was
        generated by this programme
    @param: yes_i_know (boolean): if true, no confirmation.  if false, prompt.

    @output: a new record on the invenio site

    @return: None
    """
    if not yes_i_know:
        wait_for_user(wrap_text_in_a_box('You are going to upload new ' +
                                         'plots to the server.'))
    task_low_level_submission('bibupload', 'admin', '-a', marcxml)
Пример #13
0
def populate(packages=[], default_data=True, files=None,
             job_id=0, extra_info=None, yes_i_know=False):
    """Load demo records.  Useful for testing purposes."""
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user

    # Load cli interfaces for tools that we are going to need
    from invenio_records.manage import main as rmanager

    # Step 0: confirm deletion
    wait_for_user(wrap_text_in_a_box(
        "WARNING: You are going to override data in tables!"
    ))

    if not default_data:
        print('>>> Default data has been skiped (--no-data).')
        return
    if not packages:
        packages = ['invenio_demosite.base']

    from werkzeug.utils import import_string
    map(import_string, packages)

    print(">>> Going to load demo records...")

    if files is None:
        files = [pkg_resources.resource_filename(
            'invenio',
            os.path.join('testsuite', 'data', 'demo_record_marc_data.xml'))]

    # upload demo site files:
    bibupload_flags = '-t marcxml'
    for f in files:
        job_id += 1
        for cmd in (
            (rmanager, "records create %s %s" % (bibupload_flags, f)),
        ):
            if run_py_func(*cmd, passthrough=True).exit_code:
                print("ERROR: failed execution of", *cmd)
                sys.exit(1)

    print(">>> Demo records loaded successfully.")
def pre_upgrade():
    """Check for potentially invalid revisions"""
    res = run_sql("""SELECT DISTINCT(changeset_id) FROM bibHOLDINGPEN
                     WHERE LENGTH(changeset_xml) =  %s""", [2**16 - 1])
    if res:
        warnings.warn("""You have %s holding pen entries with potentially corrupt data!
                         You can find the rows affected with the sql command:
                         SELECT DISTINCT(changeset_id) FROM bibHOLDINGPEN
                         WHERE LENGTH(changeset_xml) =  65535""" % len(res))

        from invenio.utils.text import wait_for_user
        try:
            wait_for_user(
                "\nThis upgrade will delete all the corrupted entries. A backup table bibHOLDINGPEN_backup will be created.\n")
        except SystemExit:
            raise RuntimeError("Upgrade aborted by user.")

        for r in res:
            run_sql(
                """DELETE FROM bibHOLDINGPEN WHERE changeset_id=%s""" %
                r[0])
def pre_upgrade():
    """Check for potentially invalid revisions"""

    # Check for any inconsistent data
    missing_parents = list(db.engine.execute(
        """SELECT id, id_parent FROM bwlOBJECT WHERE id_parent NOT IN (SELECT id FROM bwlOBJECT)"""
    ))
    if missing_parents:
        warnings.warn("Inconsistent parent IDs in bwlOBJECT '{0}'".format(
            missing_parents
        ))
        try:
            wait_for_user("\nSetting all dangling parents to NULL? (Ctrl+C to stop)\n")
        except SystemExit:
            raise RuntimeError("Upgrade aborted by user.")
        for parent in missing_parents:
            db.engine.execute(
                """UPDATE bwlOBJECT SET id_parent=NULL WHERE id={0}""".format(parent[0])
            )

    dangling_logs = list(db.engine.execute(
        """SELECT id_object FROM bwlOBJECTLOGGING WHERE id_object NOT IN (SELECT id FROM bwlOBJECT)"""
    )) + list(db.engine.execute(
        """SELECT id_object FROM bwlWORKFLOWLOGGING WHERE id_object NOT IN (SELECT uuid FROM bwlWORKFLOW)"""
    ))
    if dangling_logs:
        warnings.warn("Inconsistent logs in bwlOBJECT/bwlWORKFLOW '{0}'".format(
            len(dangling_logs)
        ))
        try:
            wait_for_user("\nDelete all dangling logs? (Ctrl+C to stop)\n")
        except SystemExit:
            raise RuntimeError("Upgrade aborted by user.")
        db.engine.execute(
            """DELETE FROM bwlOBJECTLOGGING WHERE id_object NOT IN (SELECT id FROM bwlOBJECT)"""
        )
        db.engine.execute(
            """DELETE FROM bwlWORKFLOWLOGGING WHERE id_object NOT IN (SELECT uuid FROM bwlWORKFLOW)"""
        )
Пример #16
0
def _make_upload(name, description="Going to load demo records"):
    """Upload the demodata from the given file.

    :param str name: the demodata file name
    :param str description: the user message
    """
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user
    from invenio.config import CFG_PREFIX
    from invenio.modules.scheduler.models import SchTASK

    wait_for_user(wrap_text_in_a_box(
        "WARNING: You are going to override data in tables!"
    ))

    print(">>> {0}".format(description))
    xml_data = pkg_resources.resource_filename(
        'cds',
        os.path.join('demosite', 'data', name))

    job_id = SchTASK.query.count()

    for cmd in ["bin/bibupload -u admin -i %s" % (xml_data, ),
                "bin/bibupload %d" % (job_id + 1, ),
                "bin/bibindex -u admin",
                "bin/bibindex %d" % (job_id + 2,),
                "bin/bibindex -u admin -w global",
                "bin/bibindex %d" % (job_id + 3,),
                "bin/bibreformat -u admin -o HB",
                "bin/bibreformat %d" % (job_id + 4,),
                "bin/webcoll -u admin",
                "bin/webcoll %d" % (job_id + 5,),
                ]:
        cmd = os.path.join(CFG_PREFIX, cmd)
        if os.system(cmd):
            print("ERROR: failed execution of", cmd)
            sys.exit(1)
    print(">>> CDS Demo records loaded successfully.")
Пример #17
0
def _make_upload(name, description="Going to load demo records"):
    """Upload the demodata from the given file.

    :param str name: the demodata file name
    :param str description: the user message
    """
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user
    from invenio.config import CFG_PREFIX
    from invenio.modules.scheduler.models import SchTASK

    wait_for_user(
        wrap_text_in_a_box(
            "WARNING: You are going to override data in tables!"))

    print(">>> {0}".format(description))
    xml_data = pkg_resources.resource_filename(
        'cds', os.path.join('demosite', 'data', name))

    job_id = SchTASK.query.count()

    for cmd in [
            "bin/bibupload -u admin -i %s" % (xml_data, ),
            "bin/bibupload %d" % (job_id + 1, ),
            "bin/bibindex -u admin",
            "bin/bibindex %d" % (job_id + 2, ),
            "bin/bibindex -u admin -w global",
            "bin/bibindex %d" % (job_id + 3, ),
            "bin/bibreformat -u admin -o HB",
            "bin/bibreformat %d" % (job_id + 4, ),
            "bin/webcoll -u admin",
            "bin/webcoll %d" % (job_id + 5, ),
    ]:
        cmd = os.path.join(CFG_PREFIX, cmd)
        if os.system(cmd):
            print("ERROR: failed execution of", cmd)
            sys.exit(1)
    print(">>> CDS Demo records loaded successfully.")
Пример #18
0
def populate(packages=[], default_data=True, files=None,
             job_id=0, extra_info=None, yes_i_know=False):
    """Load demo records.  Useful for testing purposes."""
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user

    ## Step 0: confirm deletion
    wait_for_user(wrap_text_in_a_box(
        "WARNING: You are going to override data in tables!"
    ))

    if not default_data:
        print('>>> Default data has been skiped (--no-data).')
        return
    if not packages:
        packages = ['invenio_demosite.base']

    from werkzeug.utils import import_string
    from invenio.config import CFG_PREFIX
    map(import_string, packages)

    from invenio.ext.sqlalchemy import db
    print(">>> Going to load demo records...")
    db.session.execute("TRUNCATE schTASK")
    db.session.commit()
    if files is None:
        files = [pkg_resources.resource_filename(
            'invenio',
            os.path.join('testsuite', 'data', 'demo_record_marc_data.xml'))]

    # upload demo site files:
    bibupload_flags = '-i'
    if extra_info is not None and 'force-recids' in extra_info:
        bibupload_flags = '-i -r --force'
    for f in files:
        job_id += 1
        for cmd in ["%s/bin/bibupload -u admin %s %s" % (CFG_PREFIX, bibupload_flags, f),
                    "%s/bin/bibupload %d" % (CFG_PREFIX, job_id)]:
            if os.system(cmd):
                print("ERROR: failed execution of", cmd)
                sys.exit(1)

    for cmd in ["bin/bibdocfile --textify --with-ocr --recid 97",
                "bin/bibdocfile --textify --all",
                "bin/bibindex -u admin",
                "bin/bibindex %d" % (job_id + 1,),
                "bin/bibindex -u admin -w global",
                "bin/bibindex %d" % (job_id + 2,),
                "bin/bibreformat -u admin -o HB",
                "bin/bibreformat %d" % (job_id + 3,),
                "bin/webcoll -u admin",
                "bin/webcoll %d" % (job_id + 4,),
                "bin/bibrank -u admin",
                "bin/bibrank %d" % (job_id + 5,),
                "bin/bibsort -u admin -R",
                "bin/bibsort %d" % (job_id + 6,),
                "bin/oairepositoryupdater -u admin",
                "bin/oairepositoryupdater %d" % (job_id + 7,),
                "bin/bibupload %d" % (job_id + 8,)]:
        cmd = os.path.join(CFG_PREFIX, cmd)
        if os.system(cmd):
            print("ERROR: failed execution of", cmd)
            sys.exit(1)
    print(">>> Demo records loaded successfully.")
Пример #19
0
def main():
    """Core loop."""
    check_running_process_user()
    logfilename = '%s/fulltext_files_migration_kit-%s.log' % (CFG_LOGDIR, datetime.today().strftime('%Y%m%d%H%M%S'))
    try:
        logfile = open(logfilename, 'w')
    except IOError as e:
        print(wrap_text_in_a_box('NOTE: it\'s impossible to create the log:\n\n  %s\n\nbecause of:\n\n  %s\n\nPlease run this migration kit as the same user who runs Invenio (e.g. Apache)' % (logfilename, e), style='conclusion', break_long=False))
        sys.exit(1)

    bibdoc_bibdoc = retrieve_bibdoc_bibdoc()

    print(wrap_text_in_a_box ("""This script migrate the filesystem structure used to store icons files to the new stricter structure.
This script must not be run during normal Invenio operations.
It is safe to run this script. No file will be deleted.
Anyway it is recommended to run a backup of the filesystem structure just in case.
A backup of the database tables involved will be automatically performed.""", style='important'))
    if not bibdoc_bibdoc:
        print(wrap_text_in_a_box("No need for migration", style='conclusion'))
        return
    print("%s icons will be migrated/fixed." % len(bibdoc_bibdoc))
    wait_for_user()
    print("Backing up database tables")
    try:
        if not backup_tables():
            print(wrap_text_in_a_box("""It appears that is not the first time that you run this script.
Backup tables have been already created by a previous run.
In order for the script to go further they need to be removed.""", style='important'))

            wait_for_user()
            print("Backing up database tables (after dropping previous backup)", end=' ')
            backup_tables(drop=True)
            print("-> OK")
        else:
            print("-> OK")
    except Exception as e:
        print(wrap_text_in_a_box("Unexpected error while backing up tables. Please, do your checks: %s" % e, style='conclusion'))
        sys.exit(1)

    to_fix_marc = intbitset()
    print("Created a complete log file into %s" % logfilename)
    try:
        try:
            for id_bibdoc1, id_bibdoc2 in bibdoc_bibdoc:
                try:
                    record_does_exist = True
                    recids = get_recid_from_docid(id_bibdoc1)
                    if not recids:
                        print("Skipping %s" % id_bibdoc1)
                        continue
                    for recid in recids:
                        if record_exists(recid[0]) > 0:
                            to_fix_marc.add(recid[0])
                        else:
                            record_does_exist = False
                    if not fix_bibdoc_bibdoc(id_bibdoc1, id_bibdoc2, logfile):
                        if record_does_exist:
                            raise StandardError("Error when correcting document ID %s" % id_bibdoc1)
                except Exception as err:
                    print("ERROR: %s" % err, file=logfile)
            print(wrap_text_in_a_box("DONE", style='conclusion'))
        except:
            logfile.close()
            register_exception()
            print(wrap_text_in_a_box(
                title = "INTERRUPTED BECAUSE OF ERROR!",
                body = """Please see the log file %s for what was the status prior to the error. Contact %s in case of problems, attaching the log.""" % (logfilename, CFG_SITE_SUPPORT_EMAIL),
            style = 'conclusion'))
            sys.exit(1)
    finally:
        print("Scheduling FIX-MARC to synchronize MARCXML for updated records.")
        cli_fix_marc(options={}, explicit_recid_set=to_fix_marc)
Пример #20
0
def populate(packages=[], default_data=True, files=None,
             job_id=0, extra_info=None, yes_i_know=False):
    """Load demo records.  Useful for testing purposes."""
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user

    # Load cli interfaces for tools that we are going to need
    from invenio.legacy.bibupload.engine import main as bibupload
    from invenio.legacy.bibindex.engine import main as bibindex
    from invenio.legacy.oairepository.updater import main as oairepositoryupdater
    from invenio.legacy.bibsort.daemon import main as bibsort
    from invenio.legacy.bibdocfile.cli import main as bibdocfile
    from invenio.legacy.bibrank.cli import main as bibrank

    ## Step 0: confirm deletion
    wait_for_user(wrap_text_in_a_box(
        "WARNING: You are going to override data in tables!"
    ))

    if not default_data:
        print('>>> Default data has been skiped (--no-data).')
        return
    if not packages:
        packages = ['invenio_demosite.base']

    from werkzeug.utils import import_string
    map(import_string, packages)

    from invenio.ext.sqlalchemy import db
    print(">>> Going to load demo records...")
    db.session.execute("TRUNCATE schTASK")
    db.session.commit()
    if files is None:
        files = [pkg_resources.resource_filename(
            'invenio',
            os.path.join('testsuite', 'data', 'demo_record_marc_data.xml'))]

    # upload demo site files:
    bibupload_flags = '-i'
    if extra_info is not None and 'force-recids' in extra_info:
        bibupload_flags = '-i -r --force'
    for f in files:
        job_id += 1
        for cmd in (
            (bibupload, "bibupload -u admin %s %s" % (bibupload_flags, f)),
            (bibupload, "bibupload %d" % (job_id))
        ):
            if run_py_func(*cmd, passthrough=True).exit_code:
                print("ERROR: failed execution of", *cmd)
                sys.exit(1)

    i = count(job_id + 1).next
    for cmd in (
        #(bibdocfile, "bibdocfile --textify --with-ocr --recid 97"),
        #(bibdocfile, "bibdocfile --textify --all"),
        (bibindex, "bibindex -u admin"),
        (bibindex, "bibindex %d" % i()),
        (bibindex, "bibindex -u admin -w global"),
        (bibindex, "bibindex %d" % i()),
        (bibrank, "bibrank -u admin"),
        (bibrank, "bibrank %d" % i()),
        (bibsort, "bibsort -u admin -R"),
        (bibsort, "bibsort %d" % i()),
        (oairepositoryupdater, "oairepositoryupdater -u admin"),
        (oairepositoryupdater, "oairepositoryupdater %d" % i()),
        (bibupload, "bibupload %d" % i()),
    ):
        if run_py_func(*cmd, passthrough=True).exit_code:
            print("ERROR: failed execution of", *cmd)
            sys.exit(1)
    print(">>> Demo records loaded successfully.")
Пример #21
0
def main():
    """Core loop."""
    check_running_process_user()
    logfilename = '%s/fulltext_files_migration_kit-%s.log' % (
        CFG_LOGDIR, datetime.today().strftime('%Y%m%d%H%M%S'))
    try:
        logfile = open(logfilename, 'w')
    except IOError as e:
        print(
            wrap_text_in_a_box(
                'NOTE: it\'s impossible to create the log:\n\n  %s\n\nbecause of:\n\n  %s\n\nPlease run this migration kit as the same user who runs Invenio (e.g. Apache)'
                % (logfilename, e),
                style='conclusion',
                break_long=False))
        sys.exit(1)

    bibdoc_bibdoc = retrieve_bibdoc_bibdoc()

    print(
        wrap_text_in_a_box(
            """This script migrate the filesystem structure used to store icons files to the new stricter structure.
This script must not be run during normal Invenio operations.
It is safe to run this script. No file will be deleted.
Anyway it is recommended to run a backup of the filesystem structure just in case.
A backup of the database tables involved will be automatically performed.""",
            style='important'))
    if not bibdoc_bibdoc:
        print(wrap_text_in_a_box("No need for migration", style='conclusion'))
        return
    print("%s icons will be migrated/fixed." % len(bibdoc_bibdoc))
    wait_for_user()
    print("Backing up database tables")
    try:
        if not backup_tables():
            print(
                wrap_text_in_a_box(
                    """It appears that is not the first time that you run this script.
Backup tables have been already created by a previous run.
In order for the script to go further they need to be removed.""",
                    style='important'))

            wait_for_user()
            print(
                "Backing up database tables (after dropping previous backup)",
                end=' ')
            backup_tables(drop=True)
            print("-> OK")
        else:
            print("-> OK")
    except Exception as e:
        print(
            wrap_text_in_a_box(
                "Unexpected error while backing up tables. Please, do your checks: %s"
                % e,
                style='conclusion'))
        sys.exit(1)

    to_fix_marc = intbitset()
    print("Created a complete log file into %s" % logfilename)
    try:
        try:
            for id_bibdoc1, id_bibdoc2 in bibdoc_bibdoc:
                try:
                    record_does_exist = True
                    recids = get_recid_from_docid(id_bibdoc1)
                    if not recids:
                        print("Skipping %s" % id_bibdoc1)
                        continue
                    for recid in recids:
                        if record_exists(recid[0]) > 0:
                            to_fix_marc.add(recid[0])
                        else:
                            record_does_exist = False
                    if not fix_bibdoc_bibdoc(id_bibdoc1, id_bibdoc2, logfile):
                        if record_does_exist:
                            raise StandardError(
                                "Error when correcting document ID %s" %
                                id_bibdoc1)
                except Exception as err:
                    print("ERROR: %s" % err, file=logfile)
            print(wrap_text_in_a_box("DONE", style='conclusion'))
        except:
            logfile.close()
            register_exception()
            print(
                wrap_text_in_a_box(
                    title="INTERRUPTED BECAUSE OF ERROR!",
                    body=
                    """Please see the log file %s for what was the status prior to the error. Contact %s in case of problems, attaching the log."""
                    % (logfilename, CFG_SITE_SUPPORT_EMAIL),
                    style='conclusion'))
            sys.exit(1)
    finally:
        print(
            "Scheduling FIX-MARC to synchronize MARCXML for updated records.")
        cli_fix_marc(options={}, explicit_recid_set=to_fix_marc)
Пример #22
0
def populate(packages=[], default_data=True, files=None, job_id=0, extra_info=None, yes_i_know=False):
    """Load demo records.  Useful for testing purposes."""
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user

    ## Step 0: confirm deletion
    wait_for_user(wrap_text_in_a_box("WARNING: You are going to override data in tables!"))

    if not default_data:
        print(">>> Default data has been skiped (--no-data).")
        return
    if not packages:
        packages = ["invenio_demosite.base"]

    from werkzeug.utils import import_string
    from invenio.config import CFG_PREFIX

    map(import_string, packages)

    from invenio.ext.sqlalchemy import db

    print(">>> Going to load demo records...")
    db.session.execute("TRUNCATE schTASK")
    db.session.commit()
    if files is None:
        files = [
            pkg_resources.resource_filename("invenio", os.path.join("testsuite", "data", "demo_record_marc_data.xml"))
        ]

    # upload demo site files:
    bibupload_flags = "-i"
    if extra_info is not None and "force-recids" in extra_info:
        bibupload_flags = "-i -r --force"
    for f in files:
        job_id += 1
        for cmd in [
            "%s/bin/bibupload -u admin %s %s" % (CFG_PREFIX, bibupload_flags, f),
            "%s/bin/bibupload %d" % (CFG_PREFIX, job_id),
        ]:
            if os.system(cmd):
                print("ERROR: failed execution of", cmd)
                sys.exit(1)

    i = count(1).next
    for cmd in [
        "bin/bibdocfile --textify --with-ocr --recid 97",
        "bin/bibdocfile --textify --all",
        "bin/bibindex -u admin",
        "bin/bibindex %d" % (job_id + i(),),
        "bin/bibindex -u admin -w global",
        "bin/bibindex %d" % (job_id + i(),),
        "bin/bibreformat -u admin -o HB",
        "bin/bibreformat %d" % (job_id + i(),),
        "bin/bibrank -u admin",
        "bin/bibrank %d" % (job_id + i(),),
        "bin/bibsort -u admin -R",
        "bin/bibsort %d" % (job_id + i(),),
        "bin/oairepositoryupdater -u admin",
        "bin/oairepositoryupdater %d" % (job_id + i(),),
        "bin/bibupload %d" % (job_id + i(),),
    ]:
        cmd = os.path.join(CFG_PREFIX, cmd)
        if os.system(cmd):
            print("ERROR: failed execution of", cmd)
            sys.exit(1)
    print(">>> Demo records loaded successfully.")
Пример #23
0
def drop(yes_i_know=False):
    """Drops database tables"""

    print(">>> Going to drop tables and related data on filesystem ...")

    from sqlalchemy import event
    from invenio.utils.date import get_time_estimator
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user
    from invenio.legacy.webstat.api import destroy_customevents
    from invenio.legacy.inveniocfg import test_db_connection
    from invenio.ext.sqlalchemy import db, models
    from invenio.legacy.bibdocfile.api import _make_base_dir

    ## Step 0: confirm deletion
    wait_for_user(wrap_text_in_a_box(
        "WARNING: You are going to destroy your database tables and related "
        "data on filesystem!"))

    ## Step 1: test database connection
    test_db_connection()
    list(models)

    ## Step 2: disable foreign key checks
    if db.engine.name == 'mysql':
        db.engine.execute('SET FOREIGN_KEY_CHECKS=0;')

    ## Step 3: destroy associated data
    try:
        msg = destroy_customevents()
        if msg:
            print(msg)
    except:
        print("ERROR: Could not destroy customevents.")

    ## FIXME: move to bibedit_model
    def bibdoc_before_drop(target, connection_dummy, **kw_dummy):
        print
        print(">>> Going to remove records data...")
        for (docid,) in db.session.query(target.c.id).all():
            directory = _make_base_dir(docid)
            if os.path.isdir(directory):
                print('    >>> Removing files for docid =', docid)
                shutil.rmtree(directory)
        db.session.commit()
        print(">>> Data has been removed.")

    from invenio.modules.editor.models import Bibdoc
    event.listen(Bibdoc.__table__, "before_drop", bibdoc_before_drop)

    tables = list(reversed(db.metadata.sorted_tables))
    N = len(tables)

    prefix = '>>> Dropping %d tables ...' % N

    e = get_time_estimator(N)
    dropped = 0

    for i, table in enumerate(tables):
        try:
            print_progress(1.0 * i / N, prefix=prefix,
                           suffix=str(datetime.timedelta(seconds=e()[0])))
            table.drop(bind=db.engine)
            dropped += 1
        except:
            print('\r', '>>> problem with dropping table', table)

    print
    if dropped == N:
        print(">>> Tables dropped successfully.")
    else:
        print("ERROR: not all tables were properly dropped.")
        print(">>> Dropped", dropped, 'out of', N)
Пример #24
0
if __name__ == "__main__":
    from invenio.utils.text import wait_for_user

    wait_for_user(
        """
    #######################################################
    # This is the test suite for the BibEncode module     #
    #                                                     #
    # You need to have installed ffmpeg with H.264, WebM  #
    # and Theora support! Please see the manual!          #
    #                                                     #
    # Please be aware that not every aspect can be tested #
    # due to the nature of video encoding and wrapping    #
    # external libraries like ffmpeg. The results should  #
    # only be seen as an indicator and do not necessarily #
    # mean that there is something wrong.                 #
    #                                                     #
    # You should evaluate the output manually in the tmp  #
    # folder of your Invenio installation                 #
    #                                                     #
    # The test suite will download and create several     #
    # gigabytes of video material to perform the test!    #
    # The whole test might take up half an hour           #
    #                                                     #
    # Do you wich to continue? Then enter "Yes, I know!". #
    # Else press 'ctrl + c' to leave this tool.           #
    #######################################################
    """
    )
    run_test_suite(TEST_SUITE)
Пример #25
0
    TestBatchEngineFunctions,
    TestDaemonFunctions)

if __name__ == "__main__":
    from invenio.utils.text import wait_for_user
    wait_for_user("""
    #######################################################
    # This is the test suite for the BibEncode module     #
    #                                                     #
    # You need to have installed ffmpeg with H.264, WebM  #
    # and Theora support! Please see the manual!          #
    #                                                     #
    # Please be aware that not every aspect can be tested #
    # due to the nature of video encoding and wrapping    #
    # external libraries like ffmpeg. The results should  #
    # only be seen as an indicator and do not necessarily #
    # mean that there is something wrong.                 #
    #                                                     #
    # You should evaluate the output manually in the tmp  #
    # folder of your Invenio installation                 #
    #                                                     #
    # The test suite will download and create several     #
    # gigabytes of video material to perform the test!    #
    # The whole test might take up half an hour           #
    #                                                     #
    # Do you wich to continue? Then enter "Yes, I know!". #
    # Else press 'ctrl + c' to leave this tool.           #
    #######################################################
    """)
    run_test_suite(TEST_SUITE)
Пример #26
0
def populate(packages=[],
             default_data=True,
             files=None,
             job_id=0,
             extra_info=None,
             yes_i_know=False):
    """Load demo records.  Useful for testing purposes."""
    from invenio.utils.text import wrap_text_in_a_box, wait_for_user

    # Load cli interfaces for tools that we are going to need
    from invenio.legacy.bibupload.engine import main as bibupload
    from invenio.legacy.bibindex.engine import main as bibindex
    from invenio.legacy.bibformat.bibreformat import main as bibreformat
    from invenio.legacy.oairepository.updater import main as oairepositoryupdater
    from invenio.legacy.bibsort.daemon import main as bibsort
    from invenio.legacy.bibdocfile.cli import main as bibdocfile
    from invenio.legacy.bibrank.cli import main as bibrank

    ## Step 0: confirm deletion
    wait_for_user(
        wrap_text_in_a_box(
            "WARNING: You are going to override data in tables!"))

    if not default_data:
        print('>>> Default data has been skiped (--no-data).')
        return
    if not packages:
        packages = ['invenio_demosite.base']

    from werkzeug.utils import import_string
    map(import_string, packages)

    from invenio.ext.sqlalchemy import db
    print(">>> Going to load demo records...")
    db.session.execute("TRUNCATE schTASK")
    db.session.commit()
    if files is None:
        files = [
            pkg_resources.resource_filename(
                'invenio',
                os.path.join('testsuite', 'data', 'demo_record_marc_data.xml'))
        ]

    # upload demo site files:
    bibupload_flags = '-i'
    if extra_info is not None and 'force-recids' in extra_info:
        bibupload_flags = '-i -r --force'
    for f in files:
        job_id += 1
        for cmd in ((bibupload,
                     "bibupload -u admin %s %s" % (bibupload_flags, f)),
                    (bibupload, "bibupload %d" % (job_id))):
            if run_py_func(*cmd, passthrough=True).exit_code:
                print("ERROR: failed execution of", *cmd)
                sys.exit(1)

    i = count(job_id + 1).next
    for cmd in (
        (bibdocfile, "bibdocfile --textify --with-ocr --recid 97"),
        (bibdocfile, "bibdocfile --textify --all"),
        (bibindex, "bibindex -u admin"),
        (bibindex, "bibindex %d" % i()),
        (bibindex, "bibindex -u admin -w global"),
        (bibindex, "bibindex %d" % i()),
        (bibreformat, "bibreformat -u admin -o HB"),
        (bibreformat, "bibreformat %d" % i()),
        (bibrank, "bibrank -u admin"),
        (bibrank, "bibrank %d" % i()),
        (bibsort, "bibsort -u admin -R"),
        (bibsort, "bibsort %d" % i()),
        (oairepositoryupdater, "oairepositoryupdater -u admin"),
        (oairepositoryupdater, "oairepositoryupdater %d" % i()),
        (bibupload, "bibupload %d" % i()),
    ):
        if run_py_func(*cmd, passthrough=True).exit_code:
            print("ERROR: failed execution of", *cmd)
            sys.exit(1)
    print(">>> Demo records loaded successfully.")
Пример #27
0
def main():
    """
    Entry point for the CLI
    """
    def get_json_parameters_from_cli(option, dummy_opt_str, value, dummy_parser):
        try:
            option.parameters = json_unicode_to_utf8(json.loads(value))
        except Exception as err:
            raise optparse.OptionValueError("Cannot parse as a valid JSON serialization the provided parameters: %s. %s" % (value, err))

    def get_parameter_from_cli(option, dummy_opt_str, value, dummy_parser):
        if not hasattr(option, 'parameters'):
            option.parameters = {}
        param, value = value.split('=', 1)
        try:
            value = int(value)
        except:
            pass
        option.parameters[param] = value

    parser = optparse.OptionParser()

    plugin_group = optparse.OptionGroup(parser, "Plugin Administration Options")
    plugin_group.add_option("--list-plugins", action="store_const", dest="action", const="list-goto-plugins", help="List available GOTO plugins and their documentation")
    #plugin_group.add_option("--list-broken-plugins", action="store_const", dest="action", const="list-broken-goto-plugins", help="List broken GOTO plugins")
    parser.add_option_group(plugin_group)

    redirection_group = optparse.OptionGroup(parser, "Redirection Manipultation Options")
    redirection_group.add_option("-r", "--register-redirection", metavar="LABEL", action="store", dest="register", help="Register a redirection with the provided LABEL")
    redirection_group.add_option("-u", "--update-redirection", metavar="LABEL", action="store", dest="update", help="Update the redirection specified by the provided LABEL")
    redirection_group.add_option("-g", "--get-redirection", metavar="LABEL", action="store", dest="get_redirection", help="Get all information about a redirection specified by LABEL")
    redirection_group.add_option("-d", "--drop-redirection", metavar="LABEL", action="store", dest="drop_redirection", help="Drop an existing redirection specified by LABEL")
    parser.add_option_group(redirection_group)

    specific_group = optparse.OptionGroup(parser, "Specific Options")
    specific_group.add_option("-P", "--plugin", metavar="PLUGIN", action="store", dest="plugin", help="Specify the plugin to use when registering or updating a redirection")
    specific_group.add_option("-j", "--json-parameters", metavar="PARAMETERS", action="callback", type="string", callback=get_json_parameters_from_cli, help="Specify the parameters to provide to the plugin (serialized in JSON)")
    specific_group.add_option("-p", "--parameter", metavar="PARAM=VALUE", action="callback", callback=get_parameter_from_cli, help="Specify a single PARAM=VALUE parameter to be provided to the plugin (alternative to the JSON serialization)", type="string")
    parser.add_option_group(specific_group)

    (options, dummy_args) = parser.parse_args()
    if options.action == "list-goto-plugins":
        print("GOTO plugins found:")
        for component, goto in REDIRECT_METHODS.items():
            print(component + ' -> ' + getattr(goto, '__doc__',
                                               'No documentation'))
    #elif options.action == 'list-broken-goto-plugins':
    #    print "Broken GOTO plugins found:"
    #    for component, error in REDIRECT_METHODS.get_broken_plugins().items():
    #        print component + '->' + str(error)
    elif options.register:
        label = options.register
        plugin = options.plugin
        parameters = getattr(options, 'parameters', {})
        if not plugin in REDIRECT_METHODS:
            parser.error("%s is not a valid plugin" % plugin)
        if is_redirection_label_already_taken(label):
            parser.error("The specified label %s is already taken" % label)
        register_redirection(label, plugin, parameters)
        print("The redirection %s was successfully registered for the plugin %s with parameters %s" % (label, plugin, parameters))
    elif options.update:
        label = options.update
        if not is_redirection_label_already_taken(label):
            parser.error("The specified label %s does not exist" % label)
        redirection_data = get_redirection_data(label)
        plugin = options.plugin or redirection_data['plugin']
        parameters = options.parameters or redirection_data['parameters']
        if not plugin in REDIRECT_METHODS:
            parser.error("%s is not a valid plugin" % plugin)
        update_redirection(label, plugin, parameters=None)
        print("The redirection %s was successfully updated for the plugin %s with parameters %s" % (label, plugin, parameters))
    elif options.get_redirection:
        label = options.get_redirection
        if not is_redirection_label_already_taken(label):
            parser.error("The specified label %s does not exist" % label)
        print(get_redirection_data(label))
    elif options.drop_redirection:
        label = options.drop_redirection
        if is_redirection_label_already_taken(label):
            wait_for_user("Are you sure you want to drop the redirection: %s\n%s" % (label, get_redirection_data(label)))
            drop_redirection(label)
            print("The redirection %s was successfully dropped" % label)
        else:
            print("The specified label %s is not registered with any redirection" % label)
    else:
        parser.print_help()