3838import tee_print
3939
4040from safe_open import open
41+ from vcast_utils import getVectorCASTEncoding
4142
4243# adding path
4344workspace = os .getenv ("WORKSPACE" )
8182 from vector .apps .DataAPI .vcproject_api import VCProjectApi
8283except :
8384 pass
84-
85+
86+ encFmt = getVectorCASTEncoding ()
87+
8588#global variables
8689global verbose
8790global print_exc
@@ -178,11 +181,14 @@ def readManageVersion(ManageFile):
178181 version = 14
179182 if os .path .isfile (ManageFile + ".vcm" ):
180183 ManageFile = ManageFile + '.vcm'
181- with open (ManageFile , 'r' ) as projFile :
182- for line in projFile :
184+
185+ with open (ManageFile , 'rb' ) as projFile :
186+ for raw_line in projFile : # iterates lazily, line by line
187+ line = raw_line .decode (encFmt , "replace" ) # decode each line
183188 if 'version' in line and 'project' in line :
184189 version = int (re .findall (r'\d+' , line )[0 ])
185190 break
191+
186192 if verbose :
187193 print ("Version of VectorCAST project file = %d" % version )
188194 print ("(Levels change in version 17 (*maybe) and above)" )
@@ -308,8 +314,8 @@ def fixup_css(report_name):
308314 if not need_fixup :
309315 return
310316
311- with open (report_name ,"r " ) as fd :
312- data = fd .read ()
317+ with open (report_name ,"rb " ) as fd :
318+ data = fd .read (). decode ( encFmt , "replace" )
313319
314320 #fix up inline CSS because of Content Security Policy violation
315321 newData = data [: data .index ("<style>" )- 1 ] + """
@@ -323,8 +329,8 @@ def fixup_css(report_name):
323329 regex_str = r"<img alt=\"Vector\".*"
324330 newData = re .sub (regex_str ,"<img alt=\" Vector\" src=\" vectorcast.png\" />" ,newData )
325331
326- with open (report_name , "w " ) as fd :
327- fd .write (newData )
332+ with open (report_name , "wb " ) as fd :
333+ fd .write (newData . encode ( encFmt , "replace" ) )
328334
329335 workspace = os .getenv ("WORKSPACE" )
330336 if workspace is None :
@@ -577,20 +583,13 @@ def buildReports(FullManageProjectName = None,
577583 use_archive_extract , report_only_failures , no_full_report ,
578584 useStartLine , teePrint , use_cte )
579585
580- with open ("unit_test_fail_count.txt" , "w" ) as fd :
581- failed_str = str (failed_count )
582- try :
583- fd .write (unicode (failed_str ))
584- except :
585- fd .write (failed_str )
586-
587- with open ("unit_test_passfail_count.txt" , "w" ) as fd :
588- passfail_str = str (passed_count ) + " " + str (failed_count )
589- try :
590- fd .write (unicode (passfail_str ))
591- except :
592- fd .write (passfail_str )
593-
586+ with open ("unit_test_fail_count.txt" , "wb" ) as fd :
587+ fd .write (str (failed_count ).encode (encFmt , "replace" ))
588+
589+ with open ("unit_test_passfail_count.txt" , "wb" ) as fd :
590+ text = "{} {}" .format (passed_count , failed_count )
591+ fd .write (text .encode (encFmt , "replace" ))
592+
594593 if timing :
595594 print ("XML and Individual reports: " + str (time .time ()))
596595
@@ -671,8 +670,8 @@ def buildReports(FullManageProjectName = None,
671670 print (out )
672671
673672 # save the output of the manage command for debug purposes
674- with open ("build.log" ,"w " ) as fd :
675- fd .write (out )
673+ with open ("build.log" ,"wb " ) as fd :
674+ fd .write (out . encode ( encFmt , "replace" ) )
676675
677676 copyList = []
678677 jobName = ""
@@ -754,9 +753,9 @@ def buildReports(FullManageProjectName = None,
754753 passed_count = 0
755754 try :
756755 for file in glob .glob ("xml_data/test_results_*.xml" ):
757- with open (file ,"r " ) as fd :
758- lines = fd .readlines ()
759-
756+ with open (file ,"rb " ) as fd :
757+ lines = [ line . decode ( encFmt , "replace" ) for line in fd .readlines ()]
758+
760759 for line in lines :
761760 if "failures" in line :
762761 failed_count += int (line .split ("\" " )[5 ])
@@ -766,20 +765,12 @@ def buildReports(FullManageProjectName = None,
766765 teePrint .teePrint (" *INFO: Problem parsing test results file for unit testcase failure count: " + file )
767766 if print_exc : traceback .print_exc ()
768767
769- with open ("unit_test_fail_count.txt" , "w" ) as fd :
770- failed_str = str (failed_count )
771- try :
772- fd .write (unicode (failed_str ))
773- except :
774- fd .write (failed_str )
768+ with open ("unit_test_fail_count.txt" , "wb" ) as fd :
769+ fd .write (str (failed_count ).encode (encFmt , "replace" ))
775770
776- with open ("unit_test_passfail_count.txt" , "w" ) as fd :
777- passfail_str = str (passed_count ) + " " + str (failed_count )
778- try :
779- fd .write (unicode (passfail_str ))
780- except :
781- fd .write (passfail_str )
782-
771+ with open ("unit_test_passfail_count.txt" , "wb" ) as fd :
772+ text = "{} {}" .format (passed_count , failed_count )
773+ fd .write (text .encode (encFmt , "replace" ))
783774
784775 for file in copyList :
785776
@@ -837,8 +828,8 @@ def buildReports(FullManageProjectName = None,
837828
838829 try :
839830 tool_version = os .path .join (os .environ ['VECTORCAST_DIR' ], "DATA" , "tool_version.txt" )
840- with open (tool_version ,"r " ) as fd :
841- ver = fd .read ()
831+ with open (tool_version ,"rb " ) as fd :
832+ ver = fd .read (). decode ( encFmt , "replace" )
842833
843834 if ver .startswith ("19 " ):
844835 need_fixup = True
@@ -871,8 +862,9 @@ def new_init(self):
871862
872863
873864 if args .buildlog and os .path .exists (args .buildlog ):
874- with open (args .buildlog ,"r" ) as fd :
875- buildLogData = fd .readlines ()
865+ with open (args .buildlog ,"rb" ) as fd :
866+ buildLogData = [line .decode (encFmt , "replace" ) for line in fd .readlines ()]
867+
876868 cbt = ParseConsoleForCBT (verbose )
877869 cbtDict = cbt .parse (buildLogData )
878870
0 commit comments