From 26403e55e0daf10fd790238d9005d87431c16e57 Mon Sep 17 00:00:00 2001 From: Timofey Date: Fri, 5 Jun 2015 17:08:49 +0300 Subject: [PATCH] Cleaned up the code, fixed the bug that prevented the XML logs to be written on Python 2.6 --- tools/testlog_to_xml_converter.py | 11 +++-- tools/testserver.py | 78 ++++++++++++------------------- 2 files changed, 37 insertions(+), 52 deletions(-) diff --git a/tools/testlog_to_xml_converter.py b/tools/testlog_to_xml_converter.py index af7a93afa7..945fb3947f 100755 --- a/tools/testlog_to_xml_converter.py +++ b/tools/testlog_to_xml_converter.py @@ -49,11 +49,16 @@ class TestInfo: local_comment = "" if self.test_comment is not None: local_comment = self.test_comment - return "{suite}::{name}: {comment} -> {result}\n".format(suite=self.test_suite, name=self.test_name, comment=local_comment, result=self.test_result) + return "{suite}::{name}: {comment} -> {result}\n".format(suite=self.test_suite, + name=self.test_name, + comment=local_comment, + result=self.test_result) def xml(self): - d = ElementTree.Element("testcase", {"name":self.test_name, "classname":self.test_suite, "time":str(self.test_duration)}) + d = ElementTree.Element("testcase", {"name":self.test_name, + "classname":self.test_suite, + "time":str(self.test_duration)}) if self.test_comment is not None: b = ElementTree.SubElement(d, "system-err") b.text = self.test_comment @@ -107,7 +112,7 @@ class Parser: def write_xml_file(self): - ElementTree.ElementTree(self.root).write(self.xml_file, encoding="UTF-8", xml_declaration=True) + ElementTree.ElementTree(self.root).write(self.xml_file, encoding="UTF-8") def usage(): diff --git a/tools/testserver.py b/tools/testserver.py index ace16f34f2..36e08135df 100644 --- a/tools/testserver.py +++ b/tools/testserver.py @@ -48,7 +48,7 @@ from scipy.stats.stats import trim_mean PORT = 34568 -LIFESPAN = 18000.0 # timeout for the self destruction timer - how much time +LIFESPAN = 180.0 # timeout for the self destruction timer - how much time # passes between the last request and the server killing # itself PING_TIMEOUT = 5 # Nubmer of seconds to wait for ping response @@ -62,11 +62,11 @@ class InternalServer(HTTPServer): print("The server's life has come to an end") self.shutdown() - + def reset_selfdestruct_timer(self): if self.self_destruct_timer is not None: self.self_destruct_timer.cancel() - + self.self_destruct_timer = Timer(LIFESPAN, self.kill_me) self.self_destruct_timer.start() @@ -85,14 +85,14 @@ class InternalServer(HTTPServer): if self.clients == 0: if self.self_destruct_timer is not None: self.self_destruct_timer.cancel() - + quick_and_painless_timer = Timer(0.1, self.kill_me) quick_and_painless_timer.start() class TestServer: - + def __init__(self): self.server = None html = str() @@ -102,7 +102,7 @@ class TestServer: html = response.read() except (urllib2.URLError, socket.timeout): print("The server does not currently serve...") - + if html != "pong": print("html != pong") try: @@ -117,7 +117,7 @@ class TestServer: def init_server(self): self.server = InternalServer(('localhost', PORT), PostHandler) - + def start_serving(self): if self.server is not None: thread = threading.Thread(target=self.server.serve_forever) @@ -135,16 +135,16 @@ class TestServer: def kill_siblings(self): output = gen_to_list(re.sub("\s{1,}", " ", x.strip()) for x in exec_command("ps -w")[1].split("\n")) - + my_pid = str(os.getpid()) - + my_name = map(lambda x: x.split(" ")[4], # the name of python script filter(lambda x: x.startswith(my_pid), output))[0] siblings = filter(lambda x: x != my_pid, map(lambda x: x.split(" ")[0], filter(lambda x: my_name in x, output))) - + if len(siblings) > 0: command = "kill {siblings}".format(siblings=" ".join(siblings)) exec_command(command) @@ -160,14 +160,14 @@ def gen_to_list(generator): BIG_FILE_SIZE = 47684 - + class PostHandler(BaseHTTPRequestHandler): def init_vars(self): self.byterange = None self.is_chunked = False self.response_code = 200 - + def do_POST(self): self.init_vars() @@ -175,7 +175,6 @@ class PostHandler(BaseHTTPRequestHandler): print("URL is: " + self.path) ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) -# print ("My ctype == {}".format(ctype)) if ctype == 'multipart/form-data': self.send_response(500) self.end_headers() @@ -190,14 +189,12 @@ class PostHandler(BaseHTTPRequestHandler): self.end_headers() self.wfile.write(data) - -# print("\n\n~~~\n\n") def do_GET(self): self.init_vars() self.chunk_requested() - + self.server.reset_selfdestruct_timer() switch = {"/unit_tests/1.txt": self.test1, "/unit_tests/notexisting_unittest": self.test_404, @@ -207,12 +204,9 @@ class PostHandler(BaseHTTPRequestHandler): "/kill": self.kill, } switch[self.path]() -# print("\n\n~~~\n\n") -# return def chunk_requested(self): - the_range = self.headers.get('Range') if the_range is not None: self.is_chunked = True @@ -220,8 +214,7 @@ class PostHandler(BaseHTTPRequestHandler): meaningful_string = the_range[6:] first, last = meaningful_string.split("-") self.byterange = (int(first), int(last)) - -# print("My headers are: {0}".format( self.headers)) + def trim_message(self, message): if not self.is_chunked: @@ -229,6 +222,7 @@ class PostHandler(BaseHTTPRequestHandler): m = message[self.byterange[0]: self.byterange[1] + 1] return m + def pong(self): self.server.clients += 1 self.send_response(200) @@ -242,31 +236,24 @@ class PostHandler(BaseHTTPRequestHandler): message.append(chr(i / 256)) message.append(chr(i % 256)) -# print("{}{}\n".format(i/256, i%256)) - return "".join(message) - - - + def test1(self): message = "Test1" message = self.trim_message(message) - - -# print("This is what TEST1 is about to write: {}; {};".format(message, self.response_code)) - + self.send_response(self.response_code) self.send_header("Content-Length", len(message)) self.end_headers() self.wfile.write(message) - + def test_404(self): self.send_response(404) self.end_headers() - + def test_301(self): self.send_response(301) self.send_header("Location", "google.com") @@ -280,29 +267,22 @@ class PostHandler(BaseHTTPRequestHandler): self.byterange = (0, BIG_FILE_SIZE) else: length = min([length, self.byterange[1] - self.byterange[0] + 1]) - -# print ("The length of message to return is: {0}".format(length)) - + self.send_header("Content-Length", length) if self.byterange is not None: - self.send_header("Content-Range", "bytes {start}-{end}/{out_of}".format(start=self.byterange[0], end=self.byterange[1], out_of=BIG_FILE_SIZE)) + self.send_header("Content-Range", + "bytes {start}-{end}/{out_of}".format(start=self.byterange[0], + end=self.byterange[1], + out_of=BIG_FILE_SIZE)) self.end_headers() message = self.message_for_47kb_file() if message is None: print("The message is none for some reason") - self.wfile.write(self.message_for_47kb_file()[self.byterange[0]: self.byterange[1] + 1]) -# for i in range(0, length + 1): -# try: -# self.wfile.write(chr(i%256)) -# except IOError, e: -# if e.errno == errno.EPIPE: -# print("This is indeed a broken pipe") - - # EPIPE error - - + self.wfile.write(self.message_for_47kb_file()[self.byterange[0] : self.byterange[1] + 1]) + + def kill(self): message = "Bye..." self.send_response(200) @@ -310,8 +290,8 @@ class PostHandler(BaseHTTPRequestHandler): self.end_headers() self.wfile.write(message) self.server.suicide() - - + + if __name__ == '__main__': server = TestServer() server.start_serving()