dycw-pre-commit-hooks 0.12.0__py3-none-any.whl → 0.12.2__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.12.0
3
+ Version: 0.12.2
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
@@ -8,8 +8,10 @@ Requires-Dist: dycw-utilities<0.167,>=0.166.5
8
8
  Requires-Dist: gitpython<3.2,>=3.1.45
9
9
  Requires-Dist: libcst<1.9,>=1.8.2
10
10
  Requires-Dist: loguru<0.8,>=0.7.3
11
+ Requires-Dist: orjson<3.12,>=3.11.3
11
12
  Requires-Dist: packaging<25.1,>=25.0
12
13
  Requires-Dist: tomlkit<0.14,>=0.13.2
14
+ Requires-Dist: xdg-base-dirs<6.1,>=6.0.2
13
15
  Description-Content-Type: text/markdown
14
16
 
15
17
  # pre-commit-hooks
@@ -1,15 +1,15 @@
1
- pre_commit_hooks/__init__.py,sha256=bQNDsEZEkXpjrS8_6ddB990uhJXsUMc12yzpPhsHOlc,59
2
- pre_commit_hooks/common.py,sha256=x-a-v0esyBPJqPZf6uypbrFaxUKEgfnuAxiUvYSaLbA,2350
1
+ pre_commit_hooks/__init__.py,sha256=tiNQ40NwKZSP_S-wg-yypkGWxyH5-ueuWipPiizKx8U,59
2
+ pre_commit_hooks/common.py,sha256=rIfVTUYfmeQq7Vk4m_Of-JGqABZlB7dfCyGo2WTibas,2078
3
3
  pre_commit_hooks/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  pre_commit_hooks/format_requirements/__init__.py,sha256=bn1JsblINCf0ro2QrhsM8XtTOHnPFSZC2t-Hx5AJDfA,3392
5
5
  pre_commit_hooks/format_requirements/__main__.py,sha256=15JSp_rhjI_Ddoj4MRkHFShfnYxs6GggUhLRlGtrQ0E,156
6
6
  pre_commit_hooks/replace_sequence_str/__init__.py,sha256=a-pCAcBaa7FDsGDNLzMiD-xG20wKl-SRmhWSbm9hrvo,1618
7
7
  pre_commit_hooks/replace_sequence_str/__main__.py,sha256=B1dxOxngV4vUVnDVrXSywiySOs1P_zF30_4ZMRsOSaY,157
8
- pre_commit_hooks/run_bump_my_version/__init__.py,sha256=UGaOu7Ie5LRGsMe34eygSgMWteKfDRGNX49R3kafczk,1353
8
+ pre_commit_hooks/run_bump_my_version/__init__.py,sha256=EOKRq04eIKojyfgfvlaPfWeKxOWkEBjmz2UOcuk6U_o,2068
9
9
  pre_commit_hooks/run_bump_my_version/__main__.py,sha256=w2V3y61jrSau-zxjl8ciHtWPlJQwXbYxNJ2tGYVyI4s,156
10
- pre_commit_hooks/tag_commits/__init__.py,sha256=YmYGKu8YPWIjUTNyju0rKSivTnZLMZBiJ2mXrQGvO3Y,2146
10
+ pre_commit_hooks/tag_commits/__init__.py,sha256=AmHKjIAMfXZ6T0nw6CiOFTjFzmmVSOonmKyhASZ_sgE,3665
11
11
  pre_commit_hooks/tag_commits/__main__.py,sha256=qefgYw7LWbvmzZS45-ym6olS4cHqw1Emw2wlqZBXN_o,148
12
- dycw_pre_commit_hooks-0.12.0.dist-info/METADATA,sha256=2zXSUhMvjNisfDcMcRzZFlDXDeyxkBMzfUz_0fvnccc,952
13
- dycw_pre_commit_hooks-0.12.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
14
- dycw_pre_commit_hooks-0.12.0.dist-info/entry_points.txt,sha256=r1tnPzaGvOrSdX-ZRriidf8AhZgUtXfjdTpwpkTBA8U,260
15
- dycw_pre_commit_hooks-0.12.0.dist-info/RECORD,,
12
+ dycw_pre_commit_hooks-0.12.2.dist-info/METADATA,sha256=DdndJ2NzELgAG-gBuL1cvnDLTEPCAe04GMhKLjju-zs,1029
13
+ dycw_pre_commit_hooks-0.12.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
14
+ dycw_pre_commit_hooks-0.12.2.dist-info/entry_points.txt,sha256=r1tnPzaGvOrSdX-ZRriidf8AhZgUtXfjdTpwpkTBA8U,260
15
+ dycw_pre_commit_hooks-0.12.2.dist-info/RECORD,,
@@ -1,3 +1,3 @@
1
1
  from __future__ import annotations
2
2
 
