Skip to content

Commit cb0c685

Browse files
committed
Updates to get different encodings working in Jenkins
1 parent 837ad8d commit cb0c685

13 files changed

+184
-62
lines changed

src/main/resources/scripts/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
# THE SOFTWARE.
2323
#
24+

src/main/resources/scripts/baselinePostBuild.groovy

Lines changed: 111 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,59 @@
11
import hudson.FilePath
2+
import java.nio.charset.*
3+
import java.nio.ByteBuffer
4+
import java.nio.CharBuffer
5+
6+
import java.io.StringWriter
7+
import java.io.PrintWriter
8+
import hudson.model.Result
9+
10+
out = manager.listener.logger // same as console log
11+
out.println "Post Build Groovy: Starting"
12+
13+
14+
// Returns only the decoded text; logs the charset used.
15+
def readWithFallback(FilePath fp) {
16+
byte[] bytes
17+
fp.read().withStream { is -> bytes = is.readAllBytes() }
18+
19+
// Quick BOM hint
20+
String bomHint = null
21+
if (bytes.length >= 3 && bytes[0]==(byte)0xEF && bytes[1]==(byte)0xBB && bytes[2]==(byte)0xBF) bomHint = 'UTF-8'
22+
else if (bytes.length >= 2 && bytes[0]==(byte)0xFF && bytes[1]==(byte)0xFE) bomHint = 'UTF-16LE'
23+
else if (bytes.length >= 2 && bytes[0]==(byte)0xFE && bytes[1]==(byte)0xFF) bomHint = 'UTF-16BE'
24+
25+
List<String> charsets = [
26+
'UTF-8', 'UTF-16LE', 'UTF-16BE',
27+
'GB18030', 'GBK',
28+
'windows-31j', 'Shift_JIS', 'EUC-JP', 'ISO-2022-JP',
29+
'MS949', 'x-windows-949', 'EUC-KR', 'ISO-2022-KR',
30+
'windows-1252', 'ISO-8859-1'
31+
].unique()
32+
33+
if (bomHint && charsets.contains(bomHint)) {
34+
charsets = [bomHint] + (charsets - bomHint)
35+
}
36+
37+
for (String name : charsets) {
38+
try {
39+
Charset cs = Charset.forName(name)
40+
CharsetDecoder dec = cs.newDecoder()
41+
.onMalformedInput(CodingErrorAction.REPORT)
42+
.onUnmappableCharacter(CodingErrorAction.REPORT)
43+
CharBuffer cb = dec.decode(ByteBuffer.wrap(bytes))
44+
out.println "Post Build Groovy: Decoded ${fp.getName()} with charset: ${name}"
45+
return cb.toString()
46+
} catch (CharacterCodingException ignore) {
47+
// try next
48+
} catch (Exception ignore) {
49+
// try next
50+
}
51+
}
52+
53+
// Fallback: ISO-8859-1
54+
out.println "Post Build Groovy: Fall Back Decode ${fp.getName()} with charset: ISO-8859-1 (fallback)"
55+
return new String(bytes, Charset.forName('ISO-8859-1'))
56+
}
257

358
Boolean buildFailed = false
459
Boolean buildUnstable = false
@@ -84,55 +139,71 @@ if(manager.logContains(".*Abnormal Termination on Environment.*")) {
84139
manager.addBadge("icon-error icon-xlg", "Abnormal Termination of at least one Environment")
85140
}
86141

87-
def debugInfo = ""
88-
def summaryStr = ""
142+
try {
143+
def summaryStr = ""
89144

90-
FilePath fp_cd = new FilePath(manager.build.getWorkspace(),'coverage_diffs.html_tmp')
91-
FilePath fp_i = new FilePath(manager.build.getWorkspace(),'@PROJECT_BASE@_rebuild.html_tmp')
92-
FilePath fp_f = new FilePath(manager.build.getWorkspace(),'@PROJECT_BASE@_full_report.html_tmp')
93-
FilePath fp_m = new FilePath(manager.build.getWorkspace(),'@PROJECT_BASE@_metrics_report.html_tmp')
145+
FilePath fp_cd = new FilePath(manager.build.getWorkspace(),'coverage_diffs.html_tmp')
146+
FilePath fp_i = new FilePath(manager.build.getWorkspace(),'@PROJECT_BASE@_rebuild.html_tmp')
147+
FilePath fp_f = new FilePath(manager.build.getWorkspace(),'@PROJECT_BASE@_full_report.html_tmp')
148+
FilePath fp_m = new FilePath(manager.build.getWorkspace(),'@PROJECT_BASE@_metrics_report.html_tmp')
94149

95-
if (fp_cd.exists()) {
96-
summaryStr = "<hr style=\"height:5px;border-width:0;color:gray;background-color:gray\"> "
97-
summaryStr += fp_cd.readToString()
98-
}
150+
if (fp_cd.exists()) {
151+
summaryStr = "<hr style=\"height:5px;border-width:0;color:gray;background-color:gray\"> "
152+
summaryStr += readWithFallback(fp_cd)
99153

100-
if (fp_i.exists()) {
101-
summaryStr += "<hr style=\"height:5px;border-width:0;color:gray;background-color:gray\"> "
102-
summaryStr += fp_i.readToString()
103-
}
154+
}
104155

105-
if (fp_f.exists()) {
106-
summaryStr += "<hr style=\"height:5px;border-width:0;color:gray;background-color:gray\"> "
107-
summaryStr += fp_f.readToString()
108-
}
156+
if (fp_i.exists()) {
157+
summaryStr += "<hr style=\"height:5px;border-width:0;color:gray;background-color:gray\"> "
158+
summaryStr += readWithFallback(fp_i)
159+
}
109160

110-
if (fp_m.exists())
111-
{
112-
summaryStr += "<hr style=\"height:5px;border-width:0;color:gray;background-color:gray\"> "
113-
summaryStr += fp_m.readToString()
161+
if (fp_f.exists()) {
162+
summaryStr += "<hr style=\"height:5px;border-width:0;color:gray;background-color:gray\"> "
163+
summaryStr += readWithFallback(fp_f)
164+
}
114165

115-
}
166+
if (fp_m.exists())
167+
{
168+
summaryStr += "<hr style=\"height:5px;border-width:0;color:gray;background-color:gray\"> "
169+
summaryStr += readWithFallback(fp_m)
170+
}
116171

117-
manager.createSummary("icon-orange-square icon-xlg").appendText(summaryStr, false)
172+
manager.createSummary("icon-orange-square icon-xlg").appendText(summaryStr, false)
118173

119-
if (!fp_f.exists() && !fp_m.exists())
120-
{
121-
manager.createSummary("icon-error icon-xlg").appendText("General Failure", false, false, false, "red")
122-
manager.build.description = "General Failure, Incremental Build Report or Full Report Not Present. Please see the console for more information"
123-
manager.addBadge("icon-error icon-xlg", "General Error")
174+
if (!fp_f.exists() && !fp_m.exists())
175+
{
176+
manager.createSummary("icon-error icon-xlg").appendText("General Failure", false, false, false, "red")
177+
manager.build.description = "General Failure, Incremental Build Report or Full Report Not Present. Please see the console for more information"
178+
manager.addBadge("icon-error icon-xlg", "General Error")
124179

125-
if (!buildFailed) {
126-
buildUnstable = true
180+
if (!buildFailed) {
181+
buildUnstable = true
182+
}
127183
}
128-
}
129184

130-
if (buildFailed)
131-
{
132-
manager.buildFailure()
133-
}
134-
if (buildUnstable)
135-
{
136-
manager.buildUnstable()
137-
}
185+
if (buildFailed)
186+
{
187+
manager.buildFailure()
188+
}
189+
if (buildUnstable)
190+
{
191+
manager.buildUnstable()
192+
}
193+
194+
} catch (Throwable t) {
195+
out.println "Post Build Groovy: Failed reading the reports"
196+
197+
def sw = new StringWriter()
198+
t.printStackTrace(new PrintWriter(sw))
138199

200+
out.println "Post Build Groovy: ERROR: ${t.class.name}: ${t.message}"
201+
out.println sw.toString() // full stack with file/line numbers when available
202+
203+
// Mark build as failed (or UNSTABLE if you prefer)
204+
manager.build.setResult(Result.FAILURE)
205+
206+
// Re-throw if you want the Post Build Groovy: step itself to error out:
207+
throw t
208+
}
209+
out.println "Post Build Groovy: Complete - No errors"

src/main/resources/scripts/cobertura.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
from collections import defaultdict
3838
from pprint import pprint
3939
import argparse
40+
try:
41+
from safe_open import open
42+
except:
43+
pass
4044

4145
fileList = []
4246

@@ -52,7 +56,7 @@ def write_xml(x, name, verbose = False):
5256

5357
xml_str += etree.tostring(x,pretty_print=True).decode()
5458

55-
open(name + ".xml", "w").write(xml_str)
59+
with open(name + ".xml", "w") as fd: fd.write(xml_str)
5660

5761
def getCoveredFunctionCount(source):
5862
if len(source.functions) == 0:
@@ -86,7 +90,12 @@ def getFileXML(testXml, coverAPI, verbose = False, extended = False, source_root
8690

8791

8892
fname = coverAPI.display_name
89-
fpath = os.path.relpath(coverAPI.display_path,prj_dir).replace("\\","/")
93+
fpath = coverAPI.display_path
94+
try:
95+
fpath = os.path.relpath(fpath,prj_dir).replace("\\","/")
96+
except:
97+
fpath = fpath.replace("\\","/")
98+
pass
9099

91100
branch_totals = float(coverAPI.metrics.branches + coverAPI.metrics.mcdc_branches)
92101
branch_covered = float(coverAPI.metrics.max_covered_branches + coverAPI.metrics.max_covered_mcdc_branches)
@@ -447,7 +456,11 @@ def runCoberturaResults(packages, api, verbose = False, extended = False, source
447456

448457
fname = file.display_name
449458
fpath = file.display_path.rsplit('.',1)[0]
450-
fpath = os.path.relpath(fpath,prj_dir).replace("\\","/")
459+
try:
460+
fpath = os.path.relpath(fpath,prj_dir).replace("\\","/")
461+
except:
462+
fpath = fpath.replace("\\","/")
463+
pass
451464

452465
# print("*", file.name, file.display_name, fpath)
453466

src/main/resources/scripts/fixup_reports.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@
4141
sys.path.append(python_path_updates)
4242

4343
from bs4 import BeautifulSoup
44-
from safe_open import open
44+
try:
45+
from safe_open import open
46+
except:
47+
pass
4548

4649
import tee_print
4750

src/main/resources/scripts/generate_lcov.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
import argparse
4242

4343
from vcast_utils import dump, checkVectorCASTVersion
44+
try:
45+
from safe_open import open
46+
except:
47+
pass
4448

4549
fileList = []
4650

@@ -155,7 +159,10 @@ def runGcovResults(api, verbose = False, testName = "", source_root = "") :
155159

156160
fname = file.display_name
157161
fpath = file.display_path.rsplit('.',1)[0]
158-
fpath = os.path.relpath(fpath,prj_dir).replace("\\","/")
162+
try:
163+
fpath = os.path.relpath(fpath,prj_dir).replace("\\","/")
164+
except:
165+
fpath = fpath.replace("\\","/")
159166

160167
fileDict[fpath] = file
161168

@@ -316,7 +323,7 @@ def generateCoverageResults(inFile, xml_data_dir = "xml_data", verbose = False,
316323
os.makedirs(lcov_data_dir)
317324

318325
pathToInfo = os.path.join(lcov_data_dir, name + ".info")
319-
open(pathToInfo, "w").write(output)
326+
with open(pathToInfo, "w") as fd: fd.write(output)
320327

321328
cmdStr = "genhtml " + pathToInfo + " --output-directory out"
322329
cmdArr = cmdStr.split()

src/main/resources/scripts/generate_pclp_reports.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def parse_msgs(filename):
6868
try:
6969
adjustedFname = os.path.relpath(child.find('file').text,basepath).replace("\\","/")
7070
except:
71-
adjustedFname = child.find('file').text
71+
adjustedFname = child.find('file').text.replace("\\","/")
7272

7373
if adjustedFname is None:
7474
adjustedFname = "GLOBAL"

src/main/resources/scripts/generate_sonarqube_pclp_reports.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919

2020
from pprint import pprint
2121

22+
try:
23+
from safe_open import open
24+
except:
25+
pass
26+
2227
# PC-lint Plus message representation and parsing
2328

2429
class Message:

src/main/resources/scripts/generate_xml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ def skipReporting(self, env):
874874
try:
875875
build_dir = os.path.relpath(env.build_directory,prj_dir).replace("\\","/")
876876
except:
877-
build_dir = env.build_directory
877+
build_dir = env.build_directory.replace("\\","/")
878878

879879
try:
880880
build_dir = "build/" + build_dir.rsplit("build/",1)[-1]
@@ -1308,7 +1308,7 @@ def write_testcase(self, tc, unit_name, func_name, st_is_monitored = False, unit
13081308
try:
13091309
fpath = os.path.relpath(filePath,prj_dir).replace("\\","/")
13101310
except:
1311-
fpath = filePath
1311+
fpath = filePath.replace("\\","/")
13121312

13131313
if self.useStartLine:
13141314
try:

src/main/resources/scripts/incremental_build_report_aggregator.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,16 @@ def parse_html_files(mpName):
270270

271271
if __name__ == "__main__":
272272

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+
273283
parser = argparse.ArgumentParser()
274284
parser.add_argument('ManageProject')
275285
parser.add_argument('--rptfmt')

src/main/resources/scripts/managewait.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,16 @@ def __init__(self, verbose, command_line, wait_time, wait_loops, mpName = "", us
5757
# get the VC langaguge and encoding
5858
self.lang, self.encFmt = getVectorCASTEncoding()
5959

60-
os.environ['PYTHONIOENCODING'] = self.encFmt
60+
import sys
61+
62+
try:
63+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
64+
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
65+
except AttributeError:
66+
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")
69+
6170

6271
def enqueueOutput(self, io_target, queue, logfile):
6372
while True:
@@ -67,7 +76,10 @@ def enqueueOutput(self, io_target, queue, logfile):
6776
continue
6877
output = ( datetime.now().strftime("%H:%M:%S.%f") + " " + line + "\n" )
6978
if not self.silent:
70-
print(line)
79+
try:
80+
print(line.decode(self.encFmt))
81+
except:
82+
pass
7183
try:
7284
logfile.write(output)
7385
except:

0 commit comments

Comments
 (0)