-
Notifications
You must be signed in to change notification settings - Fork 1
/
precommit.py
executable file
·137 lines (113 loc) · 4 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
#!/usr/bin/env python3
"""Run precommit checks on the repository."""
import argparse
import os
import pathlib
import re
import subprocess
import sys
def main() -> int:
"""Execute the main routine."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--overwrite",
help="Overwrites the unformatted source files with the "
"well-formatted code in place. If not set, "
"an exception is raised if any of the files do not conform "
"to the style guide.",
action='store_true')
args = parser.parse_args()
overwrite = bool(args.overwrite)
repo_root = pathlib.Path(__file__).parent
if overwrite:
print('Removing trailing whitespace...')
for pth in (sorted((repo_root / "mapry").glob("**/*.py")) + sorted(
(repo_root / "tests").glob("**/*.py"))):
pth.write_text(
re.sub(r'[ \t]+$', '', pth.read_text(), flags=re.MULTILINE))
print("YAPF'ing...")
if overwrite:
# yapf: disable
subprocess.check_call([
"yapf", "--in-place", "--style=style.yapf", "--recursive",
"tests", "mapry", "setup.py", "precommit.py"],
cwd=str(repo_root))
# yapf: enable
else:
# yapf: disable
subprocess.check_call([
"yapf", "--diff", "--style=style.yapf", "--recursive",
"tests", "mapry", "setup.py", "precommit.py"],
cwd=str(repo_root))
# yapf: enable
print("Mypy'ing...")
subprocess.check_call(["mypy", "--strict", "mapry", "tests"],
cwd=str(repo_root))
print("Isort'ing...")
# yapf: disable
isort_files = map(
str,
sorted((repo_root / "mapry").glob("**/*.py")) +
sorted((repo_root / "tests").glob("**/*.py")))
# yapf: enable
if overwrite:
# yapf: disable
cmd = [
"isort", "--balanced", "--multi-line", "4", "--line-width", "80",
"--dont-skip", "__init__.py", "--project", "mapry"
]
# yapf: enable
cmd.extend(isort_files)
subprocess.check_call(cmd)
else:
# yapf: disable
cmd = [
"isort",
"--check-only",
"--diff",
"--balanced",
"--multi-line", "4",
"--line-width", "80",
"--dont-skip", "__init__.py",
"--project", "mapry"
]
# yapf: enable
cmd.extend(isort_files)
subprocess.check_call(cmd)
print("Pydocstyle'ing...")
subprocess.check_call(["pydocstyle", "mapry"], cwd=str(repo_root))
print("Pylint'ing...")
subprocess.check_call(["pylint", "--rcfile=pylint.rc", "tests", "mapry"],
cwd=str(repo_root))
print("Testing...")
env = os.environ.copy()
env['ICONTRACT_SLOW'] = 'true'
# yapf: disable
subprocess.check_call(
["coverage", "run",
"--source", "mapry",
"-m", "unittest", "discover", "tests"],
cwd=str(repo_root),
env=env)
# yapf: enable
subprocess.check_call(["coverage", "report"])
print("Doctesting...")
subprocess.check_call(
[sys.executable, "-m", "doctest",
str(repo_root / "README.rst")])
for pth in sorted((repo_root / "mapry").glob("**/*.py")):
# ``__main__.py``'s cause doctest to go banana, so we need to skip them;
# see https://stackoverflow.com/questions/58731519/doctest-fails-on-main-py
if pth.name == '__main__.py':
continue
subprocess.check_call([sys.executable, "-m", "doctest", str(pth)])
print("pyicontract-lint'ing...")
for pth in sorted((repo_root / "mapry").glob("**/*.py")):
subprocess.check_call(["pyicontract-lint", str(pth)])
print("Checking with twine ...")
subprocess.check_call([sys.executable, "setup.py", "sdist"],
cwd=str(repo_root))
subprocess.check_call(["twine", "check", "dist/*"], cwd=str(repo_root))
return 0
if __name__ == "__main__":
sys.exit(main())