|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import typing |
| 4 | + |
| 5 | +from .. import commands |
| 6 | +from .. import comparison |
| 7 | +from .. import files |
| 8 | +from . import cli_classes |
| 9 | +from . import cli_summary |
| 10 | +from . import cli_utils |
| 11 | + |
| 12 | +if typing.TYPE_CHECKING: |
| 13 | + import argparse |
| 14 | + |
| 15 | + |
| 16 | +def run_cli() -> None: |
| 17 | + try: |
| 18 | + # parse inputs |
| 19 | + args = parse_args() |
| 20 | + |
| 21 | + if set(args.steps) in [ |
| 22 | + {'compare'}, |
| 23 | + {'collect'}, |
| 24 | + {'collect', 'compare'}, |
| 25 | + ]: |
| 26 | + rerun = True |
| 27 | + else: |
| 28 | + rerun = args.rerun |
| 29 | + |
| 30 | + # load commands |
| 31 | + if rerun: |
| 32 | + if args.dir is not None: |
| 33 | + comparison_dir = args.dir |
| 34 | + else: |
| 35 | + comparison_dir = files.get_most_recent_comparison_dir() |
| 36 | + batches = files.load_commands(comparison_dir) |
| 37 | + else: |
| 38 | + comparison_dir, batches = create_command_batches(args) |
| 39 | + files.save_commands(comparison_dir=comparison_dir, batches=batches) |
| 40 | + |
| 41 | + # summarize |
| 42 | + cli_summary.summarize_args(args, comparison_dir, batches) |
| 43 | + |
| 44 | + # perform comparison |
| 45 | + comparison.perform_comparison( |
| 46 | + batches=batches, |
| 47 | + comparison_dir=comparison_dir, |
| 48 | + steps=args.steps, |
| 49 | + ) |
| 50 | + |
| 51 | + # enter interactive session |
| 52 | + if args.interactive or args.scan_interactive: |
| 53 | + print('entering interactive session') |
| 54 | + if args.scan_interactive: |
| 55 | + all_data: typing.Any = files.scan_all_data(comparison_dir) |
| 56 | + else: |
| 57 | + all_data = files.load_all_data(comparison_dir) |
| 58 | + cli_utils.open_interactive_session({'data': all_data}) |
| 59 | + except Exception as e: |
| 60 | + if args.debug: |
| 61 | + cli_utils._enter_debugger() |
| 62 | + else: |
| 63 | + raise e |
| 64 | + |
| 65 | + |
| 66 | +def parse_args() -> argparse.Namespace: |
| 67 | + parser = cli_classes.CryoTestArgParser() |
| 68 | + # |
| 69 | + # specifying batch args |
| 70 | + parser.add_argument( |
| 71 | + '-e', |
| 72 | + '--executable', |
| 73 | + '--executables', |
| 74 | + nargs='+', |
| 75 | + help='executable(s) to use', |
| 76 | + ) |
| 77 | + parser.add_argument('--rpc', nargs='+', help='rpc endpoint(s) to use') |
| 78 | + parser.add_argument( |
| 79 | + '-d', |
| 80 | + '--datatype', |
| 81 | + '--datatypes', |
| 82 | + nargs='+', |
| 83 | + help='datatype(s) to collect', |
| 84 | + ) |
| 85 | + parser.add_argument( |
| 86 | + '-p', '--python', action='store_true', help='use python for all batches' |
| 87 | + ) |
| 88 | + parser.add_argument( |
| 89 | + '--cli-vs-python', action='store_true', help='compare cli to python' |
| 90 | + ) |
| 91 | + # |
| 92 | + # specifying how to run |
| 93 | + parser.add_argument( |
| 94 | + '-r', '--rerun', action='store_true', help='rerun previous comparison' |
| 95 | + ) |
| 96 | + parser.add_argument('--label', help='name of comparison') |
| 97 | + parser.add_argument( |
| 98 | + '-s', |
| 99 | + '--steps', |
| 100 | + help='steps to perform {setup, collect, compare}', |
| 101 | + nargs='+', |
| 102 | + choices=['setup', 'collect', 'compare'], |
| 103 | + default=['setup', 'collect', 'compare'], |
| 104 | + ) |
| 105 | + parser.add_argument('--dir', help='directory for storing comparison data') |
| 106 | + parser.add_argument( |
| 107 | + '-i', |
| 108 | + '--interactive', |
| 109 | + action='store_true', |
| 110 | + help='load data in interactive python session', |
| 111 | + ) |
| 112 | + parser.add_argument( |
| 113 | + '--scan-interactive', |
| 114 | + action='store_true', |
| 115 | + help='scan data in interactive python session', |
| 116 | + ) |
| 117 | + parser.add_argument( |
| 118 | + '--debug', |
| 119 | + '--pdb', |
| 120 | + action='store_true', |
| 121 | + help='enter debug mode upon error', |
| 122 | + ) |
| 123 | + return parser.parse_args() |
| 124 | + |
| 125 | + |
| 126 | +def create_command_batches( |
| 127 | + args: argparse.Namespace, |
| 128 | +) -> tuple[str, dict[str, dict[str, str]]]: |
| 129 | + command_arg_combos: dict[str, list[str]] = {} |
| 130 | + common_command_args: commands.PartialCommandArgs = {} |
| 131 | + batch_specific_args: dict[str, commands.PartialCommandArgs] = {} |
| 132 | + |
| 133 | + # determine batch mode (the variables that change across batches) |
| 134 | + if args.executable is not None and len(args.executable) > 1: |
| 135 | + batch_mode = 'executable' |
| 136 | + elif args.rpc is not None and len(args.rpc) > 1: |
| 137 | + batch_mode = 'rpc' |
| 138 | + else: |
| 139 | + batch_mode = 'default' |
| 140 | + |
| 141 | + # determine comparison dir |
| 142 | + comparison_dir = files.create_comparison_dir(args.dir, args.label) |
| 143 | + |
| 144 | + # determine interface |
| 145 | + if args.python: |
| 146 | + common_command_args['interface'] = 'python' |
| 147 | + |
| 148 | + # |
| 149 | + if batch_mode == 'default': |
| 150 | + batch_specific_args = {'default': {}} |
| 151 | + |
| 152 | + # arg: executable |
| 153 | + if batch_mode == 'executable': |
| 154 | + for raw_executable in args.executable: |
| 155 | + batch_name, executable = raw_executable.split('=', maxsplit=1) |
| 156 | + batch_specific_args[batch_name] = {'executable': executable} |
| 157 | + else: |
| 158 | + if args.executable is None: |
| 159 | + common_command_args['executable'] = 'cryo' |
| 160 | + elif len(args.executable) == 1: |
| 161 | + common_command_args['executable'] = args.executable[0] |
| 162 | + else: |
| 163 | + raise Exception() |
| 164 | + |
| 165 | + # arg: rpc |
| 166 | + if batch_mode == 'rpc': |
| 167 | + for raw_rpc in args.rpcs: |
| 168 | + batch_name, rpc = raw_rpc.split('=', maxsplits=1) |
| 169 | + batch_specific_args[batch_name] = {'rpc': rpc} |
| 170 | + else: |
| 171 | + if args.rpc is None: |
| 172 | + pass |
| 173 | + elif len(args.rpc) == 1: |
| 174 | + common_command_args['rpc'] = args.rpc[0] |
| 175 | + else: |
| 176 | + raise Exception() |
| 177 | + |
| 178 | + # arg: datatypes |
| 179 | + if args.datatype is not None: |
| 180 | + command_arg_combos['datatype'] = args.datatype |
| 181 | + |
| 182 | + # if cli-vs-python, create cli and python version of each batch |
| 183 | + if args.cli_vs_python: |
| 184 | + new_batch_specific_args = {} |
| 185 | + for batch_name, batch_kwargs in batch_specific_args.items(): |
| 186 | + new_batch_specific_args[batch_name + '_cli'] = dict( |
| 187 | + batch_kwargs, interface='cli' |
| 188 | + ) |
| 189 | + new_batch_specific_args[batch_name + '_python'] = dict( |
| 190 | + batch_kwargs, interface='python' |
| 191 | + ) |
| 192 | + |
| 193 | + # arg: output_dir |
| 194 | + for batch_name in batch_specific_args.keys(): |
| 195 | + batch_specific_args[batch_name]['data_root'] = files.get_batch_data_dir( |
| 196 | + comparison_dir=comparison_dir, |
| 197 | + batch_name=batch_name, |
| 198 | + ) |
| 199 | + |
| 200 | + # generate commands |
| 201 | + batches = {} |
| 202 | + for batch_name in batch_specific_args.keys(): |
| 203 | + kwargs: commands.PartialCommandArgs = dict( |
| 204 | + **common_command_args, **batch_specific_args[batch_name] |
| 205 | + ) |
| 206 | + batches[batch_name] = commands.generate_commands( |
| 207 | + common_command_args=kwargs, |
| 208 | + command_arg_combos=command_arg_combos, |
| 209 | + ) |
| 210 | + |
| 211 | + return comparison_dir, batches |
| 212 | + |
0 commit comments