dycw-pre-commit-hooks 0.11.2__tar.gz → 0.12.0__tar.gz

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.

Files changed (23) hide show
  1. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/PKG-INFO +5 -3
  2. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/README.md +2 -1
  3. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/pyproject.toml +11 -9
  4. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/pre_commit_hooks/__init__.py +1 -1
  5. dycw_pre_commit_hooks-0.12.0/src/pre_commit_hooks/common.py +66 -0
  6. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/pre_commit_hooks/format_requirements/__init__.py +3 -3
  7. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/pre_commit_hooks/replace_sequence_str/__init__.py +3 -3
  8. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/pre_commit_hooks/run_bump_my_version/__init__.py +3 -20
  9. dycw_pre_commit_hooks-0.12.0/src/pre_commit_hooks/tag_commits/__init__.py +70 -0
  10. {dycw_pre_commit_hooks-0.11.2/src/pre_commit_hooks/run_ruff_format → dycw_pre_commit_hooks-0.12.0/src/pre_commit_hooks/tag_commits}/__main__.py +1 -1
  11. dycw_pre_commit_hooks-0.11.2/src/pre_commit_hooks/common.py +0 -33
  12. dycw_pre_commit_hooks-0.11.2/src/pre_commit_hooks/run_ruff_format/__init__.py +0 -81
  13. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/.gitignore +0 -0
  14. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/pre_commit_hooks/format_requirements/__main__.py +0 -0
  15. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/pre_commit_hooks/py.typed +0 -0
  16. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/pre_commit_hooks/replace_sequence_str/__main__.py +0 -0
  17. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/pre_commit_hooks/run_bump_my_version/__main__.py +0 -0
  18. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/tests/__init__.py +0 -0
  19. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/tests/format_requirements/__init__.py +0 -0
  20. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/tests/format_requirements/in.toml +0 -0
  21. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/tests/format_requirements/out.toml +0 -0
  22. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/tests/format_requirements/test_format_requirements.py +0 -0
  23. {dycw_pre_commit_hooks-0.11.2 → dycw_pre_commit_hooks-0.12.0}/src/tests/test_main.py +0 -0
@@ -1,10 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dycw-pre-commit-hooks
3
- Version: 0.11.2
3
+ Version: 0.12.0
4
4
  Author-email: Derek Wan <d.wan@icloud.com>
5
5
  Requires-Python: >=3.12
6
6
  Requires-Dist: click<8.3,>=8.2.1
7
- Requires-Dist: dycw-utilities<0.151,>=0.150.11
7
+ Requires-Dist: dycw-utilities<0.167,>=0.166.5
8
+ Requires-Dist: gitpython<3.2,>=3.1.45
8
9
  Requires-Dist: libcst<1.9,>=1.8.2
9
10
  Requires-Dist: loguru<0.8,>=0.7.3
10
11
  Requires-Dist: packaging<25.1,>=25.0
