diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2022-02-10 08:56:27 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-10 16:56:27 +0200 |
| commit | 0b1e3ee299fd0eae0d85637fb469934743bc7c38 (patch) | |
| tree | 417110f07576f96a1056ddc6b169dd87770bbc2e /task | |
| parent | Replace toml with tomli for TOML v1 support (#43) (diff) | |
| download | taskipy2-0b1e3ee299fd0eae0d85637fb469934743bc7c38.zip | |
Add support for recursive variables (#44)
* Use `self` in pyproject
* Add support for recursive variables
* Remove duplicate psutil from pyproject
* Tweak implementation based on issue feedback
* Add type hint to vars_dict
* Use `count_of_previously_resolved_vars` name
* Clarify test name
* Revert refactor and use tuple for variable type return
* Add non recursive variable test case
* Alter test case name
* Add recursive variable docs
* Add recursive variables to table of contents
* Use PyProject to access static methods
* Add better handling of invalid variables
* Fix lockfile hash
* Move variable import
* Only resolve variables once if we are using vars
* Fix minor type issue
* Reword error message
Diffstat (limited to '')
| -rw-r--r-- | taskipy/exceptions.py | 19 | ||||
| -rw-r--r-- | taskipy/pyproject.py | 45 | ||||
| -rw-r--r-- | taskipy/task_runner.py | 176 | ||||
| -rw-r--r-- | taskipy/variable.py | 17 |
4 files changed, 203 insertions, 54 deletions
diff --git a/taskipy/exceptions.py b/taskipy/exceptions.py index d54a022..10eccfa 100644 --- a/taskipy/exceptions.py +++ b/taskipy/exceptions.py @@ -79,3 +79,22 @@ class MissingTaskipyTasksSectionError(TaskipyError): 'no tasks found. add a [tool.taskipy.tasks] ' 'section to your pyproject.toml' ) + + +class CircularVariableError(TaskipyError): + exit_code = 127 + + def __str__(self): + return 'cannot resolve variables, found variables that depend on each other.' + + +class InvalidVariableError(TaskipyError): + exit_code = 127 + + def __init__(self, variable: str, reason: str) -> None: + super().__init__() + self.variable = variable + self.reason = reason + + def __str__(self): + return f'variable {self.variable} is invalid. reason: {self.reason}' diff --git a/taskipy/pyproject.py b/taskipy/pyproject.py index d59d6ac..1ffcb3e 100644 --- a/taskipy/pyproject.py +++ b/taskipy/pyproject.py @@ -4,35 +4,64 @@ from pathlib import Path from typing import Any, Dict, MutableMapping, Optional, Union from taskipy.task import Task +from taskipy.variable import Variable from taskipy.exceptions import ( InvalidRunnerTypeError, + InvalidVariableError, MalformedPyProjectError, MissingPyProjectFileError, - MissingTaskipyTasksSectionError + MissingTaskipyTasksSectionError, ) class PyProject: def __init__(self, base_dir: Path): - pyproject_path = self.__find_pyproject_path(base_dir) + pyproject_path = PyProject.__find_pyproject_path(base_dir) self.__items = PyProject.__load_toml_file(pyproject_path) @property def tasks(self) -> Dict[str, Task]: try: - return { - task_name: Task(task_name, task_toml_contents) for - task_name, task_toml_contents in self.__items['tool']['taskipy']['tasks'].items() } + toml_tasks = self.__items['tool']['taskipy']['tasks'].items() except KeyError: raise MissingTaskipyTasksSectionError() + tasks = {} + for name, toml_contents in toml_tasks: + tasks[name] = Task(name, toml_contents) + + return tasks + @property - def variables(self) -> Dict[str, Task]: + def variables(self) -> Dict[str, Variable]: try: - return self.__items['tool']['taskipy'].get('variables', {}) + toml_vars = self.__items['tool']['taskipy'].get('variables', {}) except KeyError: return {} + vars_dict: Dict[str, Variable] = {} + for name, toml_contents in toml_vars.items(): + if isinstance(toml_contents, str): + vars_dict[name] = Variable(name, toml_contents, recursive=False) + elif ( + isinstance(toml_contents, dict) + and isinstance(toml_contents.get('var'), str) + ): + vars_dict[name] = Variable( + name, + toml_contents['var'], + toml_contents.get('recursive', False), + ) + else: + raise InvalidVariableError( + name, + f'expected variable to contain a string or be a table ' + 'with a key "var" that contains a string value, got ' + f'{toml_contents}.' + ) + + return vars_dict + @property def settings(self) -> dict: try: @@ -71,8 +100,10 @@ class PyProject: yield base for parent in base.parents: yield parent + for candidate_dir in candidate_dirs(base_dir): pyproject = candidate_dir / 'pyproject.toml' if pyproject.exists(): return pyproject + raise MissingPyProjectFileError() diff --git a/taskipy/task_runner.py b/taskipy/task_runner.py index 9ebac6d..bf5a3f2 100644 --- a/taskipy/task_runner.py +++ b/taskipy/task_runner.py @@ -1,15 +1,18 @@ import sys import platform -import psutil # type: ignore import signal import subprocess from pathlib import Path -from typing import List, Union, Optional +from types import FrameType +from typing import Callable, Dict, List, Tuple, Union, Optional + +import psutil # type: ignore +from taskipy.exceptions import CircularVariableError, TaskNotFoundError, MalformedTaskError +from taskipy.help import HelpFormatter from taskipy.pyproject import PyProject -from taskipy.exceptions import TaskNotFoundError, MalformedTaskError from taskipy.task import Task -from taskipy.help import HelpFormatter +from taskipy.variable import Variable if platform.system() == 'Windows': import mslex as shlex # type: ignore # pylint: disable=E0401 @@ -29,44 +32,146 @@ class TaskRunner: formatter.print() def run(self, task_name: str, args: List[str]) -> int: - try: - task = self.__project.tasks[task_name] - except KeyError: - raise TaskNotFoundError(task_name) + pre_command, command, post_command = self.__get_formatted_commands(task_name) - pre_task = self.__pre_task(task_name) - if pre_task is not None: - exit_code = self.__run_command_and_return_exit_code(pre_task) + if pre_command is not None: + exit_code = self.__run_command_and_return_exit_code(pre_command) if exit_code != 0: return exit_code - exit_code = self.__run_command_and_return_exit_code(task, args) + exit_code = self.__run_command_and_return_exit_code(command, args) if exit_code != 0: return exit_code - post_task = self.__post_task(task_name) - if post_task is not None: - exit_code = self.__run_command_and_return_exit_code(post_task) + if post_command is not None: + exit_code = self.__run_command_and_return_exit_code(post_command) if exit_code != 0: return exit_code return 0 - def __run_command_and_return_exit_code(self, task: Task, args: List[str] = None) -> int: - if args is None: - args = [] + def __get_formatted_commands( + self, task_name: str + ) -> Tuple[Optional[str], str, Optional[str]]: + pre_task, task, post_task = self.__get_tasks(task_name) + + should_resolve_vars = ( + self.__is_using_vars([pre_task, task, post_task]) + or self.__project.settings.get('use_vars') is True + ) + variables = self.__resolve_variables() if should_resolve_vars else {} + + pre_command = None + if pre_task is not None: + pre_command = self.__format_task_command(pre_task, variables) + + command = self.__format_task_command(task, variables) + + post_command = None + if post_task is not None: + post_command = self.__format_task_command(post_task, variables) + + return pre_command, command, post_command + + def __get_tasks(self, task_name: str) -> Tuple[Optional[Task], Task, Optional[Task]]: + pre_task = self.__pre_task(task_name) + post_task = self.__post_task(task_name) + + try: + task = self.__project.tasks[task_name] + except KeyError: + raise TaskNotFoundError(task_name) + + return pre_task, task, post_task + + def __pre_task(self, task_name: str) -> Optional[Task]: + return self.__project.tasks.get(f'pre_{task_name}') + + def __post_task(self, task_name: str) -> Optional[Task]: + return self.__project.tasks.get(f'post_{task_name}') + + def __is_using_vars(self, tasks: List[Optional[Task]]) -> bool: + use_vars = False + + for task in tasks: + if task is not None and task.use_vars: + use_vars = True + break + + return use_vars - command = task.command - if task.use_vars or self.__project.settings.get('use_vars', {}): + def __resolve_variables(self) -> Dict[str, str]: + nonrecursive_vars, recursive_vars = self.__get_variable_types( + self.__project.variables + ) + + while len(recursive_vars) > 0: + count_of_previously_resolved_vars = len(nonrecursive_vars) # pylint: disable=C0103 + + for name, value in recursive_vars.copy().items(): + try: + nonrecursive_vars[name] = value.format(**nonrecursive_vars) + recursive_vars.pop(name) + except KeyError: + pass + + if count_of_previously_resolved_vars == len(nonrecursive_vars): + raise CircularVariableError() + + return nonrecursive_vars + + def __get_variable_types( + self, variables: Dict[str, Variable] + ) -> Tuple[Dict[str, str], Dict[str, str]]: + nonrecursive_vars = {} + recursive_vars = {} + + for name, var in variables.items(): + if var.recursive: + recursive_vars[name] = var.value + else: + nonrecursive_vars[name] = var.value + + return nonrecursive_vars, recursive_vars + + def __format_task_command(self, task: Task, variables: Optional[dict]) -> str: + if task.use_vars or self.__project.settings.get('use_vars'): try: - command = command.format(**self.__project.variables) + return task.command.format(**variables) except KeyError as e: - raise MalformedTaskError(task.name, f"{e} variable expected in [tool.taskipy.variables]") + raise MalformedTaskError( + task.name, + f'{e} variable expected in [tool.taskipy.variables]', + ) + + return task.command + + def __run_command_and_return_exit_code( + self, command: str, args: Optional[List[str]] = None + ) -> int: + if args is None: + args = [] if self.__project.runner is not None: command = f'{self.__project.runner} {command}' - def send_signal_to_task_process(signum: int, _frame) -> None: + command_with_args = ' '.join([command] + [shlex.quote(arg) for arg in args]) + process = subprocess.Popen( + command_with_args, shell=True, cwd=self.__working_dir + ) + signal.signal(signal.SIGTERM, self.__send_signal_to_task_process(process)) + + try: + process.wait() + except KeyboardInterrupt: + pass + + return process.returncode + + def __send_signal_to_task_process( + self, process: subprocess.Popen + ) -> Callable[[int, Optional[FrameType]], None]: + def signal_handler(signum: int, _frame): # pylint: disable=W0640 psutil_process_wrapper = psutil.Process(process.pid) is_direct_subprocess_a_shell_process = sys.platform != 'darwin' # pylint: disable=C0103 @@ -80,27 +185,4 @@ class TaskRunner: else: process.send_signal(signum) - command_with_args = ' '.join([command] + [shlex.quote(arg) for arg in args]) - process = subprocess.Popen(command_with_args, - shell=True, - cwd=self.__working_dir) - signal.signal(signal.SIGTERM, send_signal_to_task_process) - - try: - process.wait() - except KeyboardInterrupt: - pass - - return process.returncode - - def __pre_task(self, task_name: str): - return self.__find_hooks('pre', task_name) - - def __post_task(self, task_name: str): - return self.__find_hooks('post', task_name) - - def __find_hooks(self, hook_type: str, task_name: str) -> Optional[Task]: - try: - return self.__project.tasks[f'{hook_type}_{task_name}'] - except KeyError: - return None + return signal_handler diff --git a/taskipy/variable.py b/taskipy/variable.py new file mode 100644 index 0000000..07efda8 --- /dev/null +++ b/taskipy/variable.py @@ -0,0 +1,17 @@ +class Variable: + def __init__(self, name: str, value: str, recursive: bool) -> None: + self.__name = name + self.__value = value + self.__recursive = recursive + + @property + def name(self) -> str: + return self.__name + + @property + def value(self) -> str: + return self.__value + + @property + def recursive(self) -> bool: + return self.__recursive |