diff options
| -rw-r--r-- | README.md | 19 | ||||
| -rw-r--r-- | poetry.lock | 17 | ||||
| -rw-r--r-- | pyproject.toml | 2 | ||||
| -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 | ||||
| -rw-r--r-- | tests/test_taskipy.py | 167 |
8 files changed, 404 insertions, 58 deletions
@@ -16,6 +16,7 @@ - [Using Variables](#using-variables) - [String Formatting](#string-formatting) - [Always Use Variables](#always-use-variables) + - [Recursive Variables](#recursive-variables) - [Using Taskipy Without Poetry](#using-taskipy-without-poetry) - [Installing With PIP](#installing-with-pip) - [Running Tasks](#running-tasks-1) @@ -258,6 +259,24 @@ lint = "pylint {path}" black = "black {path}" ``` +#### Recursive Variables + +If we want to use variables within other variables, we can utilize recursive variables. By default, variables are not recursive, but we can specify a variable to be recursive by setting the `recursive` key to `true`. + +```toml +[tool.taskipy.settings] +use_vars = true + +[tool.taskipy.variables] +src_dir = "src" +package_dir = { var = "{src_dir}/package", recursive = true } + +[tool.taskipy.tasks] +echo = "echo {package_dir}" +``` + +In this example, we could run `task echo` and we would then see `src/package`. + ### Using Taskipy Without Poetry Taskipy was created with poetry projects in mind, but actually only requires a valid `pyproject.toml` file in your project's directory. As a result, you can use it even eithout poetry: diff --git a/poetry.lock b/poetry.lock index 4be47f3..d3267dd 100644 --- a/poetry.lock +++ b/poetry.lock @@ -82,6 +82,17 @@ optional = false python-versions = "*" [[package]] +name = "parameterized" +version = "0.8.1" +description = "Parameterized testing with any Python test framework" +category = "dev" +optional = false +python-versions = "*" + +[package.extras] +dev = ["jinja2"] + +[[package]] name = "platformdirs" version = "2.4.0" description = "A small Python module for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." @@ -172,7 +183,7 @@ python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" [metadata] lock-version = "1.1" python-versions = "^3.6" -content-hash = "9978d30f09e84edc769ea9e4467cb58e789bb74a8c87c3367402ea670cf71b45" +content-hash = "0f2902f0dc8c20fa5616e38c240226e931e782e1647369e0549e33982a387a11" [metadata.files] astroid = [ @@ -262,6 +273,10 @@ mypy-extensions = [ {file = "mypy_extensions-0.4.3-py2.py3-none-any.whl", hash = "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d"}, {file = "mypy_extensions-0.4.3.tar.gz", hash = "sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8"}, ] +parameterized = [ + {file = "parameterized-0.8.1-py2.py3-none-any.whl", hash = "sha256:9cbb0b69a03e8695d68b3399a8a5825200976536fe1cb79db60ed6a4c8c9efe9"}, + {file = "parameterized-0.8.1.tar.gz", hash = "sha256:41bbff37d6186430f77f900d777e5bb6a24928a1c46fb1de692f8b52b8833b5c"}, +] platformdirs = [ {file = "platformdirs-2.4.0-py3-none-any.whl", hash = "sha256:8868bbe3c3c80d42f20156f22e7131d2fb321f5bc86a2a345375c6481a67021d"}, {file = "platformdirs-2.4.0.tar.gz", hash = "sha256:367a5e80b3d04d2428ffa76d33f124cf11e8fff2acdaa9b43d545f5c7d661ef2"}, diff --git a/pyproject.toml b/pyproject.toml index 6708d32..df657cb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,7 +30,7 @@ colorama = "^0.4.4" pylint = "^2.4.4" mypy = "^0.812" rope = "^0.14.0" -psutil = "^5.6.7" +parameterized = "^0.8.1" [tool.taskipy.tasks] test = { cmd = "python -m unittest -v tests/test_*.py", help = "runs all tests" } 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 diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py index 6ce63f7..7b67fd3 100644 --- a/tests/test_taskipy.py +++ b/tests/test_taskipy.py @@ -5,10 +5,12 @@ import subprocess import time import unittest import warnings -import psutil # type: ignore from os import path from typing import List, Tuple +from parameterized import parameterized # type: ignore +import psutil # type: ignore + from tests.utils.project import ( GenerateProjectFromFixture, GenerateProjectWithPyProjectToml, @@ -504,7 +506,7 @@ class UseVarsTestCase(TaskipyTestCase): ''' cwd = self.create_test_dir_with_py_project_toml(py_project_toml) exit_code, stdout, _ = self.run_task('echo', cwd=cwd) - self.assertSubstr("reason: 'name' variable expected in [tool.taskipy.variables", stdout) + self.assertSubstr("reason: 'name' variable expected in [tool.taskipy.variables]", stdout) self.assertEqual(exit_code, 1) def test_use_vars_setting(self): @@ -522,3 +524,164 @@ class UseVarsTestCase(TaskipyTestCase): exit_code, stdout, _ = self.run_task('echo', cwd=cwd) self.assertSubstr('hello John Doe :', stdout) self.assertEqual(exit_code, 0) + + +class RecursiveVariablesTestCase(TaskipyTestCase): + def test_recursive_variables_can_use_other_variables(self): + py_project_toml = ''' + [tool.taskipy.settings] + use_vars = true + + [tool.taskipy.variables] + first_name = "John" + last_name = "Doe" + full_name = { var = "{first_name} {last_name}", recursive = true } + + [tool.taskipy.tasks] + echo = "echo hello {full_name}" + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('hello John Doe', stdout) + self.assertEqual(exit_code, 0) + + def test_recursive_variables_can_use_other_recursive_variables(self): + py_project_toml = ''' + [tool.taskipy.settings] + use_vars = true + + [tool.taskipy.variables] + first_name = "John" + last_name = "Doe" + full_name = { var = "{first_name} {last_name}", recursive = true } + another_name = { var = "{full_name} another name!", recursive = true } + even_another_name = { var = "{another_name} {full_name}", recursive = true } + + [tool.taskipy.tasks] + echo = "echo hello {even_another_name}" + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('hello John Doe another name! John Doe', stdout) + self.assertEqual(exit_code, 0) + + def test_error_is_raised_if_a_cycle_is_detected(self): + py_project_toml = ''' + [tool.taskipy.settings] + use_vars = true + + [tool.taskipy.variables] + last_name = { var = "{first_name}", recursive = true } + first_name = { var = "{last_name}", recursive = true } + + [tool.taskipy.tasks] + echo = "echo hello {first_name} {last_name}!" + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('cannot resolve variables, found variables that depend on each other', stdout) + self.assertEqual(exit_code, 127) + + def test_non_recursive_variables_cant_use_other_variables(self): + py_project_toml = ''' + [tool.taskipy.settings] + use_vars = true + + [tool.taskipy.variables] + first_name = "John" + last_name = "Doe" + full_name_1 = "{first_name} {last_name}" + full_name_2 = { var = "{first_name} {last_name}", recursive = false } + + [tool.taskipy.tasks] + echo = "echo full_name_1: {full_name_1} full_name_2: {full_name_2}!" + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('full_name_1: {first_name} {last_name} full_name_2: {first_name} {last_name}', stdout) + self.assertEqual(exit_code, 0) + + +class VariableSchemaTestCase(TaskipyTestCase): + def test_variables_can_be_strings(self): + py_project_toml = ''' + [tool.taskipy.variables] + name = "John" + + [tool.taskipy.tasks] + echo = { cmd = "echo {name}", use_vars = true } + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('John', stdout) + self.assertEqual(exit_code, 0) + + def test_variables_can_be_a_table_with_var_key_and_no_recursive_key(self): + py_project_toml = ''' + [tool.taskipy.variables] + name = { var = "John" } + + [tool.taskipy.tasks] + echo = { cmd = "echo {name}", use_vars = true } + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('John', stdout) + self.assertEqual(exit_code, 0) + + def test_variables_can_be_a_table_with_var_key_and_recursive_key(self): + py_project_toml = ''' + [tool.taskipy.variables] + name = { var = "John", recursive = true } + + [tool.taskipy.tasks] + echo = { cmd = "echo {name}", use_vars = true } + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('John', stdout) + self.assertEqual(exit_code, 0) + + def test_variable_table_by_default_is_not_recursive(self): + py_project_toml = ''' + [tool.taskipy.variables] + test = "test var" + name = { var = "{test}" } + + [tool.taskipy.tasks] + echo = { cmd = "echo {name}", use_vars = true } + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('{test}', stdout) + self.assertEqual(exit_code, 0) + + def test_regular_string_variable_are_not_recursive(self): + py_project_toml = ''' + [tool.taskipy.variables] + test = "test var" + name = "{test}" + + [tool.taskipy.tasks] + echo = { cmd = "echo {name}", use_vars = true } + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('{test}', stdout) + self.assertEqual(exit_code, 0) + + @parameterized.expand([(5,), (5.5, ), ([], ), ("false",), ({},)]) + def test_other_variable_schemas_are_rejected_with_invalid_variable_error( + self, value + ): + py_project_toml = f''' + [tool.taskipy.variables] + test = {value} + ''' + ''' + [tool.taskipy.tasks] + echo = { cmd = "echo {test}", use_vars = true } + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('echo', cwd=cwd) + self.assertSubstr('variable test is invalid', stdout) + self.assertEqual(exit_code, 127) |