@@ -28,9 +29,10 @@ My [`pre-commit`](https://pre-commit.com/) hooks.
28
29
  - repo: https://github.com/dycw/pre-commit-hooks
29
30
  rev: master
30
31
  hooks:
32
+ - id: format-requirements
31
33
  - id: replace-sequence-str
32
34
  - id: run-bump-my-version
33
- - id: run-ruff-format
35
+ - id: tag-commits
34
36
  ```
35
37
 
36
38
  1. Update your `.pre-commit-config.yaml`:
@@ -15,9 +15,10 @@ My [`pre-commit`](https://pre-commit.com/) hooks.
15
15
  - repo: https://github.com/dycw/pre-commit-hooks
16
16
  rev: master
17
17
  hooks:
18
+ - id: format-requirements
18
19
  - id: replace-sequence-str
19
20
  - id: run-bump-my-version
20
- - id: run-ruff-format
21
+ - id: tag-commits
21
22
  ```
22
23
 
23
24
  1. Update your `.pre-commit-config.yaml`:
@@ -17,28 +17,29 @@ dev = [
17
17
  [project]
18
18
  authors = [{name = "Derek Wan", email = "d.wan@icloud.com"}]
19
19
  dependencies = [
20
- "click >= 8.2.1, < 8.3",
21
- "dycw-utilities >= 0.150.11, < 0.151",
22
- "libcst >= 1.8.2, < 1.9",
23
- "loguru >= 0.7.3, < 0.8",
24
- "packaging >= 25.0, < 25.1",
25
- "tomlkit >= 0.13.2, < 0.14",
20
+ "click >=8.2.1, <8.3",
21
+ "dycw-utilities >=0.166.5, <0.167",
22
+ "gitpython >=3.1.45, <3.2",
23
+ "libcst >=1.8.2, <1.9",
24
+ "loguru >=0.7.3, <0.8",
25
+ "packaging >=25.0, <25.1",
26
+ "tomlkit >=0.13.2, <0.14",
26
27
  ]
27
28
  name = "dycw-pre-commit-hooks"
28
29
  readme = "README.md"
29
30
  requires-python = ">= 3.12"
30
- version = "0.11.2"
31
+ version = "0.12.0"
31
32
 
32
33
  [project.scripts]
33
34
  format-requirements = "pre_commit_hooks.format_requirements:main"
34
35
  replace-sequence-str = "pre_commit_hooks.replace_sequence_str:main"
35
36
  run-bump-my-version = "pre_commit_hooks.run_bump_my_version:main"
36
- run-ruff-format = "pre_commit_hooks.run_ruff_format:main"
37
+ tag-commits = "pre_commit_hooks.tag_commits:main"
37
38
 
38
39
  # bump-my-version
39
40
  [tool.bumpversion]
40
41
  allow_dirty = true
41
- current_version = "0.11.2"
42
+ current_version = "0.12.0"
42
43
 
43
44
  [[tool.bumpversion.files]]
44
45
  filename = "src/pre_commit_hooks/__init__.py"
@@ -114,6 +115,7 @@ addopts = [
114
115
  "-ra",
115
116
  "-vv",
116
117
  "--color=auto",
118
+ "--pdbcls=pudb.debugger:Debugger",
117
119
  "--strict-markers",
118
120
  ]
119
121
  asyncio_default_fixture_loop_scope = "function"
@@ -1,3 +1,3 @@
1
1
  from __future__ import annotations
2
2
 
3
- __version__ = "0.11.2"
3
+ __version__ = "0.12.0"
@@ -0,0 +1,66 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import assert_never
5
+
6
+ from loguru import logger
7
+ from tomlkit import TOMLDocument, parse
8
+ from tomlkit.items import Table
9
+ from utilities.pathlib import get_repo_root
10
+ from utilities.version import Version, parse_version
11
+
12
+ PYPROJECT_TOML = get_repo_root().joinpath("pyproject.toml")
13
+
14
+
15
+ def get_version(
16
+ path_or_text: Path | str | bytes | TOMLDocument, /, *, desc: str = ""
17
+ ) -> Version:
18
+ """Parse the version from a block of text."""
19
+ match path_or_text:
20
+ case Path() as path:
21
+ return get_version(
22
+ path.read_text(), desc=f" from {str(path)!r}" if desc == "" else desc
23
+ )
24
+ case str() | bytes() as text:
25
+ return get_version(parse(text), desc=desc)
26
+ case TOMLDocument() as doc:
27
+ try:
28
+ tool = doc["tool"]
29
+ except KeyError:
30
+ logger.exception(
31
+ f"Failed to get version{desc}; key 'tool' does not exist"
32
+ )
33
+ raise
34
+ if not isinstance(tool, Table):
35
+ logger.exception(f"Failed to get version{desc}; `tool` is not a Table")
36
+ raise TypeError
37
+ try:
38
+ bumpversion = tool["bumpversion"]
39
+ except KeyError:
40
+ logger.exception(
41
+ f"Failed to get version{desc}; key 'bumpversion' does not exist"
42
+ )
43
+ raise
44
+ if not isinstance(bumpversion, Table):
45
+ logger.exception(
46
+ f"Failed to get version{desc}; `bumpversion` is not a Table"
47
+ )
48
+ raise TypeError
49
+ try:
50
+ version = bumpversion["current_version"]
51
+ except KeyError:
52
+ logger.exception(
53
+ f"Failed to get version{desc}; key 'current_version' does not exist"
54
+ )
55
+ raise
56
+ if not isinstance(version, str):
57
+ logger.exception(
58
+ f"Failed to get version{desc}; `version` is not a string"
59
+ )
60
+ raise TypeError
61
+ return parse_version(version)
62
+ case never: # pyright: ignore[reportUnnecessaryComparison]
63
+ assert_never(never)
64
+
65
+
66
+ __all__ = ["PYPROJECT_TOML", "get_version"]
@@ -2,6 +2,7 @@ from __future__ import annotations
2
2
 
3
3
  from typing import TYPE_CHECKING, Any, override
4
4
 
5
+ import utilities.click
5
6
  from click import argument, command
6
7
  from packaging._tokenizer import ParserSyntaxError
7
8
  from packaging.requirements import (
@@ -13,7 +14,6 @@ from packaging.specifiers import Specifier, SpecifierSet
13
14
  from tomlkit import array, dumps, loads, string
14
15
  from tomlkit.items import Array, Table
15
16
  from utilities.atomicwrites import writer
16
- from utilities.click import FilePath
17
17
 
18
18
  from pre_commit_hooks.common import PYPROJECT_TOML
19
19
 
@@ -25,10 +25,10 @@ if TYPE_CHECKING:
25
25
 
26
26
 
27
27
  @command()
28
- @argument("paths", nargs=-1, type=FilePath)
28
+ @argument("paths", nargs=-1, type=utilities.click.Path())
29
29
  def main(*, paths: tuple[Path, ...]) -> bool:
30
30
  """CLI for the `format_requirements` hook."""
31
- results = list(map(_process, paths))
31
+ results = list(map(_process, paths)) # run all
32
32
  return all(results)
33
33
 
34
34
 
@@ -2,6 +2,7 @@ from __future__ import annotations
2
2
 
3
3
  from typing import TYPE_CHECKING, override
4
4
 
5
+ import utilities.click
5
6
  from click import argument, command
6
7
  from libcst import CSTTransformer, Name, Subscript, parse_module
7
8
  from libcst.matchers import Index as MIndex
@@ -10,17 +11,16 @@ from libcst.matchers import Subscript as MSubscript
10
11
  from libcst.matchers import SubscriptElement as MSubscriptElement
11
12
  from libcst.matchers import matches
12
13
  from libcst.metadata import MetadataWrapper
13
- from utilities.click import FilePath
14
14
 
15
15
  if TYPE_CHECKING:
16
16
  from pathlib import Path
17
17
 
18
18
 
19
19
  @command()
20
- @argument("paths", nargs=-1, type=FilePath)
20
+ @argument("paths", nargs=-1, type=utilities.click.Path())
21
21
  def main(*, paths: tuple[Path, ...]) -> bool:
22
22
  """CLI for the `replace_sequence_str` hook."""
23
- results = list(map(_process, paths))
23
+ results = list(map(_process, paths)) # run all
24
24
  return all(results)
25
25
 
26
26
 
@@ -1,15 +1,12 @@
1
1
  from __future__ import annotations
2
2
 
3
- import re
4
3
  from pathlib import Path
5
- from re import MULTILINE
6
4
  from subprocess import PIPE, STDOUT, CalledProcessError, check_call, check_output
7
5
 
8
6
  from click import command
9
7
  from loguru import logger
10
- from utilities.version import Version, parse_version
11
8
 
12
- from pre_commit_hooks.common import PYPROJECT_TOML
9
+ from pre_commit_hooks.common import PYPROJECT_TOML, get_version
13
10
 
14
11
 
15
12
  @command()
@@ -20,10 +17,10 @@ def main() -> bool:
20
17
 
21
18
  def _process() -> bool:
22
19
  path = PYPROJECT_TOML.relative_to(Path.cwd())
23
- current = _parse_version_from_file_or_text(path)
20
+ current = get_version(path)
24
21
  commit = check_output(["git", "rev-parse", "origin/master"], text=True).rstrip("\n")
25
22
  contents = check_output(["git", "show", f"{commit}:{path}"], text=True)
26
- master = _parse_version_from_file_or_text(contents)
23
+ master = get_version(contents)
27
24
  if current in {master.bump_patch(), master.bump_minor(), master.bump_major()}:
28
25
  return True
29
26
  cmd = [
@@ -47,18 +44,4 @@ def _process() -> bool:
47
44
  return False
48
45
 
49
46
 
50
- _PATTERN = re.compile(r'^current_version = "(\d+\.\d+\.\d+)"$', flags=MULTILINE)
51
-
52
-
53
- def _parse_version_from_file_or_text(path_or_text: Path | str, /) -> Version:
54
- """Parse the version from a block of text."""
55
- match path_or_text:
56
- case Path() as path:
57
- with path.open() as fh:
58
- return _parse_version_from_file_or_text(fh.read())
59
- case str() as text:
60
- (match,) = _PATTERN.findall(text)
61
- return parse_version(match)
62
-
63
-
64
47
  __all__ = ["main"]
@@ -0,0 +1,70 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ from click import command
6
+ from git import Commit, GitCommandError, Repo
7
+ from loguru import logger
8
+ from utilities.tzlocal import LOCAL_TIME_ZONE_NAME
9
+ from utilities.whenever import WEEK, from_timestamp, get_now_local
10
+
11
+ from pre_commit_hooks.common import get_version
12
+
13
+ if TYPE_CHECKING:
14
+ from collections.abc import Set as AbstractSet
15
+
16
+ from whenever import ZonedDateTime
17
+
18
+
19
+ @command()
20
+ def main() -> bool:
21
+ """CLI for the `tag_commits` hook."""
22
+ return _process()
23
+
24
+
25
+ def _process() -> bool:
26
+ repo = Repo(".", search_parent_directories=True)
27
+ tagged = {tag.commit.hexsha for tag in repo.tags}
28
+ min_dt = get_now_local() - WEEK
29
+ commits = reversed(list(repo.iter_commits(repo.refs["origin/master"])))
30
+ results = [_process_commit(c, tagged, min_dt, repo) for c in commits] # run all
31
+ return all(results)
32
+
33
+
34
+ def _process_commit(
35
+ commit: Commit, tagged: AbstractSet[str], min_date: ZonedDateTime, repo: Repo, /
36
+ ) -> bool:
37
+ if (commit.hexsha in tagged) or (_get_date_time(commit) < min_date):
38
+ return True
39
+ try:
40
+ _tag_commit(commit, repo)
41
+ except GitCommandError:
42
+ return False
43
+ return True
44
+
45
+
46
+ def _get_date_time(commit: Commit, /) -> ZonedDateTime:
47
+ return from_timestamp(commit.committed_date, time_zone=LOCAL_TIME_ZONE_NAME)
48
+
49
+
50
+ def _tag_commit(commit: Commit, repo: Repo, /) -> None:
51
+ sha = commit.hexsha[:7]
52
+ date = _get_date_time(commit)
53
+ try:
54
+ joined = commit.tree.join("pyproject.toml")
55
+ except KeyError:
56
+ logger.exception(f"`pyproject.toml` not found; failed to tag {sha!r} ({date})")
57
+ return
58
+ text = joined.data_stream.read()
59
+ version = get_version(text, desc=f"'pyproject.toml' @ {sha}")
60
+ try:
61
+ tag = repo.create_tag(str(version), ref=sha)
62
+ except GitCommandError as error:
63
+ desc = error.stderr.strip("\n").strip()
64
+ logger.exception(f"Failed to tag {sha!r} ({date}) due to {desc}")
65
+ return
66
+ logger.info(f"Tagging {sha!r} ({date}) as {str(version)!r}...")
67
+ _ = repo.remotes.origin.push(f"refs/tags/{tag.name}")
68
+
69
+
70
+ __all__ = ["main"]
@@ -1,6 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
- from pre_commit_hooks.run_ruff_format import main
3
+ from pre_commit_hooks.tag_commits import main
4
4
 
5
5
  if __name__ == "__main__":
6
6
  raise SystemExit(int(not main()))
@@ -1,33 +0,0 @@
1
- from __future__ import annotations
2
-
3
- from dataclasses import dataclass
4
-
5
- from loguru import logger
6
- from tomlkit import TOMLDocument, parse
7
- from utilities.pathlib import get_repo_root
8
-
9
- _ROOT = get_repo_root()
10
- PYPROJECT_TOML = _ROOT.joinpath("pyproject.toml")
11
-
12
-
13
- ##
14
-
15
-
16
- @dataclass(kw_only=True)
17
- class PyProject:
18
- contents: str
19
- doc: TOMLDocument
20
-
21
-
22
- def read_pyproject() -> PyProject:
23
- try:
24
- with PYPROJECT_TOML.open(mode="r") as fh:
25
- contents = fh.read()
26
- except FileNotFoundError:
27
- logger.exception("pyproject.toml not found")
28
- raise
29
- doc = parse(contents)
30
- return PyProject(contents=contents, doc=doc)
31
-
32
-
33
- __all__ = ["PYPROJECT_TOML", "read_pyproject"]
@@ -1,81 +0,0 @@
1
- from __future__ import annotations
2
-
3
- from subprocess import CalledProcessError, check_call
4
- from typing import TYPE_CHECKING, cast
5
-
6
- from click import command
7
- from loguru import logger
8
- from tomlkit import dumps, table
9
-
10
- from pre_commit_hooks.common import PYPROJECT_TOML, PyProject, read_pyproject
11
-
12
- if TYPE_CHECKING:
13
- from tomlkit.container import Container
14
-
15
-
16
- @command()
17
- def main() -> bool:
18
- """CLI for the `run-ruff-format` hook."""
19
- return _process()
20
-
21
-
22
- def _process() -> bool:
23
- curr = read_pyproject()
24
- new = _get_modified_pyproject()
25
- result1 = _run_ruff_format(new)
26
- result2 = _run_ruff_format(curr)
27
- _write_pyproject(curr)
28
- return result1 and result2
29
-
30
-
31
- def _get_modified_pyproject() -> PyProject:
32
- pyproject = read_pyproject()
33
- doc = pyproject.doc
34
- try:
35
- tool = cast("Container", doc["tool"])
36
- except KeyError:
37
- tool = table()
38
- try:
39
- ruff = cast("Container", tool["ruff"])
40
- except KeyError:
41
- ruff = table()
42
- ruff["line-length"] = 320
43
- try:
44
- format_ = cast("Container", ruff["format"])
45
- except KeyError:
46
- format_ = table()
47
- format_["skip-magic-trailing-comma"] = True
48
- try:
49
- lint = cast("Container", ruff["lint"])
50
- except KeyError:
51
- lint = table()
52
- try:
53
- isort = cast("Container", lint["isort"])
54
- except KeyError:
55
- isort = table()
56
- isort["split-on-trailing-comma"] = False
57
- doc["tool"] = tool
58
- tool["ruff"] = ruff
59
- ruff["format"] = format_
60
- ruff["lint"] = lint
61
- lint["isort"] = isort
62
- return PyProject(contents=dumps(doc), doc=doc)
63
-
64
-
65
- def _run_ruff_format(pyproject: PyProject, /) -> bool:
66
- _write_pyproject(pyproject)
67
- cmd = ["ruff", "format", "."]
68
- try:
69
- code = check_call(cmd)
70
- except CalledProcessError:
71
- logger.exception("Failed to run {cmd!r}", cmd=" ".join(cmd))
72
- return False
73
- return code == 0
74
-
75
-
76
- def _write_pyproject(pyproject: PyProject, /) -> None:
77
- with PYPROJECT_TOML.open(mode="w") as fh:
78
- _ = fh.write(pyproject.contents)
79
-
80
-
81
- __all__ = ["main"]