def test_main(self):
        from invenio.config import CFG_TMPDIR
        from invenio.legacy.docextract.scripts.convert_journals import main
        xml = """<record>
            <datafield tag="999" ind1="C" ind2="5">
                <subfield code="s">Test Journal Name,100,10</subfield>
            </datafield>
        </record>"""
        xml_temp_file = NamedTemporaryFile(dir=CFG_TMPDIR)
        xml_temp_file.write(xml)
        xml_temp_file.flush()

        kb = "TEST JOURNAL NAME---Converted"
        kb_temp_file = NamedTemporaryFile(dir=CFG_TMPDIR)
        kb_temp_file.write(kb)
        kb_temp_file.flush()

        dest_temp_fd, dest_temp_path = mkstemp(dir=CFG_TMPDIR)
        try:
            os.close(dest_temp_fd)
            run_py_func(main, ['convert_journals', xml_temp_file.name,
                               '--kb', kb_temp_file.name,
                               '-o', dest_temp_path])

            transformed_xml = open(dest_temp_path).read()
            self.assertXmlEqual(transformed_xml, """<?xml version="1.0" encoding="UTF-8"?>
<collection xmlns="http://www.loc.gov/MARC21/slim">
<record><datafield ind1="C" ind2="5" tag="999"><subfield code="s">Converted,100,10</subfield></datafield></record>
</collection>""")
        finally:
            os.unlink(dest_temp_path)
    def test_main(self):
        from invenio.config import CFG_TMPDIR
        from invenio.legacy.docextract.scripts.convert_journals import main
        xml = """<record>
            <datafield tag="999" ind1="C" ind2="5">
                <subfield code="s">Test Journal Name,100,10</subfield>
            </datafield>
        </record>"""
        xml_temp_file = NamedTemporaryFile(dir=CFG_TMPDIR)
        xml_temp_file.write(xml)
        xml_temp_file.flush()

        kb = "TEST JOURNAL NAME---Converted"
        kb_temp_file = NamedTemporaryFile(dir=CFG_TMPDIR)
        kb_temp_file.write(kb)
        kb_temp_file.flush()

        dest_temp_fd, dest_temp_path = mkstemp(dir=CFG_TMPDIR)
        try:
            os.close(dest_temp_fd)
            run_py_func(main, [
                'convert_journals', xml_temp_file.name, '--kb',
                kb_temp_file.name, '-o', dest_temp_path
            ])

            transformed_xml = open(dest_temp_path).read()
            self.assertXmlEqual(
                transformed_xml, """<?xml version="1.0" encoding="UTF-8"?>
<collection xmlns="http://www.loc.gov/MARC21/slim">
<record><datafield ind1="C" ind2="5" tag="999"><subfield code="s">Converted,100,10</subfield></datafield></record>
</collection>""")
        finally:
            os.unlink(dest_temp_path)
Exemplo n.º 3
0
    def test_signals_usage(self):
        """ Test signal handling. """
        from invenio.base.scripts.database import main as db_main
        from invenio.base.signals import pre_command, post_command
        from invenio.base.manage import main, version as im_version

        def pre_handler_version(sender, *args, **kwargs):
            print('>>> pre_handler_version')

        def post_handler_version(sender, *args, **kwargs):
            print('>>> post_handler_version')

        # Bind only `inveniomanage version` command to pre/post handler.
        pre_command.connect(pre_handler_version, sender=im_version)
        post_command.connect(post_handler_version, sender=im_version)

        def pre_handler_general_test(sender, *args, **kwargs):
            print('>>> pre_handler_general')

        def post_handler_general_test(sender, *args, **kwargs):
            print('>>> post_handler_general')

        # Bind all commands to pre/post general handler.
        pre_command.connect(pre_handler_general_test)
        pre_command.connect(post_handler_general_test)

        # Expect both version and general handlers.
        out = run_py_func(main, 'inveniomanage version').out

        lines = out.split('\n')
        expected = ('>>> pre_handler_version', '>>> post_handler_version',
                    '>>> pre_handler_general', '>>> post_handler_general')
        for line in expected:
            self.assertTrue(line in lines,
                            "%s was not found in output %s" % (line, lines))

        # Expect only general handlers.
        out = run_py_func(db_main, 'database uri').out

        lines = out.split('\n')
        expected = ('>>> pre_handler_general', '>>> post_handler_general')
        for line in expected:
            self.assertTrue(line in lines,
                            "%s was not found in output %s" % (line, lines))

        notexpected = ('>>> pre_handler_version', '>>> post_handler_version')
        for line in notexpected:
            self.assertFalse(
                line in lines,
                "%s was not expected in output %s" % (line, lines))
