<commit_before>"""\
Run this to upgrade the site.

Examples

To update on the production server:

    %(prog)s production.ini

For the development.ini you must supply the paster app name:

    %(prog)s development.ini --app-name app

"""
import logging
import transaction

EPILOG = __doc__

logger = logging.getLogger(__name__)


def internal_app(configfile, app_name=None, username=None):
    from webtest import TestApp
    from pyramid import paster
    app = paster.get_app(configfile, app_name)
    if not username:
        username = 'IMPORT'
    environ = {
        'HTTP_ACCEPT': 'application/json',
        'REMOTE_USER': username,
    }
    return TestApp(app, environ)


def run(testapp):
    root = testapp.app.root_factory(testapp.app)
    for collection_name in ['biosample', 'experiment', 'dataset', 'antibody_approval', 'antibody_characterization', 'biosample_characterization', 'rnai_characterization' , 'construct_characterization']:
        collection = root[collection_name]
        count = 0
        errors = 0
        logger.info('Upgrading %s', collection_name)
        for uuid in collection:
            count += 1
            try:
                testapp.patch_json('/%s' % uuid, {})
            except Exception:
                logger.exception('Upgrade failed for: /%s/%s', collection_name, uuid)
                errors += 1
        logger.info('Upgraded %s: %d (errors: %d)', collection_name, count, errors)


def main():
    import argparse
    parser = argparse.ArgumentParser(
        description="Update links and keys", epilog=EPILOG,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument('--app-name', help="Pyramid app name in configfile")
    parser.add_argument('--abort', action='store_true', help="Rollback transaction")
    parser.add_argument('config_uri', help="path to configfile")
    args = parser.parse_args()

    logging.basicConfig()
    testapp = internal_app(args.config_uri, args.app_name)

    # Loading app will have configured from config file. Reconfigure here:
    logging.getLogger('encoded').setLevel(logging.DEBUG)

    run(testapp)


if __name__ == '__main__':
    main()
<commit_msg>Add document to upgrade command; make item type an argument.<commit_after>"""\
Run this to upgrade the site.

Examples

To update on the production server:

    %(prog)s production.ini

For the development.ini you must supply the paster app name:

    %(prog)s development.ini --app-name app

"""
import logging

EPILOG = __doc__

logger = logging.getLogger(__name__)


DEFAULT_COLLECTIONS = [
    'biosample',
    'experiment',
    'dataset',
    'antibody_approval',
    'antibody_characterization',
    'biosample_characterization',
    'rnai_characterization',
    'construct_characterization',
    'document',
]


def internal_app(configfile, app_name=None, username=None):
    from webtest import TestApp
    from pyramid import paster
    app = paster.get_app(configfile, app_name)
    if not username:
        username = 'IMPORT'
    environ = {
        'HTTP_ACCEPT': 'application/json',
        'REMOTE_USER': username,
    }
    return TestApp(app, environ)


def run(testapp, collections):
    if not collections:
        collections = DEFAULT_COLLECTIONS
    root = testapp.app.root_factory(testapp.app)
    for collection_name in collections:
        collection = root[collection_name]
        count = 0
        errors = 0
        logger.info('Upgrading %s', collection_name)
        for uuid in collection:
            count += 1
            try:
                testapp.patch_json('/%s' % uuid, {})
            except Exception:
                logger.exception('Upgrade failed for: /%s/%s', collection_name, uuid)
                errors += 1
        logger.info('Upgraded %s: %d (errors: %d)', collection_name, count, errors)


def main():
    import argparse
    parser = argparse.ArgumentParser(
        description="Update links and keys", epilog=EPILOG,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument('--app-name', help="Pyramid app name in configfile")
    parser.add_argument('--item-type', action='append', help="Item type")
    parser.add_argument('config_uri', help="path to configfile")
    args = parser.parse_args()

    logging.basicConfig()
    testapp = internal_app(args.config_uri, args.app_name)

    # Loading app will have configured from config file. Reconfigure here:
    logging.getLogger('encoded').setLevel(logging.DEBUG)

    run(testapp, args.item_type)


if __name__ == '__main__':
    main()

{'fraction_non_alphanumeric': 0.10836252189141857, 'fraction_numerical': 0.0017513134851138354, 'mean_word_length': 3.146098003629764, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1533154', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1336, 'n_words': 347}