7
"""This gets executed on 'git commit' and rejects the commit in case
8
the submitted code does not pass validation. Validation is run only
9
against the files which were modified in the commit. Install this with
10
"make install-git-hooks".
13
from __future__ import print_function
21
PYTHON = sys.executable
22
PY3 = sys.version_info[0] >= 3
23
THIS_SCRIPT = os.path.realpath(__file__)
26
def term_supports_colors():
30
assert sys.stderr.isatty()
32
assert curses.tigetnum("colors") > 0
38
def hilite(s, ok=True, bold=False):
39
"""Return an highlighted version of 'string'."""
40
if not term_supports_colors():
51
return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), s)
55
print(hilite("commit aborted: " + msg, ok=False), file=sys.stderr)
60
if isinstance(cmd, str):
61
cmd = shlex.split(cmd)
64
stdout=subprocess.PIPE,
65
stderr=subprocess.PIPE,
66
universal_newlines=True,
68
stdout, stderr = p.communicate()
70
raise RuntimeError(stderr)
72
print(stderr, file=sys.stderr)
73
if stdout.endswith('\n'):
79
kw = {'encoding': 'utf8'} if PY3 else {}
80
return open(path, **kw)
83
def git_commit_files():
84
out = sh(["git", "diff", "--cached", "--name-only"])
86
x for x in out.split('\n') if x.endswith('.py') and os.path.exists(x)
90
for x in out.split('\n')
91
if x.endswith(('.c', '.h')) and os.path.exists(x)
94
x for x in out.split('\n') if x.endswith('.rst') and os.path.exists(x)
97
x for x in out.split("\n") if x.endswith(".toml") and os.path.exists(x)
100
["git", "diff", "--name-only", "--diff-filter=ADR", "--cached"]
103
new_rm_mv = new_rm_mv.split()
104
return (py_files, c_files, rst_files, toml_files, new_rm_mv)
108
print("running black (%s)" % len(files))
109
cmd = [PYTHON, "-m", "black", "--check", "--safe"] + files
110
if subprocess.call(cmd) != 0:
112
"Python code didn't pass 'ruff' style check."
113
"Try running 'make fix-ruff'."
118
print("running ruff (%s)" % len(files))
119
cmd = [PYTHON, "-m", "ruff", "check", "--no-cache"] + files
120
if subprocess.call(cmd) != 0:
122
"Python code didn't pass 'ruff' style check."
123
"Try running 'make fix-ruff'."
128
print("running clinter (%s)" % len(files))
130
cmd = [PYTHON, "scripts/internal/clinter.py"] + files
131
if subprocess.call(cmd) != 0:
132
return sys.exit("C code didn't pass style check")
136
print("running toml linter (%s)" % len(files))
137
cmd = ["toml-sort", "--check"] + files
138
if subprocess.call(cmd) != 0:
139
return sys.exit("%s didn't pass style check" % ' '.join(files))
143
print("running rst linter (%s)" % len(files))
144
cmd = ["rstcheck", "--config=pyproject.toml"] + files
145
if subprocess.call(cmd) != 0:
146
return sys.exit("RST code didn't pass style check")
150
py_files, c_files, rst_files, toml_files, new_rm_mv = git_commit_files()
159
toml_sort(toml_files)
161
out = sh([PYTHON, "scripts/internal/generate_manifest.py"])
162
with open_text('MANIFEST.in') as f:
163
if out.strip() != f.read().strip():
165
"some files were added, deleted or renamed; "
166
"run 'make generate-manifest' and commit again"