Exemplo n.º 4
0
    def test_signals_usage(self):
        """ Test signal handling. """
        from invenio.base.scripts.database import main as db_main
        from invenio.base.signals import pre_command, post_command
        from invenio.base.manage import main, version as im_version

        def pre_handler_version(sender, *args, **kwargs):
            print(">>> pre_handler_version")

        def post_handler_version(sender, *args, **kwargs):
            print(">>> post_handler_version")

        # Bind only `inveniomanage version` command to pre/post handler.
        pre_command.connect(pre_handler_version, sender=im_version)
        post_command.connect(post_handler_version, sender=im_version)

        def pre_handler_general_test(sender, *args, **kwargs):
            print(">>> pre_handler_general")

        def post_handler_general_test(sender, *args, **kwargs):
            print(">>> post_handler_general")

        # Bind all commands to pre/post general handler.
        pre_command.connect(pre_handler_general_test)
        pre_command.connect(post_handler_general_test)

        # Expect both version and general handlers.
        out = run_py_func(main, "inveniomanage version").out

        lines = out.split("\n")
        expected = (
            ">>> pre_handler_version",
            ">>> post_handler_version",
            ">>> pre_handler_general",
            ">>> post_handler_general",
        )
        for line in expected:
            self.assertTrue(line in lines, "%s was not found in output %s" % (line, lines))

        # Expect only general handlers.
        out = run_py_func(db_main, "database uri").out

        lines = out.split("\n")
        expected = (">>> pre_handler_general", ">>> post_handler_general")
        for line in expected:
            self.assertTrue(line in lines, "%s was not found in output %s" % (line, lines))

        notexpected = (">>> pre_handler_version", ">>> post_handler_version")
        for line in notexpected:
            self.assertFalse(line in lines, "%s was not expected in output %s" % (line, lines))
Exemplo n.º 5
0
def cli_cmd_reset(sender, yes_i_know=False, drop=True, **kwargs):
    """Reset legacy values."""
    from invenio.ext.sqlalchemy import db
    from invenio.modules.accounts.models import User
    # from invenio.legacy.inveniocfg import cli_cmd_reset_sitename
    # from invenio.legacy.inveniocfg import cli_cmd_reset_fieldnames
    from invenio.legacy.bibsort.daemon import main as bibsort
    from invenio.modules.access.scripts.webaccessadmin import main as \
        webaccessadmin

    # FIXME refactor fixtures so these calls are not needed
    # cli_cmd_reset_sitename(conf)
    User.query.filter_by(id=1).delete()
    siteadminemail = current_app.config.get('CFG_SITE_ADMIN_EMAIL')
    u = User(id=1, email=siteadminemail, password='', note=1, nickname='admin')
    db.session.add(u)
    db.session.commit()
    # cli_cmd_reset_fieldnames(conf)

    for cmd in (
        (webaccessadmin, "webaccessadmin -u admin -c -a -D"),
        (bibsort, "bibsort -u admin --load-config"),
        (bibsort, "bibsort 1"),
    ):
        if run_py_func(*cmd, passthrough=True).exit_code:
            print("ERROR: failed execution of", *cmd)
            sys.exit(1)
Exemplo n.º 6
0
def cli_cmd_reset(sender, yes_i_know=False, drop=True, **kwargs):
    """Reset legacy values."""
    from invenio.ext.sqlalchemy import db
    from invenio.modules.accounts.models import User
    # from invenio.legacy.inveniocfg import cli_cmd_reset_sitename
    # from invenio.legacy.inveniocfg import cli_cmd_reset_fieldnames
    from invenio.legacy.bibsort.daemon import main as bibsort
    from invenio.modules.access.scripts.webaccessadmin import main as \
        webaccessadmin

    # FIXME refactor fixtures so these calls are not needed
    # cli_cmd_reset_sitename(conf)
    User.query.filter_by(id=1).delete()
    siteadminemail = current_app.config.get('CFG_SITE_ADMIN_EMAIL')
    u = User(id=1, email=siteadminemail, password='', note=1, nickname='admin')
    db.session.add(u)
    db.session.commit()
    # cli_cmd_reset_fieldnames(conf)

    for cmd in (
        (webaccessadmin, "webaccessadmin -u admin -c -a -D"),
        (bibsort, "bibsort -u admin --load-config"),
        (bibsort, "bibsort 1"),
    ):
        if run_py_func(*cmd, passthrough=True).exit_code:
            print("ERROR: failed execution of", *cmd)
            sys.exit(1)
