-
Notifications
You must be signed in to change notification settings - Fork 34
/
lint.py
76 lines (62 loc) · 2.77 KB
/
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
#!/usr/bin/env python
import sys
import yaml
import re
CONFIG = './data/main.yml'
REG_IDS = re.compile('.*-id-[0-9]+.*')
MISSING_SPACE_AFTER_PARAMS = re.compile('.*?[a-z]\[.*?\][a-z].*')
errors = []
def add_error(s):
errors.append(s)
def lint(appName):
location = './data/'
appFile = appName + '.yml'
fullPathname = location + appFile
with open(fullPathname, 'r') as data:
try:
yml = yaml.safe_load(data)
except Exception as e:
print('Error: Unable to parse yaml config file ({})'.format(CONFIG))
print('Details: """')
print(e)
print('"""')
sys.exit(1)
for group_name, group in yml.items():
if not 'pages' in group:
add_error('Error: no *pages* section found in group "{}"'.format(group_name))
# if not 'features' in group:
# print('Error: no *features* section found in group "{}"'.format(group_name))
# sys.exit(1)
if 'features' in group:
for feature_name, feature in group['features'].items():
if feature_name == '_scope':
continue
if 'selectors' not in feature:
add_error('Error: found a feature without a "selectors" array ({} -> {})'.format(group_name, feature_name))
for selector in feature['selectors']:
if MISSING_SPACE_AFTER_PARAMS.match(selector):
add_error('Error: found a selector that has no space after a param block "example: [...]button" ({} -> {} -> {})'.format(group_name, feature_name, selector))
if REG_IDS.match(selector):
add_error('Error: found an id-NN pattern in a feature selector ({} -> {} -> {})'.format(group_name, feature_name, selector))
for page_name, page in group['pages'].items():
if page_name == 'All':
add_error('Error: found a page name of "All" the standard is "All pages" ({} -> {})'.format(group_name, page_name))
for url in page['url_rules']:
if url.startswith('//console.redhat.com'):
add_error("""Error: invalid page URL prefix in the group "{}" on the page "{}"
Found: {}
Expected: /*'
We automatically add the //console.redhat.com""".format(group_name, page_name, url))
if len(errors):
print('Custom linter found {} error(s)\n'.format(len(errors)))
for num, error in enumerate(errors, start = 1):
print('#### {} ####'.format(num))
print(error)
print()
sys.exit(1)
with open('./data/main.yml', 'r') as data:
yml = yaml.safe_load(data)
ymlArray = yml["applications"]
for app in ymlArray:
print('linting ', app)
lint(app)