Skip to content

Commit 197a5fb

Browse files
committed
more updates for encoding issues
1 parent fcc10fc commit 197a5fb

File tree

9 files changed

+140
-94
lines changed

9 files changed

+140
-94
lines changed

src/main/resources/scripts/fixup_reports.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from __future__ import division
2626
from __future__ import print_function
2727

28-
import sys, os
28+
import sys, os, locale
2929
# adding path
3030
workspace = os.getenv("WORKSPACE")
3131
if workspace is None:
@@ -47,6 +47,7 @@
4747
pass
4848

4949
import tee_print
50+
from vcast_utils import getVectorCASTEncoding
5051

5152
def fixup_2020_soup(main_soup):
5253

@@ -111,21 +112,28 @@ def fixup_2020_soup(main_soup):
111112
return main_soup
112113

113114
def fixup_2020_reports(report_name):
115+
116+
lang, enc = getVectorCASTEncoding()
117+
114118
with open(report_name, "rb") as fd:
119+
raw = fd.read()
115120
try:
116-
main_soup = BeautifulSoup(fd,features="lxml", from_encoding="utf-8")
121+
main_soup = BeautifulSoup(raw, features="lxml", from_encoding=enc)
117122
except Exception as e:
118-
import traceback
119-
print( "EXCEPTION FORMAT PRINT:\n{}".format( e ) )
120-
print( "EXCEPTION TRACE PRINT:\n{}".format( "".join(traceback.format_exception(type(e), e, e.__traceback__))))
121-
main_soup = BeautifulSoup(fd)
122-
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")
129+
123130
main_soup = fixup_2020_soup(main_soup)
124131

125-
with open(report_name,"wb") as fd:
126-
fd.write(main_soup.prettify(formatter="html",encoding="utf-8"))
127-
132+
with open(report_name, "w", encoding=enc, errors="replace") as fd:
133+
fd.write(main_soup.prettify(formatter="html"))
134+
128135
if __name__ == '__main__':
136+
129137
report_name = sys.argv[1]
130138
fixup_2020_reports(report_name)
131139

src/main/resources/scripts/generate_xml.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,17 +1524,21 @@ def __get_testcase_execution_results(self, tc, classname, tc_name):
15241524
sections=[ "TESTCASE_SECTIONS"],
15251525
testcase_sections=["EXECUTION_RESULTS"])
15261526

1527-
with open(report_name,"rb") as fd:
1527+
with open(report_name, "rb") as fd:
15281528
out = fd.read()
15291529

1530-
out = out.decode(self.encFmt)
1530+
try:
1531+
# Prefer UTF-8 if possible
1532+
out = out.decode("utf-8")
1533+
except UnicodeDecodeError:
1534+
# Fallback to system/default encoding (e.g. cp936 in CN) with replace
1535+
out = out.decode(self.encFmt, errors="replace")
1536+
15311537
os.remove(report_name)
15321538
except:
15331539
out = "No execution results found"
15341540
parse_traceback.parse(traceback.format_exc(), self.print_exc, self.compiler, self.testsuite, self.env, self.build_dir)
15351541

1536-
#out = bytes(out, 'utf-8').decode('utf-8', 'ignore')
1537-
15381542
return out
15391543

15401544
## GenerateXml

src/main/resources/scripts/incremental_build_report_aggregator.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import os
2727
import sys
2828
import shutil
29+
import locale
2930

3031
# adding path
3132
workspace = os.getenv("WORKSPACE")
@@ -120,9 +121,6 @@ def parse_text_files(mpName):
120121

121122
def parse_html_files(mpName):
122123

123-
# get the VC langaguge and encoding
124-
lang, encFmt = getVectorCASTEncoding()
125-
126124
if os.path.exists(mpName + "_rebuild.html"):
127125
os.remove(mpName + "_rebuild.html")
128126

@@ -139,20 +137,30 @@ def parse_html_files(mpName):
139137

140138
while keepLooping:
141139
try:
142-
with open(report_file_list[0],"r", encoding='utf-8') as fd:
140+
with open(report_file_list[0],"rb") as fd:
141+
142+
raw = fd.read()
143+
lang, encFmt = getVectorCASTEncoding()
144+
143145
try:
144-
main_soup = BeautifulSoup((fd),features="lxml")
145-
except:
146-
main_soup = BeautifulSoup(fd)
146+
main_soup = BeautifulSoup(fd,features="lxml", from_encoding=encFmt)
147+
except Exception as e:
148+
try:
149+
# Try UTF-8 first
150+
main_soup = BeautifulSoup(raw, "lxml", from_encoding="utf-8")
151+
except Exception:
152+
# Fall back to system ACP (cp936 in China, cp1252 in US)
153+
main_soup = BeautifulSoup(raw, "lxml", from_encoding=encFmt)
147154

148155
preserved_count = 0
149156
executed_count = 0
150157
total_count = 0
151158

152159
if main_soup.find(id="report-title"):
153160
main_manage_api_report = True
154-
# New Manage reports have div with id=report-title
155-
# Want second table (skip config data section)
161+
162+
# New Manage reports have div with id=report-title
163+
# Want second table (skip config data section)
156164
main_row_list = main_soup.find_all('table')[1].tr.find_next_siblings()
157165
main_count_list = main_row_list[-1].th.find_next_siblings()
158166
else:
@@ -270,16 +278,6 @@ def parse_html_files(mpName):
270278

271279
if __name__ == "__main__":
272280

273-
encoding = "utf-8"
274-
try:
275-
if sys.stdout.encoding.lower() != encoding:
276-
sys.stdout.reconfigure(encoding=encoding)
277-
if sys.stderr.encoding.lower() != encoding:
278-
sys.stderr.reconfigure(encoding=encoding)
279-
except AttributeError:
280-
print("except AttributeError")
281-
pass
282-
283281
parser = argparse.ArgumentParser()
284282
parser.add_argument('ManageProject')
285283
parser.add_argument('--rptfmt')

src/main/resources/scripts/managewait.py

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
try:
4242
from safe_open import open
4343
except:
44-
pass
45-
44+
pass
45+
4646
from vcast_utils import getVectorCASTEncoding
4747

4848
class ManageWait(object):
@@ -53,38 +53,50 @@ def __init__(self, verbose, command_line, wait_time, wait_loops, mpName = "", us
5353
self.command_line = command_line
5454
self.mpName = mpName
5555
self.useCI = useCI
56+
self.stop_requested = False
5657

5758
# get the VC langaguge and encoding
5859
self.lang, self.encFmt = getVectorCASTEncoding()
5960

60-
import sys
61+
import sys, locale
6162

6263
try:
63-
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
64-
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
64+
sys.stdout.reconfigure(encoding=self.encFmt, errors="replace")
65+
sys.stderr.reconfigure(encoding=self.encFmt, errors="replace")
6566
except AttributeError:
6667
import io
67-
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
68-
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8", errors="replace")
68+
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding=self.encFmt, errors="replace")
69+
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding=self.encFmt, errors="replace")
70+
6971

70-
7172
def enqueueOutput(self, io_target, queue, logfile):
72-
while True:
73+
74+
while not self.stop_requested:
7375
line = io_target.readline()
76+
if not line:
77+
continue
7478
line = line.rstrip()
79+
7580
if line == '':
7681
continue
7782
output = ( datetime.now().strftime("%H:%M:%S.%f") + " " + line + "\n" )
83+
7884
if not self.silent:
7985
try:
80-
print(line.decode(self.encFmt))
81-
except:
86+
# Python 2: line may be str (bytes) - decode it
87+
if isinstance(line, bytes):
88+
print(line.decode(self.encFmt, "replace"))
89+
else:
90+
print(line) # already unicode/str
91+
except Exception:
8292
pass
8393
try:
8494
logfile.write(output)
85-
except:
86-
logfile.write(output.decode(self.encFmt))
87-
95+
except Exception:
96+
if isinstance(output, bytes):
97+
logfile.write(output.decode(self.encFmt, "replace"))
98+
else:
99+
logfile.write(output)
88100
queue.put(line)
89101

90102
def startOutputThread(self, io_target, logfile):
@@ -98,20 +110,20 @@ def exec_manage_command(self, cmd_line, silent = False):
98110
if self.verbose:
99111
print (self.command_line)
100112
return self.exec_manage(silent)
101-
113+
102114
def exec_manage(self, silent=False):
103115
try:
104-
with open("command.log", 'a', encoding=self.encFmt) as logfile:
116+
with open("command.log", 'a', encoding=self.encFmt, errors="replace") as logfile:
105117
return self.__exec_manage(silent, logfile)
106-
except:
118+
except:
107119
with open("command.log", 'a') as logfile:
108120
return self.__exec_manage(silent, logfile)
109-
121+
110122
def __exec_manage(self, silent, logfile):
111123
self.silent = silent
112124
callStr = os.environ.get('VECTORCAST_DIR') + os.sep + "manage " + self.command_line
113125
ret_out = ''
114-
126+
115127
if self.verbose:
116128
logfile.write( "\nVerbose: %s\n" % callStr)
117129