Exemplo n.º 7
0
    def test_upgrade_show_applied_cmd(self):
        """ Test `upgrade show applied` command. """
        from invenio.modules.upgrader.manage import main
        out = run_py_func(main, 'upgrader show applied').out

        expected = '>>> Following upgrade(s) have been applied:'
        self.assertTrue(expected in out.split('\n'),
                        "%s was not found in output %s" % (expected, out))
Exemplo n.º 8
0
    def test_upgrade_show_applied_cmd(self):
        """ Test `upgrade show applied` command. """
        from invenio.modules.upgrader.manage import main

        out = run_py_func(main, "upgrader show applied").out

        expected = ">>> Following upgrade(s) have been applied:"
        self.assertTrue(expected in out.split("\n"), "%s was not found in output %s" % (expected, out))
Exemplo n.º 9
0
def cli_cmd_reset(sender, yes_i_know=False, drop=True, **kwargs):
    """Reset legacy values."""
    from invenio.modules.access.scripts.webaccessadmin import main as \
        webaccessadmin

    for cmd in ((webaccessadmin, "webaccessadmin -u admin -c -a -D"), ):
        if run_py_func(*cmd, passthrough=True).exit_code:
            print("ERROR: failed execution of", *cmd)
            sys.exit(1)
Exemplo n.º 10
0
    def test_upgrade_show_pending_cmd(self):
        """ Test `upgrade show pending` command. """
        from invenio.modules.upgrader.manage import main
        out = run_py_func(main, 'upgrader show pending').out

        lines = out.split('\n')
        expected = ('>>> Following upgrade(s) are ready to be applied:',
                    '>>> All upgrades have been applied.')
        self.assertTrue(expected[0] in lines or expected[1] in lines,
                        "%s was not found in output %s" % (expected, lines))
Exemplo n.º 11
0
    def test_upgrade_show_pending_cmd(self):
        """ Test `upgrade show pending` command. """
        from invenio.modules.upgrader.manage import main

        out = run_py_func(main, "upgrader show pending").out

        lines = out.split("\n")
        expected = (">>> Following upgrade(s) are ready to be applied:", ">>> All upgrades have been applied.")
        self.assertTrue(
            expected[0] in lines or expected[1] in lines, "%s was not found in output %s" % (expected, lines)
        )
Exemplo n.º 12
0
def cli_cmd_reset(sender, yes_i_know=False, drop=True, **kwargs):
    """Reset legacy values."""
    from invenio.modules.access.scripts.webaccessadmin import main as \
        webaccessadmin

    for cmd in (
        (webaccessadmin, "webaccessadmin -u admin -c -a -D"),
    ):
        if run_py_func(*cmd, passthrough=True).exit_code:
            print("ERROR: failed execution of", *cmd)
            sys.exit(1)
Exemplo n.º 13
0
def cli_cmd_reset(sender, yes_i_know=False, drop=True, **kwargs):
    """Reset legacy values."""
    cmds = []
    if 'invenio_access' in current_app.config['PACKAGES']:
        from invenio_access.scripts.webaccessadmin import main as \
            webaccessadmin
        cmds.append(
            (webaccessadmin, "webaccessadmin -u admin -c -a -D")
        )

    for cmd in cmds:
        if run_py_func(*cmd, passthrough=True).exit_code:
            print("ERROR: failed execution of", *cmd)
            sys.exit(1)
Exemplo n.º 14
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.")
Exemplo n.º 15
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 test_usage(self):
        from invenio.legacy.docextract.convert_journals import USAGE_MESSAGE
        from invenio.legacy.docextract.scripts.convert_journals import main

        err = run_py_func(main, ["convert_journals", "-h"]).err
        self.assert_(USAGE_MESSAGE in err)
Exemplo n.º 17
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.")
Exemplo n.º 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

    # 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.")
    def test_usage(self):
        from invenio.legacy.docextract.convert_journals import USAGE_MESSAGE
        from invenio.legacy.docextract.scripts.convert_journals import main

        err = run_py_func(main, ["convert_journals", "-h"]).err
        self.assert_(USAGE_MESSAGE in err)