3
- __version__ = "0.12.0"
3
+ __version__ = "0.12.2"
@@ -12,51 +12,41 @@ from utilities.version import Version, parse_version
12
12
  PYPROJECT_TOML = get_repo_root().joinpath("pyproject.toml")
13
13
 
14
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:
15
+ def get_version(source: Path | str | bytes | TOMLDocument, /) -> Version:
16
+ """Get the `[tool.bumpversion]` version from a TOML file."""
17
+ match source:
20
18
  case Path() as path:
21
- return get_version(
22
- path.read_text(), desc=f" from {str(path)!r}" if desc == "" else desc
23
- )
19
+ return get_version(path.read_text())
24
20
  case str() | bytes() as text:
25
- return get_version(parse(text), desc=desc)
21
+ return get_version(parse(text))
26
22
  case TOMLDocument() as doc:
27
23
  try:
28
24
  tool = doc["tool"]
29
25
  except KeyError:
30
- logger.exception(
31
- f"Failed to get version{desc}; key 'tool' does not exist"
32
- )
26
+ logger.exception("Failed to get version; key 'tool' does not exist")
33
27
  raise
34
28
  if not isinstance(tool, Table):
35
- logger.exception(f"Failed to get version{desc}; `tool` is not a Table")
29
+ logger.exception("Failed to get version; `tool` is not a Table")
36
30
  raise TypeError
37
31
  try:
38
32
  bumpversion = tool["bumpversion"]
39
33
  except KeyError:
40
34
  logger.exception(
41
- f"Failed to get version{desc}; key 'bumpversion' does not exist"
35
+ "Failed to get version; key 'bumpversion' does not exist"
42
36
  )
43
37
  raise
44
38
  if not isinstance(bumpversion, Table):
45
- logger.exception(
46
- f"Failed to get version{desc}; `bumpversion` is not a Table"
47
- )
39
+ logger.exception("Failed to get version; `bumpversion` is not a Table")
48
40
  raise TypeError
49
41
  try:
50
42
  version = bumpversion["current_version"]
51
43
  except KeyError:
52
44
  logger.exception(
53
- f"Failed to get version{desc}; key 'current_version' does not exist"
45
+ "Failed to get version; key 'current_version' does not exist"
54
46
  )
55
47
  raise
56
48
  if not isinstance(version, str):
57
- logger.exception(
58
- f"Failed to get version{desc}; `version` is not a string"
59
- )
49
+ logger.exception("Failed to get version; `version` is not a string")
60
50
  raise TypeError
61
51
  return parse_version(version)
62
52
  case never: # pyright: ignore[reportUnnecessaryComparison]
@@ -2,21 +2,32 @@ from __future__ import annotations
2
2
 
3
3
  from pathlib import Path
4
4
  from subprocess import PIPE, STDOUT, CalledProcessError, check_call, check_output
5
+ from typing import Literal, assert_never
5
6
 
6
- from click import command
7
+ from click import Choice, command, option
7
8
  from loguru import logger
9
+ from utilities.pathlib import get_repo_root
8
10
 
9
11
  from pre_commit_hooks.common import PYPROJECT_TOML, get_version
10
12
 
13
+ type _Mode = Literal["pyproject", "bumpversion"]
14
+ _MODE: _Mode = "pyproject"
15
+
11
16
 
12
17
  @command()
13
- def main() -> bool:
18
+ @option(
19
+ "--mode",
20
+ type=Choice(["pyproject", "bumpversion"], case_sensitive=False),
21
+ default=_MODE,
22
+ show_default=True,
23
+ )
24
+ def main(*, mode: _Mode = _MODE) -> bool:
14
25
  """CLI for the `run_bump_my_version` hook."""
15
- return _process()
26
+ return _process(mode=mode)
16
27
 
17
28
 
18
- def _process() -> bool:
19
- path = PYPROJECT_TOML.relative_to(Path.cwd())
29
+ def _process(*, mode: _Mode = _MODE) -> bool:
30
+ path = _get_rel_path(mode=mode)
20
31
  current = get_version(path)
21
32
  commit = check_output(["git", "rev-parse", "origin/master"], text=True).rstrip("\n")
22
33
  contents = check_output(["git", "show", f"{commit}:{path}"], text=True)
@@ -44,4 +55,15 @@ def _process() -> bool:
44
55
  return False
45
56
 
46
57
 
58
+ def _get_rel_path(*, mode: _Mode = _MODE) -> Path:
59
+ match mode:
60
+ case "pyproject":
61
+ path = PYPROJECT_TOML
62
+ case "bumpversion":
63
+ path = get_repo_root().joinpath(".bumpversion.toml")
64
+ case never: # pyright: ignore[reportUnnecessaryComparison]
65
+ assert_never(never)
66
+ return path.relative_to(Path.cwd())
67
+
68
+
47
69
  __all__ = ["main"]
