From 3b998d4085e744cdf3d6c805844cc3d28ab26b7d Mon Sep 17 00:00:00 2001 From: MasloMaslane Date: Sun, 10 Sep 2023 15:48:23 +0200 Subject: [PATCH 1/7] Add function for validating tests, change `extract_test_id` function --- src/sinol_make/commands/export/__init__.py | 5 +-- src/sinol_make/commands/gen/__init__.py | 1 + src/sinol_make/commands/inwer/__init__.py | 1 + src/sinol_make/commands/run/__init__.py | 21 ++++++------ src/sinol_make/helpers/package_util.py | 40 ++++++++++++++++++++-- tests/helpers/test_package_util.py | 1 + 6 files changed, 55 insertions(+), 14 deletions(-) diff --git a/src/sinol_make/commands/export/__init__.py b/src/sinol_make/commands/export/__init__.py index ac643d87..145ed1c1 100644 --- a/src/sinol_make/commands/export/__init__.py +++ b/src/sinol_make/commands/export/__init__.py @@ -45,7 +45,7 @@ def get_generated_tests(self): util.exit_with_error('Failed to run ingen.') tests = glob.glob(os.path.join(working_dir, f'{self.task_id}*.in')) - return [package_util.extract_test_id(test) for test in tests] + return [package_util.extract_test_id(test, self.task_id) for test in tests] def copy_package_required_files(self, target_dir: str): """ @@ -74,7 +74,7 @@ def copy_package_required_files(self, target_dir: str): tests_to_copy = [] for ext in ['in', 'out']: for test in glob.glob(os.path.join(os.getcwd(), ext, f'{self.task_id}*.{ext}')): - if package_util.extract_test_id(test) not in generated_tests: + if package_util.extract_test_id(test, self.task_id) not in generated_tests: tests_to_copy.append(test) if len(tests_to_copy) > 0: @@ -132,6 +132,7 @@ def run(self, args: argparse.Namespace): self.args = args self.task_id = package_util.get_task_id() + package_util.validate_files(self.task_id) with open(os.path.join(os.getcwd(), 'config.yml'), 'r') as config_file: config = yaml.load(config_file, Loader=yaml.FullLoader) diff --git a/src/sinol_make/commands/gen/__init__.py b/src/sinol_make/commands/gen/__init__.py index 7b8feae2..665a038b 100644 --- a/src/sinol_make/commands/gen/__init__.py +++ b/src/sinol_make/commands/gen/__init__.py @@ -93,6 +93,7 @@ def run(self, args: argparse.Namespace): self.args = args self.task_id = package_util.get_task_id() + package_util.validate_files(self.task_id) self.ingen = gen_util.get_ingen(self.task_id, args.ingen_path) print(util.info(f'Using ingen file {os.path.basename(self.ingen)}')) diff --git a/src/sinol_make/commands/inwer/__init__.py b/src/sinol_make/commands/inwer/__init__.py index 7f7dad12..97fccaeb 100644 --- a/src/sinol_make/commands/inwer/__init__.py +++ b/src/sinol_make/commands/inwer/__init__.py @@ -123,6 +123,7 @@ def run(self, args: argparse.Namespace): util.exit_if_not_package() self.task_id = package_util.get_task_id() + package_util.validate_files(self.task_id) self.inwer = inwer_util.get_inwer_path(self.task_id, args.inwer_path) if self.inwer is None: if args.inwer_path is None: diff --git a/src/sinol_make/commands/run/__init__.py b/src/sinol_make/commands/run/__init__.py index 35d556f2..cc7bd1a0 100644 --- a/src/sinol_make/commands/run/__init__.py +++ b/src/sinol_make/commands/run/__init__.py @@ -56,7 +56,7 @@ def update_group_status(group_status, new_status): return group_status -def print_view(term_width, term_height, program_groups_scores, all_results, print_data: PrintData, names, executions, +def print_view(term_width, term_height, task_id, program_groups_scores, all_results, print_data: PrintData, names, executions, groups, scores, tests, possible_score, cpus, hide_memory, config, contest, args): width = term_width - 13 # First column has 6 characters, the " | " separator has 3 characters and 4 for margin programs_in_row = width // 13 # Each program has 10 characters and the " | " separator has 3 characters @@ -191,7 +191,7 @@ def print_group_seperator(): print_group_seperator() last_group = group - print(margin + "%6s" % package_util.extract_test_id(test), end=" | ") + print(margin + "%6s" % package_util.extract_test_id(test, task_id), end=" | ") for program in program_group: lang = package_util.get_file_lang(program) result = all_results[program][package_util.get_group(test)][test] @@ -275,9 +275,9 @@ def extract_file_name(self, file_path): def get_group(self, test_path): - if package_util.extract_test_id(test_path).endswith("ocen"): + if package_util.extract_test_id(test_path, self.ID).endswith("ocen"): return 0 - return int("".join(filter(str.isdigit, package_util.extract_test_id(test_path)))) + return int("".join(filter(str.isdigit, package_util.extract_test_id(test_path, self.ID)))) def get_executable_key(self, executable): @@ -580,7 +580,7 @@ def run_solution(self, data_for_execution: ExecutionData): """ (name, executable, test, time_limit, memory_limit, timetool_path) = data_for_execution - file_no_ext = paths.get_executions_path(name, package_util.extract_test_id(test)) + file_no_ext = paths.get_executions_path(name, package_util.extract_test_id(test, self.ID)) output_file = file_no_ext + ".out" result_file = file_no_ext + ".res" hard_time_limit_in_s = math.ceil(2 * time_limit / 1000.0) @@ -634,8 +634,8 @@ def run_solutions(self, compiled_commands, names, solutions): run_event = threading.Event() run_event.set() thr = threading.Thread(target=printer.printer_thread, - args=(run_event, print_view, program_groups_scores, all_results, print_data, names, - executions, self.groups, self.scores, self.tests, self.possible_score, + args=(run_event, print_view, self.ID, program_groups_scores, all_results, print_data, + names, executions, self.groups, self.scores, self.tests, self.possible_score, self.cpus, self.args.hide_memory, self.config, self.contest, self.args)) thr.start() @@ -657,7 +657,7 @@ def run_solutions(self, compiled_commands, names, solutions): run_event.clear() thr.join() - print("\n".join(print_view(terminal_width, terminal_height, program_groups_scores, all_results, print_data, + print("\n".join(print_view(terminal_width, terminal_height, self.ID, program_groups_scores, all_results, print_data, names, executions, self.groups, self.scores, self.tests, self.possible_score, self.cpus, self.args.hide_memory, self.config, self.contest, self.args)[0])) @@ -1047,10 +1047,10 @@ def get_valid_input_files(self): Returns list of input files that have corresponding output file. """ output_tests = glob.glob(os.path.join(os.getcwd(), "out", "*.out")) - output_tests_ids = [package_util.extract_test_id(test) for test in output_tests] + output_tests_ids = [package_util.extract_test_id(test, self.ID) for test in output_tests] valid_input_files = [] for test in self.tests: - if package_util.extract_test_id(test) in output_tests_ids: + if package_util.extract_test_id(test, self.ID) in output_tests_ids: valid_input_files.append(test) return valid_input_files @@ -1105,6 +1105,7 @@ def run(self, args): util.exit_if_not_package() self.set_constants() + package_util.validate_files(self.ID) self.args = args with open(os.path.join(os.getcwd(), "config.yml"), 'r') as config: try: diff --git a/src/sinol_make/helpers/package_util.py b/src/sinol_make/helpers/package_util.py index 150bc868..4de4948a 100644 --- a/src/sinol_make/helpers/package_util.py +++ b/src/sinol_make/helpers/package_util.py @@ -1,4 +1,5 @@ import os +import re import yaml import glob from enum import Enum @@ -22,14 +23,22 @@ def get_task_id() -> str: util.exit_with_error("Invalid task id. Task id should be 3 characters long.") -def extract_test_id(test_path): +def extract_test_id(test_path, task_id=None): """ Extracts test group and number from test path. For example for test abc1a.in it returns 1a. :param test_path: Path to test file. + :param task_id: Task id. If None, it is extracted from config.yml. + If not found, it's assumed that the length of task id is 3. :return: Test group and number. """ - return os.path.split(os.path.splitext(test_path)[0])[1][3:] + if task_id is None: + try: + task_id = get_task_id() + except FileNotFoundError: + task_id = "abc" + + return os.path.split(os.path.splitext(test_path)[0])[1][len(task_id):] def get_group(test_path): @@ -143,3 +152,30 @@ def get_memory_limit(test_path, config, lang, args=None): str_config = util.stringify_keys(config) return _get_limit(LimitTypes.MEMORY_LIMIT, test_path, str_config, lang) + + +def validate_files(task_id): + """ + Checks if all files in the package have valid names. + """ + def get_invalid_files(path, *patterns): + invalid_files = [] + for file in glob.glob(os.path.join(os.getcwd(), path)): + invalid = True + for pattern in patterns: + if pattern.match(os.path.basename(file)): + invalid = False + break + if invalid: + invalid_files.append(os.path.basename(file)) + return invalid_files + + in_test_re = re.compile(r'^(%s(([0-9]+)([a-z]?[a-z0-9]*))).in$' % (re.escape(task_id))) + invalid_in_tests = get_invalid_files(os.path.join("in", "*.in"), in_test_re) + if len(invalid_in_tests) > 0: + util.exit_with_error(f'Input tests with invalid names: {", ".join(invalid_in_tests)}.') + + out_test_re = re.compile(r'^(%s(([0-9]+)([a-z]?[a-z0-9]*))).out$' % (re.escape(task_id))) + invalid_out_tests = get_invalid_files(os.path.join("out", "*.out"), out_test_re) + if len(invalid_out_tests) > 0: + util.exit_with_error(f'Output tests with invalid names: {", ".join(invalid_out_tests)}.') diff --git a/tests/helpers/test_package_util.py b/tests/helpers/test_package_util.py index d71efb6a..8fd54769 100644 --- a/tests/helpers/test_package_util.py +++ b/tests/helpers/test_package_util.py @@ -21,6 +21,7 @@ def test_extract_test_id(): assert package_util.extract_test_id("in/abc10a.in") == "10a" assert package_util.extract_test_id("in/abc12ca.in") == "12ca" assert package_util.extract_test_id("in/abc0ocen.in") == "0ocen" + assert package_util.extract_test_id("in/long_task_id2bc.in", "long_task_id") == "2bc" def test_get_group(): From 2b6d9f214bda2ca0f5861c47a9501f9bbfb52c3f Mon Sep 17 00:00:00 2001 From: MasloMaslane Date: Sun, 10 Sep 2023 15:52:24 +0200 Subject: [PATCH 2/7] Change `get_group` function --- src/sinol_make/commands/inwer/__init__.py | 2 +- src/sinol_make/commands/inwer/structs.py | 4 ++-- src/sinol_make/commands/run/__init__.py | 10 +++++----- src/sinol_make/helpers/package_util.py | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/sinol_make/commands/inwer/__init__.py b/src/sinol_make/commands/inwer/__init__.py index 97fccaeb..399147f9 100644 --- a/src/sinol_make/commands/inwer/__init__.py +++ b/src/sinol_make/commands/inwer/__init__.py @@ -87,7 +87,7 @@ def verify_and_print_table(self) -> Dict[str, TestResult]: sorted_tests = sorted(self.tests, key=lambda x: x[0]) executions: List[InwerExecution] = [] for test in sorted_tests: - results[test] = TestResult(test) + results[test] = TestResult(test, self.task_id) executions.append(InwerExecution(test, results[test].test_name, self.inwer_executable)) has_terminal, terminal_width, terminal_height = util.get_terminal_size() diff --git a/src/sinol_make/commands/inwer/structs.py b/src/sinol_make/commands/inwer/structs.py index 5dfd30b5..f78d6f7c 100644 --- a/src/sinol_make/commands/inwer/structs.py +++ b/src/sinol_make/commands/inwer/structs.py @@ -14,10 +14,10 @@ class TestResult: valid: bool output: str - def __init__(self, test_path): + def __init__(self, test_path, task_id): self.test_path = test_path self.test_name = os.path.split(test_path)[-1] - self.test_group = str(package_util.get_group(self.test_path)) + self.test_group = str(package_util.get_group(self.test_path, task_id)) self.verified = False self.valid = False diff --git a/src/sinol_make/commands/run/__init__.py b/src/sinol_make/commands/run/__init__.py index cc7bd1a0..bed7cbb2 100644 --- a/src/sinol_make/commands/run/__init__.py +++ b/src/sinol_make/commands/run/__init__.py @@ -185,7 +185,7 @@ def print_group_seperator(): last_group = None for test in tests: - group = package_util.get_group(test) + group = package_util.get_group(test, task_id) if last_group != group: if last_group is not None: print_group_seperator() @@ -194,7 +194,7 @@ def print_group_seperator(): print(margin + "%6s" % package_util.extract_test_id(test, task_id), end=" | ") for program in program_group: lang = package_util.get_file_lang(program) - result = all_results[program][package_util.get_group(test)][test] + result = all_results[program][package_util.get_group(test, task_id)][test] status = result.Status if status == Status.PENDING: print(10 * ' ', end=" | ") else: @@ -206,7 +206,7 @@ def print_group_seperator(): print(8*" ", end=" | ") for program in program_group: lang = package_util.get_file_lang(program) - result = all_results[program][package_util.get_group(test)][test] + result = all_results[program][package_util.get_group(test, task_id)][test] print(("%20s" % color_memory(result.Memory, package_util.get_memory_limit(test, config, lang, args))) if result.Memory is not None else 10*" ", end=" | ") print() @@ -715,14 +715,14 @@ def get_whole_groups(self): """ group_sizes = {} for test in package_util.get_tests(): - group = package_util.get_group(test) + group = package_util.get_group(test, self.ID) if group not in group_sizes: group_sizes[group] = 0 group_sizes[group] += 1 run_group_sizes = {} for test in self.tests: - group = package_util.get_group(test) + group = package_util.get_group(test, self.ID) if group not in run_group_sizes: run_group_sizes[group] = 0 run_group_sizes[group] += 1 diff --git a/src/sinol_make/helpers/package_util.py b/src/sinol_make/helpers/package_util.py index 4de4948a..f632884c 100644 --- a/src/sinol_make/helpers/package_util.py +++ b/src/sinol_make/helpers/package_util.py @@ -41,10 +41,10 @@ def extract_test_id(test_path, task_id=None): return os.path.split(os.path.splitext(test_path)[0])[1][len(task_id):] -def get_group(test_path): - if extract_test_id(test_path).endswith("ocen"): +def get_group(test_path, task_id=None): + if extract_test_id(test_path, task_id).endswith("ocen"): return 0 - return int("".join(filter(str.isdigit, extract_test_id(test_path)))) + return int("".join(filter(str.isdigit, extract_test_id(test_path, task_id)))) def get_test_key(test): From 7002643157f3a5302289241dda2c6331ccf8356e Mon Sep 17 00:00:00 2001 From: MasloMaslane Date: Sun, 10 Sep 2023 16:00:36 +0200 Subject: [PATCH 3/7] Add test for `validate_files` function --- tests/helpers/test_package_util.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/helpers/test_package_util.py b/tests/helpers/test_package_util.py index 8fd54769..e02dc5e0 100644 --- a/tests/helpers/test_package_util.py +++ b/tests/helpers/test_package_util.py @@ -179,3 +179,25 @@ def test_get_memory_limit(): package_util.get_memory_limit("in/abc2a.in", config, "cpp") assert package_util.get_memory_limit("in/abc1a.in", config, "py") == 1024 assert package_util.get_memory_limit("in/abc2a.in", config, "py") == 512 + + +@pytest.mark.parametrize("create_package", [util.get_simple_package_path()], indirect=True) +def test_validate_files(create_package, capsys): + package_path = create_package + util.create_ins_outs(package_path) + task_id = package_util.get_task_id() + assert task_id == "abc" + package_util.validate_files(task_id) + + os.rename(os.path.join(package_path, "in", "abc1a.in"), os.path.join(package_path, "in", "def1a.in")) + with pytest.raises(SystemExit): + package_util.validate_files(task_id) + out = capsys.readouterr().out + assert "def1a.in" in out + + os.rename(os.path.join(package_path, "in", "def1a.in"), os.path.join(package_path, "in", "abc1a.in")) + os.rename(os.path.join(package_path, "out", "abc1a.out"), os.path.join(package_path, "out", "def1a.out")) + with pytest.raises(SystemExit): + package_util.validate_files(task_id) + out = capsys.readouterr().out + assert "def1a.out" in out From 7780a7afc3366b4209e5483b4c552517522e2980 Mon Sep 17 00:00:00 2001 From: Mateusz Masiarz Date: Wed, 13 Sep 2023 11:03:46 +0200 Subject: [PATCH 4/7] Refactor --- src/sinol_make/commands/export/__init__.py | 2 +- src/sinol_make/commands/gen/__init__.py | 2 +- src/sinol_make/commands/inwer/__init__.py | 2 +- src/sinol_make/commands/run/__init__.py | 2 +- src/sinol_make/helpers/package_util.py | 11 +++-------- tests/helpers/test_package_util.py | 6 +++--- 6 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/sinol_make/commands/export/__init__.py b/src/sinol_make/commands/export/__init__.py index 145ed1c1..af47a037 100644 --- a/src/sinol_make/commands/export/__init__.py +++ b/src/sinol_make/commands/export/__init__.py @@ -132,7 +132,7 @@ def run(self, args: argparse.Namespace): self.args = args self.task_id = package_util.get_task_id() - package_util.validate_files(self.task_id) + package_util.validate_test_names(self.task_id) with open(os.path.join(os.getcwd(), 'config.yml'), 'r') as config_file: config = yaml.load(config_file, Loader=yaml.FullLoader) diff --git a/src/sinol_make/commands/gen/__init__.py b/src/sinol_make/commands/gen/__init__.py index 665a038b..d0ca3a9c 100644 --- a/src/sinol_make/commands/gen/__init__.py +++ b/src/sinol_make/commands/gen/__init__.py @@ -93,7 +93,7 @@ def run(self, args: argparse.Namespace): self.args = args self.task_id = package_util.get_task_id() - package_util.validate_files(self.task_id) + package_util.validate_test_names(self.task_id) self.ingen = gen_util.get_ingen(self.task_id, args.ingen_path) print(util.info(f'Using ingen file {os.path.basename(self.ingen)}')) diff --git a/src/sinol_make/commands/inwer/__init__.py b/src/sinol_make/commands/inwer/__init__.py index 399147f9..41ace8f0 100644 --- a/src/sinol_make/commands/inwer/__init__.py +++ b/src/sinol_make/commands/inwer/__init__.py @@ -123,7 +123,7 @@ def run(self, args: argparse.Namespace): util.exit_if_not_package() self.task_id = package_util.get_task_id() - package_util.validate_files(self.task_id) + package_util.validate_test_names(self.task_id) self.inwer = inwer_util.get_inwer_path(self.task_id, args.inwer_path) if self.inwer is None: if args.inwer_path is None: diff --git a/src/sinol_make/commands/run/__init__.py b/src/sinol_make/commands/run/__init__.py index bed7cbb2..cac69de5 100644 --- a/src/sinol_make/commands/run/__init__.py +++ b/src/sinol_make/commands/run/__init__.py @@ -1105,7 +1105,7 @@ def run(self, args): util.exit_if_not_package() self.set_constants() - package_util.validate_files(self.ID) + package_util.validate_test_names(self.ID) self.args = args with open(os.path.join(os.getcwd(), "config.yml"), 'r') as config: try: diff --git a/src/sinol_make/helpers/package_util.py b/src/sinol_make/helpers/package_util.py index f632884c..0ee3556c 100644 --- a/src/sinol_make/helpers/package_util.py +++ b/src/sinol_make/helpers/package_util.py @@ -154,19 +154,14 @@ def get_memory_limit(test_path, config, lang, args=None): return _get_limit(LimitTypes.MEMORY_LIMIT, test_path, str_config, lang) -def validate_files(task_id): +def validate_test_names(task_id): """ Checks if all files in the package have valid names. """ - def get_invalid_files(path, *patterns): + def get_invalid_files(path, pattern): invalid_files = [] for file in glob.glob(os.path.join(os.getcwd(), path)): - invalid = True - for pattern in patterns: - if pattern.match(os.path.basename(file)): - invalid = False - break - if invalid: + if not pattern.match(os.path.basename(file)): invalid_files.append(os.path.basename(file)) return invalid_files diff --git a/tests/helpers/test_package_util.py b/tests/helpers/test_package_util.py index e02dc5e0..4e993442 100644 --- a/tests/helpers/test_package_util.py +++ b/tests/helpers/test_package_util.py @@ -187,17 +187,17 @@ def test_validate_files(create_package, capsys): util.create_ins_outs(package_path) task_id = package_util.get_task_id() assert task_id == "abc" - package_util.validate_files(task_id) + package_util.validate_test_names(task_id) os.rename(os.path.join(package_path, "in", "abc1a.in"), os.path.join(package_path, "in", "def1a.in")) with pytest.raises(SystemExit): - package_util.validate_files(task_id) + package_util.validate_test_names(task_id) out = capsys.readouterr().out assert "def1a.in" in out os.rename(os.path.join(package_path, "in", "def1a.in"), os.path.join(package_path, "in", "abc1a.in")) os.rename(os.path.join(package_path, "out", "abc1a.out"), os.path.join(package_path, "out", "def1a.out")) with pytest.raises(SystemExit): - package_util.validate_files(task_id) + package_util.validate_test_names(task_id) out = capsys.readouterr().out assert "def1a.out" in out From 72833f2a91e0a42a3c5af868d66f3548e8c1c82b Mon Sep 17 00:00:00 2001 From: Mateusz Masiarz Date: Wed, 13 Sep 2023 11:21:32 +0200 Subject: [PATCH 5/7] Refactor `extract_test_id` function --- src/sinol_make/commands/run/__init__.py | 28 ++++----- src/sinol_make/helpers/package_util.py | 23 +++----- tests/helpers/test_package_util.py | 76 ++++++++++++------------- 3 files changed, 61 insertions(+), 66 deletions(-) diff --git a/src/sinol_make/commands/run/__init__.py b/src/sinol_make/commands/run/__init__.py index cac69de5..af46abc4 100644 --- a/src/sinol_make/commands/run/__init__.py +++ b/src/sinol_make/commands/run/__init__.py @@ -77,7 +77,7 @@ def print_view(term_width, term_height, task_id, program_groups_scores, all_resu for solution in names: lang = package_util.get_file_lang(solution) for test in tests: - time_sum += package_util.get_time_limit(test, config, lang, args) + time_sum += package_util.get_time_limit(test, config, lang, task_id, args) time_remaining = (len(executions) - print_data.i - 1) * 2 * time_sum / cpus / 1000.0 title = 'Done %4d/%4d. Time remaining (in the worst case): %5d seconds.' \ @@ -120,17 +120,17 @@ def print_table_end(): if results[test].Time is not None: if program_times[program][0] < results[test].Time: program_times[program] = (results[test].Time, package_util.get_time_limit(test, config, - lang, args)) + lang, task_id, args)) elif status == Status.TL: - program_times[program] = (2 * package_util.get_time_limit(test, config, lang, args), - package_util.get_time_limit(test, config, lang, args)) + program_times[program] = (2 * package_util.get_time_limit(test, config, lang, task_id, args), + package_util.get_time_limit(test, config, lang, task_id, args)) if results[test].Memory is not None: if program_memory[program][0] < results[test].Memory: program_memory[program] = (results[test].Memory, package_util.get_memory_limit(test, config, - lang, args)) + lang, task_id, args)) elif status == Status.ML: - program_memory[program] = (2 * package_util.get_memory_limit(test, config, lang, args), - package_util.get_memory_limit(test, config, lang, args)) + program_memory[program] = (2 * package_util.get_memory_limit(test, config, lang, task_id, args), + package_util.get_memory_limit(test, config, lang, task_id, args)) if status == Status.PENDING: group_status = Status.PENDING else: @@ -199,7 +199,7 @@ def print_group_seperator(): if status == Status.PENDING: print(10 * ' ', end=" | ") else: print("%3s" % colorize_status(status), - ("%17s" % color_time(result.Time, package_util.get_time_limit(test, config, lang, args))) + ("%17s" % color_time(result.Time, package_util.get_time_limit(test, config, lang, task_id, args))) if result.Time is not None else 7*" ", end=" | ") print() if not hide_memory: @@ -207,7 +207,7 @@ def print_group_seperator(): for program in program_group: lang = package_util.get_file_lang(program) result = all_results[program][package_util.get_group(test, task_id)][test] - print(("%20s" % color_memory(result.Memory, package_util.get_memory_limit(test, config, lang, args))) + print(("%20s" % color_memory(result.Memory, package_util.get_memory_limit(test, config, lang, task_id, args))) if result.Memory is not None else 10*" ", end=" | ") print() @@ -616,8 +616,10 @@ def run_solutions(self, compiled_commands, names, solutions): lang = package_util.get_file_lang(name) if result: for test in self.tests: - executions.append((name, executable, test, package_util.get_time_limit(test, self.config, lang, self.args), - package_util.get_memory_limit(test, self.config, lang, self.args), self.timetool_path)) + executions.append((name, executable, test, + package_util.get_time_limit(test, self.config, lang, self.ID, self.args), + package_util.get_memory_limit(test, self.config, lang, self.ID, self.args), + self.timetool_path)) all_results[name][self.get_group(test)][test] = ExecutionResult(Status.PENDING) os.makedirs(paths.get_executions_path(name), exist_ok=True) else: @@ -1152,8 +1154,8 @@ def run(self, args): lang = package_util.get_file_lang(solution) for test in self.tests: # The functions will exit if the limits are not set - _ = package_util.get_time_limit(test, self.config, lang, self.args) - _ = package_util.get_memory_limit(test, self.config, lang, self.args) + _ = package_util.get_time_limit(test, self.config, lang, self.ID, self.args) + _ = package_util.get_memory_limit(test, self.config, lang, self.ID, self.args) results, all_results = self.compile_and_run(solutions) self.check_errors(all_results) diff --git a/src/sinol_make/helpers/package_util.py b/src/sinol_make/helpers/package_util.py index 0ee3556c..d6a72289 100644 --- a/src/sinol_make/helpers/package_util.py +++ b/src/sinol_make/helpers/package_util.py @@ -23,21 +23,14 @@ def get_task_id() -> str: util.exit_with_error("Invalid task id. Task id should be 3 characters long.") -def extract_test_id(test_path, task_id=None): +def extract_test_id(test_path, task_id): """ Extracts test group and number from test path. For example for test abc1a.in it returns 1a. :param test_path: Path to test file. - :param task_id: Task id. If None, it is extracted from config.yml. - If not found, it's assumed that the length of task id is 3. + :param task_id: Task id. :return: Test group and number. """ - if task_id is None: - try: - task_id = get_task_id() - except FileNotFoundError: - task_id = "abc" - return os.path.split(os.path.splitext(test_path)[0])[1][len(task_id):] @@ -114,8 +107,8 @@ def _get_limit_from_dict(dict: Dict[str, Any], limit_type: LimitTypes, test_id: return None -def _get_limit(limit_type: LimitTypes, test_path: str, config: Dict[str, Any], lang: str): - test_id = extract_test_id(test_path) +def _get_limit(limit_type: LimitTypes, test_path: str, config: Dict[str, Any], lang: str, task_id: str): + test_id = extract_test_id(test_path, task_id) test_group = str(get_group(test_path)) global_limit = _get_limit_from_dict(config, limit_type, test_id, test_group, test_path) override_limits_dict = config.get("override_limits", {}).get(lang, {}) @@ -132,7 +125,7 @@ def _get_limit(limit_type: LimitTypes, test_path: str, config: Dict[str, Any], l util.exit_with_error(f'Memory limit was not defined for test {os.path.basename(test_path)} in config.yml.') -def get_time_limit(test_path, config, lang, args=None): +def get_time_limit(test_path, config, lang, task_id, args=None): """ Returns time limit for given test. """ @@ -140,10 +133,10 @@ def get_time_limit(test_path, config, lang, args=None): return args.tl * 1000 str_config = util.stringify_keys(config) - return _get_limit(LimitTypes.TIME_LIMIT, test_path, str_config, lang) + return _get_limit(LimitTypes.TIME_LIMIT, test_path, str_config, lang, task_id) -def get_memory_limit(test_path, config, lang, args=None): +def get_memory_limit(test_path, config, lang, task_id, args=None): """ Returns memory limit for given test. """ @@ -151,7 +144,7 @@ def get_memory_limit(test_path, config, lang, args=None): return int(args.ml * 1024) str_config = util.stringify_keys(config) - return _get_limit(LimitTypes.MEMORY_LIMIT, test_path, str_config, lang) + return _get_limit(LimitTypes.MEMORY_LIMIT, test_path, str_config, lang, task_id) def validate_test_names(task_id): diff --git a/tests/helpers/test_package_util.py b/tests/helpers/test_package_util.py index 4e993442..d30eef8a 100644 --- a/tests/helpers/test_package_util.py +++ b/tests/helpers/test_package_util.py @@ -17,10 +17,10 @@ def test_get_task_id(create_package): def test_extract_test_id(): - assert package_util.extract_test_id("in/abc1a.in") == "1a" - assert package_util.extract_test_id("in/abc10a.in") == "10a" - assert package_util.extract_test_id("in/abc12ca.in") == "12ca" - assert package_util.extract_test_id("in/abc0ocen.in") == "0ocen" + assert package_util.extract_test_id("in/abc1a.in", "abc") == "1a" + assert package_util.extract_test_id("in/abc10a.in", "abc") == "10a" + assert package_util.extract_test_id("in/abc12ca.in", "abc") == "12ca" + assert package_util.extract_test_id("in/abc0ocen.in", "abc") == "0ocen" assert package_util.extract_test_id("in/long_task_id2bc.in", "long_task_id") == "2bc" @@ -61,17 +61,17 @@ def test_get_time_limit(): } } - assert package_util.get_time_limit("in/abc1a.in", config, "cpp") == 1000 - assert package_util.get_time_limit("in/abc2a.in", config, "cpp") == 2000 - assert package_util.get_time_limit("in/abc2b.in", config, "cpp") == 2000 - assert package_util.get_time_limit("in/abc3a.in", config, "cpp") == 1000 - assert package_util.get_time_limit("in/abc3ocen.in", config, "cpp") == 5000 + assert package_util.get_time_limit("in/abc1a.in", config, "cpp", "abc") == 1000 + assert package_util.get_time_limit("in/abc2a.in", config, "cpp", "abc") == 2000 + assert package_util.get_time_limit("in/abc2b.in", config, "cpp", "abc") == 2000 + assert package_util.get_time_limit("in/abc3a.in", config, "cpp", "abc") == 1000 + assert package_util.get_time_limit("in/abc3ocen.in", config, "cpp", "abc") == 5000 - assert package_util.get_time_limit("in/abc1a.in", config, "py") == 2000 - assert package_util.get_time_limit("in/abc2a.in", config, "py") == 3000 - assert package_util.get_time_limit("in/abc2b.in", config, "py") == 3000 - assert package_util.get_time_limit("in/abc3a.in", config, "py") == 2000 - assert package_util.get_time_limit("in/abc3ocen.in", config, "py") == 6000 + assert package_util.get_time_limit("in/abc1a.in", config, "py", "abc") == 2000 + assert package_util.get_time_limit("in/abc2a.in", config, "py", "abc") == 3000 + assert package_util.get_time_limit("in/abc2b.in", config, "py", "abc") == 3000 + assert package_util.get_time_limit("in/abc3a.in", config, "py", "abc") == 2000 + assert package_util.get_time_limit("in/abc3ocen.in", config, "py", "abc") == 6000 # Test getting default time limit. config = { @@ -86,12 +86,12 @@ def test_get_time_limit(): } } } - assert package_util.get_time_limit("in/abc1a.in", config, "cpp") == 1000 - assert package_util.get_time_limit("in/abc1a.in", config, "py") == 2000 + assert package_util.get_time_limit("in/abc1a.in", config, "cpp", "abc") == 1000 + assert package_util.get_time_limit("in/abc1a.in", config, "py", "abc") == 2000 with pytest.raises(SystemExit): - package_util.get_time_limit("in/abc2a.in", config, "cpp") + package_util.get_time_limit("in/abc2a.in", config, "cpp", "abc") with pytest.raises(SystemExit): - package_util.get_time_limit("in/abc2a.in", config, "py") + package_util.get_time_limit("in/abc2a.in", config, "py", "abc") config = { "time_limits": { @@ -106,11 +106,11 @@ def test_get_time_limit(): } } } - assert package_util.get_time_limit("in/abc1a.in", config, "cpp") == 1000 + assert package_util.get_time_limit("in/abc1a.in", config, "cpp", "abc") == 1000 with pytest.raises(SystemExit): - package_util.get_time_limit("in/abc2a.in", config, "cpp") - assert package_util.get_time_limit("in/abc1a.in", config, "py") == 1000 - assert package_util.get_time_limit("in/abc2a.in", config, "py") == 500 + package_util.get_time_limit("in/abc2a.in", config, "cpp", "abc") + assert package_util.get_time_limit("in/abc1a.in", config, "py", "abc") == 1000 + assert package_util.get_time_limit("in/abc2a.in", config, "py", "abc") == 500 def test_get_memory_limit(): @@ -131,15 +131,15 @@ def test_get_memory_limit(): } } - assert package_util.get_memory_limit("in/abc1a.in", config, "cpp") == 256 - assert package_util.get_memory_limit("in/abc2a.in", config, "cpp") == 512 - assert package_util.get_memory_limit("in/abc2b.in", config, "cpp") == 512 - assert package_util.get_memory_limit("in/abc3ocen.in", config, "cpp") == 128 + assert package_util.get_memory_limit("in/abc1a.in", config, "cpp", "abc") == 256 + assert package_util.get_memory_limit("in/abc2a.in", config, "cpp", "abc") == 512 + assert package_util.get_memory_limit("in/abc2b.in", config, "cpp", "abc") == 512 + assert package_util.get_memory_limit("in/abc3ocen.in", config, "cpp", "abc") == 128 - assert package_util.get_memory_limit("in/abc1a.in", config, "py") == 512 - assert package_util.get_memory_limit("in/abc2a.in", config, "py") == 1024 - assert package_util.get_memory_limit("in/abc2b.in", config, "py") == 1024 - assert package_util.get_memory_limit("in/abc3ocen.in", config, "py") == 256 + assert package_util.get_memory_limit("in/abc1a.in", config, "py", "abc") == 512 + assert package_util.get_memory_limit("in/abc2a.in", config, "py", "abc") == 1024 + assert package_util.get_memory_limit("in/abc2b.in", config, "py", "abc") == 1024 + assert package_util.get_memory_limit("in/abc3ocen.in", config, "py", "abc") == 256 # Test getting default memory limit. config = { @@ -154,12 +154,12 @@ def test_get_memory_limit(): } } } - assert package_util.get_memory_limit("in/abc1a.in", config, "cpp") == 1024 - assert package_util.get_memory_limit("in/abc1a.in", config, "py") == 2048 + assert package_util.get_memory_limit("in/abc1a.in", config, "cpp", "abc") == 1024 + assert package_util.get_memory_limit("in/abc1a.in", config, "py", "abc") == 2048 with pytest.raises(SystemExit): - package_util.get_memory_limit("in/abc2a.in", config, "cpp") + package_util.get_memory_limit("in/abc2a.in", config, "cpp", "abc") with pytest.raises(SystemExit): - package_util.get_memory_limit("in/abc2a.in", config, "py") + package_util.get_memory_limit("in/abc2a.in", config, "py", "abc") config = { "memory_limits": { @@ -174,11 +174,11 @@ def test_get_memory_limit(): } } } - assert package_util.get_memory_limit("in/abc1a.in", config, "cpp") == 1024 + assert package_util.get_memory_limit("in/abc1a.in", config, "cpp", "abc") == 1024 with pytest.raises(SystemExit): - package_util.get_memory_limit("in/abc2a.in", config, "cpp") - assert package_util.get_memory_limit("in/abc1a.in", config, "py") == 1024 - assert package_util.get_memory_limit("in/abc2a.in", config, "py") == 512 + package_util.get_memory_limit("in/abc2a.in", config, "cpp", "abc") + assert package_util.get_memory_limit("in/abc1a.in", config, "py", "abc") == 1024 + assert package_util.get_memory_limit("in/abc2a.in", config, "py", "abc") == 512 @pytest.mark.parametrize("create_package", [util.get_simple_package_path()], indirect=True) From e66595fc3532d7a318e8fe67f6a7b0f51cc7a5f5 Mon Sep 17 00:00:00 2001 From: Mateusz Masiarz Date: Wed, 13 Sep 2023 12:57:56 +0200 Subject: [PATCH 6/7] Add functions for better searching for files --- src/sinol_make/commands/gen/gen_util.py | 13 +++--- src/sinol_make/commands/inwer/__init__.py | 2 +- src/sinol_make/commands/inwer/inwer_util.py | 2 +- src/sinol_make/commands/run/__init__.py | 8 ++-- src/sinol_make/helpers/package_util.py | 49 ++++++++++++++++++--- tests/commands/gen/test_unit.py | 6 --- tests/commands/inwer/test_integration.py | 16 ++++--- tests/commands/inwer/test_unit.py | 3 +- tests/commands/run/test_integration.py | 5 ++- tests/commands/run/test_unit.py | 8 ++-- tests/helpers/test_package_util.py | 8 ++-- tests/util.py | 18 ++++---- 12 files changed, 88 insertions(+), 50 deletions(-) diff --git a/src/sinol_make/commands/gen/gen_util.py b/src/sinol_make/commands/gen/gen_util.py index 42f9d94f..5123372f 100644 --- a/src/sinol_make/commands/gen/gen_util.py +++ b/src/sinol_make/commands/gen/gen_util.py @@ -16,15 +16,14 @@ def ingen_exists(task_id): :param task_id: task id, for example abc :return: True if exists, False otherwise """ - return len(glob.glob(os.path.join(os.getcwd(), 'prog', task_id + 'ingen.*'))) > 0 + return package_util.any_files_matching_pattern(task_id, f'{task_id}ingen.*') -def get_ingen(task_id=None, ingen_path=None): +def get_ingen(task_id, ingen_path=None): """ Find ingen source file in `prog/` directory. If `ingen_path` is specified, then it will be used (if exists). - :param task_id: task id, for example abc. If None, then - will return any ingen matching "*ingen.*" + :param task_id: task id, for example abc. :param ingen_path: path to ingen source file :return: path to ingen source file or None if not found """ @@ -35,9 +34,7 @@ def get_ingen(task_id=None, ingen_path=None): else: util.exit_with_error(f'Ingen source file {ingen_path} does not exist.') - if task_id is None: - task_id = '*' - ingen = glob.glob(os.path.join(os.getcwd(), 'prog', task_id + 'ingen.*')) + ingen = package_util.get_files_matching_pattern(task_id, f'{task_id}ingen.*') if len(ingen) == 0: util.exit_with_error(f'Ingen source file for task {task_id} does not exist.') @@ -78,7 +75,7 @@ def get_correct_solution(task_id): :param task_id: task id, for example abc :return: path to correct solution or None if not found """ - correct_solution = glob.glob(os.path.join(os.getcwd(), 'prog', task_id + '.*')) + correct_solution = package_util.get_files_matching_pattern(task_id, f'{task_id}.*') if len(correct_solution) == 0: util.exit_with_error(f'Correct solution for task {task_id} does not exist.') return correct_solution[0] diff --git a/src/sinol_make/commands/inwer/__init__.py b/src/sinol_make/commands/inwer/__init__.py index 41ace8f0..d30c252e 100644 --- a/src/sinol_make/commands/inwer/__init__.py +++ b/src/sinol_make/commands/inwer/__init__.py @@ -134,7 +134,7 @@ def run(self, args: argparse.Namespace): print(f'Verifying with inwer {util.bold(relative_path)}') self.cpus = args.cpus or mp.cpu_count() - self.tests = package_util.get_tests(args.tests) + self.tests = package_util.get_tests(self.task_id, args.tests) if len(self.tests) == 0: util.exit_with_error('No tests found.') diff --git a/src/sinol_make/commands/inwer/inwer_util.py b/src/sinol_make/commands/inwer/inwer_util.py index b9ec0653..58ed5a9a 100644 --- a/src/sinol_make/commands/inwer/inwer_util.py +++ b/src/sinol_make/commands/inwer/inwer_util.py @@ -18,7 +18,7 @@ def get_inwer_path(task_id: str, path = None) -> Union[str, None]: Returns path to inwer executable for given task or None if no inwer was found. """ if path is None: - inwers = glob.glob(os.path.join(os.getcwd(), 'prog', f'{task_id}inwer.*')) + inwers = package_util.get_files_matching_pattern(task_id, f'{task_id}inwer.*') if len(inwers) == 0: return None return inwers[0] diff --git a/src/sinol_make/commands/run/__init__.py b/src/sinol_make/commands/run/__init__.py index af46abc4..447a5c9e 100644 --- a/src/sinol_make/commands/run/__init__.py +++ b/src/sinol_make/commands/run/__init__.py @@ -716,7 +716,7 @@ def get_whole_groups(self): Returns a list of groups for which all tests were run. """ group_sizes = {} - for test in package_util.get_tests(): + for test in package_util.get_tests(self.ID): group = package_util.get_group(test, self.ID) if group not in group_sizes: group_sizes[group] = 0 @@ -1005,7 +1005,7 @@ def exit(self): cnt=len(self.failed_compilations), letter='' if len(self.failed_compilations) == 1 else 's')) def set_scores(self): - self.tests = package_util.get_tests(self.args.tests) + self.tests = package_util.get_tests(self.ID, self.args.tests) self.groups = self.get_groups(self.tests) self.scores = collections.defaultdict(int) @@ -1129,7 +1129,7 @@ def run(self, args): print("Task: %s (tag: %s)" % (title, self.ID)) self.cpus = args.cpus or mp.cpu_count() - checker = glob.glob(os.path.join(os.getcwd(), "prog", f'{self.ID}chk.*')) + checker = package_util.get_files_matching_pattern(self.ID, f'{self.ID}chk.*') if len(checker) != 0: print(util.info("Checker found: %s" % os.path.basename(checker[0]))) self.checker = checker[0] @@ -1142,7 +1142,7 @@ def run(self, args): else: self.checker = None - lib = glob.glob(os.path.join(os.getcwd(), "prog", f'{self.ID}lib.*')) + lib = package_util.get_files_matching_pattern(self.ID, f'{self.ID}lib.*') self.has_lib = len(lib) != 0 self.set_scores() diff --git a/src/sinol_make/helpers/package_util.py b/src/sinol_make/helpers/package_util.py index d6a72289..1721d3de 100644 --- a/src/sinol_make/helpers/package_util.py +++ b/src/sinol_make/helpers/package_util.py @@ -2,6 +2,7 @@ import re import yaml import glob +import fnmatch from enum import Enum from typing import List, Union, Dict, Any @@ -34,28 +35,29 @@ def extract_test_id(test_path, task_id): return os.path.split(os.path.splitext(test_path)[0])[1][len(task_id):] -def get_group(test_path, task_id=None): +def get_group(test_path, task_id): if extract_test_id(test_path, task_id).endswith("ocen"): return 0 return int("".join(filter(str.isdigit, extract_test_id(test_path, task_id)))) -def get_test_key(test): - return get_group(test), test +def get_test_key(test, task_id): + return get_group(test, task_id), test -def get_tests(arg_tests: Union[List[str], None] = None) -> List[str]: +def get_tests(task_id: str, arg_tests: Union[List[str], None] = None) -> List[str]: """ Returns list of tests to run. + :param task_id: Task id. :param arg_tests: Tests specified in command line arguments. If None, all tests are returned. :return: List of tests to run. """ if arg_tests is None: all_tests = ["in/%s" % test for test in os.listdir("in/") if test[-3:] == ".in"] - return sorted(all_tests, key=get_test_key) + return sorted(all_tests, key=lambda test: get_test_key(test, task_id)) else: - return sorted(list(set(arg_tests)), key=get_test_key) + return sorted(list(set(arg_tests)), key=lambda test: get_test_key(test, task_id)) def get_file_name(file_path): @@ -109,7 +111,7 @@ def _get_limit_from_dict(dict: Dict[str, Any], limit_type: LimitTypes, test_id: def _get_limit(limit_type: LimitTypes, test_path: str, config: Dict[str, Any], lang: str, task_id: str): test_id = extract_test_id(test_path, task_id) - test_group = str(get_group(test_path)) + test_group = str(get_group(test_path, task_id)) global_limit = _get_limit_from_dict(config, limit_type, test_id, test_group, test_path) override_limits_dict = config.get("override_limits", {}).get(lang, {}) overriden_limit = _get_limit_from_dict(override_limits_dict, limit_type, test_id, test_group, test_path) @@ -167,3 +169,36 @@ def get_invalid_files(path, pattern): invalid_out_tests = get_invalid_files(os.path.join("out", "*.out"), out_test_re) if len(invalid_out_tests) > 0: util.exit_with_error(f'Output tests with invalid names: {", ".join(invalid_out_tests)}.') + + +def get_all_code_files(task_id: str) -> List[str]: + """ + Returns all code files in package. + :param task_id: Task id. + :return: List of code files. + """ + result = glob.glob(os.path.join(os.getcwd(), "prog", f"{task_id}ingen.sh")) + for ext in ["c", "cpp", "py", "java"]: + result += glob.glob(os.path.join(os.getcwd(), f"prog/{task_id}*.{ext}")) + return result + + +def get_files_matching_pattern(task_id: str, pattern: str) -> List[str]: + """ + Returns all files in package matching given pattern. + :param task_id: Task id. + :param pattern: Pattern to match. + :return: List of files matching the pattern. + """ + all_files = get_all_code_files(task_id) + return [file for file in all_files if fnmatch.fnmatch(os.path.basename(file), pattern)] + + +def any_files_matching_pattern(task_id: str, pattern: str) -> bool: + """ + Returns True if any file in package matches given pattern. + :param task_id: Task id. + :param pattern: Pattern to match. + :return: True if any file in package matches given pattern. + """ + return len(get_files_matching_pattern(task_id, pattern)) > 0 diff --git a/tests/commands/gen/test_unit.py b/tests/commands/gen/test_unit.py index e82eea59..fe823cfd 100644 --- a/tests/commands/gen/test_unit.py +++ b/tests/commands/gen/test_unit.py @@ -20,9 +20,6 @@ def test_get_ingen(): shutil.copytree(simple_package_path, os.path.join(tmpdir, 'simple')) os.chdir(os.path.join(tmpdir, 'simple')) - ingen_path = gen_util.get_ingen() - assert os.path.basename(ingen_path) == "abcingen.cpp" - ingen_path = gen_util.get_ingen("abc") assert os.path.basename(ingen_path) == "abcingen.cpp" @@ -37,9 +34,6 @@ def test_get_ingen(): shutil.copytree(gen_package_path, os.path.join(tmpdir, 'gen')) os.chdir(os.path.join(tmpdir, 'gen')) - ingen_path = gen_util.get_ingen() - assert os.path.basename(ingen_path) == "geningen.sh" - ingen_path = gen_util.get_ingen("gen") assert os.path.basename(ingen_path) == "geningen.sh" diff --git a/tests/commands/inwer/test_integration.py b/tests/commands/inwer/test_integration.py index 767e2d4f..9fadfb6d 100644 --- a/tests/commands/inwer/test_integration.py +++ b/tests/commands/inwer/test_integration.py @@ -2,6 +2,7 @@ from sinol_make import configure_parsers from sinol_make.commands.inwer import Command +from sinol_make.helpers import package_util from tests import util from tests.fixtures import * @@ -12,7 +13,8 @@ def test_default(capsys, create_package): Test `inwer` command with no parameters. """ package_path = create_package - util.create_ins(package_path) + task_id = package_util.get_task_id() + util.create_ins(package_path, task_id) parser = configure_parsers() args = parser.parse_args(["inwer"]) command = Command() @@ -33,7 +35,8 @@ def test_specified_inwer(capsys, create_package): Test `inwer` command with specified inwer. """ package_path = create_package - util.create_ins(package_path) + task_id = package_util.get_task_id() + util.create_ins(package_path, task_id) parser = configure_parsers() args = parser.parse_args(["inwer", "prog/werinwer.cpp"]) command = Command() @@ -66,7 +69,8 @@ def test_asserting_inwer(capsys, create_package): Test `inwer` command with inwer that uses assert for verifying. """ package_path = create_package - util.create_ins(package_path) + task_id = package_util.get_task_id() + util.create_ins(package_path, task_id) parser = configure_parsers() args = parser.parse_args(["inwer", "prog/werinwer3.cpp"]) command = Command() @@ -87,7 +91,8 @@ def test_flag_tests(capsys, create_package): Test `inwer` command with --tests flag. """ package_path = create_package - util.create_ins(package_path) + task_id = package_util.get_task_id() + util.create_ins(package_path, task_id) parser = configure_parsers() args = parser.parse_args(["inwer", "prog/werinwer.cpp", "--tests", "in/wer2a.in"]) command = Command() @@ -125,7 +130,8 @@ def test_no_output(capsys, create_package): Test `inwer` command when inwer doesn't print anything. """ package_path = create_package - util.create_ins(package_path) + task_id = package_util.get_task_id() + util.create_ins(package_path, task_id) parser = configure_parsers() args = parser.parse_args(["inwer", "prog/werinwer4.cpp"]) command = Command() diff --git a/tests/commands/inwer/test_unit.py b/tests/commands/inwer/test_unit.py index 83c738a9..744fc404 100644 --- a/tests/commands/inwer/test_unit.py +++ b/tests/commands/inwer/test_unit.py @@ -36,7 +36,8 @@ def test_asserting_inwer(create_package): Test asserting inwer. """ package_path = create_package - util.create_ins(package_path) + task_id = package_util.get_task_id() + util.create_ins(package_path, task_id) inwer_path = os.path.join(os.getcwd(), 'prog', 'werinwer3.cpp') args = argparse.Namespace( c_compiler_path=compiler.get_c_compiler_path(), diff --git a/tests/commands/run/test_integration.py b/tests/commands/run/test_integration.py index d648f51a..ffe59bd0 100644 --- a/tests/commands/run/test_integration.py +++ b/tests/commands/run/test_integration.py @@ -59,7 +59,7 @@ def test_no_expected_scores(capsys, create_package, time_tool): out = capsys.readouterr().out assert "Solutions were added:" in out - solution = glob.glob(os.path.join(package_path, "prog", "???.*"))[0] + solution = package_util.get_files_matching_pattern(command.ID, f"{command.ID}.*")[0] assert os.path.basename(solution) in out @@ -219,7 +219,8 @@ def test_flag_solutions(capsys, create_package, time_tool): package_path = create_package create_ins_outs(package_path) - solutions = glob.glob(os.path.join(package_path, "prog", "????.*")) + task_id = package_util.get_task_id() + solutions = package_util.get_files_matching_pattern(task_id, f'{task_id}?.*') parser = configure_parsers() args = parser.parse_args(["run", "--solutions", solutions[0], "--time-tool", time_tool]) command = Command() diff --git a/tests/commands/run/test_unit.py b/tests/commands/run/test_unit.py index d0f80407..2af4d417 100644 --- a/tests/commands/run/test_unit.py +++ b/tests/commands/run/test_unit.py @@ -50,7 +50,7 @@ def test_execution(create_package, time_tool): assert result == [True] create_ins_outs(package_path) - test = package_util.get_tests(None)[0] + test = package_util.get_tests("abc", None)[0] with open(os.path.join(package_path, "config.yml"), "r") as config_file: config = yaml.load(config_file, Loader=yaml.FullLoader) @@ -78,7 +78,7 @@ def test_run_solutions(create_package, time_tool): command.args = argparse.Namespace(solutions_report=False, time_tool=time_tool, weak_compilation_flags=False, hide_memory=False) create_ins_outs(package_path) - command.tests = package_util.get_tests(None) + command.tests = package_util.get_tests("abc", None) command.groups = list(sorted(set([command.get_group(test) for test in command.tests]))) command.scores = command.config["scores"] command.possible_score = command.get_possible_score(command.groups) @@ -132,7 +132,7 @@ def test_validate_expected_scores_success(): os.chdir(get_simple_package_path()) command = get_command() command.scores = command.config["scores"] - command.tests = package_util.get_tests(None) + command.tests = package_util.get_tests("abc", None) # Test with correct expected scores. command.args = argparse.Namespace(solutions=["prog/abc.cpp"], tests=None) @@ -439,7 +439,7 @@ def test_get_valid_input_files(create_package): package_path = create_package command = get_command(package_path) create_ins_outs(package_path) - command.tests = package_util.get_tests(None) + command.tests = package_util.get_tests(command.ID, None) outputs = glob.glob(os.path.join(package_path, "out", "*.out")) os.unlink(outputs[0]) diff --git a/tests/helpers/test_package_util.py b/tests/helpers/test_package_util.py index d30eef8a..de5f294a 100644 --- a/tests/helpers/test_package_util.py +++ b/tests/helpers/test_package_util.py @@ -25,13 +25,15 @@ def test_extract_test_id(): def test_get_group(): - assert package_util.get_group("in/abc1a.in") == 1 + assert package_util.get_group("in/abc1a.in", "abc") == 1 + assert package_util.get_group("in/long_name2ocen.in", "long_name") == 0 def test_get_tests(create_package): - create_ins(create_package) os.chdir(create_package) - tests = package_util.get_tests(None) + task_id = package_util.get_task_id() + create_ins(create_package, task_id) + tests = package_util.get_tests("abc", None) assert tests == ["in/abc1a.in", "in/abc2a.in", "in/abc3a.in", "in/abc4a.in"] diff --git a/tests/util.py b/tests/util.py index 409d1f1a..d6d2454f 100644 --- a/tests/util.py +++ b/tests/util.py @@ -2,7 +2,7 @@ import glob import subprocess -from sinol_make.helpers import compile, paths +from sinol_make.helpers import compile, paths, package_util def get_simple_package_path(): @@ -87,11 +87,11 @@ def get_long_name_package_path(): return os.path.join(os.path.dirname(__file__), "packages", "long_package_name") -def create_ins(package_path): +def create_ins(package_path, task_id): """ Create .in files for package. """ - ingen = glob.glob(os.path.join(package_path, "prog", "*ingen.*"))[0] + ingen = package_util.get_files_matching_pattern(task_id, f'{task_id}ingen.*')[0] ingen_executable = paths.get_executables_path("ingen.e") os.makedirs(paths.get_executables_path(), exist_ok=True) assert compile.compile(ingen, ingen_executable) @@ -100,11 +100,11 @@ def create_ins(package_path): os.chdir(package_path) -def create_outs(package_path): +def create_outs(package_path, task_id): """ Create .out files for package. """ - solution = glob.glob(os.path.join(package_path, "prog", "???.*"))[0] + solution = package_util.get_files_matching_pattern(task_id, f'{task_id}.*')[0] solution_executable = paths.get_executables_path("solution.e") os.makedirs(paths.get_executables_path(), exist_ok=True) assert compile.compile(solution, solution_executable) @@ -120,7 +120,9 @@ def create_ins_outs(package_path): """ Create .in and .out files for package. """ - create_ins(package_path) - has_lib = len(glob.glob(os.path.join(package_path, "prog", "???lib.*"))) > 0 + os.chdir(package_path) + task_id = package_util.get_task_id() + create_ins(package_path, task_id) + has_lib = package_util.any_files_matching_pattern(task_id, f"{task_id}lib.*") if not has_lib: - create_outs(package_path) + create_outs(package_path, task_id) From c3489b827942b6a18eb0177a73d35e986aac54e8 Mon Sep 17 00:00:00 2001 From: Mateusz Masiarz Date: Tue, 19 Sep 2023 10:40:09 +0200 Subject: [PATCH 7/7] Bump version for release --- src/sinol_make/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sinol_make/__init__.py b/src/sinol_make/__init__.py index ab133f2e..1d53ae8b 100644 --- a/src/sinol_make/__init__.py +++ b/src/sinol_make/__init__.py @@ -8,7 +8,7 @@ from sinol_make import util, oiejq -__version__ = "1.5.5" +__version__ = "1.5.7" def configure_parsers(): parser = argparse.ArgumentParser(