Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add poetry scripts for common commands #578

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ structlog = ["structlog"]
redis = ["redis"]
memcached = ["aiomcache"]

[tool.poetry.scripts]
Copy link
Member

@cofin cofin Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea, but I think entries in poetry.scripts will be installed on any pip install/poetry install execution (even outside of development). I think we'd only want these in a developer environment.

The leaves something like poethepoet or poetry-pyinvoke-plugin as a potential option. Alternately, a Makefile could be a solid alternative that requires no additional deps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you're saying, and it's specifically mentioned here: python-poetry/poetry#241 (comment)

test = "scripts:test"
all_checks = "scripts:all_checks"
lint = "scripts:lint"
type_check = "scripts:type_check"
fmt = "scripts:fmt"
docs = "scripts:docs"


[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
118 changes: 118 additions & 0 deletions scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import subprocess
import sys
from typing import List, Optional, Union


def _run_command(command: List[str], exit_after_run: Optional[bool] = True) -> int:
"""Run a CLI command.

Args:
command: the CLI command to run
exit_after_run: exit after the command finished running

Returns:
The command's return code
"""
completed_process = subprocess.run(command, check=False)
if exit_after_run:
sys.exit(completed_process.returncode)

return completed_process.returncode


def _pre_commit(hook_ids: Optional[Union[str, List[str]]] = None) -> None:
"""Run one or more pre-commit hooks.

Args:
hook_ids: one or more hooks to run. It's an optional parameter, `None` means run all hooks

Returns:
None
"""
files = ["--files", *sys.argv[1:]] if len(sys.argv) > 1 else ["--all-files"]

if not hook_ids:
_run_command(["pre-commit", "run", "--color=always", *files])
return

if isinstance(hook_ids, str):
hook_ids = [hook_ids]

overall_return_code = 0
for hook_id in hook_ids:
cur_return_code = _run_command(["pre-commit", "run", "--color=always", hook_id, *files], exit_after_run=False)
overall_return_code = overall_return_code or cur_return_code

sys.exit(overall_return_code)


def test() -> None:
"""Run tests.

Args:
argv: a directory or file to run on

Returns:
None
"""

_run_command(["pytest", *sys.argv[1:]])


def all_checks() -> None:
"""Run all pre-commit checks.

Args:
argv: a directory or files to run on

Returns:
None
"""

_pre_commit()


def lint() -> None:
"""Run pylint from pre-commit.

Args:
argv: a directory or files to run on

Returns:
None
"""

_pre_commit("pylint")


def fmt() -> None:
"""Run black, isort, prettier, blacken-docs and docformatter from pre-
commit.

Args:
argv: a directory or files to run on

Returns:
None
"""

_pre_commit(["black", "isort", "prettier", "blacken-docs", "docformatter"])


def type_check() -> None:
"""Run mypy from pre-commit.

Args:
argv: a directory or files to run on

Returns:
None
"""

_pre_commit("mypy")


def docs() -> None:
"""Build docs."""

_run_command(["mkdocs", "build"])