diff options
| author | Roy Sommer <roy@sommer.co.il> | 2019-12-23 00:08:21 +0200 |
|---|---|---|
| committer | Roy Sommer <roy@sommer.co.il> | 2019-12-23 00:08:21 +0200 |
| commit | 51036248bd00d84f684e60ec5f7a2da6d10ee0d0 (patch) | |
| tree | 068d4bf2f94dbc9b1c7c55c0605489319d1dc703 | |
| parent | initial commit (diff) | |
| download | taskipy2-51036248bd00d84f684e60ec5f7a2da6d10ee0d0.zip | |
v1.0.0
| -rw-r--r-- | LICENSE | 21 | ||||
| -rw-r--r-- | README.md | 118 | ||||
| -rw-r--r-- | pyproject.toml | 21 | ||||
| l---------[-rwxr-xr-x] | task | 63 | ||||
| -rwxr-xr-x | taskipy/cli.py | 13 | ||||
| -rw-r--r--[-rwxr-xr-x] | taskipy/run.py | 37 | ||||
| -rw-r--r-- | tests/test_taskipy.py | 2 |
7 files changed, 188 insertions, 87 deletions
@@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Roy Sommer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE.
\ No newline at end of file @@ -1 +1,117 @@ -# taskipy
\ No newline at end of file +<img src="./logo.svg" width="150" /> + +> the complementary task runner for python + +## General +Every development pipeline has tasks, such as `test`, `lint` or `publish`. With taskipy, you can define those tasks in one file and run them with a simple command. + +For instance, instead of running the following command: +```bash +python -m unittest tests/test_*.py +``` + +You can create a task called `test` and simply run: +```bash +task test +``` + +In addition, you can compose tasks and group them together, and also create dependencies between them. + +This project is heavily inspired by [npm's run script command](https://docs.npmjs.com/cli/run-script). + +## Requirements +Python 3.5 or newer. + +Your project directory should include a valid `pyproject.toml` file, as specified in [PEP-518](https://www.python.org/dev/peps/pep-0518/). + +## Usage +### Installation +To install taskipy using pip, simply run: +```bash +pip install taskipy +``` + +#### With Poetry +If you're using [poetry](https://python-poetry.org/), add taskipy as a dev dependency: +```bash +poetry add --dev taskipy +``` + +### Adding Tasks +In your `pyproject.toml` file, add a new section called `[tool.taskify.tasks]`. + +The section is a key-value map, from the names of the task to the actual command that should be run in the shell. + +Example: + +__pyproject.toml__ +```toml +[tool.taskify.tasks] +test = "python -m unittest tests/test_*.py" +lint = "pylint tests taskipy" +``` + +### Running Tasks +In order to run a task, run the following command in your terminal (or virtualenv): +```bash +task test +``` + +#### With Poetry +If you're using poetry, you can use its `run` function: +```bash +poetry run task test +``` + +### Composing Tasks +#### Grouping Subtasks Together +Some tasks are composed of multiple subtasks. Instead of writing plain shell commands and stringing them together, you can break them down into multiple subtasks: +```toml +[tool.taskify.tasks] +lint_pylint = "pylint tests taskipy" +lint_mypy = "mypy tests taskipy" +``` + +And then create a composite task: +```toml +[tool.taskify.tasks] +lint = "task lint_pylint && task lint_mypy" +lint_pylint = "pylint tests taskipy" +lint_mypy = "mypy tests taskipy" +``` + +#### Pre Task Hook +Tasks might also depend on one another. For example, tests might require some binaries to be built. Take the two following commands, for instance: +```toml +[tool.taskify.tasks] +test = "python -m unittest tests/test_*.py" +build = "make ." +``` + +You could make tests depend on building, by using the **pretask hook**: +```toml +[tool.taskify.tasks] +pre_test = "task build" +test = "python -m unittest tests/test_*.py" +build = "make ." +``` + +The pretask hook looks for `pre_<task_name>` task for a given `task_name`. It will run it before running the task itself. If the pretask fails, then taskipy will exit without running the task itself. + +#### Post Task Hook +From time to time, you might want to run a task in conjuction with another. For example, you might want to run linting after a successful test run. Take the two following commands, for instance: +```toml +[tool.taskify.tasks] +test = "python -m unittest tests/test_*.py" +lint = "pylint tests taskipy" +``` + +You could make tests trigger linting, by using the **posttask hook**: +```toml +[tool.taskify.tasks] +test = "python -m unittest tests/test_*.py" +post_test = "task lint" +lint = "pylint tests taskipy" +``` + +The posttask hook looks for `post_<task_name>` task for a given `task_name`. It will run it after running the task itself. If the task failed, then taskipy will not run the posttask hook. diff --git a/pyproject.toml b/pyproject.toml index 4a47fca..c5060ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,8 +1,23 @@ [tool.poetry] name = "taskipy" description = "tasks runner for python projects" -version = "0.1.0" +version = "1.0.0" authors = ["Roy Sommer <roy@sommer.co.il>"] +readme = "README.md" +repository = "https://github.com/illBeRoy/taskipy" +license = "MIT" +keywords = ["tasks", "task runner", "development"] +classifiers = [ + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Topic :: Software Development :: Build Tools" +] +packages = [ + { include = "taskipy" } +] + +[tool.poetry.scripts] +task = 'taskipy.cli:main' [tool.poetry.dependencies] python = "^3.5" @@ -16,10 +31,14 @@ rope = "^0.14.0" [tool.taskify.tasks] test = "python -m unittest tests/test_*.py" post_test = "./task lint" + lint = "./task lint_pylint && ./task lint_mypy" lint_pylint = "pylint tests taskipy" lint_mypy = "mypy tests taskipy" +pre_publish = "./task test" +publish = "poetry publish --build" + [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api" @@ -1,62 +1 @@ -#!/usr/bin/env python3 -import argparse -import subprocess -import sys -import toml -from typing import List - -def run_commands_and_bail_on_first_fail(cmds: List[str]) -> int: - for cmd in cmds: - p = subprocess.Popen(cmd, shell=True) - p.wait() - if p.returncode != 0: - return p.returncode - - return 0 - - -if __name__ == '__main__': - parser = argparse.ArgumentParser(prog='task') - parser.add_argument('name', help='name of the task. found in pyproject.toml under [tool.taskify.tasks]') - - args = parser.parse_args() - - try: - pyproject = toml.load('pyproject.toml') - except FileNotFoundError: - print('no pyproject.toml file found in this directory') - sys.exit(1) - except: - print('pyproject.toml file is malformed and could not be read') - sys.exit(1) - - try: - tasks = pyproject['tool']['taskify']['tasks'] - except: - print('no tasks found. add a [tool.taskify.tasks] section to your pyproject.toml') - sys.exit(127) - - commands = [] - - try: - task = tasks[args.name] - commands.append(task) - except: - print(f'could not find task "{args.name}""') - sys.exit(127) - - try: - pre_task = tasks[f'pre_{args.name}'] - commands.insert(0, pre_task) - except: - pass - - try: - post_task = tasks[f'post_{args.name}'] - commands.append(post_task) - except: - pass - - exit_code = run_commands_and_bail_on_first_fail(commands) - sys.exit(exit_code) -
\ No newline at end of file +taskipy/cli.py
\ No newline at end of file diff --git a/taskipy/cli.py b/taskipy/cli.py new file mode 100755 index 0000000..4ef6170 --- /dev/null +++ b/taskipy/cli.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 +import argparse +from taskipy.run import run_task + +def main(): + parser = argparse.ArgumentParser(prog='task', description='runs a task specified in your pyproject.toml under [tool.taskify.tasks]') + parser.add_argument('name', help='name of the task') + args = parser.parse_args() + + run_task(args.name) + +if __name__ == '__main__': + main() diff --git a/taskipy/run.py b/taskipy/run.py index a5ce8b6..4c7d553 100755..100644 --- a/taskipy/run.py +++ b/taskipy/run.py @@ -1,28 +1,22 @@ -#!/usr/bin/env python3 -import argparse +import os import subprocess import sys import toml +from os import path from typing import List -def run_commands_and_bail_on_first_fail(cmds: List[str]) -> int: - for cmd in cmds: - p = subprocess.Popen(cmd, shell=True) - p.wait() - if p.returncode != 0: - return p.returncode +def run_task(task_name: str, cwd=os.curdir): + def run_commands_and_bail_on_first_fail(cmds: List[str]) -> int: + for cmd in cmds: + p = subprocess.Popen(cmd, shell=True, cwd=cwd) + p.wait() + if p.returncode != 0: + return p.returncode - return 0 - - -if __name__ == '__main__': - parser = argparse.ArgumentParser(prog='task') - parser.add_argument('name', help='name of the task. found in pyproject.toml under [tool.taskify.tasks]') - - args = parser.parse_args() + return 0 try: - pyproject = toml.load('pyproject.toml') + pyproject = toml.load(path.join(cwd, 'pyproject.toml')) except FileNotFoundError: print('no pyproject.toml file found in this directory') sys.exit(1) @@ -39,24 +33,23 @@ if __name__ == '__main__': commands = [] try: - task = tasks[args.name] + task = tasks[task_name] commands.append(task) except: - print(f'could not find task "{args.name}""') + print(f'could not find task "{task_name}""') sys.exit(127) try: - pre_task = tasks[f'pre_{args.name}'] + pre_task = tasks[f'pre_{task_name}'] commands.insert(0, pre_task) except: pass try: - post_task = tasks[f'post_{args.name}'] + post_task = tasks[f'post_{task_name}'] commands.append(post_task) except: pass exit_code = run_commands_and_bail_on_first_fail(commands) sys.exit(exit_code) -
\ No newline at end of file diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py index 450c65a..fdd9ac5 100644 --- a/tests/test_taskipy.py +++ b/tests/test_taskipy.py @@ -14,7 +14,7 @@ class TaskipyTestCase(unittest.TestCase): tmp_dir.clean() def run_task(self, task: str, cwd=os.curdir) -> Tuple[int, str, str]: - executable_path = path.abspath('taskipy/run.py') + executable_path = path.abspath('task') proc = subprocess.Popen([executable_path, task], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) stdout, stderr = proc.communicate() |