-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a hook to autoupdate
black
in user configuration (#124)
* add a rough script * register the script as a hook * move the script code to a function * register the script as a executable * try to use python -m * finish the script * remove the additional dependency * remove the autoupdate console entrypoint * changelog [skip-ci] * rename the hook [skip-ci]
- Loading branch information
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters