forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_metadata.py
191 lines (171 loc) · 6.89 KB
/
check_metadata.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os, fnmatch, sys
def checkFile(directory, filePattern):
filecount = 0;
for path, dirs, files in os.walk(os.path.abspath(directory)):
for filename in fnmatch.filter(files, filePattern):
filecount += 1
filepath = os.path.join(path, filename)
print("\nChecking File: " + filename + " in " + filepath)
with open(filepath) as f:
s = f.read()
words = s.split()
snippetStartCheck(words)
snippets = s.split('snippet-')
snippetAuthorCheck(snippets)
snippetServiceCheck(snippets)
snippetDescriptionCheck(snippets)
snippetTypeCheck(snippets)
snippetDateCheck(snippets)
snippetKeywordCheck(snippets)
print(str(filecount) + " files scanned in " + directory)
print("")
def checkFileStrings(directory, filePattern):
filecount = 0;
for path, dirs, files in os.walk(os.path.abspath(directory)):
for filename in fnmatch.filter(files, filePattern):
wordcount = 0;
filecount += 1
filepath = os.path.join(path, filename)
with open(filepath) as f:
s = f.read()
words = s.split()
snippetStartCheck(words)
for word in words:
checkStringLength(word, filename)
wordcount +=1;
f.close();
print("File: " + filename + " has been scanned. " + str(wordcount) + " words found.")
print("")
print(str(filecount) + " files scanned in " + directory)
print("")
def checkStringLength (word, filename):
length = len(word)
if length == 40 or length == 20:
sys.exit ("WARNING -- String found in " + filename + " \n" + word + " is " + str(length) + " characters long")
def snippetStartCheck(words):
#print (words)
snippetStart = 'snippet-start:['
snippetEnd = 'snippet-end:['
if any(snippetStart in word for word in words) :
matching = [s for s in words if snippetStart in s]
Endmatching = [s for s in words if snippetEnd in s]
#print(matching)
snippettags = []
for string in Endmatching:
snippettags += string.split(snippetEnd)
if '//' in snippettags: snippettags.remove('//')
if '#' in snippettags: snippettags.remove('#')
#print(snippettags)
#print(Endmatching)
for string in matching:
match = False
for end in snippettags:
if string.endswith(end):
match = True
#print("True: "+ string + " has matching end tag." )
if match == False:
sys.exit("ERROR -- " + string + "'s matching end tag not found.")
else:
#print("WARNING -- Snippet Start not detected")
return False
def snippetAuthorCheck(words):
author = 'sourceauthor:['
matching = [s for s in words if author in s]
if matching == []:
print("WARNING -- Missing snippet-sourceauthor:[Your Name]")
def snippetServiceCheck(words):
service = 'service:['
matching = [s for s in words if service in s]
if matching == []:
print("WARNING -- Missing snippet-service:[AWS service name]")
print("Find a list of AWS service names under AWS Service Namespaces in the General Reference Guide: https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html")
def snippetDescriptionCheck(words):
desc = 'sourcedescription:['
matching = [s for s in words if desc in s]
if matching == []:
print("WARNING -- Missing snippet-sourcedescription:[Filename demonstrates how to ... ]")
def snippetTypeCheck(words):
author = 'sourcetype:['
matching = [s for s in words if author in s]
containsType = False
if matching == []:
containsType = False
for match in matching:
if match.startswith('sourcetype:[full-example'):
containsType = True
break
elif match.startswith('sourcetype:[snippet'):
containsType = True
break
if not containsType:
print("WARNING -- Missing snippet-sourcetype:[full-example] or snippet-sourcetype:[snippet]")
def snippetDateCheck(words):
datetag = 'sourcedate:['
matching = [s for s in words if datetag in s]
if matching == []:
print("WARNING -- Missing snippet-sourcedate:[YYYY-MM-DD]")
def snippetKeywordCheck(words):
snippetkeyword = 'keyword:['
matching = [s for s in words if snippetkeyword in s]
# print(matching)
codeSample = [s for s in words if 'keyword:[Code Sample]\n' in s]
if not codeSample:
print("WARNING -- Missing snippet-keyword:[Code Sample]")
keywordServiceName(matching)
keywordLanguageCheck(matching)
keywordSDKCheck(matching)
def keywordServiceName(words):
containsServiceTag = False;
AWS = 'keyword:[AWS'
matching = [s for s in words if AWS in s]
if matching:
containsServiceTag = True;
Amazon = 'keyword:[Amazon'
matching = [s for s in words if Amazon in s]
if matching:
containsServiceTag = True;
if not containsServiceTag:
print("WARNING -- Missing snippet-keyword:[FULL SERVICE NAME]")
def keywordLanguageCheck(words):
languages = ['C++', 'C', '.NET', 'Go', 'Java', 'JavaScript', 'PHP', 'Python', 'Ruby','TypeScript' ]
containsLanguageTag = False;
for language in languages:
languagekeyword = [s for s in words if 'keyword:[' + language + ']' in s]
if languagekeyword:
containsLanguageTag = True;
break
if containsLanguageTag == False:
print("WARNING -- Missing snippet-keyword:[Language]")
print("Options include:")
print(languages)
def keywordSDKCheck(words):
sdkVersions = ['AWS SDK for PHP v3', 'AWS SDK for Python (Boto3)', 'CDK V0.14.1' ]
containsSDKTag = False;
for sdk in sdkVersions:
sdkkeyword = [s for s in words if 'keyword:[' + sdk + ']']
if sdkkeyword:
containsSDKTag = True;
break
if containsSDKTag == False:
print("WARNING -- Missing snippet-keyword:[SDK Version used]")
print("Options include:")
print(sdkVersions)
print ('----------\n\nRun Tests\n')
print ('----------\n\nAWS SDK for C++\n')
checkFile( './', '*.cpp')
print ('----------\n\nAWS SDK for .NET\n')
checkFile( './', '*.cs')
print ('----------\n\nAWS SDK for Go\n')
checkFile( './', '*.go')
print ('----------\n\nAWS SDK for Java\n')
checkFile( './', '*.java')
print ('----------\n\nAWS SDK for JavaScript\n')
checkFile( './', '*.js')
print ('----------\n\nAWS SDK for PHP\n')
checkFile( './', '*.php')
print ('----------\n\nAWS SDK for Python\n')
checkFile( './', '*.py')
print ('----------\n\nAWS SDK for Ruby\n')
checkFile( './', '*.rb')
print ('----------\n\nAWS SDK for TypeScript\n')
checkFile( './', '*.ts')