-
Notifications
You must be signed in to change notification settings - Fork 14
/
precommit.py
executable file
·326 lines (259 loc) · 8.87 KB
/
precommit.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python3
"""
Pre-commit hook for Python, C++, etc.
If run like
python3 precommit.py install
in Linux or Windows it will install itself as a git hook.
When run, it check staged files for style, lint errors, type errors
etc. It will also run Python unit tests if any Python tests are
modified.
It requires the following Python packages:
mypy pyflakes pytest yapf
If requires the following LLVM programs:
clang-format
It requires the following Node packages:
eslint prettier
"""
import os
import subprocess
import stat
import sys
import time
from typing import List
import colorama
from colorama import Fore, Style
SOURCE_TO_CHECK = ["minimum", "misc"]
colorama.init()
class PrecommitFailure(Exception):
"""Means that a precommit failed and the program will terminate."""
def __init__(self, message: str) -> None:
self.message = message
class PrecommitWarning(Exception):
"""Issues a warning and the program will continue."""
def __init__(self, message: str) -> None:
self.message = message
class Precommit:
"""Base class that describes a precommit."""
def run(self) -> None:
"""Runs the precommit and raises PrecommitFailure on failure."""
pass
def check_for_modifications(self, files: List[str], message: str) -> None:
"""Fails the test with an error if there are local modifications."""
run = subprocess.run(["git", "ls-files", "-m"], stdout=subprocess.PIPE)
assert run.returncode == 0
stdout = run.stdout.decode("utf-8")
modified_files = filter(None, stdout.split("\n")) # type: ignore
# We don't allow any file in files to be modified.
if set(modified_files) & set(files):
raise PrecommitFailure(message)
def run_with_check(self, args: List[str], errormessage: str) -> None:
"""Runs the list of arguments and exits the current process on failure."""
run = subprocess.run(args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
if run.returncode != 0:
raise PrecommitFailure(
run.stdout.decode("utf-8") + "\n\n" + errormessage)
class NoModifiedFiles(Precommit):
"""Checks that no files are modified.
The presubmit script assumes that files are either staged or unmodified.
"""
def __init__(self, files):
self.files = files
def run(self):
self.check_for_modifications(
self.files, "There are local modifications to the added files. "
"Please stage/stash them.")
class LineEndings(Precommit):
"""Checks that all files do not contain CRLF line endings."""
def __init__(self, files):
self.files = files
def run(self):
failure_message = ""
for name in self.files:
with open(name, "rb") as f:
data = f.read()
if b"\r\n" in data:
data = data.replace(b'\r\n', b'\n')
with open(name, "wb") as f:
f.write(data)
failure_message += f"{name} contains CRLF line endings.\n"
if failure_message:
raise PrecommitFailure(failure_message +
"\nThe files have been fixed." +
" Please stage them if OK.")
class ValidUtf8(Precommit):
"""Checks that all files decode as UTF-8."""
def __init__(self, files):
self.files = files
def run(self):
for name in self.files:
try:
with open(name, "rb") as f:
f.read().decode("utf-8")
except UnicodeDecodeError:
raise PrecommitFailure(f"{name} is not valid UTF-8.")
class UnixLineEndings(Precommit):
"""Checks that all files decode as UTF-8."""
def __init__(self, files):
self.files = files
def run(self):
for name in self.files:
with open(name, "rb") as f:
data = f.read()
unix_data = data.replace(b"\r\n", b"\n")
if unix_data != data:
with open(name, "rb") as f:
f.write(unix_data)
self.check_for_modifications(
self.files, "Line endings have been changed to UNIX. " +
"Please stage them if OK.")
class Pyflakes(Precommit):
"""Runs Pyflakes.
Pyflakes is very fast and has essentially zero false positives.
"""
def __init__(self, files):
self.files = files
def run(self):
self.run_with_check([sys.executable, "-m", "pyflakes"] + self.files,
"Pyflakes failed.")
class MyPy(Precommit):
"""Runs the type-checker Mypy on the provided Python files."""
def __init__(self, files):
self.files = files
def run(self):
self.run_with_check(
[sys.executable, "-m", "mypy", "--ignore-missing-imports"] +
self.files, "Mypy failed.")
class PyTest(Precommit):
"""Runs the Python unit tests in the code base."""
def run(self):
self.run_with_check([sys.executable, "-m", "pytest", "-v"] +
SOURCE_TO_CHECK, "Pytest failed.")
class Yapf(Precommit):
"""Checks that all provided Python files are unmodifed by the formatter."""
def __init__(self, files):
self.files = files
def run(self):
self.run_with_check([
sys.executable, "-m", "yapf", "--recursive", "--in-place",
"--parallel", "-vv"
] + self.files, "Yapf failed.")
# If Yapf made any changes, we fail the precommit to let the user review
# them.
self.check_for_modifications(
self.files, "Python formatter made modifications. " +
"Please stage them if OK.")
class Prettier(Precommit):
def __init__(self, files):
self.files = files
def run(self):
subprocess.run(["prettier", "--write", "--loglevel", "log"] +
self.files,
check=True,
shell=True)
self.check_for_modifications(
self.files, "Javascript formatter made modifications.")
class ClangFormat(Precommit):
"""Checks that all provided C++ files are unmodifed by the formatter."""
def __init__(self, files):
self.files = files
def run(self):
try:
self.run_with_check(["clang-format", "--version"], "Version")
self.run_with_check(["clang-format", "-i", "-style=file"] +
self.files, "Clang-format failed.")
except FileNotFoundError:
raise PrecommitWarning("clang-format not found.")
# If Clang made any changes, we fail the precommit to let the user review
# them.
self.check_for_modifications(
self.files,
"C++ formatter made modifications. " + "Please stage them if OK.")
def get_staged_files(pattern: str) -> List[str]:
"""Returns all staged files about to be committed."""
run = subprocess.run([
"git", "diff", "--cached", "--name-only", "--diff-filter=ACM", pattern
],
stdout=subprocess.PIPE)
assert run.returncode == 0
return [
file for file in run.stdout.decode("utf-8").split("\n")
if len(file) > 0 and not "third-party/" in file
]
def print_files(name: str, files: List[str]) -> None:
if not files:
return
print(Style.BRIGHT, "The following", Fore.YELLOW, name, Fore.WHITE,
"files will be checked:")
for f in files:
print(Fore.YELLOW, " ", f)
print(Style.RESET_ALL)
def install_if_needed() -> None:
precommit_hook = os.path.join(os.path.dirname(__file__), ".git", "hooks",
"pre-commit")
if os.path.isfile(precommit_hook):
return
with open(precommit_hook, "w") as f:
print("#!/bin/bash", file=f)
print("set -e", file=f)
print("pipenv run python precommit.py", file=f)
# Make the script executable. Required on Linux.
st = os.stat(precommit_hook)
os.chmod(precommit_hook, st.st_mode | stat.S_IEXEC)
print("Installed pre-commit hook in", precommit_hook)
def main():
if not os.path.isdir(".git"):
print("Need to run in the git root directory.")
sys.exit(1)
install_if_needed()
all_files = get_staged_files("*")
if not all_files:
print("No files to commit. OK.")
return
python_files = get_staged_files("*.py")
cpp_files = get_staged_files("*.cpp") + get_staged_files("*.h")
js_files = get_staged_files("*.js")
txt_files = get_staged_files("*.txt")
cmake_files = get_staged_files("*.cmake")
all_text_files = (python_files + cpp_files + js_files + txt_files +
cmake_files)
precommits: List[Precommit] = [
NoModifiedFiles(all_files),
LineEndings(all_text_files),
ValidUtf8(all_text_files),
UnixLineEndings(all_text_files),
]
if python_files:
precommits += [
Pyflakes(python_files),
MyPy(python_files),
PyTest(),
Yapf(python_files)
]
if cpp_files:
precommits += [ClangFormat(cpp_files)]
if js_files:
precommits += [Prettier(js_files)]
print(Style.BRIGHT, "Pre-commit run starting.")
print_files("Python", python_files)
print_files("C++", cpp_files)
print_files("Javascript", js_files)
for precommit in precommits:
try:
print(precommit.__class__.__name__, "...", end="")
sys.stdout.flush()
start_time = time.time()
precommit.run()
elapsed = time.time() - start_time
print(Fore.GREEN, Style.BRIGHT, "OK", Style.RESET_ALL,
"{:.1f}s.".format(elapsed))
except PrecommitWarning as warning:
print(Fore.YELLOW, Style.BRIGHT, "WARNING", Style.RESET_ALL,
warning.message)
except PrecommitFailure as failure:
print(Fore.RED, Style.BRIGHT, "FAIL")
print(failure.message, Style.RESET_ALL)
sys.exit(1)
if __name__ == "__main__":
main()