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 /task | |
| parent | initial commit (diff) | |
| download | taskipy2-51036248bd00d84f684e60ec5f7a2da6d10ee0d0.zip | |
v1.0.0
Diffstat (limited to '')
| l---------[-rwxr-xr-x] | task | 63 | ||||
| -rwxr-xr-x | taskipy/cli.py | 13 | ||||
| -rw-r--r--[-rwxr-xr-x] | taskipy/run.py | 37 |
3 files changed, 29 insertions, 84 deletions
@@ -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 |