-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrepo_check.py
124 lines (102 loc) · 2.8 KB
/
repo_check.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
from pathlib import Path
from subprocess import run
import sys
TASKS = {}
def task(func):
TASKS[func.__name__] = func
return func
@task
def build_docs():
print("========== Generating docs...")
proc = run(["sphinx-build", ".", "docs", "-c", "docs"])
if proc.returncode != 0:
print("sphinx-build failed.")
sys.exit(1)
@task
def fix_crlf():
print("========== Removing CRLF...")
def helper(path):
path_str = str(path)
if any(
token in path_str
for token in (".git", ".pytest_cache", "build", "dist", ".png", ".mid")
):
return
if path.is_dir():
for subpath in path.iterdir():
helper(subpath)
else:
try:
path.write_text(path.read_text().replace("\r\n", "\n"))
except UnicodeDecodeError:
pass
helper(Path("."))
@task
def check_clean():
print("========== Checking if anything changed...")
porcelain_after = (
run(["git", "status", "--porcelain"], capture_output=True)
.stdout.decode("ascii", errors="ignore")
.strip()
)
if porcelain_after:
print(porcelain_after)
run(["git", "status", "-vvv"])
print(
"Repo is dirty.\n"
"If this is run locally, please commit the files that were updated.\n"
"If this is in CI, please run python repo_check.py locally and commit the changes."
)
sys.exit(1)
@task
def format():
print("========== Formatting python code...")
proc = run(["black", "."])
if proc.returncode != 0:
print("black failed.")
sys.exit(1)
@task
def pytest():
print("========== Checking if tests pass...")
proc = run(["pytest"])
if proc.returncode != 0:
print("pytest failed.")
sys.exit(1)
@task
def pyflakes():
print("========== Running pyflakes...")
proc = run(["pyflakes", "jchord", "test", "examples"])
if proc.returncode != 0:
print("pyflakes failed.")
sys.exit(1)
@task
def doctest():
print("========== Checking if doctests pass...")
proc = run(
[
"python",
"-m",
"doctest",
"-v",
"jchord/core.py",
"jchord/chords.py",
"jchord/progressions.py",
]
)
if proc.returncode != 0:
print("pytest failed.")
sys.exit(1)
if __name__ == "__main__":
git = "git" in sys.argv
fix_crlf()
if git:
check_clean()
pyflakes()
format()
pytest()
doctest()
build_docs()
fix_crlf()
if git:
check_clean()
print("OK, everything is up to date")