1919# -----------------------------------------------------------------------------
2020
2121
22+ class colors :
23+ SUCCESS = "\033 [92m"
24+ WARNING = "\033 [93m"
25+ ERROR = "\033 [91m"
26+ END = "\033 [0m"
27+
28+
29+ def print_success (msg ):
30+ print (f"{ colors .SUCCESS } { msg } { colors .END } " )
31+
32+
33+ def print_warning (msg ):
34+ print (f"{ colors .WARNING } { msg } { colors .END } " )
35+
36+
37+ def print_error (msg ):
38+ print (f"{ colors .ERROR } { msg } { colors .END } " )
39+
40+
2241# -----------------------------------------------------------------------------
2342# main routine
2443# -----------------------------------------------------------------------------
2544def main ():
2645
2746 import argparse
28- import sys , os , shutil
47+ import os , shutil
2948
3049 parser = argparse .ArgumentParser (description = "Update output files" )
3150
@@ -44,68 +63,80 @@ def main():
4463
4564 # check inputs
4665 if not os .path .isdir (args .source ):
47- print (f"Error: could not find { args .source } " )
66+ print_error (f"Error: could not find { args .source } " )
4867 return - 1
4968
5069 if not os .path .isdir (args .destination ):
51- print (f"Error: could not find { args .destination } " )
70+ print_error (f"Error: could not find { args .destination } " )
5271 return - 1
5372
5473 # check that the output directory exists
5574 output = os .path .join (args .source , "Testing" , "output" )
5675 if not os .path .isdir (output ):
57- print (f"Error: could not find { output } " )
76+ print_error (f"Error: could not find { output } " )
5877 return - 1
5978
60- # create a list of all test run or just the failed tests
61- tests = []
79+ # create a list of output files for all tests run or just the failed tests
80+ output_files = []
6281
6382 if args .all :
6483 # get names of all .out files
6584 for f in os .listdir (output ):
6685 if f .endswith (".out" ):
67- tests .append (f )
86+ output_files .append (f )
6887 else :
6988 failed = os .path .join (args .source , "Testing" , "Temporary" , "LastTestsFailed.log" )
7089
7190 # extract test names from list and append .out
7291 with open (failed , "r" ) as f :
7392 for line in f :
74- tests .append (line .split (":" )[1 ].rstrip () + ".out" )
93+ output_files .append (line .split (":" )[1 ].rstrip () + ".out" )
7594
7695 # if failed tests were found update the output files
77- if tests :
96+ if output_files :
97+ num_output_files = len (output_files )
7898 paths = [
7999 os .path .join (args .destination , "examples" ),
80100 os .path .join (args .destination , "test" , "unit_tests" ),
81101 args .destination ,
82102 ]
83- for t in tests :
103+ for idx , out_file in enumerate ( output_files ) :
84104 if args .verbose > 0 :
85- print (f"Searching for { t } " )
105+ print (f"[{ idx + 1 } of { num_output_files } ] Test { out_file [:- 4 ]} " )
106+ # Some tests do not have an output file (e.g., unit tests)
107+ if not os .path .isfile (os .path .join (output , out_file )):
108+ print_warning (f" Warning: did not find the output file { out_file } " )
109+ continue
110+ if args .verbose > 1 :
111+ print (f" Searching for answer file { out_file } " )
86112 found = False
87113 for p in paths :
114+ if not os .path .isdir (p ):
115+ continue
88116 if args .verbose == 2 :
89117 print (f" Looking in { p } " )
90- for root , dirs , files in os .walk (p ):
118+ for root , _ , files in os .walk (p ):
91119 if args .verbose == 3 :
92120 print (f" Looking in { root } " )
93- if t in files :
121+ if out_file in files :
94122 if args .verbose == 1 :
95123 print (f" Found file in { root } " )
96124 if args .verbose > 1 :
97125 print (" Found file" )
98- shutil .copy (os .path .join (output , t ), os .path .join (root , t ))
126+ shutil .copy (os .path .join (output , out_file ), os .path .join (root , out_file ))
99127 found = True
128+ if args .verbose > 0 :
129+ print_success (f" Answer file updated" )
100130 break
101131 if found :
102132 break
103133 if not found :
134+ print_warning (f" Warning: did not find the answer file { out_file } " )
104135 if args .copy :
105- print (f"Warning: did not find { t } , copying to { args .destination } " )
106- shutil .copy (os . path . join ( output , t ), os . path . join ( args . destination , t ))
107- else :
108- print ( f"Warning: did not find { t } " )
136+ print (f" Copying { out_file } to { args .destination } " )
137+ shutil .copy (
138+ os . path . join ( output , out_file ), os . path . join ( args . destination , out_file )
139+ )
109140
110141
111142# -----------------------------------------------------------------------------
0 commit comments