From 5af811fdfcf5d721e6d20ce965b74328b8a55ba3 Mon Sep 17 00:00:00 2001 From: Mateusz Masiarz Date: Fri, 5 Apr 2024 10:30:59 +0200 Subject: [PATCH] Allow caching with cwd --- src/sinol_make/helpers/compiler.py | 8 +++---- src/sinol_make/helpers/func_cache.py | 32 +++++++++++++++++++------- src/sinol_make/helpers/package_util.py | 2 +- src/sinol_make/util.py | 2 +- tests/util.py | 1 + 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/sinol_make/helpers/compiler.py b/src/sinol_make/helpers/compiler.py index e5167ecf..8a1a68a4 100644 --- a/src/sinol_make/helpers/compiler.py +++ b/src/sinol_make/helpers/compiler.py @@ -22,7 +22,7 @@ def check_if_installed(compiler): return True -@cache_result +@cache_result() def get_c_compiler_path(): """ Get the C compiler @@ -42,7 +42,7 @@ def get_c_compiler_path(): return None -@cache_result +@cache_result() def get_cpp_compiler_path(): """ Get the C++ compiler @@ -64,7 +64,7 @@ def get_cpp_compiler_path(): return None -@cache_result +@cache_result() def get_python_interpreter_path(): """ Get the Python interpreter @@ -78,7 +78,7 @@ def get_python_interpreter_path(): return None -@cache_result +@cache_result() def get_java_compiler_path(): """ Get the Java compiler diff --git a/src/sinol_make/helpers/func_cache.py b/src/sinol_make/helpers/func_cache.py index c329971c..6b493489 100644 --- a/src/sinol_make/helpers/func_cache.py +++ b/src/sinol_make/helpers/func_cache.py @@ -1,14 +1,30 @@ +import os + __cache = {} -def cache_result(func): +def cache_result(cwd=False): """ Function to cache the result of a function. """ - def wrapper(*args, **kwargs): - if func.__name__ in __cache: - return __cache[func.__name__] - result = func(*args, **kwargs) - __cache[func.__name__] = result - return result - return wrapper + def decorator(func): + def wrapper(*args, **kwargs): + if cwd: + key = (func.__name__, os.getcwd()) + else: + key = func.__name__ + + if key in __cache: + return __cache[key] + result = func(*args, **kwargs) + __cache[key] = result + return result + return wrapper + return decorator + + +def clear_cache(): + """ + Function to clear the cache. + """ + __cache.clear() diff --git a/src/sinol_make/helpers/package_util.py b/src/sinol_make/helpers/package_util.py index 6b32cd83..34286a49 100644 --- a/src/sinol_make/helpers/package_util.py +++ b/src/sinol_make/helpers/package_util.py @@ -11,7 +11,7 @@ from sinol_make.helpers import paths -@cache_result +@cache_result(cwd=True) def get_task_id() -> str: config = get_config() if "sinol_task_id" in config: diff --git a/src/sinol_make/util.py b/src/sinol_make/util.py index ca145f09..86cfa7a0 100644 --- a/src/sinol_make/util.py +++ b/src/sinol_make/util.py @@ -13,7 +13,7 @@ from sinol_make.structs.status_structs import Status -@cache_result +@cache_result() def get_commands(): """ Function to get an array of all available commands. diff --git a/tests/util.py b/tests/util.py index c29d99c3..e1a57782 100644 --- a/tests/util.py +++ b/tests/util.py @@ -202,6 +202,7 @@ def create_ins_outs(package_path): """ os.chdir(package_path) task_id = package_util.get_task_id() + print(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: