<commit_before>import os.path as osp
import logging

import click

from wepy.orchestration.orchestrator import deserialize_orchestrator, \
                                            reconcile_orchestrators, \
                                            Orchestrator, \
                                            recover_run_by_time

from wepy.reporter.hdf5 import WepyHDF5Reporter
from wepy.hdf5 import WepyHDF5

ORCHESTRATOR_DEFAULT_FILENAME = \
            Orchestrator.ORCH_FILENAME_TEMPLATE.format(config=Orchestrator.DEFAULT_CONFIG_NAME,
                                                       narration=Orchestrator.DEFAULT_NARRATION)

@click.group()
def cli():
    pass

def set_loglevel(loglevel):

    # try to cast the loglevel as an integer. If that fails interpret
    # it as a string.
    try:
        loglevel_num = int(loglevel)
    except ValueError:
        loglevel_num = getattr(logging, loglevel, None)

    # if no such log level exists in logging the string was invalid
    if loglevel_num is None:
        raise ValueError("invalid log level given")

    logging.basicConfig(level=loglevel_num)

START_HASH = '<start_hash>'
CURDIR = '<curdir>'

def settle_run_options(n_workers=None,
                       job_dir=None,
                       job_name=None,
                       narration=None):

    # the default for the job name is the start hash if none is given
    if job_name == START_HASH:
        job_name = start_hash

    # if the job_name is given and the default value for th job_dir is
    # given we set the job-dir as the job_name
    if job_name is not None and job_dir == CURDIR:
            job_dir = job_name

    # if the special value for curdir is given we get the systems
    # current directory, this is the default.
    if job_dir == CURDIR:
        job_dir = osp.curdir

    # normalize the job_dir
    job_dir = osp.realpath(job_dir)

    return n_workers, job_dir, job_name, narration

@click.option('--log', default="WARNING")
@click.option('--n-workers', type=click.INT)
@click.option('--checkpoint-freq', default=None, type=click.INT)
@click.option('--job-dir', default=CURDIR, type=click.Path(writable=True))
@click.option('--job-name', default=START_HASH)
@click.option('--narration', default="")
@click.argument('n_cycle_steps', type=click.INT)
@click.argument('run_time', type=click.FLOAT)
@click.argument('start_hash')
@click.argument('orchestrator', type=click.File(mode='rb'))
@click.command()
def run(log, n_workers, checkpoint_freq, job_dir, job_name, narration,
        n_cycle_steps, run_time, start_hash, orchestrator):

    set_loglevel(log)

    # settle what the defaults etc. are for the different options as they are interdependent
    n_workers, job_dir, job_name, narration = settle_run_options(n_workers=n_workers,
                                                                 job_dir=job_dir,
                                                                 job_name=job_name,
                                                                 narration=narration)

    orch = deserialize_orchestrator(orchestrator.read())

    logging.info("Orchestrator loaded")

    start_hash, end_hash = orch.orchestrate_snapshot_run_by_time(start_hash,
                                                                 run_time, n_cycle_steps,
                                                                 checkpoint_freq=checkpoint_freq,
                                                                 work_dir=job_dir,
                                                                 config_name=job_name,
                                                                 narration=narration,
                                                                 n_workers=n_workers)

    # write the run tuple out to the log
    run_line_str = "Run start and end hashes: {}, {}".format(start_hash, end_hash)

    # log it
    logging.info(run_line_str)

    # also put it to the terminal
    click.echo(run_line_str)

