This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_push.py
executable file
·70 lines (52 loc) · 1.58 KB
/
pre_push.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
#!/usr/bin/env python3
"""Run static analysis on the project."""
import sys
from os import path
from shutil import rmtree
from subprocess import CalledProcessError, check_call
from tempfile import mkdtemp
current_directory = path.abspath(path.join(__file__, ".."))
def do_process(args, shell=False):
"""Run program provided by args.
Return True on success.
Output failed message on non-zero exit and return False.
Exit if command is not found.
"""
print(f"Running: {' '.join(args)}")
try:
check_call(args, shell=shell)
except CalledProcessError:
print(f"\nFailed: {' '.join(args)}")
return False
except Exception as exc:
sys.stderr.write(f"{str(exc)}\n")
sys.exit(1)
return True
def run_static():
"""Runs static tests.
Returns a statuscode of 0 if everything ran correctly. Otherwise, it will return
statuscode 1
"""
success = True
# Formatters
success &= do_process(["black", "."])
success &= do_process(["isort", "."])
# Linters
success &= do_process(["flake8", "--exclude=.eggs,build,docs,.venv*,env*"])
tmp_dir = mkdtemp()
try:
success &= do_process(["sphinx-build", "-W", "--keep-going", "docs", tmp_dir])
finally:
rmtree(tmp_dir)
return success
def main():
success = True
try:
success &= run_static()
except KeyboardInterrupt:
return 1
return int(not success)
if __name__ == "__main__":
exit_code = main()
print("\npre_push.py: Success!" if not exit_code else "\npre_push.py: Fail")
sys.exit(exit_code)