-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeleniumIniDriver.py
471 lines (405 loc) · 15.8 KB
/
SeleniumIniDriver.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import errors
import os
import ast
from ConfigParser import RawConfigParser
import Template
from multiprocessing import Process, Pool
def temp_to_orig_section(temp_sec):
"""
Change a temp section into its original.
If it is its original it will return what was given.
:param temp_sec: temporary section created by 'create_temp_batch'
:return: orig section
"""
section = temp_sec
if temp_sec.startswith(('1', '2', '3', '4', '5', '6', '7', '8', '9')) and '~|~' in temp_sec:
# This is a temp section
section = temp_sec.split('~|~', 1)[1]
return section
def func_run_dynamic(input_file, dynamic_dic, exclude, pprint):
"""
Execute one dynamic template
:param input_file: (string) The template file name
:param dynamic_dic: (dict) The dictionary of the dynamic variables
:return:
"""
new_template_filename = create_dynamic_template(input_file, dynamic_dic)
t = Template.Template()
t.file_path = new_template_filename
t.load_sections()
t.set_execute_order()
t.start_driver()
report = t.run()
if pprint:
t.pprint(exclude_none=exclude)
return t
def create_dynamic_template(fname, dynamic_dic):
"""
Create a dynamic template by filling in the dynamic variables
:param fname: (string) The name of the dynamic template file
:param dynamic_dic: (dict) A dictionary of dynamic variables
:return:
"""
# open the template and find lines with dynamic variables
# Once variables are found replace them with the designated value
f = open(fname, 'r')
orig_lines = f.readlines()
f.close()
new_lines = []
for line in orig_lines:
new_line = line
for key in dynamic_dic:
if "<{}>".format(key) in line:
new_line = line.replace("<{}>".format(key), dynamic_dic[key])
new_lines.append(new_line)
# Creating the new template file name
new_fname = ""
for key in dynamic_dic:
new_fname += "{}-".format(str(dynamic_dic[key]).replace('/', '|'))
new_fname += fname
if os.path.isfile(new_fname):
raise errors.DynamicTemplateAlreadyExistsException("Dynamic Template '{}' already exists.".format(new_fname),9053)
else:
# Creating the new template file with dynamic variables included
f = open(new_fname, 'a')
for line in new_lines:
f.write(line)
f.close()
return new_fname
class SeleniumIniDriver():
"""
The Driver to SeleniumINI
Uses the template class to create and manage templates
Also adds functionality for dynamic templates
"""
def __init__(self):
self.batch = False
self.concurrent = False
self.dynamic = False
self.exclude = False
self.input_file = False
self.output_file = False
self.pprint = False
self.templates = []
self.input_files = []
self.temp_files = []
self.keep_temps = False
self.easy_report = False
self.no_temp = False
##################################
# Check Methods #
##################################
def check_input_file(self):
"""
Check if the input file was given, exists and is a ini file.
:return: N/A
"""
if self.input_file is False:
raise errors.NoINITemplateGivenException("input-file argument cannot be empty.", 9011)
if not os.path.isfile(self.input_file):
raise errors.TemplateINIFileDoesNotExistException("input-file '{}' does not exist.".format(self.input_file), 9012)
if not self.input_file.endswith(".ini"):
raise errors.TemplateFileIsNotAnINIFileException("input-file '{}' is not an INI file.".format(self.input_file), 9013)
def check_output_file(self):
"""
Check if the output file exists and is a valid path.
:return: N/A
"""
if self.output_file is not False:
if os.path.isfile(self.output_file):
raise errors.OutputFileAlreadyExistsException("output-file '{}' already exists.".format(self.output_file), 9021)
try:
f = open(self.output_file, 'w')
except OSError:
raise errors.InvalidOutputFilePathException("output-file '{}' is not a valid path.".format(self.output_file), 9022)
f.close()
def check_batch(self):
"""
Check if the batch file given follows the guidelines and all of the template files in the batch file actually exsist.
This method also populate the self.input_files with dictionaries contains the template filename and attribute associated with it
:return:
"""
if self.batch is not False:
if not os.path.isfile(self.input_file):
raise errors.BatchFileDoesNotExistException("input-file for batch '{}' does not exist.".format(self.input_file), 9031)
self.input_file = self.create_temp_batch(self.input_file)
config = RawConfigParser()
config.read(self.input_file)
sections = config._sections
for sec in sections:
if not os.path.isfile(temp_to_orig_section(sec)):
raise errors.TemplateINIFileDoesNotExistException("input-file '{}' does not exist.".format(sec), 9032)
if not sec.endswith(".ini"):
raise errors.TemplateFileIsNotAnINIFileException("input-file '{}' is not an INI file.".format(sec), 9033)
res = {'fname': sec}
for attr in sections[sec]:
res[attr] = sections[sec][attr]
self.input_files.append(res)
self.dynamic = True
def check_concurrent(self):
"""
Check if concurrent is enabled and if so make sure a batch file was given
:return:
"""
if self.concurrent is not False:
self.batch = True
if self.batch is False:
raise errors.NoBatchFileWithConcurrentEnabled("Cannot run concurrent without batch file given.", 9041)
def check_dynamic_variables(self, template_fname, dynamic_dict):
"""
Find the dynamic variables given to make sure they are in the template filename.
If a dynamic variable was given but not in the template it will error out
:param template_fname: (string) The filename of the template file
:param dynamic_dict: (dict) A dictionary of the dynamic variables
:return:
"""
f = open(template_fname, 'r')
lines = f.readlines()
f.close()
for key in dynamic_dict:
found = False
for line in lines:
if "<{}>".format(key) in line:
found = True
if not found:
raise errors.DynamicVariableNotFoundInTemplateException("Dynamic variable '{}' cannot be found in template file '{}'".format(dynamic_dict[key], template_fname), 9052)
def check_dynamic(self):
"""
Checking if dynamic input is valid.
:return:
"""
if self.dynamic is not False:
# Dynamic is only True if batch file is given
if self.dynamic is True:
for ini_file in self.input_files:
fname = temp_to_orig_section(ini_file['fname'])
ini_file.__delitem__('fname')
ini_file.__delitem__('__name__')
self.check_dynamic_variables(fname, ini_file)
ini_file['fname'] = fname
# Dynamic is a string given by user
else:
try:
self.dynamic = ast.literal_eval(self.dynamic)
except:
raise errors.InvalidDynamicInputStringException("Dynamic string '{}' is not in dictionary form".format(self.dynamic), 9051)
self.check_dynamic_variables(self.input_file, self.dynamic)
def check_args(self):
"""
Check all of the arguments
:return:
"""
self.check_input_file()
self.check_output_file()
self.check_concurrent()
self.check_batch()
self.check_dynamic()
##################################
# Run Methods #
##################################
def run_concurrent(self):
"""Create worked os the run_dynamic method so they can run in parrell"""
print "input_files: {}".format(self.input_files)
p = Pool(self.concurrent)
# Get the arguments we will need for our process
args = []
for ini_dict in self.input_files:
fname = ini_dict['fname']
ini_dict.__delitem__('fname')
fname = temp_to_orig_section(fname)
args.append((fname, ini_dict, self.exclude, self.pprint))
results = []
for arg in args:
# Run the Pool
results.append(p.apply_async(func_run_dynamic, arg))
p.close()
p.join()
# The results contain the new templates that were created
# Update the Driver with new templates
templates = [r.get() for r in results]
for t in templates:
self.temp_files.append(t.file_path)
self.templates.append(t)
def run_batch(self):
"""
Execute a batch amount of templates
:return:
"""
print "Input Files: {}".format(self.input_files)
for ini_dict in self.input_files:
fname = ini_dict['fname']
ini_dict.__delitem__('fname')
fname = temp_to_orig_section(fname)
if self.no_temp:
self.run_dynamic_no_temp(fname, ini_dict)
else:
self.run_dynamic_temp(fname, ini_dict)
def run_dynamic_temp(self, input_file, dynamic_dic):
"""
Execute one dynamic template
:param input_file: (string) The template file name
:param dynamic_dic: (dict) The dictionary of the dynamic variables
:return:
"""
new_template_filename = self.create_dynamic_template(input_file, dynamic_dic)
t = Template.Template()
t.file_path = new_template_filename
t.load_sections()
t.set_execute_order()
t.start_driver()
report = t.run()
if self.pprint:
t.pprint(exclude_none=self.exclude)
if self.easy_report:
t.easy_report(exclude_none=self.exclude)
self.templates.append(t)
def run_dynamic_no_temp(self, input_file, dynamic_dict):
"""
Execute one dynamic template
:param input_file: (string) The template file name
:param dynamic_dic: (dict) The dictionary of the dynamic variables
:return:
"""
t = Template.Template()
t.file_path = input_file
t.load_sections()
t.set_execute_order()
t.set_dynamic_vars(dynamic_dict)
t.start_driver()
report = t.run()
if self.pprint:
t.pprint(exclude_none=self.exclude)
if self.easy_report:
t.easy_report(exclude_none=self.exclude)
self.templates.append(t)
def run_normal(self):
"""
Execute one static template
:return:
"""
t = Template.Template()
print "Input file: {}".format(self.input_file)
t.file_path = self.input_file
t.load_sections()
t.set_execute_order()
t.start_driver()
report = t.run()
if self.pprint:
t.pprint(exclude_none=self.exclude)
if self.easy_report:
t.easy_report(exclude_none=self.exclude)
self.templates.append(t)
def run(self):
"""
Check the args and make sure to run the specific run method associated with the args given.
:return:
"""
self.check_args()
# TODO: I need to add concurrent support and figure out what the batch file will look like
# if self.concurrent is True:
# self
if self.batch is True and self.concurrent is not False:
print "Running Concurrent"
self.run_concurrent()
elif self.batch is True:
print "Running Batch"
self.run_batch()
elif self.dynamic is not False:
print "Running Dynamic"
print "Dynamic: {}".format(self.dynamic)
if self.no_temp:
self.run_dynamic_no_temp(self.input_file, self.dynamic)
else:
self.run_dynamic_temp(self.input_file, self.dynamic)
elif self.input_file is not False:
print "Running Normal"
self.run_normal()
print "OUTPUT FILE: {}".format(self.output_file)
if self.output_file is not False:
print "Writing Output File"
self.write_output()
if not self.keep_temps:
self.delete_temp_files()
##################################
# MISC Methods #
##################################
def create_dynamic_template(self, fname, dynamic_dic):
"""
Create a dynamic template by filling in the dynamic variables
:param fname: (string) The name of the dynamic template file
:param dynamic_dic: (dict) A dictionary of dynamic variables
:return:
"""
# open the template and find lines with dynamic variables
# Once variables are found replace them with the designated value
f = open(fname, 'r')
orig_lines = f.readlines()
f.close()
new_lines = []
for line in orig_lines:
new_line = line
for key in dynamic_dic:
if "<{}>".format(key) in line:
new_line = line.replace("<{}>".format(key), dynamic_dic[key])
new_lines.append(new_line)
# Creating the new template file name
new_fname = ""
for key in dynamic_dic:
new_fname += "{}-".format(str(dynamic_dic[key]).replace('/', '|'))
new_fname += fname
if os.path.isfile(new_fname):
raise errors.DynamicTemplateAlreadyExistsException("Dynamic Template '{}' already exists.".format(new_fname), 9053)
else:
# Creating the new template file with dynamic variables included
f = open(new_fname, 'a')
for line in new_lines:
f.write(line)
f.close()
self.temp_files.append(new_fname)
return new_fname
def write_output(self):
"""
Write all the reports to an output file
:return:
"""
for t in self.templates:
t.append_log_report(self.output_file, self.exclude)
def create_temp_batch(self, fname):
"""
Read files and get sections of INI file
:param fname:
:return:
"""
new_fname = ""
f = open(fname, 'r')
lines = f.readlines()
sections = []
new_lines = []
section = {}
i = 0
for line in lines:
line = line.strip()
if line.startswith('[') and line.endswith(']'):
# This is a section
line = line.lstrip('[').rstrip(']')
# IS the section already in sections?
if line in sections:
new_line = "[{}~|~{}]".format(i, line)
new_lines.append(new_line)
else: # If not then append
new_lines.append("[{}]".format(line))
sections.append(line)
i += 1
else:
new_lines.append(line)
new_fname = "temp-{}".format(fname)
# Write new tempate file
f = open(new_fname, 'w')
for line in new_lines:
f.write(line + "\n")
f.close()
self.temp_files.append(new_fname)
return new_fname
def delete_temp_files(self):
for temp_file in self.temp_files:
os.remove(temp_file)