-
Notifications
You must be signed in to change notification settings - Fork 1
/
code_check.py
executable file
·87 lines (63 loc) · 2.54 KB
/
code_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
#!/usr/bin/env python3
import argparse
import subprocess
import colorama
import polib
parser = argparse.ArgumentParser(description="Code checks")
parser.add_argument("--skip-flake", action="store_true")
parser.add_argument("--debug", action="store_true")
args = parser.parse_args()
DEBUG = args.debug
def cmd(line):
if DEBUG:
print(colorama.Style.DIM + "% " + line + colorama.Style.RESET_ALL)
try:
output = subprocess.check_output(line, shell=True).decode("utf-8")
if DEBUG:
print(colorama.Style.DIM + output + colorama.Style.RESET_ALL)
return output
except subprocess.CalledProcessError as e:
print(colorama.Fore.RED + e.stdout.decode("utf-8") + colorama.Style.RESET_ALL)
exit(1)
def status(line):
print(colorama.Fore.GREEN + f">>> {line}..." + colorama.Style.RESET_ALL)
def update_po_files():
def get_current_msgids():
pot = polib.pofile("locale/en_US/LC_MESSAGES/django.po")
return {e.msgid for e in pot if not e.fuzzy and not e.obsolete}
cmd(f"git restore --staged --worktree locale")
# get the current set of msgids
saved_msgids = get_current_msgids()
# re-extract locale files from source code
ignore_paths = ("env/*", "fabric/*", "media/*", "sitestatic/*", "static/*", "node_modules/*")
ignore_args = " ".join([f'--ignore="{p}"' for p in ignore_paths])
cmd(f"python manage.py makemessages -a -e haml,html,txt,py --no-location --no-wrap {ignore_args}")
# get the new set of msgids
actual_msgids = get_current_msgids()
added_msgids = actual_msgids.difference(saved_msgids)
removed_msgids = saved_msgids.difference(actual_msgids)
if DEBUG:
for mid in added_msgids:
print(f" + {repr(mid)}")
for mid in removed_msgids:
print(f" - {repr(mid)}")
# if there are no actual changes to msgids, revert
if not added_msgids and not removed_msgids:
cmd(f"git restore locale")
if __name__ == "__main__":
colorama.init()
status("Make any missing migrations")
cmd("python manage.py makemigrations")
status("Running isort")
cmd("isort temba")
status("Running black")
cmd("black --line-length=119 temba")
if not args.skip_flake:
status("Running flake8")
cmd("flake8")
# if any code changes were made, exit with error
if cmd("git diff temba locale"):
print("👎 " + colorama.Fore.RED + "Changes to be committed")
exit(1)
else:
print("👍 " + colorama.Fore.GREEN + "Code looks good. Make that PR!")