Skip to content

Commit 0d87acb

Browse files
committed
Remove tests cache when correct solutions changes
1 parent 40fdbbc commit 0d87acb

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/sinol_make/commands/gen/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from sinol_make import util
99
from sinol_make.commands.gen import gen_util
1010
from sinol_make.structs.gen_structs import OutputGenerationArguments
11-
from sinol_make.helpers import parsers, package_util
11+
from sinol_make.helpers import parsers, package_util, cache
1212
from sinol_make.interfaces.BaseCommand import BaseCommand
1313

1414

@@ -108,6 +108,7 @@ def run(self, args: argparse.Namespace):
108108
self.task_id = package_util.get_task_id()
109109
package_util.validate_test_names(self.task_id)
110110
util.change_stack_size_to_unlimited()
111+
cache.check_correct_solution(self.task_id)
111112
if self.ins:
112113
self.ingen = gen_util.get_ingen(self.task_id, args.ingen_path)
113114
print(util.info(f'Using ingen file {os.path.basename(self.ingen)}'))

src/sinol_make/helpers/cache.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,31 @@ def check_can_access_cache():
131131
except PermissionError:
132132
util.exit_with_error("You don't have permission to access the `.cache/` directory. "
133133
"`sinol-make` needs to be able to write to this directory.")
134+
135+
136+
def has_file_changed(file_path: str) -> bool:
137+
"""
138+
Checks if file has changed since last compilation.
139+
:param file_path: Path to the file
140+
:return: True if file has changed, False otherwise
141+
"""
142+
try:
143+
info = get_cache_file(file_path)
144+
return info.md5sum != util.get_file_md5(file_path)
145+
except FileNotFoundError:
146+
return True
147+
148+
149+
def check_correct_solution(task_id: str):
150+
"""
151+
Checks if correct solution has changed. If it did, removes cache for input files.
152+
:param task_id: Task id
153+
"""
154+
try:
155+
solution = package_util.get_correct_solution(task_id)
156+
except FileNotFoundError:
157+
return
158+
159+
if has_file_changed(solution):
160+
if os.path.exists(os.path.join(os.getcwd(), 'in', '.md5sums')):
161+
os.unlink(os.path.join(os.getcwd(), 'in', '.md5sums'))

src/sinol_make/helpers/package_util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ def get_solutions(task_id: str, args_solutions: Union[List[str], None] = None) -
146146
return sorted(solutions, key=lambda solution: get_executable_key(solution, task_id))
147147

148148

149+
def get_correct_solution(task_id: str) -> str:
150+
"""
151+
Returns path to correct solution.
152+
:param task_id: Task id.
153+
:return: Path to correct solution.
154+
"""
155+
correct_solution = get_solutions(task_id, [f'{task_id}.*'])
156+
if len(correct_solution) == 0:
157+
raise FileNotFoundError("Correct solution not found.")
158+
return os.path.join(os.getcwd(), "prog", correct_solution[0])
159+
160+
149161
def get_file_name(file_path):
150162
return os.path.split(file_path)[1]
151163

0 commit comments

Comments
 (0)