dycw-pre-commit-hooks 0.10.20__py3-none-any.whl → 0.10.21__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.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dycw-pre-commit-hooks
3
- Version: 0.10.20
3
+ Version: 0.10.21
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
@@ -0,0 +1,13 @@
1
+ pre_commit_hooks/__init__.py,sha256=Kl1CGvFCtMKhwmNrSv6UxFVZCcuERQS4LE1u87kW7ZQ,60
2
+ pre_commit_hooks/common.py,sha256=v2pUvEMLMfAXg2nNJOH9LYHbFWkdOssLFwnDXsIEkqk,2703
3
+ pre_commit_hooks/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ pre_commit_hooks/run_bump2version/__init__.py,sha256=1Qgt7njFfTlH39ZY4TucZREUJgqtGMe37U5mLkFmupc,1433
5
+ pre_commit_hooks/run_bump2version/__main__.py,sha256=BZy8mDElAK3-X2iKvqg1XnGbCiYXcLoMDefaPanWx4g,153
6
+ pre_commit_hooks/run_bump_my_version/__init__.py,sha256=cl6KTxPaKPmHuebddgfs_OqPK_oYI3R15AbLo8kcFRY,1837
7
+ pre_commit_hooks/run_bump_my_version/__main__.py,sha256=w2V3y61jrSau-zxjl8ciHtWPlJQwXbYxNJ2tGYVyI4s,156
8
+ pre_commit_hooks/run_ruff_format/__init__.py,sha256=Lr_9M0WHhJl6WARAf8Lr830PG9bMbkDXvNSmEkrSxK8,1993
9
+ pre_commit_hooks/run_ruff_format/__main__.py,sha256=faesqqpMaesg5r-LvkkQt1W9kahvNr-60K3SMYv1NgY,152
10
+ dycw_pre_commit_hooks-0.10.21.dist-info/METADATA,sha256=d07FIgkDzxrNd9xYm63JCiCC0W2ovk9wNhChlh6ZM3Q,884
11
+ dycw_pre_commit_hooks-0.10.21.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
12
+ dycw_pre_commit_hooks-0.10.21.dist-info/entry_points.txt,sha256=URODtkWLHrg6PagXnVbubNWnEUjExjCM_lCPf2LEllo,196
13
+ dycw_pre_commit_hooks-0.10.21.dist-info/RECORD,,
@@ -0,0 +1,3 @@
1
+ from __future__ import annotations
2
+
3
+ __version__ = "0.10.21"
@@ -0,0 +1,96 @@
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
+
19
+
20
+ ##
21
+
22
+
23
+ _VERSION_BUMP_SCRIPTS = Literal[
24
+ "run-bump-my-version", "run-bump2version", "run-hatch-version"
25
+ ]
26
+
27
+
28
+ def check_versions(
29
+ path: Path, pattern: str, name: _VERSION_BUMP_SCRIPTS, /
30
+ ) -> VersionInfo | None:
31
+ """Check the versions: current & master.
32
+
33
+ If the current is a correct bumping of master, then return `None`. Else,
34
+ return the patch-bumped master.
35
+ """
36
+ with path.open() as fh:
37
+ current = _parse_version(pattern, fh.read())
38
+ master = _get_master_version(name, path, pattern)
39
+ patched = master.bump_patch()
40
+ if current in {master.bump_major(), master.bump_minor(), patched}:
41
+ return None
42
+ return patched
43
+
44
+
45
+ def _parse_version(pattern: str, text: str, /) -> VersionInfo:
46
+ """Parse the version from a block of text."""
47
+ (match,) = findall(pattern, text, flags=MULTILINE)
48
+ return VersionInfo.parse(match)
49
+
50
+
51
+ def _get_master_version(
52
+ name: _VERSION_BUMP_SCRIPTS, path: Path, pattern: str, /
53
+ ) -> VersionInfo:
54
+ repo = md5(Path.cwd().as_posix().encode(), usedforsecurity=False).hexdigest()
55
+ commit = check_output(["git", "rev-parse", "origin/master"], text=True).rstrip("\n")
56
+ cache = xdg_cache_home().joinpath("pre-commit-hooks", name, repo, commit)
57
+ try:
58
+ with cache.open() as fh:
59
+ return VersionInfo.parse(fh.read())
60
+ except FileNotFoundError:
61
+ cache.parent.mkdir(parents=True, exist_ok=True)
62
+ text = check_output(["git", "show", f"{commit}:{path}"], text=True)
63
+ version = _parse_version(pattern, text)
64
+ with cache.open(mode="w") as fh:
65
+ _ = fh.write(str(version))
66
+ return version
67
+
68
+
69
+ ##
70
+
71
+
72
+ @dataclass(kw_only=True)
73
+ class PyProject:
74
+ contents: str
75
+ doc: TOMLDocument
76
+
77
+
78
+ def read_pyproject() -> PyProject:
79
+ try:
80
+ with PYPROJECT_TOML.open(mode="r") as fh:
81
+ contents = fh.read()
82
+ except FileNotFoundError:
83
+ logger.exception("pyproject.toml not found")
84
+ raise
85
+ doc = parse(contents)
86
+ return PyProject(contents=contents, doc=doc)
87
+
88
+
89
+ ##
90
+
91
+
92
+ def trim_trailing_whitespaces(path: Path, /) -> None:
93
+ with path.open() as fh:
94
+ lines = fh.readlines()
95
+ with path.open(mode="w") as fh:
96
+ fh.writelines([line.rstrip(" ") for line in lines])
File without changes
@@ -0,0 +1,46 @@
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 (
11
+ PYPROJECT_TOML,
12
+ check_versions,
13
+ trim_trailing_whitespaces,
14
+ )
15
+
16
+
17
+ @command()
18
+ @option(
19
+ "--setup-cfg", is_flag=True, help="Read `setup.cfg` instead of `bumpversion.cfg`"
20
+ )
21
+ def main(*, setup_cfg: bool) -> bool:
22
+ """CLI for the `run_bump2version` hook."""
23
+ filename = "setup.cfg" if setup_cfg else ".bumpversion.cfg"
24
+ return _process(filename=filename)
25
+
26
+
27
+ def _process(*, filename: Literal["setup.cfg", ".bumpversion.cfg"]) -> bool:
28
+ path = Path(filename)
29
+ pattern = r"current_version = (\d+\.\d+\.\d+)$"
30
+ version = check_versions(PYPROJECT_TOML, pattern, "run-bump2version")
31
+ if version is None:
32
+ return True
33
+ cmd = ["bump2version", "--allow-dirty", f"--new-version={version}", "patch"]
34
+ try:
35
+ _ = check_call(cmd, stdout=PIPE, stderr=STDOUT)
36
+ except CalledProcessError as error:
37
+ if error.returncode != 1:
38
+ logger.exception("Failed to run {cmd!r}", cmd=" ".join(cmd))
39
+ except FileNotFoundError:
40
+ logger.exception(
41
+ "Failed to run {cmd!r}. Is `bump2version` installed?", cmd=" ".join(cmd)
42
+ )
43
+ else:
44
+ trim_trailing_whitespaces(path)
45
+ return True
46
+ return False
@@ -0,0 +1,6 @@
1
+ from __future__ import annotations
2
+
3
+ from pre_commit_hooks.run_bump2version import main
4
+
5
+ if __name__ == "__main__":
6
+ raise SystemExit(int(not main()))
@@ -0,0 +1,55 @@
1
+ from __future__ import annotations
2
+
3
+ import re
4
+ from pathlib import Path
5
+ from re import MULTILINE
6
+ from subprocess import PIPE, STDOUT, CalledProcessError, check_call, check_output
7
+
8
+ from click import command
9
+ from loguru import logger
10
+ from utilities.version import Version, parse_version
11
+
12
+ from pre_commit_hooks.common import PYPROJECT_TOML
13
+
14
+
15
+ @command()
16
+ def main() -> bool:
17
+ """CLI for the `run_bump_my_version` hook."""
18
+ return _process()
19
+
20
+
21
+ def _process() -> bool:
22
+ path = PYPROJECT_TOML.relative_to(Path.cwd())
23
+ current = _parse_version_from_file_or_text(path)
24
+ commit = check_output(["git", "rev-parse", "origin/master"], text=True).rstrip("\n")
25
+ contents = check_output(["git", "show", f"{commit}:{path}"], text=True)
26
+ master = _parse_version_from_file_or_text(contents)
27
+ if current in {master.bump_patch(), master.bump_minor(), master.bump_major()}:
28
+ return True
29
+ cmd = ["bump-my-version", "bump", "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 `bump-my-version` installed?", cmd=" ".join(cmd)
38
+ )
39
+ else:
40
+ return True
41
+ return False
42
+
43
+
44
+ _PATTERN = re.compile(r'^current_version = "(\d+\.\d+\.\d+)"$', flags=MULTILINE)
45
+
46
+
47
+ def _parse_version_from_file_or_text(path_or_text: Path | str, /) -> Version:
48
+ """Parse the version from a block of text."""
49
+ match path_or_text:
50
+ case Path() as path:
51
+ with path.open() as fh:
52
+ return _parse_version_from_file_or_text(fh.read())
53
+ case str() as text:
54
+ (match,) = _PATTERN.findall(text)
55
+ return parse_version(match)
@@ -0,0 +1,6 @@
1
+ from __future__ import annotations
2
+
3
+ from pre_commit_hooks.run_bump_my_version import main
4
+
5
+ if __name__ == "__main__":
6
+ raise SystemExit(int(not main()))
@@ -0,0 +1,76 @@
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)
@@ -0,0 +1,6 @@
1
+ from __future__ import annotations
2
+
3
+ from pre_commit_hooks.run_ruff_format import main
4
+
5
+ if __name__ == "__main__":
6
+ raise SystemExit(int(not main()))
@@ -1,4 +0,0 @@
1
- dycw_pre_commit_hooks-0.10.20.dist-info/METADATA,sha256=3HMd82S3AQVIGOWt0mZu-ArSgItNBRPDEh081Phkank,884
2
- dycw_pre_commit_hooks-0.10.20.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
3
- dycw_pre_commit_hooks-0.10.20.dist-info/entry_points.txt,sha256=URODtkWLHrg6PagXnVbubNWnEUjExjCM_lCPf2LEllo,196
4
- dycw_pre_commit_hooks-0.10.20.dist-info/RECORD,,