Skip to content

Commit

Permalink
Allow caching with cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
MasloMaslane committed Apr 5, 2024
1 parent 1ef0012 commit 5af811f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/sinol_make/helpers/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def check_if_installed(compiler):
return True


@cache_result
@cache_result()
def get_c_compiler_path():
"""
Get the C compiler
Expand All @@ -42,7 +42,7 @@ def get_c_compiler_path():
return None


@cache_result
@cache_result()
def get_cpp_compiler_path():
"""
Get the C++ compiler
Expand All @@ -64,7 +64,7 @@ def get_cpp_compiler_path():
return None


@cache_result
@cache_result()
def get_python_interpreter_path():
"""
Get the Python interpreter
Expand All @@ -78,7 +78,7 @@ def get_python_interpreter_path():
return None


@cache_result
@cache_result()
def get_java_compiler_path():
"""
Get the Java compiler
Expand Down
32 changes: 24 additions & 8 deletions src/sinol_make/helpers/func_cache.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion src/sinol_make/helpers/package_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/sinol_make/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 5af811f

Please sign in to comment.