import os
import sys
import time
import unittest
import subprocess
import shutil
from subprocess import Popen, PIPE
import errno
xml_escape_table = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "<",
}
def xml_escape(text):
return "".join(xml_escape_table.get(c, c) for c in text)
def readfile(rf):
with open(rf, 'r') as f1:
return f1.read()
def test_result_to_xml(result):
returncode, testname, run_time, teststdout, testerr = result
testxml = '<testcase classname="' + testname + '"' \
+ ' name="full log" time="' + str(round(run_time)) + '">'
if returncode != 0:
testxml = testxml + '<failure>' + xml_escape(readfile(testerr)) + '</failure>'
testxml = testxml + '</testcase>\n'
return testxml
def mkpath(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
pyversion = float(sys.version_info[0]) + float(sys.version_info[1]) / 10.0
if pyversion < 3:
str_encode = str
str_decode = str
def pipe_decode(output):
return output
else:
def str_encode(s):
return bytes(s, sys.getdefaultencoding())
def str_decode(bs):
return bs.decode(sys.getdefaultencoding(), "strict")
def pipe_decode(output):
if isinstance(output, bytes) or isinstance(output, bytearray):
return str_decode(output)
elif isinstance(output, tuple):
return str_decode(output[0]), str_decode(output[1])
else:
return "", ""
test_env = os.environ.copy()
def dump_output(working_dir, bname, out, err):
stdout_path = "%s/%s-stdout.txt" % (working_dir, bname)
stdout_fd = open(stdout_path, 'w')
stdout_fd.write(out)
stdout_fd.close()
stderr_path = "%s/%s-stderr.txt" % (working_dir, bname)