-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeliverable.py
More file actions
345 lines (307 loc) · 10.3 KB
/
Deliverable.py
File metadata and controls
345 lines (307 loc) · 10.3 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 7 18:19:06 2021
@author: dankovacevich
"""
from zipfile import ZipFile
import re
import py_compile
import os
from Requirements import *
#from Main import out_put
class Folder:
def __init__(self, foldername):
self.foldername = foldername + "/"
self.folderrequirements = []
self.folderoptional = []
def mustHave(self, file, *requirements):
self.folderrequirements.append([file])
for condition in requirements:
self.folderrequirements[-1].append(condition)
def mayHave(self, file, *requirements):
self.folderoptional.append([file])
for condition in requirements:
self.folderoptional[-1].append(condition)
class ZipFolder:
#-----constructor that creates array of file names-----
def __init__(self,zipfilename):
self.files = []
self.junk = []
self.requirements = []
self.optional = []
self.correct = []
self.errors = []
self.missing = []
self.zipfilename = zipfilename
self.errmsg = []
self.crrmsg = []
self.missng = []
for name in ZipFile(zipfilename,'r').namelist():
self.files.append(name)
#-------------adds must-have file to checklist----------------
def mustHave(self, name, *requirements):
self.requirements.append([name])
for condition in requirements:
self.requirements[-1].append(condition)
#-------------adds optional file to checklist------------------
def mayHave(self, name, *requirements):
self.optional.append([name])
for condition in requirements:
self.optional[-1].append(condition)
#--------prints all the contents of zipfolder----------
def printfiles(self):
for i in self.files:
print(i)
print('\n')
#----------checks all requirements in a folder----------
def checkfolderrequirements(self, folder, foldername, folderarray, types):
for k in range(0,len(folderarray)):
foundfile = 0
#checks requirements against actual zip contents
for l in range(0,len(self.files)):
#if finds match
if(re.match(foldername + folderarray[k][0], self.files[l])):
foundfile = 1
if(self.files[l].endswith(".py")):
stop = "now"
#only requirement is name
if(len(folderarray[k]) == 1):
if(types == 'r'):
self.correct.append(foldername)
elif(types == 'o'):
self.correct.append(fldrname)
else:
try:
self.foundfolderfilematch(self.files[l], folderarray[k], foldername, types)
except Exception as e:
print(e)
if(foundfile == 0 and types == 'r'):
self.missng.append(folder.folderrequirements[k][0])
#--loops through folder requirements if found foldermatch--
def foundfoldermatch(self, folder, types):
foldername = folder.foldername
self.errmsg = [foldername]
if(types == 'r'):
self.crrmsg = [foldername]
elif(types == 'o'):
fldrname = foldername.strip('/')
fldrname += " (optional)/"
self.crrmsg = [fldrname]
self.missng = [foldername]
foundanymissing = 0
self.checkfolderrequirements(folder, foldername, folder.folderrequirements, 'r')
self.checkfolderrequirements(folder, foldername, folder.folderoptional, 'o')
if(len(self.crrmsg) > 1):
self.correct.append(self.crrmsg)
if(len(self.errmsg) > 1 and types == 'r'):
self.errors.append(self.errmsg)
if(len(self.missng) > 1 and types == 'r'):
self.missing.append(self.missng)
#--loops through file requirements for file in a folder--
def foundfolderfilematch(self, file, filerequirements, foldername, types):
currentfile = file
founderror = 0
#loops through all the requirements of a required file and confirms them
for m in range(1, len(filerequirements)):
result = checkcondition(self.zipfilename, currentfile, filerequirements[m])
os.remove(currentfile)
if(result != 'correct'):
founderror = 1
self.errmsg.append(currentfile.strip(foldername))
self.errmsg.append(result)
if(founderror == 0):
if(types == 'r'):
self.crrmsg.append(currentfile.strip(foldername))
foundanycorrect = 1
elif(types == 'o'):
self.crrmsg.append(currentfile.strip(foldername) + " (optional)")
foundanycorrect = 1
#-----loops through requirements for individual file-----
def foundfilematch(self, file, types):
#only requirement is file name
if(len(file) == 1):
self.correct.append(file[0])
else:
errmsg = []
#loops through all the requirements of a required file and confirms them
for k in range(1,len(file)):
result = checkcondition(self.zipfilename, file[0], file[k])
os.remove(file[0])
if(result != 'correct'):
errmsg.append(result)
if(len(errmsg) == 0):
if(types == 'o'):
self.correct.append(file[0] + " (optional)")
else:
self.correct.append(file[0])
else:
self.errors.append([file[0]] + errmsg)
#--------------checks requirements--------------------
def checkrequirements(self, array, types):
for i in range(0, len(array)):
#loop through all files in zip to check for that requirement
foundmatch = 0
for j in range(0, len(self.files)):
#check if its a folder
if(isinstance(array[i][0], Folder)):
#check if found match of filename to requirement
if(array[i][0].foldername == self.files[j]):
foundmatch = 1
self.foundfoldermatch(array[i][0], types)
#remove from junk array
for l in range(len(self.files)):
if(self.files[l].startswith(array[i][0].foldername)):
self.junk[l] = 1
#not a folder
else:
#if file is a match
if(re.match(array[i][0], self.files[j])):
self.junk[j] = 1
foundmatch = 1
self.foundfilematch(array[i], types)
#file didn't match
if(foundmatch == 0 and types == 'r'):
if(isinstance(array[i][0], Folder)):
self.missing.append(array[i][0].foldername)
else:
self.missing.append(array[i][0])
def printhtml(self):
#Open html report and write opening tags, define style
wd = os.getcwd()
os.chdir(wd + '/templates' )
os.remove('Report.html')
f = open('Report.html','w')
message = """
<!DOCTYPE html>
<html>
<title>Deliverable Testing Tool </title>
<div>
<h1>Deliverable Testing Tool Report</h1>
</div>
<body>
"""
f.write(message)
message = """
<style>
head {color:#000000; font-family: Tahoma, sans-serif;}
body {background-color: #white; color:#000000; font-family: Tahoma, sans-serif;}
div {display: flex; flex-direction: column; justify-content: center; text-align: center;}
p {line-height: .6;}
.tab {display: inline-block; margin-left: 25px;}
</style>
"""
f.write(message)
#------------print correct deliverables---------------
f.write("<p>")
f.write("""
""")
f.write(" <b>Correct Deliverables:</b> <br></br>")
f.write("""
""")
for i in self.correct:
if(i[0].endswith('/') and len(i) > 1):
f.write(i[0] + "<br></br>")
f.write("""
""")
f.write("""
""")
for j in range(1,len(i)):
f.write("<span class=tab></span>" + i[j] + "<br></br>")
f.write("""
""")
# f.write("""
# """)
else:
f.write(i + "<br></br>" )
f.write("""
""")
f.write("</p>")
f.write("""
""")
#------------print incorrect deliverables---------------
f.write("<p>")
f.write("""
""")
f.write("<b>Errors:</b>" + "<br></br>")
f.write("""
""")
for i in self.errors:
if(i[0].endswith('/') and len(i) > 1):
f.write(i[0] + "<br></br>")
f.write("""
""")
f.write("""
""")
for j in range(1, len(i),2):
f.write("<span class=tab></span>" + i[j] + ": " + i[j+1] + "<br></br>")
f.write("""
""")
f.write("""
""")
else:
f.write(i[0] + ": " + i[1] + "<br></br>")
f.write("""
""")
f.write("</p>")
f.write("""
""")
#------------print missing deliverables---------------
f.write("<p>")
f.write("""
""")
f.write("<b>Missing Deliverables:</b> <br></br>")
f.write("""
""")
for i in self.missing:
if(i[0].endswith('/') and len(i) > 1 ):
f.write(i[0] + "<br></br>")
f.write("""
""")
f.write("""
""")
for j in range(1,len(i)):
f.write("<span class=tab></span>" + i[j] + "<br></br>")
f.write("""
""")
f.write("""
""")
else:
f.write(i + "<br></br>")
f.write("""
""")
f.write("</p>")
f.write("""
""")
#-------------------print junk--------------------
f.write("<p>")
f.write("""
""")
f.write( "<b>Junk Files:</b> <br></br>")
f.write("""
""")
for i in range(len(self.junk)):
if (self.junk[i] == 0):
f.write(self.files[i] + "<br></br>")
f.write("""
""")
f.write("</p>")
f.write("""
""")
#-----------------print ending tags----------------
message = """
</body>
</html>"""
f.write(message)
f.close()
os.chdir(wd)
#-----------------Prints Report--------------------
def report(self):
#initialize junk
self.junk = [0]*len(self.files)
#checks requirements with required flag
self.checkrequirements(self.requirements, 'r')
#checks requirements with optional flag
self.checkrequirements(self.optional, 'o')
#-------------------PRINT RESULT----------------------
self.printhtml()