|
| 1 | +# pyright: basic |
| 2 | + |
| 3 | +"""Maintain lint tools version consistency between `.pre-commit-config.yaml` and `pyproject.toml`.""" |
| 4 | + |
| 5 | + |
| 6 | +# https://packaging.pypa.io/en/stable/requirements.html |
| 7 | +# https://yaml.readthedocs.io/en/latest/example/ |
| 8 | +# https://tomlkit.readthedocs.io/en/latest/quickstart/ |
| 9 | +# https://hatch.pypa.io/latest/config/environment/overview/#dependencies |
| 10 | + |
| 11 | + |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | +from typing import ( |
| 15 | + Any, |
| 16 | + Dict, |
| 17 | + List, |
| 18 | + Union, |
| 19 | +) |
| 20 | + |
| 21 | +import tomlkit # type: ignore |
| 22 | +import tomlkit.items # type: ignore |
| 23 | +from packaging.requirements import Requirement # type: ignore |
| 24 | +from packaging.specifiers import SpecifierSet # type: ignore |
| 25 | +from packaging.version import Version # type: ignore |
| 26 | +from ruamel.yaml import YAML # type: ignore |
| 27 | + |
| 28 | +yaml = YAML(typ="safe") |
| 29 | + |
| 30 | +pre_commit_config_yaml_path = Path(".pre-commit-config.yaml") |
| 31 | +pyproject_toml_path = Path("pyproject.toml") |
| 32 | + |
| 33 | +RepoType = Dict[str, Any] |
| 34 | +HookType = Dict[str, Any] |
| 35 | + |
| 36 | +if __name__ == "__main__": |
| 37 | + # NOTE: 这三个键名应该对应 |
| 38 | + # pyproject_toml["tool"]["hatch"]["envs"]["default"]["dependencies"] 里的值 |
| 39 | + vers_in_pre_commit: Dict[str, Union[None, str]] = { |
| 40 | + "ruff": None, |
| 41 | + "black": None, |
| 42 | + "codespell": None, |
| 43 | + } |
| 44 | + |
| 45 | + # 找出pre-commit-config.yaml中的版本 |
| 46 | + pre_commit_yaml = yaml.load(pre_commit_config_yaml_path) |
| 47 | + repos_lst: List[RepoType] = pre_commit_yaml["repos"] |
| 48 | + |
| 49 | + for repo in repos_lst: |
| 50 | + hooks_lst: List[HookType] = repo["hooks"] |
| 51 | + hook = hooks_lst[0] # 特殊标记的只有一个hook |
| 52 | + hook_alias = hook.get("alias") # 只有特殊标记的才有alias |
| 53 | + if hook_alias is None: |
| 54 | + continue |
| 55 | + if hook_alias in vers_in_pre_commit: |
| 56 | + vers_in_pre_commit[hook_alias] = repo["rev"] |
| 57 | + |
| 58 | + # 检查是否正确 |
| 59 | + new_vers: Dict[str, Version] = {} |
| 60 | + for name, ver in vers_in_pre_commit.items(): |
| 61 | + if not isinstance(ver, str): |
| 62 | + sys.exit(f"Error: version of `{name}` not found in pre-commit-config.yaml") |
| 63 | + try: |
| 64 | + new_vers[name] = Version(ver) |
| 65 | + except Exception as e: |
| 66 | + sys.exit(f"{e}: version of `{name}` in pre-commit-config.yaml is invalid") |
| 67 | + |
| 68 | + # 修改pyproject.toml中的版本 |
| 69 | + with open(pyproject_toml_path, "rb") as f: # NOTE: 用二进制模式打开 |
| 70 | + pyproject_toml = tomlkit.load(f) |
| 71 | + requir_lst = pyproject_toml["tool"]["hatch"]["envs"]["default"]["dependencies"] # type: ignore |
| 72 | + assert isinstance(requir_lst, tomlkit.items.Array) |
| 73 | + |
| 74 | + for idx, require in enumerate(requir_lst): |
| 75 | + assert isinstance(require, tomlkit.items.String) |
| 76 | + parsed_requir = Requirement(require) |
| 77 | + if parsed_requir.name in new_vers: |
| 78 | + # 更新版本 |
| 79 | + parsed_requir.specifier = SpecifierSet( |
| 80 | + f"=={new_vers.pop(parsed_requir.name)}" |
| 81 | + ) |
| 82 | + requir_lst[idx] = str(parsed_requir) |
| 83 | + |
| 84 | + # 在上一步的pop操作应该把所有的依赖都更新了,如果这里字典不为空,说明发生了错误 |
| 85 | + if new_vers: |
| 86 | + sys.exit( |
| 87 | + f"Error: version of `{new_vers.popitem()}` not found in pyproject.toml" |
| 88 | + ) |
| 89 | + |
| 90 | + # 如果没错误,就准备更新 |
| 91 | + pyproject_toml["tool"]["hatch"]["envs"]["default"]["dependencies"] = requir_lst # type: ignore |
| 92 | + |
| 93 | + with open(pyproject_toml_path, "wb") as f: |
| 94 | + toml_str = tomlkit.dumps(pyproject_toml) |
| 95 | + f.write(toml_str.encode("utf-8")) # NOTE: 用utf-8二进制写入,否则文本样式会乱 |
0 commit comments