forked from cyclus/cycamore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_inputs.py.in
157 lines (135 loc) · 5.14 KB
/
run_inputs.py.in
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
#!/usr/bin/python3
import sys
import subprocess
from subprocess import Popen, PIPE, STDOUT
import os
import re
cyclus_path = "@cyclus_path@/cyclus"
input_path = "@input_path@"
def main_body(flag):
files, catalogs, catalognames = get_files(input_path)
copy_catalogs(catalogs,cyclus_path.strip("cyclus"))
summ = Summary()
for name in files :
file_to_test = TestFile(cyclus_path, name, flag)
file_to_test.run()
summ.add_to_summary(file_to_test)
clean_catalogs(cyclus_path.strip("cyclus"),catalognames)
summ.print_summary()
def main():
"""This function finds input files, runs them, and prints a summary"""
flag = check_inputs()
main_body(flag)
def check_inputs():
"""This function checks the input arguments"""
if len(sys.argv) > 2:
print_usage()
sys.exit(1)
elif len(sys.argv) == 2:
if re.search("-v*",sys.argv[1]):
flag = sys.argv[1]
else :
print_usage()
sys.exit(1)
elif len(sys.argv) == 1 :
flag = "-v0"
return flag
def print_usage() :
"""This prints the proper way to treat the command line interface"""
print(""" Usage: python3 run_inputs.py\n
Allowed Options : \n
-v arg output log verbosity. \n
Can be text: \n
LEV_ERROR (least verbose, default), LEV_WARN,\n
LEV_INFO1 (through 5), and LEV_DEBUG1 (through 5).\n
Or an integer:\n
0 (LEV_ERROR equiv) through 11 (LEV_DEBUG5 equiv)\n""")
def get_files(path):
"""This function walks the 'path' tree and finds input files"""
catalogs=[]
catalognames=[]
inputs=[]
for root, dirs, files in os.walk(path, followlinks=True):
if '.git' in dirs:
dirs.remove('.git')
for name in files:
if re.search("\.xml",name) :
if re.search("recipebook",name) or \
re.search("facilitycatalog",name):
catalogs.append(os.path.join(root,name))
catalognames.append(name)
else :
inputs.append(os.path.join(root, name))
else :
files.remove(name)
print("The catalogs to be moved are:")
print(catalogs)
print("The files to be tested are:")
print(inputs)
return inputs, catalogs, catalognames
def copy_catalogs(catalogs,cyclus_path) :
"""Copies files in the catalogs list to the cyclus executable directory"""
for cat in catalogs :
p = Popen("cp "+cat+" "+cyclus_path,
shell=True, stdout=PIPE, stderr=STDOUT)
def clean_catalogs(cyclus_path, catalogs) :
"""Removes previously copied catalogs from executable directory."""
for cat in catalogs :
p = Popen("rm "+ os.path.join(cyclus_path,cat),
shell=True, stdout=PIPE, stderr=STDOUT)
class Summary():
"""An object to hold the results of all the tests"""
def __init__(self):
self.passed = []
self.failed = []
def add_to_summary(self, test_file) :
"""Adds a test file to this summary object"""
if test_file.passed :
self.passed.append( test_file.infile )
else :
self.failed.append( test_file.infile )
def print_summary(self) :
"""Prints the summary"""
print("Input files passed = " + str(len(self.passed)))
print("Input files failed = " + str(len(self.failed)))
print("Failed input files : ")
for test in self.failed :
print(test)
class TestFile():
"""An object representing the inputxml file to test"""
def __init__(self, cyclus_path, file_path, flag):
self.infile = file_path
self.cyclus_path = cyclus_path
self.passed=True
self.flag = " "+flag+" "
def run(self):
"""Runs all of the input file tests"""
output = self.get_output()
if self.no_errors(output) :
self.passed = True
else :
self.passed = False
def get_output(self):
"""Returns the output from running the FileTest"""
try :
p = Popen(self.cyclus_path+" "+ self.infile + self.flag,
shell=True, stdout=PIPE, stderr=STDOUT)
io_tuple = p.communicate()
output = io_tuple[0]
except subprocess.CalledProcessError as e:
print(e)
return str(output)
def no_errors(self, output):
"""returns true if there were no errors or segfaults running this TestFile"""
to_ret = True
print("Input file " + self.infile)
if re.search("No such file or directory",output) :
print("Cyclus executable not found in path.")
elif re.search("ERROR",output) or re.search("Segmentation fault",output):
to_ret = False
print(" resulted in errors: ")
print(output)
else :
print(" passed. ")
return to_ret
if __name__ == '__main__' : main()