@click.option('--n-workers', type=click.INT)
@click.option('--checkpoint-freq', default=None, type=click.INT)
@click.option('--job-dir', default=CURDIR, type=click.Path(writable=True))
@click.option('--job-name', default=START_HASH)
@click.option('--narration', default="recovery")
@click.argument('n_cycle_steps', type=click.INT)
@click.argument('run_time', type=click.FLOAT)
@click.argument('checkpoint', type=click.File(mode='rb'))
@click.argument('start_hash')
@click.argument('orchestrator', type=click.File(mode='rb'))
@click.command()
def recover(logging, n_workers, checkpoint_freq, job_dir, job_name, narration,
            n_cycle_steps, run_time, checkpoint, start_hash, orchestrator):

    set_loglevel(logging)

    n_workers, job_dir, job_name, narration = settle_run_options(n_workers=n_workers,
                                                                 job_dir=job_dir,
                                                                 job_name=job_name,
                                                                 narration=narration)

    orch = deserialize_orchestrator(orchestrator.read())

    logging.info("Orchestrator loaded")

    checkpoint_orch = deserialize_orchestrator(checkpoint.read())

    logging.info("Checkpoint loadeded")

    # run the continuation from the new orchestrator with the update
    # from the checkpoint
    new_orch, run_tup = recover_run_by_time(orch, checkpoint_orch,
                                            run_time, n_cycle_steps,<commit_msg>fixed error in cli for recover<commit_after>import os.path as osp
import logging

import click

from wepy.orchestration.orchestrator import deserialize_orchestrator, \
                                            reconcile_orchestrators, \
                                            Orchestrator, \
                                            recover_run_by_time

from wepy.reporter.hdf5 import WepyHDF5Reporter
from wepy.hdf5 import WepyHDF5

ORCHESTRATOR_DEFAULT_FILENAME = \
            Orchestrator.ORCH_FILENAME_TEMPLATE.format(config=Orchestrator.DEFAULT_CONFIG_NAME,
                                                       narration=Orchestrator.DEFAULT_NARRATION)

@click.group()
def cli():
    pass

def set_loglevel(loglevel):

    # try to cast the loglevel as an integer. If that fails interpret
    # it as a string.
    try:
        loglevel_num = int(loglevel)
    except ValueError:
        loglevel_num = getattr(logging, loglevel, None)

    # if no such log level exists in logging the string was invalid
    if loglevel_num is None:
        raise ValueError("invalid log level given")

    logging.basicConfig(level=loglevel_num)

START_HASH = '<start_hash>'
CURDIR = '<curdir>'

def settle_run_options(n_workers=None,
                       job_dir=None,
                       job_name=None,
                       narration=None):

    # the default for the job name is the start hash if none is given
    if job_name == START_HASH:
        job_name = start_hash

    # if the job_name is given and the default value for th job_dir is
    # given we set the job-dir as the job_name
    if job_name is not None and job_dir == CURDIR:
            job_dir = job_name

    # if the special value for curdir is given we get the systems
    # current directory, this is the default.
    if job_dir == CURDIR:
        job_dir = osp.curdir

    # normalize the job_dir
    job_dir = osp.realpath(job_dir)

    return n_workers, job_dir, job_name, narration

@click.option('--log', default="WARNING")
@click.option('--n-workers', type=click.INT)
@click.option('--checkpoint-freq', default=None, type=click.INT)
@click.option('--job-dir', default=CURDIR, type=click.Path(writable=True))
@click.option('--job-name', default=START_HASH)
@click.option('--narration', default="")
@click.argument('n_cycle_steps', type=click.INT)
@click.argument('run_time', type=click.FLOAT)
@click.argument('start_hash')
@click.argument('orchestrator', type=click.File(mode='rb'))
@click.command()
def run(log, n_workers, checkpoint_freq, job_dir, job_name, narration,
        n_cycle_steps, run_time, start_hash, orchestrator):

    set_loglevel(log)

    # settle what the defaults etc. are for the different options as they are interdependent
    n_workers, job_dir, job_name, narration = settle_run_options(n_workers=n_workers,
                                                                 job_dir=job_dir,
                                                                 job_name=job_name,
                                                                 narration=narration)

    orch = deserialize_orchestrator(orchestrator.read())

    logging.info("Orchestrator loaded")

    start_hash, end_hash = orch.orchestrate_snapshot_run_by_time(start_hash,
                                                                 run_time, n_cycle_steps,
                                                                 checkpoint_freq=checkpoint_freq,
                                                                 work_dir=job_dir,
                                                                 config_name=job_name,
                                                                 narration=narration,
                                                                 n_workers=n_workers)

    # write the run tuple out to the log
    run_line_str = "Run start and end hashes: {}, {}".format(start_hash, end_hash)

    # log it
    logging.info(run_line_str)

    # also put it to the terminal
    click.echo(run_line_str)

@click.option('--log', default="WARNING")
@click.option('--n-workers', type=click.INT)
@click.option('--checkpoint-freq', default=None, type=click.INT)
@click.option('--job-dir', default=CURDIR, type=click.Path(writable=True))
@click.option('--job-name', default=START_HASH)
@click.option('--narration', default="recovery")
@click.argument('n_cycle_steps', type=click.INT)
@click.argument('run_time', type=click.FLOAT)
@click.argument('checkpoint', type=click.File(mode='rb'))
@click.argument('start_hash')
@click.argument('orchestrator', type=click.File(mode='rb'))
@click.command()
def recover(log, n_workers, checkpoint_freq, job_dir, job_name, narration,
            n_cycle_steps, run_time, checkpoint, start_hash, orchestrator):

    set_loglevel(log)

    n_workers, job_dir, job_name, narration = settle_run_options(n_workers=n_workers,
                                                                 job_dir=job_dir,
                                                                 job_name=job_name,
                                                                 narration=narration)

    orch = deserialize_orchestrator(orchestrator.read())

    logging.info("Orchestrator loaded")

    checkpoint_orch = deserialize_orchestrator(checkpoint.read())

    logging.info("Checkpoint loadeded")

    # run the continuation from the new orchestrator with the update
    # from the checkpoint
    new_orch, run_tup = recover_run_by_time(orch, checkpoint_orch,
                                            run_time, n_cycle_steps,
{'fraction_non_alphanumeric': 0.08955223880597014, 'fraction_numerical': 0.0007236544549977386, 'mean_word_length': 2.0100735093928668, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 68, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3680250', 'n_tokens_mistral': 3044, 'n_tokens_neox': 2983, 'n_words': 680}