From 6425c0026833e5ec2af76a43d463f1434958e34e Mon Sep 17 00:00:00 2001 From: Matt Fleming Date: Thu, 14 Mar 2024 17:51:07 +0000 Subject: [PATCH] Make it easier to call hunter from scripts (#17) * Pass in Config object to main function This allows scripts to call hunter.script_main() directly by building a Config object in-memory instead of needing to create files on disk. * Allow scripts to pass arguments to script_main() The default behaviour is to use sys.argv. --- hunter/main.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/hunter/main.py b/hunter/main.py index 256dc5b..70e6da4 100644 --- a/hunter/main.py +++ b/hunter/main.py @@ -462,6 +462,16 @@ def analysis_options_from_args(args: argparse.Namespace) -> AnalysisOptions: def main(): + try: + conf = config.load_config() + except ConfigError as err: + logging.error(err.message) + exit(1) + + script_main(conf) + + +def script_main(conf: Config, args: List[str] = None): logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO) parser = argparse.ArgumentParser(description="Hunts performance regressions in Fallout results") @@ -530,8 +540,7 @@ def main(): ) try: - args = parser.parse_args() - conf = config.load_config() + args = parser.parse_args(args=args) hunter = Hunter(conf) if args.command == "list-groups": @@ -619,9 +628,6 @@ def main(): if args.command is None: parser.print_usage() - except ConfigError as err: - logging.error(err.message) - exit(1) except TestConfigError as err: logging.error(err.message) exit(1)