<commit_before>import logging

from modularodm import Q

from website.models import NodeLog
from website.app import init_app

from scripts import utils as script_utils


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def get_targets():
    logs = NodeLog.find(
            Q('action', 'eq', 'embargo_approved') |
            Q('action', 'eq', 'embargo_initiated') |
            Q('action', 'eq', 'retraction_approved') |
            Q('action', 'eq', 'retraction_initiated') |
            Q('action', 'eq', 'registration_initiated')
    )
    return logs


def migrate_log(logs):
    for log in logs:
        if log.node.registered_from:
            log.params['node'] = log.node.registered_from_id
        else:
            log.params['node'] = log.node.logs[0].node._id
        log.save()
        logger.info('Finished migrate log {}: registration action {} of node {}'.format(log._id, log.action, log.node))


def main(dry_run):
    logs = get_targets()
    if not dry_run:
        migrate_log(logs)
        logger.info('Finished migrate {} logs '.format(len(logs)))


if __name__ == '__main__':
    import sys
    script_utils.add_file_logger(logger, __file__)
    dry_run = 'dry' in sys.argv
    init_app(set_backends=True, routes=False)
    main(dry_run=dry_run)<commit_msg>add dry run function<commit_after>import logging

from modularodm import Q

from website.models import NodeLog
from website.app import init_app

from scripts import utils as script_utils
from framework.transactions.context import TokuTransaction

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def get_targets():
    logs = NodeLog.find(
            Q('action', 'eq', 'embargo_approved') |
            Q('action', 'eq', 'embargo_initiated') |
            Q('action', 'eq', 'retraction_approved') |
            Q('action', 'eq', 'retraction_initiated') |
            Q('action', 'eq', 'registration_initiated')
    )
    return logs


def migrate_log(logs):
    for log in logs:
        if log.node.registered_from:
            log.params['node'] = log.node.registered_from_id
        else:
            log.params['node'] = log.node.logs[0].node._id
        log.save()
        logger.info('Finished migrate log {}: registration action {} of node {}'.format(log._id, log.action, log.node))


def main(dry_run):
    logs = get_targets()
    if not dry_run:
        migrate_log(logs)
        logger.info('Finished migrate {} logs '.format(len(logs)))
    else:
        raise RuntimeError('Dry Run -- Transaction rolled back')


if __name__ == '__main__':
    import sys
    script_utils.add_file_logger(logger, __file__)
    dry_run = 'dry' in sys.argv
    init_app(set_backends=True, routes=False)
    with TokuTransaction():
        main(dry_run=dry_run)
{'fraction_non_alphanumeric': 0.12446351931330472, 'fraction_numerical': 0.000715307582260372, 'mean_word_length': 3.071324599708879, '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': '112404', 'n_tokens_mistral': 900, 'n_tokens_neox': 859, 'n_words': 203}