-
Notifications
You must be signed in to change notification settings - Fork 1
/
precommit.py
executable file
·108 lines (85 loc) · 3.13 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
#!/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
# yapf: disable
source_files = (
sorted((repo_root / "datetime_glob").glob("**/*.py")) +
sorted((repo_root / "tests").glob("**/*.py")))
# yapf: enable
if overwrite:
print('Removing trailing whitespace...')
for pth in source_files:
pth.write_text(re.sub(r'[ \t]+$', '', pth.read_text(), flags=re.MULTILINE))
print("YAPF'ing...")
yapf_targets = ["tests", "datetime_glob", "setup.py", "precommit.py"]
if overwrite:
# yapf: disable
subprocess.check_call(
["yapf", "--in-place", "--style=style.yapf", "--recursive"] +
yapf_targets,
cwd=str(repo_root))
# yapf: enable
else:
# yapf: disable
subprocess.check_call(
["yapf", "--diff", "--style=style.yapf", "--recursive"] +
yapf_targets,
cwd=str(repo_root))
# yapf: enable
print("Mypy'ing...")
subprocess.check_call(["mypy", "--strict", "datetime_glob", "tests"], cwd=str(repo_root))
print("Isort'ing...")
# yapf: disable
isort_files = map(str, source_files)
# yapf: enable
# yapf: disable
subprocess.check_call(
["isort", "--project", "datetime_glob", '--line-width', '120'] +
([] if overwrite else ['--check-only']) +
[str(pth) for pth in source_files])
# yapf: enable
print("Pydocstyle'ing...")
subprocess.check_call(["pydocstyle", "datetime_glob"], cwd=str(repo_root))
print("Pylint'ing...")
subprocess.check_call(["pylint", "--rcfile=pylint.rc", "tests", "datetime_glob"], cwd=str(repo_root))
print("Testing...")
env = os.environ.copy()
env['ICONTRACT_SLOW'] = 'true'
# yapf: disable
subprocess.check_call(
["coverage", "run",
"--source", "datetime_glob",
"-m", "unittest", "discover", "tests"],
cwd=str(repo_root),
env=env)
# yapf: enable
subprocess.check_call(["coverage", "report"])
print("Doctesting...")
doctest_files = ([repo_root / "README.rst"] + sorted((repo_root / "datetime_glob").glob("**/*.py")))
for pth in doctest_files:
subprocess.check_call([sys.executable, "-m", "doctest", str(pth)])
print("Checking setup.py sdist ...")
subprocess.check_call([sys.executable, "setup.py", "sdist"], cwd=str(repo_root))
print("Checking with twine...")
subprocess.check_call(["twine", "check", "dist/*"], cwd=str(repo_root))
return 0
if __name__ == "__main__":
sys.exit(main())