diff --git a/src/sinol_make/__init__.py b/src/sinol_make/__init__.py index 69219ef2..c6c6d1e3 100644 --- a/src/sinol_make/__init__.py +++ b/src/sinol_make/__init__.py @@ -50,8 +50,10 @@ def main_exn(): parser = configure_parsers() arguments = [] curr_args = [] + commands = util.get_commands() + commands_dict = {command.get_name(): command for command in commands} for arg in sys.argv[1:]: - if arg in util.get_command_names() and not (len(curr_args) > 0 and curr_args[0] == 'init'): + if arg in commands_dict.keys() and not (len(curr_args) > 0 and curr_args[0] == 'init'): if curr_args: arguments.append(curr_args) curr_args = [arg] @@ -59,19 +61,19 @@ def main_exn(): curr_args.append(arg) if curr_args: arguments.append(curr_args) - commands = util.get_commands() + if not arguments: + parser.print_help() + exit(1) check_oiejq() for curr_args in arguments: args = parser.parse_args(curr_args) - command_found = False - for command in commands: - if command.get_name() == args.command: - command_found = True - if len(arguments) > 1: - print(f' {command.get_name()} command '.center(util.get_terminal_size()[1], '=')) - command.run(args) - if not command_found: + command = commands_dict.get(args.command, None) + if command: + if len(arguments) > 1: + print(f' {command.get_name()} command '.center(util.get_terminal_size()[1], '=')) + command.run(args) + else: parser.print_help() exit(1) diff --git a/src/sinol_make/helpers/compiler.py b/src/sinol_make/helpers/compiler.py index 6b11bc36..8a1a68a4 100644 --- a/src/sinol_make/helpers/compiler.py +++ b/src/sinol_make/helpers/compiler.py @@ -1,12 +1,12 @@ from typing import List - +import subprocess import argparse +import sys import os -import sinol_make.util as util -import sys, subprocess - +from sinol_make import util from sinol_make.structs.compiler_structs import Compilers +from sinol_make.helpers.func_cache import cache_result def check_if_installed(compiler): @@ -22,6 +22,7 @@ def check_if_installed(compiler): return True +@cache_result() def get_c_compiler_path(): """ Get the C compiler @@ -41,6 +42,7 @@ def get_c_compiler_path(): return None +@cache_result() def get_cpp_compiler_path(): """ Get the C++ compiler @@ -62,6 +64,7 @@ def get_cpp_compiler_path(): return None +@cache_result() def get_python_interpreter_path(): """ Get the Python interpreter @@ -75,6 +78,7 @@ def get_python_interpreter_path(): return None +@cache_result() def get_java_compiler_path(): """ Get the Java compiler @@ -94,7 +98,8 @@ def get_default_compilers(): c_compiler_path=get_c_compiler_path(), cpp_compiler_path=get_cpp_compiler_path(), python_interpreter_path=get_python_interpreter_path(), - java_compiler_path=get_java_compiler_path() + # Java is not currently supported by sinol-make + # java_compiler_path=get_java_compiler_path() ) @@ -141,5 +146,5 @@ def verify_compilers(args: argparse.Namespace, solutions: List[str]) -> Compiler c_compiler_path=args.c_compiler_path, cpp_compiler_path=args.cpp_compiler_path, python_interpreter_path=args.python_interpreter_path, - java_compiler_path=args.java_compiler_path + java_compiler_path=None ) diff --git a/src/sinol_make/helpers/func_cache.py b/src/sinol_make/helpers/func_cache.py new file mode 100644 index 00000000..6b493489 --- /dev/null +++ b/src/sinol_make/helpers/func_cache.py @@ -0,0 +1,30 @@ +import os + +__cache = {} + + +def cache_result(cwd=False): + """ + Function to cache the result of a function. + """ + 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 0f33c1d2..a4789697 100644 --- a/src/sinol_make/helpers/package_util.py +++ b/src/sinol_make/helpers/package_util.py @@ -6,10 +6,12 @@ from enum import Enum from typing import List, Union, Dict, Any +from sinol_make.helpers.func_cache import cache_result from sinol_make import util from sinol_make.helpers import paths +@cache_result(cwd=True) def get_task_id() -> str: config = get_config() if "sinol_task_id" in config: diff --git a/src/sinol_make/helpers/parsers.py b/src/sinol_make/helpers/parsers.py index 83f1ae74..db233ed1 100644 --- a/src/sinol_make/helpers/parsers.py +++ b/src/sinol_make/helpers/parsers.py @@ -1,5 +1,4 @@ import sys - import argparse from sinol_make.helpers import compiler @@ -20,8 +19,9 @@ def add_compilation_arguments(parser: argparse.ArgumentParser): parser.add_argument('--python-interpreter-path', dest='python_interpreter_path', type=str, default=compiler.get_python_interpreter_path(), help='Python interpreter to use (default: python3)') - parser.add_argument('--java-compiler-path', dest='java_compiler_path', type=str, - default=compiler.get_java_compiler_path(), help='Java compiler to use (default: javac)') + # Java is not currently supported by sinol-make + # parser.add_argument('--java-compiler-path', dest='java_compiler_path', type=str, + # default=compiler.get_java_compiler_path(), help='Java compiler to use (default: javac)') parser.add_argument('--compile-mode', '-C', dest='compile_mode', choices=['default', 'oioioi', 'weak', 'd', 'o', 'w'], help='Warning flag groups used to compile C/C++ files. Available options:\n' ' default / d - uses default flags: \n' diff --git a/src/sinol_make/util.py b/src/sinol_make/util.py index c137c30f..58548f30 100644 --- a/src/sinol_make/util.py +++ b/src/sinol_make/util.py @@ -1,6 +1,5 @@ import glob, importlib, os, sys, requests, yaml import math -import multiprocessing import platform import tarfile import hashlib @@ -11,19 +10,15 @@ from sinol_make.contest_types import get_contest_type from sinol_make.helpers import paths, cache +from sinol_make.helpers.func_cache import cache_result from sinol_make.structs.status_structs import Status -__cache = {} - - +@cache_result() def get_commands(): """ Function to get an array of all available commands. """ - global __cache - if 'commands' in __cache: - return __cache['commands'] commands_path = glob.glob( os.path.join( os.path.dirname(os.path.realpath(__file__)), @@ -35,7 +30,6 @@ def get_commands(): temp = importlib.import_module('sinol_make.commands.' + os.path.basename(path), 'Command') commands.append(temp.Command()) - __cache['commands'] = commands return commands diff --git a/tests/helpers/test_package_util.py b/tests/helpers/test_package_util.py index d821eb17..3d3538ce 100644 --- a/tests/helpers/test_package_util.py +++ b/tests/helpers/test_package_util.py @@ -3,7 +3,7 @@ from ..commands.run.util import create_ins from ..fixtures import * from tests import util -from sinol_make.helpers import package_util +from sinol_make.helpers import package_util, func_cache @pytest.mark.parametrize("create_package", [util.get_long_name_package_path()], indirect=True) @@ -12,6 +12,7 @@ def test_get_task_id(create_package): assert package_util.get_task_id() == "lpn" with open(os.path.join(package_path, "config.yml"), "w") as config_file: config_file.write("title: Long package name\n") + func_cache.clear_cache() with pytest.raises(SystemExit): package_util.get_task_id() diff --git a/tests/util.py b/tests/util.py index 694f5c10..d4158735 100644 --- a/tests/util.py +++ b/tests/util.py @@ -216,6 +216,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: