-
Notifications
You must be signed in to change notification settings - Fork 9
/
common.py
87 lines (71 loc) · 2.39 KB
/
common.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
#-*- coding:utf-8 -*-
from os import path, system
from sh import awk, grep, cat, mkdir, rm
from termcolor import colored
from config import config, except_paths
def get_commit_errors(file_type, function):
checkable = True
if file_type == "js":
checkable = config.getboolean("commit", "CHECK_JAVASCRIPT")
elif file_type == "php":
checkable = config.getboolean("commit", "CHECK_PHP")
if not checkable:
return None
files = _get_commit_files(file_type)
if not files:
return None
errors = []
for file_path in files:
if path.exists(file_path):
file_error = function(file_path)
if file_error:
errors.append(file_error)
if errors:
errors = colored(
"There are some errors in below %s files:\n\n" % file_type, "magenta"
) + "\n".join(errors).strip("\n")
return errors
def get_receive_errors(rev_old, rev_new, file_type, function):
checkable = True
if file_type == "js":
checkable = config.getboolean("receive", "CHECK_JAVASCRIPT")
elif file_type == "php":
checkable = config.getboolean("receive", "CHECK_PHP")
files = _get_receive_files(rev_old, rev_new, file_type)
if not files:
return None
tmp_dir = config.get("receive", "TMP_DIR")
errors = []
for file_path in files:
mkdir("-p", "/".join((tmp_dir + file_path).split("/")[:-1]))
system("git show %s:%s > %s" % (rev_new, file_path, tmp_dir + file_path))
if path.exists(tmp_dir + file_path):
file_error = function(tmp_dir + file_path)
if file_error:
errors.append(file_path + file_error)
rm("-rf", tmp_dir)
return "\n".join(errors)
def _get_commit_files(file_type):
system("git diff --cached --name-status > /tmp/git_hook")
return _get_files(file_type, 2)
def _get_receive_files(rev_old, rev_new, file_type):
system("git diff-tree -r %s..%s > /tmp/git_hook" % (rev_old, rev_new))
return _get_files(file_type, 6)
def _get_files(file_type, file_index):
files = awk(
grep(
cat("/tmp/git_hook"), "-P", "(A|M).*.%s$" % file_type,
_ok_code = [0, 1]
),
"{print $%s}" % file_index, _iter = True
)
if not files:
return None
exten = ".%s" % file_type
files = [file_path[:file_path.rindex(exten) + len(exten)] for file_path in files]
if except_paths:
for except_path in except_paths:
for file_path in files:
if file_path.startswith(except_path):
files.remove(file_path)
return files