Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MasloMaslane committed Sep 24, 2023
1 parent 310653c commit 6215bd5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
31 changes: 14 additions & 17 deletions src/sinol_make/helpers/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ def save_compiled(file_path: str, exe_path: str, is_checker: bool = False):
info.save(file_path)

if is_checker:
for solution in os.listdir(paths.get_cache_path('md5sums')):
info = get_cache_file(solution)
info.tests = {}
info.save(solution)
remove_results_cache()


def save_to_cache_extra_compilation_files(extra_compilation_files, task_id):
Expand Down Expand Up @@ -102,21 +99,21 @@ def save_to_cache_extra_compilation_files(extra_compilation_files, task_id):
info.save(file_path)


def remove_results_cache():
"""
Removes all cached test results
"""
for solution in os.listdir(paths.get_cache_path('md5sums')):
info = get_cache_file(solution)
info.tests = {}
info.save(solution)


def remove_results_if_contest_type_changed(contest_type):
"""
Checks if contest type has changed and removes all cached test results if it has.
:param contest_type: Contest type
"""
try:
with open(paths.get_cache_path("contest_type"), 'r') as cache_file:
cache_contest_type = cache_file.read().strip()
if cache_contest_type != contest_type:
for solution in os.listdir(paths.get_cache_path('md5sums')):
info = get_cache_file(solution)
info.tests = {}
info.save(solution)
except FileNotFoundError:
pass
os.makedirs(paths.get_cache_path(), exist_ok=True)
with open(paths.get_cache_path("contest_type"), 'w') as cache_file:
cache_file.write(contest_type)
if package_util.check_if_contest_type_changed(contest_type):
remove_results_cache()
package_util.save_contest_type_to_cache(contest_type)
23 changes: 23 additions & 0 deletions src/sinol_make/helpers/package_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,26 @@ def any_files_matching_pattern(task_id: str, pattern: str) -> bool:
:return: True if any file in package matches given pattern.
"""
return len(get_files_matching_pattern(task_id, pattern)) > 0


def check_if_contest_type_changed(contest_type):
"""
Checks if contest type in cache is different then contest type specified in config.yml.
:param contest_type: Contest type specified in config.yml.
:return: True if contest type in cache is different then contest type specified in config.yml.
"""
if not os.path.isfile(paths.get_cache_path("contest_type")):
return False
with open(paths.get_cache_path("contest_type"), "r") as contest_type_file:
cached_contest_type = contest_type_file.read()
return cached_contest_type != contest_type


def save_contest_type_to_cache(contest_type):
"""
Saves contest type to cache.
:param contest_type: Contest type.
"""
os.makedirs(paths.get_cache_path(), exist_ok=True)
with open(paths.get_cache_path("contest_type"), "w") as contest_type_file:
contest_type_file.write(contest_type)
2 changes: 1 addition & 1 deletion tests/commands/run/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_validate_expected_scores_success():
}
results = command.validate_expected_scores(results)
assert results.expected_scores != results.new_expected_scores
assert len(results.changes) == 2
assert len(results.changes) == 3

# Test with removed solution.
command.args = argparse.Namespace(solutions=None, tests=None, print_expected_scores=True)
Expand Down

0 comments on commit 6215bd5

Please sign in to comment.