-
Notifications
You must be signed in to change notification settings - Fork 0
/
.check.py
executable file
·67 lines (50 loc) · 1.46 KB
/
.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
#!/usr/bin/env python3
from pathlib import Path
import re
import sys
exit_code = 0
key_pattern = re.compile('key\s*"(?P<key>[a-f0-9]{64})";');
ANSI_COLOR_ERR = "\x1b[31m"
ANSI_COLOR_WARN = "\x1b[33m"
ANSI_COLOR_OK = "\x1b[32m"
ANSI_COLOR_RESET = "\x1b[0m"
def error(*arg):
print(ANSI_COLOR_ERR, *arg, file=sys.stderr,
end='%s\n' % ANSI_COLOR_RESET)
def warn(*arg):
print(ANSI_COLOR_WARN, *arg, file=sys.stderr,
end='%s\n' % ANSI_COLOR_RESET)
def ok(*arg):
print(ANSI_COLOR_OK, *arg, file=sys.stderr,
end='%s\n' % ANSI_COLOR_RESET)
def key_from_file(fn):
with open(fn, 'r', encoding='utf-8') as handle:
for line in handle:
line = line.strip()
if line.startswith('#'):
continue
if line.lower().startswith('key'):
return key_pattern.match(line)
return False
keys = {}
root = Path('.')
for fn in root.iterdir():
fn = str(fn)
if fn.startswith('.'):
continue
data = key_from_file(fn)
if not data:
error('{fn}: has no (or no valid) key'.format(fn=fn))
exit_code += 1
continue
keys.setdefault(data.group('key'), set()).add(fn)
for k, v in keys.items():
if len(v) > 1:
exit_code += 1
error('duplicate key "{key}" in files: {files}'.format(
key=k, files=', '.join(v)))
if exit_code > 0:
error("{n} errors".format(n=exit_code))
else:
ok("everything ok")
sys.exit(exit_code)