Skip to content

Commit a5ffdff

Browse files
committed
New python versions with more checks.
1 parent 87b38d9 commit a5ffdff

20 files changed

+493
-151
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.idea/
2+
/venv/
3+
4+
__pycache__
5+
.pyc

hooks/delegate.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
import os
5+
from contextlib import suppress
6+
7+
from natsort import natsorted
8+
import envoy
9+
try:
10+
import colorama
11+
except ImportError:
12+
colorama = None
13+
14+
# Possible git hooks. (from http://githooks.com/)
15+
GIT_HOOKS = """\
16+
applypatch-msg
17+
pre-applypatch
18+
post-applypatch
19+
pre-commit
20+
prepare-commit-msg
21+
commit-msg
22+
post-commit
23+
pre-rebase
24+
post-checkout
25+
post-merge
26+
pre-receive
27+
update
28+
post-receive
29+
post-update
30+
pre-auto-gc
31+
post-rewrite
32+
pre-push""".split("\n")
33+
34+
BULLET_POINTS = "*-+=#~"
35+
36+
PASS = " ✓ PASS"
37+
WARN = " ! WARN"
38+
FAIL = " ✗ FAIL"
39+
40+
41+
def bullet(depth):
42+
return BULLET_POINTS[depth % len(BULLET_POINTS)]
43+
44+
45+
def title(name):
46+
return " ".join(name.split("-")[1:]).capitalize() + "."
47+
48+
49+
def print_item(prefix, depth, name, state, suffix):
50+
point = bullet(depth)
51+
spaces = " " * (depth * 4)
52+
print("{}{} {} {:<60} {}{}".format(
53+
prefix, spaces, point, title(name), state, suffix))
54+
55+
56+
def indent(text, depth):
57+
padding = " " * (depth * 4)
58+
return padding + "\n{0}".format(padding).join(text.split("\n")).rstrip()
59+
60+
61+
if __name__ == "__main__":
62+
if colorama:
63+
colorama.init()
64+
65+
script_path = os.path.dirname(os.path.realpath(__file__))
66+
67+
hook = os.path.basename(sys.argv[0])
68+
if hook not in GIT_HOOKS:
69+
print("This script must be run as a hook.", file=sys.stderr)
70+
sys.exit(2)
71+
72+
fail = False
73+
74+
print(" {0} {1} hooks".format(bullet(0), hook.capitalize()))
75+
76+
fail_prefix = ""
77+
warn_prefix = ""
78+
pass_prefix = ""
79+
suffix = ""
80+
if colorama:
81+
fail_prefix = colorama.Fore.RED
82+
warn_prefix = colorama.Fore.YELLOW
83+
pass_prefix = colorama.Fore.GREEN
84+
suffix = colorama.Style.RESET_ALL
85+
86+
scripts_path = os.path.join(script_path, hook + ".d")
87+
for subdir, dirs, files in natsorted(os.walk(scripts_path)):
88+
groups = os.path.relpath(subdir, scripts_path).split(os.path.sep)
89+
if any(not group[0].isdigit() for group in groups):
90+
continue
91+
depth = len(groups)
92+
if files and depth > 0:
93+
print_item("", depth, groups[-1], "", "")
94+
for filename in natsorted(files):
95+
if not filename[0].isdigit():
96+
continue
97+
path = os.path.join(subdir, filename)
98+
file_depth = depth + 1
99+
result = envoy.run(path, timeout=2)
100+
out_depth = file_depth + 1
101+
out = indent(result.std_out, out_depth) if result.std_out else None
102+
err = indent(result.std_err, out_depth) if result.std_err else None
103+
if result.status_code:
104+
fail = True
105+
print_item(fail_prefix, file_depth, filename, FAIL, suffix)
106+
if err:
107+
print(fail_prefix + err + suffix)
108+
if out:
109+
print(fail_prefix + out + suffix)
110+
else:
111+
if not err:
112+
print_item(pass_prefix, file_depth, filename, PASS, suffix)
113+
else:
114+
print_item(warn_prefix, file_depth, filename, WARN, suffix)
115+
if err:
116+
print(warn_prefix + err + suffix)
117+
if out:
118+
if not err:
119+
print(out)
120+
else:
121+
print(warn_prefix + out + suffix)
122+
123+
if fail:
124+
sys.exit(1)
125+
else:
126+
sys.exit(0)

hooks/post-checkout

Lines changed: 0 additions & 4 deletions
This file was deleted.

hooks/post-merge

Lines changed: 0 additions & 4 deletions
This file was deleted.

hooks/pre-commit

Lines changed: 0 additions & 142 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
from metadata import *
6+
7+
PROBLEM = "'{}.meta' has been added but the asset it is for is missing."
8+
FIX = "To fix, add '{0}' or do not add '{0}.meta'."
9+
10+
if __name__ == "__main__":
11+
added_assets = set()
12+
added_metadata = set()
13+
14+
for path, _ in staged_paths(change_type="A"):
15+
if path.startswith(ASSETS_DIR):
16+
if path.endswith(META_EXT):
17+
added_metadata.add(path[:-len(META_EXT)])
18+
else:
19+
added_assets.add(path)
20+
21+
missing_metadata = added_metadata - added_assets
22+
23+
for path in missing_metadata:
24+
print(PROBLEM.format(path), file=sys.stderr)
25+
print(FIX.format(path), file=sys.stderr)
26+
27+
if missing_metadata:
28+
sys.exit(1)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
from metadata import *
6+
7+
PROBLEM = "'{}' has been added but its metadata file is missing."
8+
FIX = "To fix, add '{0}.meta' or do not add '{0}'."
9+
10+
if __name__ == "__main__":
11+
added_assets = set()
12+
added_metadata = set()
13+
14+
for path, _ in staged_paths(change_type="A"):
15+
if path.startswith(ASSETS_DIR):
16+
if path.endswith(META_EXT):
17+
added_metadata.add(path[:-len(META_EXT)])
18+
else:
19+
added_assets.add(path)
20+
21+
missing_metadata = added_assets - added_metadata
22+
23+
for path in missing_metadata:
24+
print(PROBLEM.format(path), file=sys.stderr)
25+
print(FIX.format(path), file=sys.stderr)
26+
27+
if missing_metadata:
28+
sys.exit(1)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
from metadata import *
6+
7+
PROBLEM = "'{}' has been deleted but its metadata file has not."
8+
FIX = "To fix, delete '{0}.meta' or do not delete '{0}'."
9+
10+
if __name__ == "__main__":
11+
deleted_assets = set()
12+
deleted_metadata = set()
13+
14+
for path, _ in staged_paths(change_type="D"):
15+
if path.startswith(ASSETS_DIR):
16+
if path.endswith(META_EXT):
17+
deleted_metadata.add(path[:-len(META_EXT)])
18+
else:
19+
deleted_assets.add(path)
20+
21+
redundant_metadata = deleted_assets - deleted_metadata
22+
23+
for path in redundant_metadata:
24+
print(PROBLEM.format(path), file=sys.stderr)
25+
print(FIX.format(path), file=sys.stderr)
26+
27+
if redundant_metadata:
28+
sys.exit(1)

0 commit comments

Comments
 (0)