|
| 1 | +""" |
| 2 | +Check that the flake8 pins are the same in: |
| 3 | +
|
| 4 | +- environment.yml |
| 5 | +- .pre-commit-config.yaml, in the flake8 hook |
| 6 | +- .pre-commit-config.yaml, in the additional dependencies of the yesqa hook |
| 7 | +
|
| 8 | +The flake8 hook revision in .pre-commit-config.yaml is taken as the reference revision. |
| 9 | +
|
| 10 | +Usage: either |
| 11 | +
|
| 12 | +- ``python scripts/sync_flake8_versions.py``, or |
| 13 | +- ``pre-commit run sync-flake8-versions --all-files``. |
| 14 | +""" |
| 15 | +import sys |
| 16 | +from typing import ( |
| 17 | + Any, |
| 18 | + Mapping, |
| 19 | + NamedTuple, |
| 20 | + Sequence, |
| 21 | + Tuple, |
| 22 | + TypeVar, |
| 23 | +) |
| 24 | + |
| 25 | +import yaml |
| 26 | + |
| 27 | + |
| 28 | +class Revisions(NamedTuple): |
| 29 | + precommit_rev: str |
| 30 | + precommit_yesqa_rev: str |
| 31 | + environment_rev: str |
| 32 | + |
| 33 | + |
| 34 | +YamlMapping = Mapping[str, Any] |
| 35 | +Repo = TypeVar("Repo", bound=YamlMapping) |
| 36 | + |
| 37 | + |
| 38 | +def _get_repo_hook(repos: Sequence[Repo], hook_name: str) -> Tuple[Repo, YamlMapping]: |
| 39 | + for repo in repos: |
| 40 | + for hook in repo["hooks"]: |
| 41 | + if hook["id"] == hook_name: |
| 42 | + return repo, hook |
| 43 | + else: |
| 44 | + raise RuntimeError(f"Repo with hook {hook_name} not found") |
| 45 | + |
| 46 | + |
| 47 | +def get_revisions(precommit_config: YamlMapping, environment: YamlMapping) -> Revisions: |
| 48 | + repos = precommit_config["repos"] |
| 49 | + flake8_repo, _ = _get_repo_hook(repos, "flake8") |
| 50 | + precommit_rev = flake8_repo["rev"] |
| 51 | + |
| 52 | + _, yesqa_hook = _get_repo_hook(repos, "yesqa") |
| 53 | + additional_dependencies = yesqa_hook.get("additional_dependencies", []) |
| 54 | + for dep in additional_dependencies: |
| 55 | + if "==" in dep: |
| 56 | + pkg, rev = dep.split("==", maxsplit=1) |
| 57 | + if pkg == "flake8": |
| 58 | + precommit_yesqa_rev = rev |
| 59 | + break |
| 60 | + else: |
| 61 | + raise RuntimeError( |
| 62 | + "flake8 not found, or not pinned, in additional dependencies of yesqa " |
| 63 | + "hook in .pre-commit-config.yaml" |
| 64 | + ) |
| 65 | + |
| 66 | + deps = environment["dependencies"] |
| 67 | + for dep in deps: |
| 68 | + if isinstance(dep, str) and "=" in dep: |
| 69 | + pkg, rev = dep.split("=", maxsplit=1) |
| 70 | + if pkg == "flake8": |
| 71 | + environment_rev = rev |
| 72 | + break |
| 73 | + else: |
| 74 | + raise RuntimeError("flake8 not found, or not pinned, in environment.yml") |
| 75 | + |
| 76 | + return Revisions(precommit_rev, precommit_yesqa_rev, environment_rev) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + with open(".pre-commit-config.yaml") as fd: |
| 81 | + precommit_config = yaml.safe_load(fd) |
| 82 | + with open("environment.yml") as fd: |
| 83 | + environment = yaml.safe_load(fd) |
| 84 | + |
| 85 | + revisions = get_revisions(precommit_config, environment) |
| 86 | + |
| 87 | + if revisions.environment_rev != revisions.precommit_rev: |
| 88 | + sys.stdout.write( |
| 89 | + f"flake8 pin in environment.yml is {revisions.environment_rev}, " |
| 90 | + f"should be {revisions.precommit_rev}\n" |
| 91 | + ) |
| 92 | + sys.exit(1) |
| 93 | + |
| 94 | + if revisions.precommit_yesqa_rev != revisions.precommit_rev: |
| 95 | + sys.stdout.write( |
| 96 | + f"flake8 pin in yesqa is {revisions.precommit_yesqa_rev}, " |
| 97 | + f"should be {revisions.precommit_rev}\n" |
| 98 | + ) |
| 99 | + sys.exit(1) |
| 100 | + |
| 101 | + sys.exit(0) |
0 commit comments