Skip to content

Commit 5cc2d42

Browse files
authored
Rename cache directory to .cache (#106)
* Rename `cache` dir to `.cache` * Remove moving cache to .cache on newer versions * Add function for getting paths in cache dir * Fix tests * Refactor `paths` module
1 parent 7819c06 commit 5cc2d42

File tree

14 files changed

+82
-55
lines changed

14 files changed

+82
-55
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ build
66
.idea
77
__pycache__
88
/tests/packages/**/cache
9+
/tests/packages/**/.cache
910
src/sinol_make/data
1011

1112
# pytest-cov

src/sinol_make/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from sinol_make import util, oiejq
1010

11-
__version__ = "1.5.4"
11+
__version__ = "1.5.5"
1212

1313
def configure_parsers():
1414
parser = argparse.ArgumentParser(

src/sinol_make/commands/export/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import yaml
77

88
from sinol_make import util
9-
from sinol_make.helpers import package_util, parsers
9+
from sinol_make.helpers import package_util, parsers, paths
1010
from sinol_make.commands.gen import gen_util
1111
from sinol_make.interfaces.BaseCommand import BaseCommand
1212

@@ -34,7 +34,7 @@ def get_generated_tests(self):
3434
if not gen_util.ingen_exists(self.task_id):
3535
return []
3636

37-
working_dir = os.path.join(os.getcwd(), 'cache', 'export', 'tests')
37+
working_dir = paths.get_cache_path('export', 'tests')
3838
if os.path.exists(working_dir):
3939
shutil.rmtree(working_dir)
4040
os.makedirs(working_dir)
@@ -136,7 +136,7 @@ def run(self, args: argparse.Namespace):
136136
with open(os.path.join(os.getcwd(), 'config.yml'), 'r') as config_file:
137137
config = yaml.load(config_file, Loader=yaml.FullLoader)
138138

139-
export_package_path = os.path.join(os.getcwd(), 'cache', 'export', self.task_id)
139+
export_package_path = paths.get_cache_path('export', self.task_id)
140140
if os.path.exists(export_package_path):
141141
shutil.rmtree(export_package_path)
142142
os.makedirs(export_package_path)

src/sinol_make/commands/inwer/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from sinol_make import util
1111
from sinol_make.commands.inwer.structs import TestResult, InwerExecution, VerificationResult, TableData
12-
from sinol_make.helpers import package_util, compile, printer
12+
from sinol_make.helpers import package_util, compile, printer, paths
1313
from sinol_make.helpers.parsers import add_compilation_arguments
1414
from sinol_make.interfaces.BaseCommand import BaseCommand
1515
from sinol_make.commands.inwer import inwer_util
@@ -52,7 +52,7 @@ def verify_test(execution: InwerExecution) -> VerificationResult:
5252
"""
5353
Verifies a test and returns the result of inwer on this test.
5454
"""
55-
output_dir = os.path.join(os.getcwd(), 'cache', 'executions', execution.test_name)
55+
output_dir = paths.get_executables_path(execution.test_name)
5656
os.makedirs(output_dir, exist_ok=True)
5757

5858
command = [execution.inwer_exe_path]

src/sinol_make/commands/run/__init__.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from sinol_make.helpers.parsers import add_compilation_arguments
1515
from sinol_make.interfaces.BaseCommand import BaseCommand
1616
from sinol_make.interfaces.Errors import CompilationError, CheckerOutputException, UnknownContestType
17-
from sinol_make.helpers import compile, compiler, package_util, printer
17+
from sinol_make.helpers import compile, compiler, package_util, printer, paths
1818
from sinol_make.structs.status_structs import Status
1919
import sinol_make.util as util
2020
import yaml, os, collections, sys, re, math, dictdiffer
@@ -339,8 +339,8 @@ def get_groups(self, tests):
339339

340340

341341
def compile_solutions(self, solutions):
342-
os.makedirs(self.COMPILATION_DIR, exist_ok=True)
343-
os.makedirs(self.EXECUTABLES_DIR, exist_ok=True)
342+
os.makedirs(paths.get_compilation_log_path(), exist_ok=True)
343+
os.makedirs(paths.get_executables_path(), exist_ok=True)
344344
print("Compiling %d solutions..." % len(solutions))
345345
args = [(solution, True) for solution in solutions]
346346
with mp.Pool(self.cpus) as pool:
@@ -349,10 +349,9 @@ def compile_solutions(self, solutions):
349349

350350

351351
def compile(self, solution, use_extras = False):
352-
compile_log_file = os.path.join(
353-
self.COMPILATION_DIR, "%s.compile_log" % package_util.get_file_name(solution))
352+
compile_log_file = paths.get_compilation_log_path("%s.compile_log" % package_util.get_file_name(solution))
354353
source_file = os.path.join(os.getcwd(), "prog", self.get_solution_from_exe(solution))
355-
output = os.path.join(self.EXECUTABLES_DIR, package_util.get_executable(solution))
354+
output = paths.get_executables_path(package_util.get_executable(solution))
356355

357356
extra_compilation_args = []
358357
extra_compilation_files = []
@@ -581,7 +580,7 @@ def run_solution(self, data_for_execution: ExecutionData):
581580
"""
582581

583582
(name, executable, test, time_limit, memory_limit, timetool_path) = data_for_execution
584-
file_no_ext = os.path.join(self.EXECUTIONS_DIR, name, package_util.extract_test_id(test))
583+
file_no_ext = paths.get_executions_path(name, package_util.extract_test_id(test))
585584
output_file = file_no_ext + ".out"
586585
result_file = file_no_ext + ".res"
587586
hard_time_limit_in_s = math.ceil(2 * time_limit / 1000.0)
@@ -620,7 +619,7 @@ def run_solutions(self, compiled_commands, names, solutions):
620619
executions.append((name, executable, test, package_util.get_time_limit(test, self.config, lang, self.args),
621620
package_util.get_memory_limit(test, self.config, lang, self.args), self.timetool_path))
622621
all_results[name][self.get_group(test)][test] = ExecutionResult(Status.PENDING)
623-
os.makedirs(os.path.join(self.EXECUTIONS_DIR, name), exist_ok=True)
622+
os.makedirs(paths.get_executions_path(name), exist_ok=True)
624623
else:
625624
for test in self.tests:
626625
all_results[name][self.get_group(test)][test] = ExecutionResult(Status.CE)
@@ -685,9 +684,8 @@ def compile_and_run(self, solutions):
685684
for i in range(len(solutions)):
686685
if not compilation_results[i]:
687686
self.failed_compilations.append(solutions[i])
688-
os.makedirs(self.EXECUTIONS_DIR, exist_ok=True)
689-
executables = [os.path.join(self.EXECUTABLES_DIR, package_util.get_executable(solution))
690-
for solution in solutions]
687+
os.makedirs(paths.get_executions_path(), exist_ok=True)
688+
executables = [paths.get_executables_path(package_util.get_executable(solution)) for solution in solutions]
691689
compiled_commands = zip(solutions, executables, compilation_results)
692690
names = solutions
693691
return self.run_solutions(compiled_commands, names, solutions)
@@ -966,12 +964,7 @@ def set_group_result(solution, group, result):
966964

967965
def set_constants(self):
968966
self.ID = package_util.get_task_id()
969-
self.TMP_DIR = os.path.join(os.getcwd(), "cache")
970-
self.COMPILATION_DIR = os.path.join(self.TMP_DIR, "compilation")
971-
self.EXECUTIONS_DIR = os.path.join(self.TMP_DIR, "executions")
972-
self.EXECUTABLES_DIR = os.path.join(self.TMP_DIR, "executables")
973967
self.SOURCE_EXTENSIONS = ['.c', '.cpp', '.py', '.java']
974-
self.PROGRAMS_IN_ROW = 8
975968
self.SOLUTIONS_RE = re.compile(r"^%s[bs]?[0-9]*\.(cpp|cc|java|py|pas)$" % self.ID)
976969

977970

@@ -1138,7 +1131,7 @@ def run(self, args):
11381131
print(util.info("Checker found: %s" % os.path.basename(checker[0])))
11391132
self.checker = checker[0]
11401133
checker_basename = os.path.basename(self.checker)
1141-
self.checker_executable = os.path.join(self.EXECUTABLES_DIR, checker_basename + ".e")
1134+
self.checker_executable = paths.get_executables_path(checker_basename + ".e")
11421135

11431136
checker_compilation = self.compile_solutions([self.checker])
11441137
if not checker_compilation[0]:

src/sinol_make/helpers/compile.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
import sinol_make.helpers.compiler as compiler
1010
from sinol_make import util
11+
from sinol_make.helpers import paths
1112
from sinol_make.interfaces.Errors import CompilationError
1213
from sinol_make.structs.compiler_structs import Compilers
1314

1415

1516
def create_compilation_cache():
16-
os.makedirs(os.path.join(os.getcwd(), "cache", "md5sums"), exist_ok=True)
17+
os.makedirs(paths.get_cache_path("md5sums"), exist_ok=True)
1718

1819

1920
def check_compiled(file_path: str):
@@ -25,7 +26,7 @@ def check_compiled(file_path: str):
2526
create_compilation_cache()
2627
md5sum = util.get_file_md5(file_path)
2728
try:
28-
info_file_path = os.path.join(os.getcwd(), "cache", "md5sums", os.path.basename(file_path))
29+
info_file_path = paths.get_cache_path("md5sums", os.path.basename(file_path))
2930
with open(info_file_path, 'r') as info_file:
3031
info = yaml.load(info_file, Loader=yaml.FullLoader)
3132
if info.get("md5sum", "") == md5sum:
@@ -39,13 +40,13 @@ def check_compiled(file_path: str):
3940

4041
def save_compiled(file_path: str, exe_path: str):
4142
"""
42-
Save the compiled executable path to cache in `cache/md5sums/<basename of file_path>`,
43+
Save the compiled executable path to cache in `.cache/md5sums/<basename of file_path>`,
4344
which contains the md5sum of the file and the path to the executable.
4445
:param file_path: Path to the file
4546
:param exe_path: Path to the compiled executable
4647
"""
4748
create_compilation_cache()
48-
info_file_path = os.path.join(os.getcwd(), "cache", "md5sums", os.path.basename(file_path))
49+
info_file_path = paths.get_cache_path("md5sums", os.path.basename(file_path))
4950
info = {
5051
"md5sum": util.get_file_md5(file_path),
5152
"executable_path": exe_path
@@ -147,11 +148,8 @@ def compile_file(file_path: str, name: str, compilers: Compilers, weak_compilati
147148
:param weak_compilation_flags: Use weaker compilation flags
148149
:return: Tuple of (executable path or None if compilation failed, log path)
149150
"""
150-
151-
executable_dir = os.path.join(os.getcwd(), 'cache', 'executables')
152-
compile_log_dir = os.path.join(os.getcwd(), 'cache', 'compilation')
153-
os.makedirs(executable_dir, exist_ok=True)
154-
os.makedirs(compile_log_dir, exist_ok=True)
151+
os.makedirs(paths.get_executables_path(), exist_ok=True)
152+
os.makedirs(paths.get_compilation_log_path(), exist_ok=True)
155153

156154
with open(os.path.join(os.getcwd(), "config.yml"), "r") as config_file:
157155
config = yaml.load(config_file, Loader=yaml.FullLoader)
@@ -164,8 +162,8 @@ def compile_file(file_path: str, name: str, compilers: Compilers, weak_compilati
164162
args = [args]
165163
extra_compilation_args = [os.path.join(os.getcwd(), "prog", file) for file in args]
166164

167-
output = os.path.join(executable_dir, name)
168-
compile_log_path = os.path.join(compile_log_dir, os.path.splitext(name)[0] + '.compile_log')
165+
output = paths.get_executables_path(name)
166+
compile_log_path = paths.get_compilation_log_path(os.path.splitext(name)[0] + '.compile_log')
169167
with open(compile_log_path, 'w') as compile_log:
170168
try:
171169
if compile(file_path, output, compilers, compile_log, weak_compilation_flags, extra_compilation_args,

src/sinol_make/helpers/package_util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import List, Union, Dict, Any
66

77
from sinol_make import util
8+
from sinol_make.helpers import paths
89

910

1011
def get_task_id() -> str:
@@ -71,7 +72,7 @@ def get_executable_path(solution: str) -> str:
7172
"""
7273
Returns path to compiled executable for given solution.
7374
"""
74-
return os.path.join(os.getcwd(), 'cache', 'executables', get_executable(solution))
75+
return paths.get_executables_path(get_executable(solution))
7576

7677

7778
def get_file_lang(file_path):

src/sinol_make/helpers/paths.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
3+
4+
def get_cache_path(*paths):
5+
"""
6+
Function to get a path in the cache directory. Works the same as os.path.join.
7+
With no arguments, it returns the path to the cache directory.
8+
"""
9+
return os.path.join(os.getcwd(), ".cache", *paths)
10+
11+
12+
def get_executables_path(*paths):
13+
"""
14+
Function to get a path in executables directory. Works the same as os.path.join.
15+
With no arguments, it returns the path to the executables directory.
16+
"""
17+
return os.path.join(get_cache_path("executables"), *paths)
18+
19+
20+
def get_compilation_log_path(*paths):
21+
"""
22+
Function to get a path in compilation log directory. Works the same as os.path.join.
23+
With no arguments, it returns the path to the compilation log directory.
24+
"""
25+
return os.path.join(get_cache_path("compilation"), *paths)
26+
27+
28+
def get_executions_path(*paths):
29+
"""
30+
Function to get a path in executions directory. Works the same as os.path.join.
31+
With no arguments, it returns the path to the executions directory.
32+
"""
33+
return os.path.join(get_cache_path("executions"), *paths)

src/sinol_make/oiejq/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ def install_oiejq():
6363
if request.status_code != 200:
6464
raise Exception('Couldn\'t download oiejq (https://oij.edu.pl/zawodnik/srodowisko/oiejq.tar.gz returned status code: ' + str(request.status_code) + ')')
6565

66-
# oiejq is downloaded to a temporary directory and not to the `cache` dir,
66+
# oiejq is downloaded to a temporary directory and not to the `.cache` dir,
6767
# as there is no guarantee that the current directory is the package directory.
68-
# The `cache` dir is only used for files that are part of the package and those
68+
# The `.cache` dir is only used for files that are part of the package and those
6969
# that the package creator might want to look into.
7070
with tempfile.TemporaryDirectory() as tmpdir:
7171
oiejq_path = os.path.join(tmpdir, 'oiejq.tar.gz')

src/sinol_make/util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import glob, importlib, os, sys, requests, yaml
22
import platform
33
import tempfile
4+
import shutil
45
import hashlib
56
import threading
67
from typing import Union
78

9+
import sinol_make
10+
811

912
def get_commands():
1013
"""

tests/commands/run/test_unit.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def test_execution(create_package, time_tool):
5555
with open(os.path.join(package_path, "config.yml"), "r") as config_file:
5656
config = yaml.load(config_file, Loader=yaml.FullLoader)
5757

58-
os.makedirs(os.path.join(command.EXECUTIONS_DIR, solution), exist_ok=True)
59-
result = command.run_solution((solution, os.path.join(command.EXECUTABLES_DIR, executable), test, config['time_limit'], config['memory_limit'], oiejq.get_oiejq_path()))
58+
os.makedirs(paths.get_executions_path(solution), exist_ok=True)
59+
result = command.run_solution((solution, paths.get_executables_path(executable), test, config['time_limit'], config['memory_limit'], oiejq.get_oiejq_path()))
6060
assert result.Status == Status.OK
6161

6262

@@ -129,8 +129,8 @@ def test_print_expected_scores(capsys):
129129

130130

131131
def test_validate_expected_scores_success():
132-
command = get_command()
133132
os.chdir(get_simple_package_path())
133+
command = get_command()
134134
command.scores = command.config["scores"]
135135
command.tests = package_util.get_tests(None)
136136

@@ -195,8 +195,8 @@ def test_validate_expected_scores_success():
195195

196196

197197
def test_validate_expected_scores_fail(capsys):
198-
command = get_command()
199198
os.chdir(get_simple_package_path())
199+
command = get_command()
200200
command.scores = {1: 20, 2: 20, 3: 20, 4: 20}
201201

202202
# Test with missing points for group in config.

tests/conftest.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import pytest
66
import multiprocessing as mp
77

8-
from sinol_make.helpers import compile
8+
from sinol_make.helpers import compile, paths
99

1010

1111
def _compile(args):
1212
package, file_path = args
1313
os.chdir(package)
14-
output = os.path.join(package, "cache", "executables", os.path.splitext(os.path.basename(file_path))[0] + ".e")
15-
with open(os.path.join(package, "cache", "compilation",
16-
os.path.basename(file_path) + ".compile_log"), "w") as compile_log:
14+
output = paths.get_executables_path(os.path.splitext(os.path.basename(file_path))[0] + ".e")
15+
with open(paths.get_compilation_log_path(os.path.basename(file_path) + ".compile_log"), "w") as compile_log:
1716
compile.compile(file_path, output, compile_log=compile_log)
1817

1918

@@ -43,7 +42,7 @@ def pytest_configure(config):
4342
continue
4443

4544
for d in ["compilation", "executables"]:
46-
os.makedirs(os.path.join(package, "cache", d), exist_ok=True)
45+
os.makedirs(os.path.join(package, ".cache", d), exist_ok=True)
4746

4847
for program in glob.glob(os.path.join(package, "prog", "*")):
4948
if os.path.isfile(program) and os.path.splitext(program)[1] in [".c", ".cpp", ".py", ".java"]:

tests/packages/gen/prog/geningen.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function generate() {
1212
done
1313
}
1414

15-
cache=$(dirname "$0")/../cache
15+
cache=$(dirname "$0")/../.cache
1616
mkdir -p "$cache"
1717
g++ gen_helper.cpp -o "$cache"/gen
1818

tests/util.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import glob
33
import subprocess
44

5-
from sinol_make.helpers import compile
6-
5+
from sinol_make.helpers import compile, paths
76

87

98
def get_simple_package_path():
@@ -93,11 +92,11 @@ def create_ins(package_path):
9392
Create .in files for package.
9493
"""
9594
ingen = glob.glob(os.path.join(package_path, "prog", "*ingen.*"))[0]
96-
ingen_executable = os.path.join(package_path, "cache", "executables", "ingen.e")
97-
os.makedirs(os.path.join(package_path, "cache", "executables"), exist_ok=True)
95+
ingen_executable = paths.get_executables_path("ingen.e")
96+
os.makedirs(paths.get_executables_path(), exist_ok=True)
9897
assert compile.compile(ingen, ingen_executable)
9998
os.chdir(os.path.join(package_path, "in"))
100-
os.system("../cache/executables/ingen.e")
99+
os.system("../.cache/executables/ingen.e")
101100
os.chdir(package_path)
102101

103102

@@ -106,13 +105,13 @@ def create_outs(package_path):
106105
Create .out files for package.
107106
"""
108107
solution = glob.glob(os.path.join(package_path, "prog", "???.*"))[0]
109-
solution_executable = os.path.join(package_path, "cache", "executables", "solution.e")
110-
os.makedirs(os.path.join(package_path, "cache", "executables"), exist_ok=True)
108+
solution_executable = paths.get_executables_path("solution.e")
109+
os.makedirs(paths.get_executables_path(), exist_ok=True)
111110
assert compile.compile(solution, solution_executable)
112111
os.chdir(os.path.join(package_path, "in"))
113112
for file in glob.glob("*.in"):
114113
with open(file, "r") as in_file, open(os.path.join("../out", file.replace(".in", ".out")), "w") as out_file:
115-
subprocess.Popen([os.path.join(package_path, "cache", "executables", "solution.e")],
114+
subprocess.Popen([os.path.join(package_path, ".cache", "executables", "solution.e")],
116115
stdin=in_file, stdout=out_file).wait()
117116
os.chdir(package_path)
118117

0 commit comments

Comments
 (0)