CYCAMORE
tests/run_inputs.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 import sys
4 import subprocess
5 from subprocess import Popen, PIPE, STDOUT
6 import os
7 import re
8 
9 cyclus_path = "/home/mouginot/.local/include/cyclus/../../bin/cyclus"
10 input_path = "/home/mouginot/work/app/cyclus/cycamore/input"
11 
12 def main_body(flag):
13  files, catalogs, catalognames = get_files(input_path)
14  copy_catalogs(catalogs,cyclus_path.strip("cyclus"))
15  summ = Summary()
16  for name in files :
17  file_to_test = TestFile(cyclus_path, name, flag)
18  file_to_test.run()
19  summ.add_to_summary(file_to_test)
20  clean_catalogs(cyclus_path.strip("cyclus"),catalognames)
21  summ.print_summary()
22 
23 def main():
24  """This function finds input files, runs them, and prints a summary"""
25  flag = check_inputs()
26  main_body(flag)
27 
28 def check_inputs():
29  """This function checks the input arguments"""
30  if len(sys.argv) > 2:
31  print_usage()
32  sys.exit(1)
33  elif len(sys.argv) == 2:
34  if re.search("-v*",sys.argv[1]):
35  flag = sys.argv[1]
36  else :
37  print_usage()
38  sys.exit(1)
39  elif len(sys.argv) == 1 :
40  flag = "-v0"
41  return flag
42 
43 def print_usage() :
44  """This prints the proper way to treat the command line interface"""
45  print(""" Usage: python run_inputs.py\n
46  Allowed Options : \n
47  -v arg output log verbosity. \n
48 
49  Can be text: \n
50 
51  LEV_ERROR (least verbose, default), LEV_WARN,\n
52 
53  LEV_INFO1 (through 5), and LEV_DEBUG1 (through 5).\n
54 
55  Or an integer:\n
56 
57  0 (LEV_ERROR equiv) through 11 (LEV_DEBUG5 equiv)\n""")
58 
59 def get_files(path):
60  """This function walks the 'path' tree and finds input files"""
61  catalogs=[]
62  catalognames=[]
63  inputs=[]
64  for root, dirs, files in os.walk(path, followlinks=True):
65  if '.git' in dirs:
66  dirs.remove('.git')
67  for name in files:
68  if re.search("\.xml",name) :
69  if re.search("recipebook",name) or \
70  re.search("facilitycatalog",name):
71  catalogs.append(os.path.join(root,name))
72  catalognames.append(name)
73  else :
74  inputs.append(os.path.join(root, name))
75  else :
76  files.remove(name)
77  print("The catalogs to be moved are:")
78  print(catalogs)
79  print("The files to be tested are:")
80  print(inputs)
81  return inputs, catalogs, catalognames
82 
83 def copy_catalogs(catalogs,cyclus_path) :
84  """Copies files in the catalogs list to the cyclus executable directory"""
85  for cat in catalogs :
86  p = Popen("cp "+cat+" "+cyclus_path,
87  shell=True, stdout=PIPE, stderr=STDOUT)
88 
89 def clean_catalogs(cyclus_path, catalogs) :
90  """Removes previously copied catalogs from executable directory."""
91  for cat in catalogs :
92  p = Popen("rm "+ os.path.join(cyclus_path,cat),
93  shell=True, stdout=PIPE, stderr=STDOUT)
94 
95 class Summary():
96  """An object to hold the results of all the tests"""
97  def __init__(self):
98  self.passed = []
99  self.failed = []
100 
101  def add_to_summary(self, test_file) :
102  """Adds a test file to this summary object"""
103  if test_file.passed :
104  self.passed.append( test_file.infile )
105  else :
106  self.failed.append( test_file.infile )
107 
108  def print_summary(self) :
109  """Prints the summary"""
110  print("Input files passed = " + str(len(self.passed)))
111  print("Input files failed = " + str(len(self.failed)))
112  print("Failed input files : ")
113  for test in self.failed :
114  print(test)
115 
116 class TestFile():
117  """An object representing the inputxml file to test"""
118  def __init__(self, cyclus_path, file_path, flag):
119  self.infile = file_path
120  self.cyclus_path = cyclus_path
121  self.passed=True
122  self.flag = " "+flag+" "
123 
124  def run(self):
125  """Runs all of the input file tests"""
126  output = self.get_output()
127  if self.no_errors(output) :
128  self.passed = True
129  else :
130  self.passed = False
131 
132  def get_output(self):
133  """Returns the output from running the FileTest"""
134  try :
135  p = Popen(self.cyclus_path+" "+ self.infile + self.flag,
136  shell=True, stdout=PIPE, stderr=STDOUT)
137  io_tuple = p.communicate()
138  output = io_tuple[0]
139  except subprocess.CalledProcessError as e:
140  print(e)
141  return str(output)
142 
143  def no_errors(self, output):
144  """returns true if there were no errors or segfaults running this TestFile"""
145  to_ret = True
146  print("Input file " + self.infile)
147  if re.search("No such file or directory",output) :
148  print("Cyclus executable not found in path.")
149  elif re.search("ERROR",output) or re.search("Segmentation fault",output):
150  to_ret = False
151  print(" resulted in errors: ")
152  print(output)
153  else :
154  print(" passed. ")
155  return to_ret
156 
157 if __name__ == '__main__' : main()
def __init__(self, cyclus_path, file_path, flag)
def add_to_summary(self, test_file)
def copy_catalogs(catalogs, cyclus_path)
def clean_catalogs(cyclus_path, catalogs)