-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgenerate_pre_commit_hooks.py
More file actions
executable file
·71 lines (58 loc) · 2 KB
/
generate_pre_commit_hooks.py
File metadata and controls
executable file
·71 lines (58 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
import contextlib
import string
import sys
import docker
hooks_template = """
- id: shfmt
name: shfmt
description: Shell source code formatter (prebuilt upstream executable)
language: python
entry: shfmt
args: [--write]
types: [shell]
exclude_types: [csh, tcsh]
stages: [pre-commit, pre-merge-commit, pre-push, manual]
minimum_pre_commit_version: 3.2.0 # for "stages" names
- id: shfmt-src
name: shfmt
description: Shell source code formatter (build from source)
language: golang
# Note: keep Go version in `go.mod` in sync with shfmt's required Go version
additional_dependencies: [mvdan.cc/sh/v3/cmd/shfmt@${shfmt_tag}]
entry: shfmt
args: [--write]
types: [shell]
exclude_types: [csh, tcsh]
stages: [pre-commit, pre-merge-commit, pre-push, manual]
minimum_pre_commit_version: 3.2.0 # for "stages" names
- id: shfmt-docker
name: shfmt
description: Shell source code formatter (Docker image)
language: docker_image
# Note: use the top level multiplatform image digest here
entry: --net none mvdan/shfmt:${shfmt_tag}@${docker_image_digest}
args: [--write]
types: [shell]
exclude_types: [csh, tcsh]
stages: [pre-commit, pre-merge-commit, pre-push, manual]
minimum_pre_commit_version: 3.2.0 # for "stages" names
"""
def main(python_pkg_tag: str) -> None:
shfmt_tag, _, _ = python_pkg_tag.partition("-")
with contextlib.closing(docker.from_env()) as client:
registry_data = client.images.get_registry_data(f"mvdan/shfmt:{shfmt_tag}")
# Top level multiplatform image digest
docker_image_digest = registry_data.attrs["Descriptor"]["digest"]
data = {
"shfmt_tag": shfmt_tag,
"docker_image_digest": docker_image_digest,
}
st = string.Template(hooks_template.lstrip())
hooks_yaml = st.substitute(data)
sys.stdout.write(hooks_yaml)
if __name__ == "__main__":
if len(sys.argv) != 2:
print(f"usage: {sys.argv[0]} PYTHON-PKG-TAG", file=sys.stderr)
sys.exit(2)
main(sys.argv[1])