-
Notifications
You must be signed in to change notification settings - Fork 65
/
DocLinkValidity.py
59 lines (49 loc) · 1.87 KB
/
DocLinkValidity.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
import os
import sys
import json
import re
from urllib.request import Request, urlopen
import urllib.error
from utils.RuleReader import RuleReader
SERVICE_FOLDER_PATH = os.getcwd() + '/services'
if __name__ == "__main__":
attrName = 'ref'
rr = RuleReader(SERVICE_FOLDER_PATH)
ruleList = rr.getRulesAttr(attrName)
invalidRefDict = {
'NoRef': [],
'InvalidSyntax': []
}
for rule in ruleList:
for ref in ruleList[rule][attrName]:
if ref.strip() == '':
invalidRefDict['NoRef'].append(rule)
continue
output = re.search(r'<(.*)>', ref)
if output is None:
invalidRefDict['InvalidSyntax'].append(rule)
continue
try:
url = output.group(1)
if url.strip() == '':
invalidRefDict['NoRef'].append(rule)
continue
print('.', end="", flush=True)
conn = Request(url)
conn.add_header('User-Agent', 'aws-cli')
resp = urlopen(conn, timeout=10)
if resp.getcode() != 200:
if e.code not in invalidRefDict:
invalidRefDict[resp.getcode()] = []
invalidRefDict[resp.getcode()].append({rule: url})
except urllib.error.HTTPError as e:
if e.code not in invalidRefDict:
invalidRefDict[e.code] = []
invalidRefDict[e.code].append({rule: url})
except Exception as e:
print(e)
if 'UnknownError' not in invalidRefDict:
invalidRefDict['UnknownError'] = []
invalidRefDict['UnknownError'].append({rule: e})
print('')
print(invalidRefDict)