Skip to content

Commit

Permalink
Create cache directories on start (#257)
Browse files Browse the repository at this point in the history
* Create all required cache paths on start

* Fix tests

* Fix conftest
  • Loading branch information
MasloMaslane authored Sep 7, 2024
1 parent 287af13 commit 3757dbd
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/sinol_make/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import argcomplete

from sinol_make import util, sio2jail
from sinol_make.helpers import cache

# Required for side effects
from sinol_make.task_type.normal import NormalTaskType # noqa
Expand Down Expand Up @@ -82,6 +83,7 @@ def main_exn():
if command:
if len(arguments) > 1:
print(f' {command.get_name()} command '.center(util.get_terminal_size()[1], '='))
cache.create_cache_dirs()
command.run(args)
else:
parser.print_help()
Expand Down
1 change: 0 additions & 1 deletion src/sinol_make/commands/chkwer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def run_test(self, execution: ChkwerExecution) -> RunResult:
return RunResult(execution.in_test_path, ok, int(points), comment)

def run_and_print_table(self) -> Dict[str, TestResult]:
os.makedirs(paths.get_chkwer_path(), exist_ok=True)
results = {}
sorted_tests = sorted(self.tests, key=lambda test: package_util.get_group(test, self.task_id))
executions: List[ChkwerExecution] = []
Expand Down
1 change: 0 additions & 1 deletion src/sinol_make/commands/doc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def make_file(self, file_path):

def move_logs(self):
output_dir = paths.get_cache_path('doc_logs')
os.makedirs(output_dir, exist_ok=True)
for pattern in self.LOG_PATTERNS:
for file in glob.glob(os.path.join(os.getcwd(), 'doc', pattern)):
os.rename(file, os.path.join(output_dir, os.path.basename(file)))
Expand Down
5 changes: 0 additions & 5 deletions src/sinol_make/commands/run/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,6 @@ def get_groups(self, tests):
return sorted(list(set([self.get_group(test) for test in tests])))

def compile_solutions(self, solutions):
os.makedirs(paths.get_compilation_log_path(), exist_ok=True)
os.makedirs(paths.get_executables_path(), exist_ok=True)
print("Compiling %d solutions..." % len(solutions))
args = [(solution, None, True, False, None) for solution in solutions]
with mp.Pool(self.cpus) as pool:
Expand Down Expand Up @@ -492,7 +490,6 @@ def compile_and_run(self, solutions):
for i in range(len(solutions)):
if not compilation_results[i]:
self.failed_compilations.append(solutions[i])
os.makedirs(paths.get_executions_path(), exist_ok=True)
executables = [paths.get_executables_path(package_util.get_executable(solution)) for solution in solutions]
compiled_commands = zip(solutions, executables, compilation_results)
names = solutions
Expand Down Expand Up @@ -932,8 +929,6 @@ def set_task_type(self, timetool_name, timetool_path):

def compile_additional_files(self):
additional_files = self.task_type.additional_files_to_compile()
os.makedirs(paths.get_compilation_log_path(), exist_ok=True)
os.makedirs(paths.get_executables_path(), exist_ok=True)
for file, dest, name, clear_cache, fail_on_error in additional_files:
print(f"Compiling {name}...")
success = self.compile(file, dest, False, clear_cache, name)
Expand Down
5 changes: 3 additions & 2 deletions src/sinol_make/commands/verify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import subprocess

from sinol_make import util, contest_types
from sinol_make.helpers import parsers, package_util, paths
from sinol_make.helpers import parsers, package_util, paths, cache
from sinol_make.interfaces.BaseCommand import BaseCommand
from sinol_make.commands.gen import Command as GenCommand
from sinol_make.commands.doc import Command as DocCommand
Expand Down Expand Up @@ -58,11 +58,12 @@ def correct_contest_type(self):

def remove_cache(self):
"""
Remove whole cache dir
Remove whole cache dir, but keep the directories.
"""
cache_dir = paths.get_cache_path()
if os.path.exists(cache_dir):
shutil.rmtree(cache_dir)
cache.create_cache_dirs()

def check_extra_files(self):
"""
Expand Down
18 changes: 16 additions & 2 deletions src/sinol_make/helpers/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def get_cache_file(solution_path: str) -> CacheFile:
:param solution_path: Path to solution
:return: Content of cache file
"""
os.makedirs(paths.get_cache_path("md5sums"), exist_ok=True)
cache_file_path = paths.get_cache_path("md5sums", os.path.basename(solution_path))
try:
with open(cache_file_path, 'r') as cache_file:
Expand Down Expand Up @@ -143,7 +142,6 @@ def check_can_access_cache():
Checks if user can access cache.
"""
try:
os.makedirs(paths.get_cache_path(), exist_ok=True)
with open(paths.get_cache_path("test"), "w") as f:
f.write("test")
os.unlink(paths.get_cache_path("test"))
Expand Down Expand Up @@ -177,3 +175,19 @@ def check_correct_solution(task_id: str):

if has_file_changed(solution) and os.path.exists(os.path.join(os.getcwd(), 'in', '.md5sums')):
os.unlink(os.path.join(os.getcwd(), 'in', '.md5sums'))


def create_cache_dirs():
"""
Creates all required cache directories.
"""
for dir in [
paths.get_cache_path(),
paths.get_executables_path(),
paths.get_compilation_log_path(),
paths.get_executions_path(),
paths.get_chkwer_path(),
paths.get_cache_path('md5sums'),
paths.get_cache_path('doc_logs'),
]:
os.makedirs(dir, exist_ok=True)
3 changes: 0 additions & 3 deletions src/sinol_make/helpers/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ def compile_file(file_path: str, name: str, compilers: Compilers, compilation_fl
:param use_extras: Whether to use extra compilation files and arguments from config
:return: Tuple of (executable path or None if compilation failed, log path)
"""
os.makedirs(paths.get_executables_path(), exist_ok=True)
os.makedirs(paths.get_compilation_log_path(), exist_ok=True)

config = package_util.get_config()

extra_compilation_args = []
Expand Down
1 change: 0 additions & 1 deletion src/sinol_make/helpers/package_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ 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)

