dycw-pre-commit-hooks 0.9.26__py3-none-any.whl → 0.10.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of dycw-pre-commit-hooks might be problematic. Click here for more details.
- {dycw_pre_commit_hooks-0.9.26.dist-info → dycw_pre_commit_hooks-0.10.5.dist-info}/METADATA +2 -6
- dycw_pre_commit_hooks-0.10.5.dist-info/RECORD +4 -0
- {dycw_pre_commit_hooks-0.9.26.dist-info → dycw_pre_commit_hooks-0.10.5.dist-info}/entry_points.txt +1 -0
- dycw_pre_commit_hooks-0.9.26.dist-info/RECORD +0 -17
- pre_commit_hooks/__init__.py +0 -3
- pre_commit_hooks/common.py +0 -84
- pre_commit_hooks/py.typed +0 -0
- pre_commit_hooks/run_bump2version/__init__.py +0 -49
- pre_commit_hooks/run_bump2version/__main__.py +0 -6
- pre_commit_hooks/run_dockfmt/__init__.py +0 -45
- pre_commit_hooks/run_dockfmt/__main__.py +0 -6
- pre_commit_hooks/run_hatch_version/__init__.py +0 -58
- pre_commit_hooks/run_hatch_version/__main__.py +0 -6
- pre_commit_hooks/run_ruff_format/__init__.py +0 -76
- pre_commit_hooks/run_ruff_format/__main__.py +0 -6
- pre_commit_hooks/run_uv_pip_compile/__init__.py +0 -121
- pre_commit_hooks/run_uv_pip_compile/__main__.py +0 -6
- {dycw_pre_commit_hooks-0.9.26.dist-info → dycw_pre_commit_hooks-0.10.5.dist-info}/WHEEL +0 -0
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dycw-pre-commit-hooks
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.10.5
|
|
4
4
|
Author-email: Derek Wan <d.wan@icloud.com>
|
|
5
5
|
Requires-Python: >=3.11
|
|
6
6
|
Requires-Dist: click<8.2,>=8.1.8
|
|
7
|
-
Requires-Dist: dycw-utilities<0.
|
|
7
|
+
Requires-Dist: dycw-utilities<0.96,>=0.95.21
|
|
8
8
|
Requires-Dist: loguru<0.8,>=0.7.3
|
|
9
9
|
Requires-Dist: semver<3.1,>=3.0.4
|
|
10
10
|
Requires-Dist: tomlkit<0.14,>=0.13.2
|
|
11
11
|
Requires-Dist: xdg-base-dirs<6.1,>=6.0.2
|
|
12
|
-
Provides-Extra: dev
|
|
13
|
-
Requires-Dist: dycw-utilities[test]; extra == 'dev'
|
|
14
|
-
Requires-Dist: hatch<1.15,>=1.14.0; extra == 'dev'
|
|
15
|
-
Requires-Dist: setuptools<75.9,>=75.8.1; extra == 'dev'
|
|
16
12
|
Description-Content-Type: text/markdown
|
|
17
13
|
|
|
18
14
|
# pre-commit-hooks
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
dycw_pre_commit_hooks-0.10.5.dist-info/METADATA,sha256=9A5gGYMWTCMFzgvJLeI7m60fDwUJZ1Z1pOv5VtqtlpI,1012
|
|
2
|
+
dycw_pre_commit_hooks-0.10.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
3
|
+
dycw_pre_commit_hooks-0.10.5.dist-info/entry_points.txt,sha256=MxQ24qIL7DfnorSw_zyRpivSVT1uZMELf-0GUjVHau4,366
|
|
4
|
+
dycw_pre_commit_hooks-0.10.5.dist-info/RECORD,,
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
pre_commit_hooks/__init__.py,sha256=68T9JoCN9uQzWrP8Ll6vLywDeELNVauiOV-1k1JLvg4,59
|
|
2
|
-
pre_commit_hooks/common.py,sha256=H8RJkUXKQ7vHbCsi6oDq7XAdggjOij5R1xGoQ1UXhKA,2512
|
|
3
|
-
pre_commit_hooks/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
-
pre_commit_hooks/run_bump2version/__init__.py,sha256=nhJ2kdUjSteGVjatTfMSeQb92zIlfiwyeHll_7_nzvM,1581
|
|
5
|
-
pre_commit_hooks/run_bump2version/__main__.py,sha256=BZy8mDElAK3-X2iKvqg1XnGbCiYXcLoMDefaPanWx4g,153
|
|
6
|
-
pre_commit_hooks/run_dockfmt/__init__.py,sha256=REJG8A77H1VbPvIhStlsIWEqqYeaK4h7q17kbLTK3TE,1070
|
|
7
|
-
pre_commit_hooks/run_dockfmt/__main__.py,sha256=aE0DIrzWy4mnQ04_n5U6znbmJwJBkjZUXv9Th3OqQ7Y,148
|
|
8
|
-
pre_commit_hooks/run_hatch_version/__init__.py,sha256=bADWwm3tTgUrDMynBag2Z_hVknlbfOsoYl7ghxTc_hY,1714
|
|
9
|
-
pre_commit_hooks/run_hatch_version/__main__.py,sha256=XeMLmgm1HMujkYhaF9LcMygBrwa2KqIGMzznY106gd8,154
|
|
10
|
-
pre_commit_hooks/run_ruff_format/__init__.py,sha256=Lr_9M0WHhJl6WARAf8Lr830PG9bMbkDXvNSmEkrSxK8,1993
|
|
11
|
-
pre_commit_hooks/run_ruff_format/__main__.py,sha256=faesqqpMaesg5r-LvkkQt1W9kahvNr-60K3SMYv1NgY,152
|
|
12
|
-
pre_commit_hooks/run_uv_pip_compile/__init__.py,sha256=bFzO49-Tw0rYJ1Eby25gX4-wChXOh2QbB5YiQUl5WNE,3629
|
|
13
|
-
pre_commit_hooks/run_uv_pip_compile/__main__.py,sha256=LLlM3xVN7l5cxZ0yD_8He8n_GEmQqn_d-2wl6BQtqoA,155
|
|
14
|
-
dycw_pre_commit_hooks-0.9.26.dist-info/METADATA,sha256=qx4GePYf_QjIUgNzDJmMV6Wvs0m07yTWwyGf1p6xbmQ,1191
|
|
15
|
-
dycw_pre_commit_hooks-0.9.26.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
16
|
-
dycw_pre_commit_hooks-0.9.26.dist-info/entry_points.txt,sha256=E5ZGYo3x6IF71IHDEhXJYfTHRRIrjzzuxsiYgVkCDQE,302
|
|
17
|
-
dycw_pre_commit_hooks-0.9.26.dist-info/RECORD,,
|
pre_commit_hooks/__init__.py
DELETED
pre_commit_hooks/common.py
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from dataclasses import dataclass
|
|
4
|
-
from hashlib import md5
|
|
5
|
-
from pathlib import Path
|
|
6
|
-
from re import MULTILINE, findall
|
|
7
|
-
from subprocess import check_output
|
|
8
|
-
from typing import Literal
|
|
9
|
-
|
|
10
|
-
from loguru import logger
|
|
11
|
-
from semver import VersionInfo
|
|
12
|
-
from tomlkit import TOMLDocument, parse
|
|
13
|
-
from utilities.git import get_repo_root
|
|
14
|
-
from xdg_base_dirs import xdg_cache_home
|
|
15
|
-
|
|
16
|
-
_ROOT = get_repo_root()
|
|
17
|
-
PYPROJECT_TOML = _ROOT.joinpath("pyproject.toml")
|
|
18
|
-
REQUIREMENTS_TXT = _ROOT.joinpath("requirements.txt")
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def check_versions(
|
|
22
|
-
path: Path,
|
|
23
|
-
pattern: str,
|
|
24
|
-
/,
|
|
25
|
-
*,
|
|
26
|
-
name: Literal["run-bump2version", "run-hatch-version"],
|
|
27
|
-
) -> VersionInfo | None:
|
|
28
|
-
"""Check the versions: current & master.
|
|
29
|
-
|
|
30
|
-
If the current is a correct bumping of master, then return `None`. Else,
|
|
31
|
-
return the patch-bumped master.
|
|
32
|
-
"""
|
|
33
|
-
with path.open() as fh:
|
|
34
|
-
current = _parse_version(pattern, fh.read())
|
|
35
|
-
master = _get_master_version(path, pattern, name=name)
|
|
36
|
-
patched = master.bump_patch()
|
|
37
|
-
if current in {master.bump_major(), master.bump_minor(), patched}:
|
|
38
|
-
return None
|
|
39
|
-
return patched
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def _parse_version(pattern: str, text: str, /) -> VersionInfo:
|
|
43
|
-
"""Parse the version from a block of text."""
|
|
44
|
-
(match,) = findall(pattern, text, flags=MULTILINE)
|
|
45
|
-
return VersionInfo.parse(match)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
def _get_master_version(
|
|
49
|
-
path: Path,
|
|
50
|
-
pattern: str,
|
|
51
|
-
/,
|
|
52
|
-
*,
|
|
53
|
-
name: Literal["run-bump2version", "run-hatch-version"],
|
|
54
|
-
) -> VersionInfo:
|
|
55
|
-
repo = md5(Path.cwd().as_posix().encode(), usedforsecurity=False).hexdigest()
|
|
56
|
-
commit = check_output(["git", "rev-parse", "origin/master"], text=True).rstrip("\n")
|
|
57
|
-
cache = xdg_cache_home().joinpath("pre-commit-hooks", name, repo, commit)
|
|
58
|
-
try:
|
|
59
|
-
with cache.open() as fh:
|
|
60
|
-
return VersionInfo.parse(fh.read())
|
|
61
|
-
except FileNotFoundError:
|
|
62
|
-
cache.parent.mkdir(parents=True, exist_ok=True)
|
|
63
|
-
text = check_output(["git", "show", f"{commit}:{path}"], text=True)
|
|
64
|
-
version = _parse_version(pattern, text)
|
|
65
|
-
with cache.open(mode="w") as fh:
|
|
66
|
-
_ = fh.write(str(version))
|
|
67
|
-
return version
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
@dataclass
|
|
71
|
-
class PyProject:
|
|
72
|
-
contents: str
|
|
73
|
-
doc: TOMLDocument
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
def read_pyproject() -> PyProject:
|
|
77
|
-
try:
|
|
78
|
-
with PYPROJECT_TOML.open(mode="r") as fh:
|
|
79
|
-
contents = fh.read()
|
|
80
|
-
except FileNotFoundError:
|
|
81
|
-
logger.exception("pyproject.toml not found")
|
|
82
|
-
raise
|
|
83
|
-
doc = parse(contents)
|
|
84
|
-
return PyProject(contents=contents, doc=doc)
|
pre_commit_hooks/py.typed
DELETED
|
File without changes
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from pathlib import Path
|
|
4
|
-
from subprocess import PIPE, STDOUT, CalledProcessError, check_call
|
|
5
|
-
from typing import Literal
|
|
6
|
-
|
|
7
|
-
from click import command, option
|
|
8
|
-
from loguru import logger
|
|
9
|
-
|
|
10
|
-
from pre_commit_hooks.common import check_versions
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
@command()
|
|
14
|
-
@option(
|
|
15
|
-
"--setup-cfg", is_flag=True, help="Read `setup.cfg` instead of `bumpversion.cfg`"
|
|
16
|
-
)
|
|
17
|
-
def main(*, setup_cfg: bool) -> bool:
|
|
18
|
-
"""CLI for the `run_bump2version` hook."""
|
|
19
|
-
filename = "setup.cfg" if setup_cfg else ".bumpversion.cfg"
|
|
20
|
-
return _process(filename=filename)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def _process(*, filename: Literal["setup.cfg", ".bumpversion.cfg"]) -> bool:
|
|
24
|
-
path = Path(filename)
|
|
25
|
-
pattern = r"current_version = (\d+\.\d+\.\d+)$"
|
|
26
|
-
version = check_versions(path, pattern, name="run-bump2version")
|
|
27
|
-
if version is None:
|
|
28
|
-
return True
|
|
29
|
-
cmd = ["bump2version", "--allow-dirty", f"--new-version={version}", "patch"]
|
|
30
|
-
try:
|
|
31
|
-
_ = check_call(cmd, stdout=PIPE, stderr=STDOUT)
|
|
32
|
-
except CalledProcessError as error:
|
|
33
|
-
if error.returncode != 1:
|
|
34
|
-
logger.exception("Failed to run {cmd!r}", cmd=" ".join(cmd))
|
|
35
|
-
except FileNotFoundError:
|
|
36
|
-
logger.exception(
|
|
37
|
-
"Failed to run {cmd!r}. Is `bump2version` installed?", cmd=" ".join(cmd)
|
|
38
|
-
)
|
|
39
|
-
else:
|
|
40
|
-
_trim_trailing_whitespaces(path)
|
|
41
|
-
return True
|
|
42
|
-
return False
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
def _trim_trailing_whitespaces(path: Path, /) -> None:
|
|
46
|
-
with path.open() as fh:
|
|
47
|
-
lines = fh.readlines()
|
|
48
|
-
with path.open(mode="w") as fh:
|
|
49
|
-
fh.writelines([line.rstrip(" ") for line in lines])
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from pathlib import Path
|
|
4
|
-
from subprocess import check_output
|
|
5
|
-
from typing import TYPE_CHECKING
|
|
6
|
-
|
|
7
|
-
import click
|
|
8
|
-
from click import argument, command
|
|
9
|
-
|
|
10
|
-
if TYPE_CHECKING:
|
|
11
|
-
from collections.abc import Iterator
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
@command()
|
|
15
|
-
@argument(
|
|
16
|
-
"paths",
|
|
17
|
-
nargs=-1,
|
|
18
|
-
type=click.Path(
|
|
19
|
-
exists=True, file_okay=True, dir_okay=False, readable=True, path_type=Path
|
|
20
|
-
),
|
|
21
|
-
)
|
|
22
|
-
def main(paths: tuple[Path, ...], /) -> bool:
|
|
23
|
-
"""CLI for the `run-dockfmt` hook."""
|
|
24
|
-
results = list(_yield_outcomes(*paths)) # run all
|
|
25
|
-
return all(results)
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
def _yield_outcomes(*paths: Path) -> Iterator[bool]:
|
|
29
|
-
for path in paths:
|
|
30
|
-
if path.name == "Dockerfile":
|
|
31
|
-
yield _process(path)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
def _process(path: Path, /) -> bool:
|
|
35
|
-
with path.open() as fh:
|
|
36
|
-
current = fh.read()
|
|
37
|
-
strip = "\t\n"
|
|
38
|
-
proposed = check_output(["dockfmt", "fmt", path.as_posix()], text=True).lstrip(
|
|
39
|
-
strip
|
|
40
|
-
)
|
|
41
|
-
if current == proposed:
|
|
42
|
-
return True
|
|
43
|
-
with path.open(mode="w") as fh:
|
|
44
|
-
_ = fh.write(proposed)
|
|
45
|
-
return False
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from pathlib import Path
|
|
4
|
-
from subprocess import PIPE, STDOUT, CalledProcessError, check_call
|
|
5
|
-
from typing import Any, cast
|
|
6
|
-
|
|
7
|
-
from click import command
|
|
8
|
-
from loguru import logger
|
|
9
|
-
from tomlkit.container import Container
|
|
10
|
-
|
|
11
|
-
from pre_commit_hooks.common import check_versions, read_pyproject
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
@command()
|
|
15
|
-
def main() -> bool:
|
|
16
|
-
"""CLI for the `run-hatch-version` hook."""
|
|
17
|
-
return _process()
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def _process() -> bool:
|
|
21
|
-
path = _get_path_version_file()
|
|
22
|
-
pattern = r'^__version__ = "(\d+\.\d+\.\d+)"$'
|
|
23
|
-
version = check_versions(path, pattern, name="run-hatch-version")
|
|
24
|
-
if version is None:
|
|
25
|
-
return True
|
|
26
|
-
cmd = ["hatch", "version", str(version)]
|
|
27
|
-
try:
|
|
28
|
-
_ = check_call(cmd, stdout=PIPE, stderr=STDOUT)
|
|
29
|
-
except CalledProcessError as error:
|
|
30
|
-
if error.returncode != 1:
|
|
31
|
-
logger.exception("Failed to run {cmd!r}", cmd=" ".join(cmd))
|
|
32
|
-
except FileNotFoundError:
|
|
33
|
-
logger.exception(
|
|
34
|
-
"Failed to run {cmd!r}. Is `hatch` installed?", cmd=" ".join(cmd)
|
|
35
|
-
)
|
|
36
|
-
else:
|
|
37
|
-
return True
|
|
38
|
-
return False
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
def _get_path_version_file() -> Path:
|
|
42
|
-
pyproject = read_pyproject()
|
|
43
|
-
try:
|
|
44
|
-
tool = cast(Container, pyproject.doc["tool"])
|
|
45
|
-
except KeyError:
|
|
46
|
-
logger.exception('pyproject.toml has no "tool" section')
|
|
47
|
-
raise
|
|
48
|
-
try:
|
|
49
|
-
hatch = cast(Container, tool["hatch"])
|
|
50
|
-
except KeyError:
|
|
51
|
-
logger.exception('pyproject.toml has no "tool.hatch" section')
|
|
52
|
-
raise
|
|
53
|
-
try:
|
|
54
|
-
version = cast(Container, hatch["version"])
|
|
55
|
-
except KeyError:
|
|
56
|
-
logger.exception('pyproject.toml has no "tool.hatch.version" section')
|
|
57
|
-
raise
|
|
58
|
-
return Path(cast(Any, version["path"]))
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from subprocess import CalledProcessError, check_call
|
|
4
|
-
from typing import cast
|
|
5
|
-
|
|
6
|
-
from click import command
|
|
7
|
-
from loguru import logger
|
|
8
|
-
from tomlkit import dumps, table
|
|
9
|
-
from tomlkit.container import Container
|
|
10
|
-
|
|
11
|
-
from pre_commit_hooks.common import PYPROJECT_TOML, PyProject, read_pyproject
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
@command()
|
|
15
|
-
def main() -> bool:
|
|
16
|
-
"""CLI for the `run-ruff-format` hook."""
|
|
17
|
-
return _process()
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def _process() -> bool:
|
|
21
|
-
curr = read_pyproject()
|
|
22
|
-
new = _get_modified_pyproject()
|
|
23
|
-
result1 = _run_ruff_format(new)
|
|
24
|
-
result2 = _run_ruff_format(curr)
|
|
25
|
-
_write_pyproject(curr)
|
|
26
|
-
return result1 and result2
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def _get_modified_pyproject() -> PyProject:
|
|
30
|
-
pyproject = read_pyproject()
|
|
31
|
-
doc = pyproject.doc
|
|
32
|
-
try:
|
|
33
|
-
tool = cast(Container, doc["tool"])
|
|
34
|
-
except KeyError:
|
|
35
|
-
tool = table()
|
|
36
|
-
try:
|
|
37
|
-
ruff = cast(Container, tool["ruff"])
|
|
38
|
-
except KeyError:
|
|
39
|
-
ruff = table()
|
|
40
|
-
ruff["line-length"] = 320
|
|
41
|
-
try:
|
|
42
|
-
format_ = cast(Container, ruff["format"])
|
|
43
|
-
except KeyError:
|
|
44
|
-
format_ = table()
|
|
45
|
-
format_["skip-magic-trailing-comma"] = True
|
|
46
|
-
try:
|
|
47
|
-
lint = cast(Container, ruff["lint"])
|
|
48
|
-
except KeyError:
|
|
49
|
-
lint = table()
|
|
50
|
-
try:
|
|
51
|
-
isort = cast(Container, lint["isort"])
|
|
52
|
-
except KeyError:
|
|
53
|
-
isort = table()
|
|
54
|
-
isort["split-on-trailing-comma"] = False
|
|
55
|
-
doc["tool"] = tool
|
|
56
|
-
tool["ruff"] = ruff
|
|
57
|
-
ruff["format"] = format_
|
|
58
|
-
ruff["lint"] = lint
|
|
59
|
-
lint["isort"] = isort
|
|
60
|
-
return PyProject(contents=dumps(doc), doc=doc)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
def _run_ruff_format(pyproject: PyProject, /) -> bool:
|
|
64
|
-
_write_pyproject(pyproject)
|
|
65
|
-
cmd = ["ruff", "format", "."]
|
|
66
|
-
try:
|
|
67
|
-
code = check_call(cmd)
|
|
68
|
-
except CalledProcessError:
|
|
69
|
-
logger.exception("Failed to run {cmd!r}", cmd=" ".join(cmd))
|
|
70
|
-
return False
|
|
71
|
-
return code == 0
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
def _write_pyproject(pyproject: PyProject, /) -> None:
|
|
75
|
-
with PYPROJECT_TOML.open(mode="w") as fh:
|
|
76
|
-
_ = fh.write(pyproject.contents)
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from pathlib import Path
|
|
4
|
-
from re import sub
|
|
5
|
-
from subprocess import CalledProcessError, check_call
|
|
6
|
-
from tempfile import TemporaryDirectory
|
|
7
|
-
from typing import TYPE_CHECKING, Literal
|
|
8
|
-
|
|
9
|
-
from click import Choice, command, option
|
|
10
|
-
from loguru import logger
|
|
11
|
-
|
|
12
|
-
from pre_commit_hooks.common import REQUIREMENTS_TXT
|
|
13
|
-
|
|
14
|
-
if TYPE_CHECKING:
|
|
15
|
-
from collections.abc import Iterable
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
@command()
|
|
19
|
-
@option(
|
|
20
|
-
"--output-file",
|
|
21
|
-
default=REQUIREMENTS_TXT,
|
|
22
|
-
help="Write the compiled requirements to the given `requirements.txt` file",
|
|
23
|
-
)
|
|
24
|
-
@option("--extra", multiple=True, help="Optional dependencies")
|
|
25
|
-
@option(
|
|
26
|
-
"--python-platform",
|
|
27
|
-
type=Choice(["windows", "linux", "macos"], case_sensitive=False),
|
|
28
|
-
help="The platform for which requirements should be resolved",
|
|
29
|
-
)
|
|
30
|
-
@option(
|
|
31
|
-
"--python-version", default=None, help="The Python version to use for resolution"
|
|
32
|
-
)
|
|
33
|
-
def main(
|
|
34
|
-
*,
|
|
35
|
-
output_file: Path | str = REQUIREMENTS_TXT,
|
|
36
|
-
extra: tuple[str, ...] = (),
|
|
37
|
-
python_platform: Literal["windows", "linux", "macos"] | None = None,
|
|
38
|
-
python_version: str | None = None,
|
|
39
|
-
) -> bool:
|
|
40
|
-
"""CLI for the `run-uv-pip-compile` hook."""
|
|
41
|
-
return _process(
|
|
42
|
-
output_file=Path(output_file),
|
|
43
|
-
extra=None if len(extra) == 0 else list(extra),
|
|
44
|
-
python_platform=python_platform,
|
|
45
|
-
python_version=python_version,
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
def _process(
|
|
50
|
-
*,
|
|
51
|
-
extra: Iterable[str] | None = None,
|
|
52
|
-
output_file: Path = REQUIREMENTS_TXT,
|
|
53
|
-
python_platform: Literal["windows", "linux", "macos"] | None = None,
|
|
54
|
-
python_version: str | None = None,
|
|
55
|
-
) -> bool:
|
|
56
|
-
curr = _read_requirements_txt(output_file)
|
|
57
|
-
latest = _run_uv_pip_compile(
|
|
58
|
-
extra=extra, python_platform=python_platform, python_version=python_version
|
|
59
|
-
)
|
|
60
|
-
if curr == latest:
|
|
61
|
-
return True
|
|
62
|
-
_write_requirements_txt(latest, path=output_file)
|
|
63
|
-
return False
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
def _read_requirements_txt(path: Path, /) -> str | None:
|
|
67
|
-
try:
|
|
68
|
-
with path.open() as fh:
|
|
69
|
-
return fh.read()
|
|
70
|
-
except FileNotFoundError:
|
|
71
|
-
return None
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
def _run_uv_pip_compile(
|
|
75
|
-
*,
|
|
76
|
-
extra: Iterable[str] | None = None,
|
|
77
|
-
python_platform: Literal["windows", "linux", "macos"] | None = None,
|
|
78
|
-
python_version: str | None = None,
|
|
79
|
-
) -> str:
|
|
80
|
-
cmd: list[str] = [
|
|
81
|
-
"uv",
|
|
82
|
-
"pip",
|
|
83
|
-
"compile",
|
|
84
|
-
"--extra=dev",
|
|
85
|
-
"--prerelease=explicit",
|
|
86
|
-
"--quiet",
|
|
87
|
-
"--upgrade",
|
|
88
|
-
]
|
|
89
|
-
if extra is not None:
|
|
90
|
-
cmd.extend(f"--extra={e}" for e in extra)
|
|
91
|
-
if python_platform is not None:
|
|
92
|
-
cmd.append(f"--python-platform={python_platform}")
|
|
93
|
-
if python_version is not None:
|
|
94
|
-
cmd.append(f"--python-version={python_version}")
|
|
95
|
-
with TemporaryDirectory() as temp:
|
|
96
|
-
temp_file = Path(temp, "temp.txt")
|
|
97
|
-
cmd.extend([
|
|
98
|
-
f"--output-file={temp_file.as_posix()}",
|
|
99
|
-
"pyproject.toml", # don't use absolute path
|
|
100
|
-
])
|
|
101
|
-
try:
|
|
102
|
-
_ = check_call(cmd)
|
|
103
|
-
except CalledProcessError:
|
|
104
|
-
logger.exception("Failed to run {cmd!r}", cmd=" ".join(cmd))
|
|
105
|
-
raise
|
|
106
|
-
with temp_file.open(mode="r") as fh:
|
|
107
|
-
contents = fh.read()
|
|
108
|
-
return _fix_header(contents, temp_file) + "\n"
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
def _fix_header(text: str, temp_file: Path, /) -> str:
|
|
112
|
-
return "\n".join(_fix_header_line(line, temp_file) for line in text.splitlines())
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
def _fix_header_line(line: str, temp_file: Path, /) -> str:
|
|
116
|
-
return sub(str(temp_file), temp_file.name, line)
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
def _write_requirements_txt(contents: str, /, *, path: Path = REQUIREMENTS_TXT) -> None:
|
|
120
|
-
with path.open(mode="w") as fh:
|
|
121
|
-
_ = fh.write(contents)
|
|
File without changes
|