diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2023-05-09 21:10:21 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-09 21:10:21 -0400 |
| commit | c71631e9835700175ee14aef53f80899aa88d568 (patch) | |
| tree | bc8c5f9987b47d63bbc37a6cbe7889d15047ada8 | |
| parent | Fix CI Badge in Readme (diff) | |
| parent | Remove extra comment (diff) | |
| download | taskipy2-c71631e9835700175ee14aef53f80899aa88d568.zip | |
Merge pull request #60 from taskipy/feature/gh-32/add-poetry-plugin
GH-32: Rework `main` for poetry plugin
| -rw-r--r-- | .github/workflows/ci.yml | 4 | ||||
| -rw-r--r-- | .pylintrc | 2 | ||||
| -rwxr-xr-x | taskipy/cli.py | 34 | ||||
| -rw-r--r-- | taskipy/list.py | 16 | ||||
| -rw-r--r-- | taskipy/py.typed | 0 | ||||
| -rw-r--r-- | tests/test_taskipy.py | 17 |
6 files changed, 51 insertions, 22 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d9aa4a5..c03a457 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,9 +24,9 @@ jobs: runs-on: ${{ matrix.os || 'ubuntu-latest' }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install Poetry @@ -15,4 +15,4 @@ max-line-length=1000 no-docstring-rgx=^.* const-rgx=^.* variable-rgx=[a-z_][a-z0-9_]{0,30}$ -missing-module-docstring=^.*
\ No newline at end of file +missing-module-docstring=^.* diff --git a/taskipy/cli.py b/taskipy/cli.py index 67baa0c..204880e 100755 --- a/taskipy/cli.py +++ b/taskipy/cli.py @@ -2,12 +2,28 @@ import argparse import sys from pathlib import Path +from typing import List, Union from taskipy.exceptions import TaskipyError, InvalidUsageError from taskipy.task_runner import TaskRunner def main(): + exit_code = run(sys.argv[1:]) + sys.exit(exit_code) + + +def run(args: List[str], cwd: Union[str, Path, None] = None) -> int: + """Run the taskipy CLI programmatically. + + Args: + args: The arguments passed to the taskipy CLI. + cwd: The working directory to run the task in. If not + provided, defaults to the current working directory. + + Returns: + 0 on success; > 0 for an error. + """ parser = argparse.ArgumentParser( prog='task', description='runs a task specified in your pyproject.toml under [tool.taskipy.tasks]', @@ -15,26 +31,26 @@ def main(): parser.add_argument('-l', '--list', help='show list of available tasks', action='store_true') parser.add_argument('name', help='name of the task', nargs='?') parser.add_argument('args', nargs=argparse.REMAINDER, help='arguments to pass to the task') - args = parser.parse_args() + parsed_args = parser.parse_args(args=args) try: - runner = TaskRunner(Path.cwd()) + cwd = Path(cwd).resolve() if cwd is not None else Path.cwd() + runner = TaskRunner(cwd) - if args.list: + if parsed_args.list: runner.list() - sys.exit(0) + return 0 - if args.name is None: + if parsed_args.name is None: raise InvalidUsageError(parser) - exit_code = runner.run(args.name, args.args) - sys.exit(exit_code) + return runner.run(parsed_args.name, parsed_args.args) except TaskipyError as e: print(e) - sys.exit(e.exit_code) + return e.exit_code except Exception as e: print(e) - sys.exit(1) + return 1 if __name__ == '__main__': diff --git a/taskipy/list.py b/taskipy/list.py index 50e95aa..04d683b 100644 --- a/taskipy/list.py +++ b/taskipy/list.py @@ -1,21 +1,23 @@ import shutil import textwrap -import colorama # type: ignore -from typing import List +import colorama # type: ignore +from typing import Iterable, Optional from taskipy.task import Task from taskipy.exceptions import EmptyTasksSectionError class TasksListFormatter: - def __init__(self, tasks: List[Task]): - self.__tasks = tasks + def __init__(self, tasks: Iterable[Task]): + if not tasks: + raise EmptyTasksSectionError() - def print(self, line_width=shutil.get_terminal_size().columns): + self.__tasks = tasks colorama.init() - if not self.__tasks: - raise EmptyTasksSectionError() + def print(self, line_width: Optional[int] = None): + if not line_width: + line_width = shutil.get_terminal_size().columns tasks_col = [task.name for task in self.__tasks] longest_item_in_tasks_col = len(max(tasks_col, key=len)) diff --git a/taskipy/py.typed b/taskipy/py.typed new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/taskipy/py.typed diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py index 4b9b347..9cf680a 100644 --- a/tests/test_taskipy.py +++ b/tests/test_taskipy.py @@ -6,7 +6,7 @@ import time import unittest import warnings from os import path -from typing import List, Tuple +from typing import List, Optional, Tuple from parameterized import parameterized # type: ignore import psutil # type: ignore @@ -26,12 +26,23 @@ class TaskipyTestCase(unittest.TestCase): for tmp_dir in self._tmp_dirs: tmp_dir.clean() - def run_task(self, task: str, args: List[str] = None, cwd=os.curdir) -> Tuple[int, str, str]: + def run_task( + self, + task: str, + args: Optional[List[str]] = None, + cwd=os.curdir, + ) -> Tuple[int, str, str]: + args = args or [] proc = self.start_taskipy_process(task, args=args, cwd=cwd) stdout, stderr = proc.communicate() return proc.returncode, stdout.decode(), str(stderr) - def start_taskipy_process(self, task: str, args: List[str] = None, cwd=os.curdir) -> subprocess.Popen: + def start_taskipy_process( + self, + task: str, + args: Optional[List[str]] = None, + cwd=os.curdir, + ) -> subprocess.Popen: executable_path = path.abspath('task') args = args or [] return subprocess.Popen([executable_path, task] + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) |