From e8542b8acd9e951cbc2a1736abbae59cf91d8fd2 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Sun, 16 Jun 2024 03:27:06 +0200 Subject: [PATCH] Generate: split ERmain out of main (#3515) --- Generate.py | 9 +++++---- test/hosting/generate.py | 3 ++- test/programs/test_generate.py | 7 ++++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Generate.py b/Generate.py index 1fbb9e76a483..d7dd6523e7f1 100644 --- a/Generate.py +++ b/Generate.py @@ -65,7 +65,7 @@ def get_seed_name(random_source) -> str: return f"{random_source.randint(0, pow(10, seeddigits) - 1)}".zfill(seeddigits) -def main(args=None): +def main(args=None) -> Tuple[argparse.Namespace, int]: # __name__ == "__main__" check so unittests that already imported worlds don't trip this. if __name__ == "__main__" and "worlds" in sys.modules: raise Exception("Worlds system should not be loaded before logging init.") @@ -237,8 +237,7 @@ def main(args=None): with open(os.path.join(args.outputpath if args.outputpath else ".", f"generate_{seed_name}.yaml"), "wt") as f: yaml.dump(important, f) - from Main import main as ERmain - return ERmain(erargs, seed) + return erargs, seed def read_weights_yamls(path) -> Tuple[Any, ...]: @@ -547,7 +546,9 @@ def roll_alttp_settings(ret: argparse.Namespace, weights): if __name__ == '__main__': import atexit confirmation = atexit.register(input, "Press enter to close.") - multiworld = main() + erargs, seed = main() + from Main import main as ERmain + multiworld = ERmain(erargs, seed) if __debug__: import gc import sys diff --git a/test/hosting/generate.py b/test/hosting/generate.py index 356cbcca25a0..d5d39dc95ee0 100644 --- a/test/hosting/generate.py +++ b/test/hosting/generate.py @@ -26,6 +26,7 @@ def _generate_local_inner(games: Iterable[str], with TemporaryDirectory() as players_dir: with TemporaryDirectory() as output_dir: import Generate + import Main for n, game in enumerate(games, 1): player_path = Path(players_dir) / f"{n}.yaml" @@ -42,7 +43,7 @@ def _generate_local_inner(games: Iterable[str], sys.argv = [sys.argv[0], "--seed", str(hash(tuple(games))), "--player_files_path", players_dir, "--outputpath", output_dir] - Generate.main() + Main.main(*Generate.main()) output_files = list(Path(output_dir).glob('*.zip')) assert len(output_files) == 1 final_file = dest / output_files[0].name diff --git a/test/programs/test_generate.py b/test/programs/test_generate.py index 887a417ec9f9..9281c9c753cd 100644 --- a/test/programs/test_generate.py +++ b/test/programs/test_generate.py @@ -9,6 +9,7 @@ from tempfile import TemporaryDirectory import Generate +import Main class TestGenerateMain(unittest.TestCase): @@ -58,7 +59,7 @@ def test_generate_absolute(self): '--player_files_path', str(self.abs_input_dir), '--outputpath', self.output_tempdir.name] print(f'Testing Generate.py {sys.argv} in {os.getcwd()}') - Generate.main() + Main.main(*Generate.main()) self.assertOutput(self.output_tempdir.name) @@ -67,7 +68,7 @@ def test_generate_relative(self): '--player_files_path', str(self.rel_input_dir), '--outputpath', self.output_tempdir.name] print(f'Testing Generate.py {sys.argv} in {os.getcwd()}') - Generate.main() + Main.main(*Generate.main()) self.assertOutput(self.output_tempdir.name) @@ -86,7 +87,7 @@ def test_generate_yaml(self): sys.argv = [sys.argv[0], '--seed', '0', '--outputpath', self.output_tempdir.name] print(f'Testing Generate.py {sys.argv} in {os.getcwd()}, player_files_path={self.yaml_input_dir}') - Generate.main() + Main.main(*Generate.main()) finally: user_path.cached_path = user_path_backup