-
Notifications
You must be signed in to change notification settings - Fork 86
/
grep-lint.py
94 lines (76 loc) · 3.22 KB
/
grep-lint.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
import subprocess
import sys
def get_command(branch=None):
arguments_list = ["git", "grep", "-P", rule_regex]
if branch is not None:
arguments_list.append(branch)
arguments_list.append("--")
arguments_list.append("scripts")
return arguments_list
rules_to_check = [
{
'name': 'Do not use `translate` filter anymore. Translation strings can\'t be extracted when the filter is used.',
'perl_regex': '\|\s*?translate',
'tolerance': True
},
{
'name': 'Do not use angularjs for views anymore. Use React components and use `reactToAngular1` if you need to use React components inside existing angular templates.',
# must match:
# template: 'test.html'
# must not match:
# template: 'test', a: '.html'
'perl_regex': 'template\s*?:\s*[\'|"|`].+?\.html[\'|"|`]',
'tolerance': True
},
{
'name': 'Do not use angularjs for views anymore. Use React components and use `reactToAngular1` if you need to use React components inside existing angular templates.',
# must match:
# templateUrl: 'test.html'
# must not match:
# templateUrl: 'test', a: '.html'
'perl_regex': 'templateUrl\s*?:\s*[\'|"|`].+?\.html[\'|"|`]',
'tolerance': True
},
# {
# 'name': 'Do not use angularjs for views anymore. Use React components and use `reactToAngular1` if you need to use React components inside existing angular templates.',
# # must match:
# # template: '<'
# # must not match:
# # template: 'a', b: '<'
# 'perl_regex': 'template\s*?:\s*[\'|"|`][^\'|"|`]*<[^\'|"|`]*[\'|"|`]',
# 'tolerance': True
# },
]
any_rule_violated = False
for rule in rules_to_check:
rule_regex = rule['perl_regex']
rule_name = rule['name']
# will not error if the amount of violations is less than on develop
rule_tolerance = rule['tolerance']
try:
violations_count_develop = len(
subprocess.check_output(get_command("develop")).decode('utf-8').splitlines()
)
except subprocess.CalledProcessError as e:
# ignore exception if grep simply didn't find matches
if len(e.output) != 0:
raise e
else:
violations_count_develop = 0
try:
violations = subprocess.check_output(get_command(), stderr=subprocess.STDOUT).decode('utf-8')
except subprocess.CalledProcessError as e:
# ignore exception if grep simply didn't find matches
if len(e.output) != 0:
raise e
else:
violations = ''
violations_count = len(violations.splitlines())
if (violations_count > 0 and rule_tolerance is False) or violations_count > violations_count_develop:
any_rule_violated = True
print('\n\n' + violations + '\n\n')
print("GREP-LINT RULE VIOLATED! '" + rule_name + "'")
print("Rule regex: `" + rule_regex + "`")
if rule_tolerance is True:
print('Tolerance is enabled, but ' + str(violations_count) + ' violations were found in the working while there only are ' + str(violations_count_develop) + ' violations on develop. See grep-lint.py for details.')
sys.exit(1 if any_rule_violated else 0)