Expand Down
1 change: 0 additions & 1 deletion src/sinol_make/structs/cache_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,5 @@ def from_dict(dict) -> 'CacheFile':
)

def save(self, solution_path: str):
os.makedirs(paths.get_cache_path("md5sums"), exist_ok=True)
with open(paths.get_cache_path("md5sums", os.path.basename(solution_path)), 'w') as cache_file:
yaml.dump(self.to_dict(), cache_file)
1 change: 0 additions & 1 deletion src/sinol_make/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ def check_version():
except PermissionError:
if find_and_chdir_package():
try:
os.makedirs(paths.get_cache_path(), exist_ok=True)
with open(paths.get_cache_path("sinol_make_version"), "w") as f:
f.write(latest_version)
except PermissionError:
Expand Down
3 changes: 2 additions & 1 deletion tests/commands/export/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sinol_make import configure_parsers
from sinol_make import util as sinol_util
from sinol_make.commands.doc import Command as DocCommand
from sinol_make.helpers import paths
from sinol_make.helpers import paths, cache
from tests import util
from tests.fixtures import create_package
from .util import *
Expand Down Expand Up @@ -54,6 +54,7 @@ def _set_contest_type(contest_type):
os.remove(f'{task_id}.tgz')
if os.path.exists(paths.get_cache_path()):
shutil.rmtree(paths.get_cache_path())
cache.create_cache_dirs()


@pytest.mark.parametrize("create_package", [util.get_simple_package_path(), util.get_library_package_path(),
Expand Down
1 change: 1 addition & 0 deletions tests/commands/gen/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def test_missing_output_files(create_package):
assert os.path.exists(outs[0])
shutil.rmtree(paths.get_cache_path())
os.unlink(os.path.join(package_path, "in", ".md5sums"))
cache.create_cache_dirs()


@pytest.mark.parametrize("create_package", [util.get_shell_ingen_pack_path(), util.get_simple_package_path()],
Expand Down
5 changes: 4 additions & 1 deletion tests/commands/verify/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
import shutil
from linecache import cache

import pytest

from sinol_make import configure_parsers
from sinol_make import util as sm_util
from sinol_make.commands.verify import Command
from sinol_make.helpers import package_util, paths, func_cache
from sinol_make.helpers import package_util, paths, func_cache, cache
from tests import util
from tests.fixtures import create_package

Expand Down Expand Up @@ -112,6 +114,7 @@ def test_scores_not_100(capsys, create_package):
for contest_type in ["oi", "oij"]:
if os.path.exists(paths.get_cache_path()):
shutil.rmtree(paths.get_cache_path())
cache.create_cache_dirs()
config = package_util.get_config()
config["sinol_contest_type"] = contest_type
sm_util.save_config(config)
Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import multiprocessing as mp

from sinol_make import util
from sinol_make.helpers import compile, paths
from sinol_make.helpers import compile, paths, cache
from sinol_make.interfaces.Errors import CompilationError


Expand Down Expand Up @@ -48,6 +48,11 @@ def pytest_configure(config):

files_to_compile = []
for package in packages:
cwd = os.getcwd()
os.chdir(package)
cache.create_cache_dirs()
os.chdir(cwd)

if os.path.exists(os.path.join(package, "no-precompile")):
print(f'Skipping precompilation for {package} due to no-precompile file')
continue
Expand Down
2 changes: 2 additions & 0 deletions tests/helpers/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
def test_compilation_caching():
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
cache.create_cache_dirs()
program = os.path.join(tmpdir, 'program.cpp')
open(program, 'w').write('int main() { return 0; }')

Expand Down Expand Up @@ -45,6 +46,7 @@ def test_compilation_caching():
def test_cache():
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
cache.create_cache_dirs()
assert cache.get_cache_file("abc.cpp") == CacheFile()

cache_file = CacheFile(
Expand Down
3 changes: 2 additions & 1 deletion tests/helpers/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
import tempfile

from sinol_make.helpers.cache import save_compiled, check_compiled
from sinol_make.helpers.cache import save_compiled, check_compiled, create_cache_dirs
from tests import util
from tests.fixtures import create_package
from tests.commands.gen.test_integration import simple_run
Expand All @@ -11,6 +11,7 @@
def test_compilation_caching():
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
create_cache_dirs()
with open(os.path.join(os.getcwd(), "test.txt"), "w") as f:
f.write("Test data")
with open(os.path.join(os.getcwd(), "test.e"), "w") as f:
Expand Down

0 comments on commit 3757dbd

Please sign in to comment.