@@ -2,39 +2,96 @@ from __future__ import annotations
2
2
 
3
3
  from typing import TYPE_CHECKING
4
4
 
5
- from click import command
5
+ import utilities.click
6
+ from click import command, option
6
7
  from git import Commit, GitCommandError, Repo
7
8
  from loguru import logger
9
+ from utilities.hashlib import md5_hash
10
+ from utilities.pathlib import get_repo_root
8
11
  from utilities.tzlocal import LOCAL_TIME_ZONE_NAME
9
- from utilities.whenever import WEEK, from_timestamp, get_now_local
12
+ from utilities.whenever import from_timestamp, get_now_local
13
+ from whenever import DateTimeDelta, ZonedDateTime
14
+ from xdg_base_dirs import xdg_cache_home
10
15
 
11
16
  from pre_commit_hooks.common import get_version
12
17
 
13
18
  if TYPE_CHECKING:
14
19
  from collections.abc import Set as AbstractSet
15
20
 
16
- from whenever import ZonedDateTime
21
+ _RUN_EVERY: DateTimeDelta | None = None
22
+ _MAX_AGE: DateTimeDelta | None = None
17
23
 
18
24
 
19
25
  @command()
20
- def main() -> bool:
26
+ @option(
27
+ "--run-every",
28
+ type=utilities.click.DateTimeDelta(),
29
+ default=_RUN_EVERY,
30
+ show_default=True,
31
+ )
32
+ @option(
33
+ "--max-age",
34
+ type=utilities.click.DateTimeDelta(),
35
+ default=_MAX_AGE,
36
+ show_default=True,
37
+ )
38
+ def main(
39
+ *,
40
+ run_every: DateTimeDelta | None = _RUN_EVERY,
41
+ max_age: DateTimeDelta | None = _MAX_AGE,
42
+ ) -> bool:
21
43
  """CLI for the `tag_commits` hook."""
22
- return _process()
44
+ return _process(run_every=run_every, max_age=max_age)
45
+
46
+
47
+ def _process(
48
+ *,
49
+ run_every: DateTimeDelta | None = _RUN_EVERY,
50
+ max_age: DateTimeDelta | None = _MAX_AGE,
51
+ ) -> bool:
52
+ if run_every is not None:
53
+ last = _get_last_run()
54
+ min_date_time = get_now_local() - run_every
55
+ if (last is not None) and (min_date_time <= last):
56
+ return True
57
+ return _process_commits(max_age=max_age)
58
+
59
+
60
+ def _get_last_run() -> ZonedDateTime | None:
61
+ hash_ = md5_hash(get_repo_root())
62
+ path = xdg_cache_home().joinpath("tag-commits", hash_)
63
+ try:
64
+ text = path.read_text()
65
+ except FileNotFoundError:
66
+ return None
67
+ try:
68
+ return ZonedDateTime.parse_common_iso(text.strip("\n"))
69
+ except ValueError:
70
+ return None
23
71
 
24
72
 
25
- def _process() -> bool:
73
+ def _process_commits(*, max_age: DateTimeDelta | None = None) -> bool:
26
74
  repo = Repo(".", search_parent_directories=True)
27
75
  tagged = {tag.commit.hexsha for tag in repo.tags}
28
- min_dt = get_now_local() - WEEK
76
+ min_date_time = None if max_age is None else (get_now_local() - max_age)
29
77
  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
78
+ results = [
79
+ _process_commit(c, tagged, repo, min_date_time=min_date_time) for c in commits
80
+ ] # run all
31
81
  return all(results)
32
82
 
33
83
 
34
84
  def _process_commit(
35
- commit: Commit, tagged: AbstractSet[str], min_date: ZonedDateTime, repo: Repo, /
85
+ commit: Commit,
86
+ tagged: AbstractSet[str],
87
+ repo: Repo,
88
+ /,
89
+ *,
90
+ min_date_time: ZonedDateTime | None = None,
36
91
  ) -> bool:
37
- if (commit.hexsha in tagged) or (_get_date_time(commit) < min_date):
92
+ if (commit.hexsha in tagged) or (
93
+ (min_date_time is not None) and (_get_date_time(commit) < min_date_time)
94
+ ):
38
95
  return True
39
96
  try:
40
97
  _tag_commit(commit, repo)
@@ -56,7 +113,7 @@ def _tag_commit(commit: Commit, repo: Repo, /) -> None:
56
113
  logger.exception(f"`pyproject.toml` not found; failed to tag {sha!r} ({date})")
57
114
  return
58
115
  text = joined.data_stream.read()
59
- version = get_version(text, desc=f"'pyproject.toml' @ {sha}")
116
+ version = get_version(text)
60
117
  try:
61
118
  tag = repo.create_tag(str(version), ref=sha)
62
119
  except GitCommandError as error: