Skip to content

Commit

Permalink
Add lock_dependencies pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamtt committed Dec 13, 2023
1 parent ef27006 commit e56d3a0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
81 changes: 81 additions & 0 deletions prosper_shared/autohooks/lock_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import re
import subprocess
import sys
from subprocess import STDOUT
from typing import Iterable, List, Optional, Union

from autohooks.api import error, ok
from autohooks.api.git import stage_files, stash_unstaged_changes
from autohooks.config import Config
from autohooks.precommit.run import ReportProgress
from autohooks.terminal import out

DEFAULT_ARGUMENTS = ()


def _get_lock_dependencies_config(config: Config) -> Config:
return config.get("tool", "autohooks", "plugins", "lock_dependencies")


def _ensure_iterable(value: Union[str, List[str]]) -> List[str]:
if isinstance(value, str):
return [value]
return value


def _get_lock_dependencies_arguments(config: Optional[Config]) -> Iterable[str]:
if not config:
return DEFAULT_ARGUMENTS

lock_dependencies_config = _get_lock_dependencies_config(config)
arguments = _ensure_iterable(
lock_dependencies_config.get_value("arguments", DEFAULT_ARGUMENTS)
)

return arguments


def precommit(
config: Optional[Config] = None,
report_progress: Optional[ReportProgress] = None,
**kwargs, # pylint: disable=unused-argument
) -> int:
"""Runs `poetry lock` to update dependencies.
Args:
config (Optional[Config]): The full config for the project. The config for this tool will be extracted from it
later.
report_progress (Optional[ReportProgress]): If set, the hook will use this to send updates when work is
finished.
**kwargs: Not used.
Returns:
int: Indicates the output status of the underlying process. 0 -> success, >0 -> failure
"""
if report_progress:
report_progress.init(1)

ret = 0
arguments = ["poetry"]
arguments.extend(_get_lock_dependencies_arguments(config))

with stash_unstaged_changes():
try:
args = arguments.copy()
subprocess.check_output(args, stderr=STDOUT)
ok("Running `poetry lock`")
if report_progress:
report_progress.update()
except subprocess.CalledProcessError as e:
ret = e.returncode
error("Running `poetry lock`")
lint_errors: List[str] = e.stdout.decode(
encoding=sys.getdefaultencoding(), errors="replace"
).split("\n")
for line in lint_errors:
if re.match(r"[ ]{4}[0-9]+:", line):
out(line)

stage_files(["poetry.lock"])

return ret
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ build-backend = "poetry.core.masonry.api"
mode = "poetry"
pre-commit = [
"prosper_shared.autohooks.clean",
"prosper_shared.autohooks.lock_dependencies",
"prosper_shared.autohooks.autoimport",
"autohooks.plugins.black",
"autohooks.plugins.ruff",
Expand Down

0 comments on commit e56d3a0

Please sign in to comment.