/
regression.py
154 lines (113 loc) · 3.74 KB
/
regression.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
import sys
import os
import getopt
import logging
from executor import Executor
from configuration import Configuration
from threading import Thread
from dispatcher import Dispatcher
import threading
import time
def create_logger(format):
"""
Constructs output stream logger.
"""
formatter = logging.Formatter(fmt=format[0], datefmt=format[1])
logging.getLogger('').setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(formatter)
logging.getLogger('').addHandler(ch)
def usage():
"""
Construct and raise Optional error
w.r.t command help.
"""
smesg =("Help: -c or --config=configuration.xml")
raise getopt.GetoptError(smesg)
def execute():
"""
Create execution semaphore and threads.
Join threads and returns result files.
"""
resultfiles = []
threads = []
semaphore = threading.BoundedSemaphore(value=Configuration.semaphore)
for (root, dirs, files) in os.walk(Configuration.sourcedir):
for file in files:
# path manipulation
abspath = os.path.join(root, file)
relpath = os.path.relpath(abspath, Configuration.sourcedir)
# Resultdir
resdir = os.path.join(Configuration.resultdir, os.path.dirname(relpath))
# Create resultdir
if not os.path.exists(resdir):
os.makedirs(resdir)
e = Executor(relpath, file, semaphore)
e.start()
threads.append(e)
for t in threads:
t.join()
que = Executor.get_queue()
while que.empty() != True:
resultfiles = resultfiles + que.get()
return resultfiles
def dispatch(resultfiles):
"""
Create dispatch semaphore and threads.
Join threads and returns dispatch status.
"""
threads = []
semaphore = threading.BoundedSemaphore(value=Configuration.semaphore)
for result in resultfiles:
expectfile = os.path.join(Configuration.expecteddir, result)
resultfile = os.path.join(Configuration.resultdir, result)
t = Dispatcher(result, expectfile, resultfile, result, semaphore)
t.start()
threads.append(t)
for t in threads:
t.join()
return(Dispatcher.get_pass(), Dispatcher.get_total())
def main():
"""
As per Main method, defines sequence of program.
"""
try:
format = ['%(asctime)s %(message)s', '%Y-%m-%d %H:%M:%S']
resultfiles = []
pas = 0
total = 0
create_logger(format)
opts, args = getopt.getopt(sys.argv[1:], "hc:", ["help", "config="])
source = str()
if not opts:
usage()
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
elif opt in ("-c", "--config"):
source = arg
# Configurations init
Configuration.load(source, format)
logging.info("="*100)
logging.info("Regression started")
logging.info("="*100)
start_time = time.clock()
# Execute Scripts
resultfiles = execute()
logging.info("="*100)
logging.info("Results")
logging.info("="*100)
# Dispatch results
pas, total = dispatch(resultfiles)
total_time = time.clock() - start_time
logging.info("="*100)
logging.info("%d passed out of %d" %(pas, total))
logging.info("Total elapsed time(secs) %.2f" %(total_time))
logging.info("="*100)
except (getopt.GetoptError, Exception) as e:
logging.info("Exception %s" %str(e))
except:
logging.info("Fatal error occured.")
if __name__ == "__main__":
main()