Skip to content

Commit 4d41052

Browse files
committed
updates for proper encoding
1 parent 391e1dd commit 4d41052

23 files changed

+328
-291
lines changed

src/main/resources/scripts/archive_extract_reports.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def extract(verbose = False):
2626
os.utime(full_path, (f.mtime, f.mtime))
2727
except Exception as e:
2828
if verbose:
29-
print("Could not set time for {}: {}".format(f.name, e)
29+
print("Could not set time for {}: {}".format(f.name, e))
3030

3131
def archive(verbose = False):
3232
if os.path.exists(archive_name):

src/main/resources/scripts/cobertura.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444

4545
fileList = []
4646

47-
from vcast_utils import dump, checkVectorCASTVersion
47+
from vcast_utils import dump, checkVectorCASTVersion, getVectorCASTEncoding
48+
49+
encFmt = getVectorCASTEncoding()
4850

4951
def write_xml(x, name, verbose = False):
5052

@@ -56,7 +58,8 @@ def write_xml(x, name, verbose = False):
5658

5759
xml_str += etree.tostring(x,pretty_print=True).decode()
5860

59-
with open(name + ".xml", "w") as fd: fd.write(xml_str)
61+
with open(name + ".xml", "wb") as fd:
62+
fd.write(xml_str.encode(encFmt,"replace"))
6063

6164
def getCoveredFunctionCount(source):
6265
if len(source.functions) == 0:
@@ -730,8 +733,8 @@ def generateCoverageResults(inFile, azure = False, xml_data_dir = "xml_data", ve
730733
coverages.attrib['timestamp'] = str(datetime.now())
731734

732735
tool_version = os.path.join(os.environ['VECTORCAST_DIR'], "DATA", "tool_version.txt")
733-
with open(tool_version,"r") as fd:
734-
ver = fd.read()
736+
with open(tool_version,"rb") as fd:
737+
ver = fd.read().decode(encFmt,"replace")
735738

736739
coverages.attrib['version'] = "VectorCAST " + ver.rstrip()
737740

src/main/resources/scripts/create_index_html.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import argparse
44
import glob
55

6+
from vcast_utils import dump, checkVectorCASTVersion, getVectorCASTEncoding
7+
8+
encFmt = getVectorCASTEncoding()
69

710
class cd:
811
"""Context manager for changing the current working directory"""
@@ -17,8 +20,9 @@ def __exit__(self, etype, value, traceback):
1720
os.chdir(self.savedPath)
1821

1922
def searchKeyword(search_string, filename):
20-
with open(filename, "r") as f:
21-
for line_number, line in enumerate(f, start=1):
23+
with open(filename, "rb") as fd:
24+
for line_number, line in enumerate(fd, start=1):
25+
line = line.decode(encFmt, "replace")
2226
if search_string in line:
2327
start_idx = line.find(search_string)
2428
if start_idx != -1:

src/main/resources/scripts/fixup_reports.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,28 @@ def fixup_2020_soup(main_soup):
113113

114114
def fixup_2020_reports(report_name):
115115

116-
lang, enc = getVectorCASTEncoding()
116+
encFmt = getVectorCASTEncoding()
117117

118118
with open(report_name, "rb") as fd:
119-
raw = fd.read()
119+
raw = fd.read().decode(encFmt, "replace")
120+
121+
try:
122+
# First attempt: use whatever encoding was detected
123+
main_soup = BeautifulSoup(raw, features="lxml")
124+
125+
except Exception as e:
120126
try:
121-
main_soup = BeautifulSoup(raw, features="lxml", from_encoding=enc)
122-
except Exception as e:
123-
try:
124-
# Try UTF-8 first
125-
main_soup = BeautifulSoup(raw, "lxml", from_encoding="utf-8")
126-
except Exception:
127-
# Fall back to system ACP (cp936 in China, cp1252 in US)
128-
main_soup = BeautifulSoup(raw.decode(enc, errors="replace"), "lxml")
127+
# Try UTF-8 first as a fallback (should rarely fail if raw is text)
128+
main_soup = BeautifulSoup(raw.encode("utf-8", "replace"), "lxml")
129+
except Exception:
130+
# Last resort: try system default encoding (ACP on Windows, etc.)
131+
main_soup = BeautifulSoup(raw.encode(encFmt, "replace"), "lxml")
129132

130133
main_soup = fixup_2020_soup(main_soup)
131134

132-
with open(report_name, "w", encoding=enc, errors="replace") as fd:
133-
fd.write(main_soup.prettify(formatter="html"))
135+
with open(report_name, "wb") as fd:
136+
fd.write(main_soup.prettify(formatter="html").encode(encFmt, "replace"))
137+
134138

135139
if __name__ == '__main__':
136140

src/main/resources/scripts/generate-results.py

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import tee_print
3939

4040
from safe_open import open
41+
from vcast_utils import getVectorCASTEncoding
4142

4243
# adding path
4344
workspace = os.getenv("WORKSPACE")
@@ -81,7 +82,9 @@
8182
from vector.apps.DataAPI.vcproject_api import VCProjectApi
8283
except:
8384
pass
84-
85+
86+
encFmt = getVectorCASTEncoding()
87+
8588
#global variables
8689
global verbose
8790
global 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

src/main/resources/scripts/generate_lcov.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@
4040
import subprocess
4141
import argparse
4242

43-
from vcast_utils import dump, checkVectorCASTVersion
43+
from vcast_utils import dump, checkVectorCASTVersion, getVectorCASTEncoding
4444
try:
4545
from safe_open import open
4646
except:
4747
pass
48+
49+
encFmt = getVectorCASTEncoding()
4850

4951
fileList = []
5052

@@ -114,9 +116,9 @@ def has_branches_covered(line):
114116

115117
def get_function_name_line_number(file_path, function, initial_guess):
116118

117-
with open(file_path,"r") as fd:
118-
lines = fd.readlines()
119-
119+
with open(file_path,"rb") as fd:
120+
lines = [line.decode(encFmt, "replace") for line in fd.readlines()]
121+
120122
line_number_closest_so_far = initial_guess;
121123
delta = 9999999999;
122124

@@ -323,7 +325,8 @@ def generateCoverageResults(inFile, xml_data_dir = "xml_data", verbose = False,
323325
os.makedirs(lcov_data_dir)
324326

325327
pathToInfo = os.path.join(lcov_data_dir, name + ".info")
326-
with open(pathToInfo, "w") as fd: fd.write(output)
328+
with open(pathToInfo, "wb") as fd:
329+
fd.write(output.encode(encFmt, "replace"))
327330

328331
cmdStr = "genhtml " + pathToInfo + " --output-directory out"
329332
cmdArr = cmdStr.split()

src/main/resources/scripts/generate_pclp_reports.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
from pprint import pprint
2121

22-
from vcast_utils import checkVectorCASTVersion
22+
from vcast_utils import checkVectorCASTVersion, getVectorCASTEncoding
23+
24+
encFmt = getVectorCASTEncoding()
2325

2426
try:
2527
from safe_open import open
@@ -45,13 +47,17 @@ def parse_msgs(filename):
4547
directoryName = os.path.dirname(filename)
4648

4749
try:
48-
basepath = os.environ['WORKSPACE'].replace("\\","/") + "/"
50+
basepath = os.environ['CI_PROJECT_DIR'].replace("\\","/") + "/"
4951
except:
50-
basepath = os.getcwd().replace("\\","/") + "/"
51-
os.environ['VCAST_RPTS_CUSTOM_CSS']=r'vc_scripts\css\tooltip.css'
52+
try:
53+
basepath = os.environ['WORKSPACE'].replace("\\","/") + "/"
54+
except:
55+
basepath = os.getcwd().replace("\\","/") + "/"
56+
57+
os.environ['VCAST_RPTS_CUSTOM_CSS']= basepath + "/vc_scripts/css/tooltip.css"
5258

53-
with open(filename, "r") as fd:
54-
pcplXmlData = fd.read()
59+
with open(filename, "rb") as fd:
60+
pcplXmlData = fd.read().decode(encFmt, "replace")
5561

5662
index = pcplXmlData.find('<')
5763

@@ -274,9 +280,9 @@ def generate_source():
274280
if os.path.isfile(fname + ".vcast.bak"):
275281
fname = fname + ".vcast.bak"
276282

277-
with open(fname , 'r') as fh:
283+
with open(fname, 'rb') as fh:
278284
# read and replace the line ending for consistency
279-
contents = fh.read()
285+
contents = fh.read().encode(encFmnt, "replace")
280286
contents = contents.replace("\r\n", "\n").replace("\r","\n")
281287
for lineno, line in enumerate(contents.splitlines(), start=1):
282288
lineno_str = str(lineno)
@@ -483,11 +489,8 @@ def emit_gitlab(msgs):
483489
# Driver
484490

485491
def write_output(output, filename):
486-
with open(filename, 'w') as file:
487-
try:
488-
file.write(output)
489-
except:
490-
file.write(output.decode('utf-8'))
492+
with open(filename, 'wb') as file:
493+
file.write(output.encode(encFmt, "replace"))
491494

492495
def generate_reports(input_xml, output_text = None, output_html = None, output_json = None, output_gitlab = None, full_mp_name = None):
493496

src/main/resources/scripts/generate_sonarqube_pclp_reports.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
except:
2525
pass
2626

27+
from vcast_utils import getVectorCASTEncoding
28+
29+
encFmt = getVectorCASTEncoding()
30+
2731
# PC-lint Plus message representation and parsing
2832

2933
class Message:
@@ -299,8 +303,8 @@ def emit_gitlab(msgs):
299303
# Driver
300304

301305
def write_output(output, filename):
302-
with open(filename, 'w') as file:
303-
file.write(output)
306+
with open(filename, 'wb') as file:
307+
file.write(output.encode(encFmt, "replace"))
304308

305309
def generate_reports(input_xml, output_text = None, output_html = None, output_json = None, output_gitlab = None):
306310
msgs = parse_msgs(input_xml)

0 commit comments

Comments
 (0)