diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 277867e..8aaec05 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -6,3 +6,12 @@ require_serial: true types: [file] files: \.(py|rst)$ + +- id: blackdoc-autoupdate-black + name: autoupdate-black + entry: python -m blackdoc.autoupdate + language: python + language_version: python3 + require_serial: true + types: [file] + files: \.pre-commit-config\.yaml diff --git a/blackdoc/autoupdate.py b/blackdoc/autoupdate.py new file mode 100644 index 0000000..9f85729 --- /dev/null +++ b/blackdoc/autoupdate.py @@ -0,0 +1,33 @@ +import argparse +import re + +version_re = re.compile(r"black\s+rev: (.+)\s+hooks:\s+- id: black") +black_pin_re = re.compile( + r"(- id: blackdoc.+?additional_dependencies:.+?black==)[.\w]+", + re.DOTALL, +) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("path") + args = parser.parse_args() + with open(args.path) as f: + content = f.read() + + match = version_re.search(content) + if match is None: + raise ValueError("cannot find the black hook") + version = match.group(1) + replaced = black_pin_re.sub(rf"\g<1>{version}", content) + + if content != replaced: + with open(args.path, mode="w") as f: + f.write(replaced) + return 1 + else: + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/doc/changelog.rst b/doc/changelog.rst index f5cfb86..855fee5 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -4,6 +4,8 @@ v0.4.0 (*unreleased*) --------------------- - replace docstrings by modifying by token (:pull:`144`) - switch the html theme to `furo `_ (:pull:`149`) +- add a new hook to synchronize `black` pinned in `additional_dependencies` with the version from + the `black` hook (:pull:`124`) v0.3.6 (25 August 2022) -----------------------