-
-
Notifications
You must be signed in to change notification settings - Fork 392
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 anypip install
/poetry install
execution (even outside of development). I think we'd only want these in a developer environment.The leaves something like
poethepoet
orpoetry-pyinvoke-plugin
as a potential option. Alternately, aMakefile
could be a solid alternative that requires no additional deps.There was a problem hiding this comment.
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)