@@ -123,22 +135,22 @@ def __exec_manage(self, silent, logfile):
123135
p = subprocess.Popen(callStr,stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True, universal_newlines=True, encoding=self.encFmt)
124136
except:
125137
p = subprocess.Popen(callStr,stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True, universal_newlines=True)
126-
138+
127139
self.startOutputThread(p.stdout, logfile)
128-
140+
129141
license_outage = False
130142
edited_license_outage_msg = ""
131143
actual_license_outage_msg = ""
132-
144+
133145
while p.poll() is None or not self.q.empty():
134146
try:
135147
while not self.q.empty():
136-
out_mgt = self.q.get(False)
137-
148+
out_mgt = self.q.get(False)
149+
138150
if len(out_mgt) > 0:
139-
151+
140152
errors = ["Unable to obtain license", "Licensed number of users already reached", "License server system does not support this feature"]
141-
153+
142154
if any(error in out_mgt for error in errors):
143155
license_outage = True
144156
# Change FLEXlm Error to FLEXlm Err.. to avoid Groovy script from
@@ -149,10 +161,13 @@ def __exec_manage(self, silent, logfile):
149161

150162
ret_out += out_mgt + "\n"
151163
time.sleep(0.5)
152-
164+
153165
except Empty:
154166
pass
155167

168+
self.stop_requested = True
169+
self.io_t.join()
170+
156171
# manage finished. Was there a license outage?
157172
if license_outage == True :
158173
if loop_count < self.wait_loops:
@@ -168,17 +183,17 @@ def __exec_manage(self, silent, logfile):
168183
break
169184
else :
170185
break #leave outer while loop
171-
186+
172187
try:
173188
ret_out = unicode(ret_out,self.encFmt)
174189
except:
175190
pass
176-
191+
177192
return ret_out # checked in generate-results.py
178-
193+
179194
## main
180195
if __name__ == '__main__':
181-
196+
182197
parser = argparse.ArgumentParser()
183198
parser.add_argument('-v', '--verbose', help='Enable verbose output', action="store_true")
184199
parser.add_argument('--command_line', help='Command line to pass to Manage', required=True)

src/main/resources/scripts/parallel_full_reports.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def dump(obj):
2525
return str(obj)
2626

2727
def generate_report(params):
28+
29+
print(f"generate report {params}")
2830

2931
key, env_name, env_is_monitored, env_orig_env_dir, env_relative_wd, env_is_coverapi, env_is_ut_api, vcm_file, workspace, jenkins_workspace, VCD = params
3032

@@ -51,12 +53,14 @@ def generate_report(params):
5153
cmd = VCD + "/clicast -e " + env_name + " COVER REPORT AGGREGATE " + report_name
5254
process = subprocess.Popen(cmd.split(), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=build_dir)
5355
stdout, stderr = process.communicate()
56+
print(cmd, stdout)
5457
result = process.returncode
5558

5659
elif env_is_ut_api:
5760
cmd = VCD + "/clicast -e " + env_name + " REPORT CUSTOM FULL " + report_name
5861
process = subprocess.Popen(cmd.split(), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=build_dir)
5962
stdout, stderr = process.communicate()
63+
print(cmd, stdout)
6064
result = process.returncode
6165

6266
else:

src/main/resources/scripts/parse_console_for_cbt.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,14 @@ def parse(self, console_log):
193193

194194
if __name__ == '__main__':
195195

196-
with open(sys.argv[1],"r") as fd:
196+
from vcast_utils import getVectorCASTEncoding
197+
198+
# get the VC langaguge and encoding
199+
lang, encFmt = getVectorCASTEncoding()
200+
201+
with open(sys.argv[1], "r", encoding=encFmt, errors="replace") as fd:
197202
buildLogData = fd.readlines()
198-
203+
199204
parser = ParseConsoleForCBT(True)
200205
parser.parse(buildLogData)
201206
pprint(parser.parse(buildLogData), width=132)

src/main/resources/scripts/safe_open.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import os
2626

2727
from io import open as _open
28+
from vcast_utils import getVectorCASTEncoding
2829

2930
@contextlib.contextmanager
3031
def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):
@@ -35,8 +36,9 @@ def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None,
3536
except:
3637
fd = _open(file, mode, buffering, encoding, errors, newline, closefd)
3738
else:
38-
encoding = "utf-8"
39-
fd = _open(file, mode, buffering, encoding, errors, newline)
39+
lang, encFmt = getVectorCASTEncoding()
40+
41+
fd = _open(file, mode, buffering, encFmt, errors, newline)
4042

4143
try:
4244
yield fd

0 commit comments

Comments
 (0)