def wrapper(mic_file): """ Generates the MIC Wrapper:a directory structure and commands required to run your model component using the information gathered from previous steps - You must pass the MIC_FILE (mic.yaml) as an argument using the (-f) option or run the command from the same directory as mic.yaml mic pkg wrapper -f <mic_file> Example: mic pkg wrapper -f mic/mic.yaml """ # Searches for mic file if user does not provide one mic_file = check_mic_path(mic_file) log_command(logging, "wrapper", mic_file=mic_file) try: info_start_wrapper() mic_config_file = Path(mic_file) user_execution_directory = mic_config_file.parent.parent repro_zip_trace_dir = find_dir(REPRO_ZIP_TRACE_DIR, user_execution_directory) repro_zip_trace_dir = Path(repro_zip_trace_dir) repro_zip_config_file = repro_zip_trace_dir / REPRO_ZIP_CONFIG_FILE mic_directory_path = mic_config_file.parent mic_inputs = get_inputs(mic_config_file) mic_parameters = get_parameters(mic_config_file) mic_outputs = get_outputs_mic(mic_config_file) mic_configs = get_configs(mic_config_file) mic_code = get_code(mic_config_file) spec = get_spec(mic_config_file) reprozip_spec = get_spec(repro_zip_config_file) logging.info("Generating wrapper code") code = f"""{generate_pre_runner(spec, user_execution_directory)} {generate_runner(reprozip_spec, user_execution_directory, mic_inputs, mic_outputs, mic_parameters)}""" render_bash_color(mic_directory_path) render_run_sh(mic_directory_path, mic_inputs, mic_parameters, mic_outputs, code) render_io_sh(mic_directory_path, mic_inputs, mic_parameters, mic_configs) render_output(mic_directory_path, mic_outputs, False) copy_code_to_src(mic_code, user_execution_directory, mic_directory_path / SRC_DIR) copy_config_to_src(mic_configs, user_execution_directory, mic_directory_path / SRC_DIR) info_end_wrapper(mic_directory_path / SRC_DIR / RUN_FILE) logging.info("wrapper done") except Exception as e: logging.exception(f"Wrapper failed: {e}") click.secho("Failed", fg="red") click.secho(e)
def check_config(mic_config_arg): files = get_configs(Path(mic_config_arg)) assert files == { 'basins_bsn': { 'format': 'bsn', 'path': 'TxtInOut/basins.bsn' }, 'file_cio': { 'format': 'cio', 'path': 'TxtInOut/file.cio' } }
def inputs(mic_file, custom_inputs): """ Describe the inputs of your model using the information obtained by the `trace` command. To identify which inputs have been automatically detected, execute `mic pkg inputs -f mic/mic.yaml` and then inspect the mic.yaml file - You must pass the MIC_FILE (mic.yaml) as an argument using the (-f) option or run the command from the same directory as mic.yaml - Identify undetected files in or directories in mic.yaml and add them as arguments to the `inputs` command mic pkg inputs -f <mic_file> [undetected files]... Usage example: mic pkg inputs -f mic/mic.yaml input.txt inputs_directory """ # Searches for mic file if user does not provide one mic_file = check_mic_path(mic_file) log_command(logging, "inputs", mic_file=mic_file, custom_inputs=custom_inputs) try: info_start_inputs() mic_config_file = Path(mic_file) mic_directory_path = mic_config_file.parent user_execution_directory = mic_config_file.parent.parent repro_zip_trace_dir = find_dir(REPRO_ZIP_TRACE_DIR, user_execution_directory) repro_zip_trace_dir = Path(repro_zip_trace_dir) repro_zip_config_file = repro_zip_trace_dir / REPRO_ZIP_CONFIG_FILE spec = get_spec(repro_zip_config_file) custom_inputs = [ str(user_execution_directory / Path(i).relative_to(user_execution_directory)) for i in list(custom_inputs) ] inputs_reprozip = get_inputs_outputs_reprozip( spec, user_execution_directory) logging.debug("Inputs found from reprozip: {}".format(inputs_reprozip)) # obtain config: if a file is a config cannot be a input config_files = get_configs(mic_config_file) config_files_list = [ str(user_execution_directory / item[PATH_KEY]) for key, item in config_files.items() ] if config_files else [] code_files = find_code_files(spec, inputs_reprozip, config_files_list, user_execution_directory) logging.debug("code files found from reprozip: {}".format(code_files)) new_inputs = [] inputs_reprozip += list(custom_inputs) data_dir = mic_directory_path.absolute() / DATA_DIR if data_dir.exists(): shutil.rmtree(data_dir) data_dir.mkdir() _outputs = get_outputs_reprozip(spec, user_execution_directory) for _input in inputs_reprozip: item = user_execution_directory / _input name = Path(_input).name if str(item) in config_files_list or str( item) in code_files or str(item) in _outputs: logging.info(f"Ignoring the config as an input: {item}") else: # Deleting the outputs of the inputs. if item.is_dir(): if sorted([str(i) for i in item.iterdir()]) == sorted(_outputs): logging.info(f"Skipping: {item}") else: logging.info( f"""Compressing the input directory ({name})""") zip_file = compress_directory( item, user_execution_directory) dst_dir = data_dir dst_file = dst_dir / Path(zip_file).name if dst_file.exists(): os.remove(dst_file) shutil.move(str(zip_file), str(dst_dir)) new_inputs.append(zip_file) click.secho(f"""Input {name} added """, fg="blue") logging.info("Input added: {}".format(name)) else: new_inputs.append(item) dst_file = mic_directory_path / DATA_DIR / str(item.name) shutil.copy(item, dst_file) click.secho(f"""Input {name} added """, fg="blue") logging.info("Input added: {}".format(name)) info_end_inputs(new_inputs) write_spec(mic_config_file, INPUTS_KEY, relative(new_inputs, user_execution_directory)) write_spec(mic_config_file, CODE_KEY, relative(code_files, user_execution_directory)) logging.info("inputs done") except Exception as e: logging.exception(f"Inputs failed: {e}") click.secho("Failed", fg="red") click.secho(e)
def test_get_configs(): assert get_configs(mic_1) == {'config_json': {'format': 'json', 'path': 'config.json'}} assert get_configs(mic_empty) == {}
def check_config(mic_config_arg): files = get_configs(Path(mic_config_arg)) assert files == {'config_json': {'path': 'config.json', 'format': 'json'}}