| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import json | |
| 6 import optparse | |
| 7 import os | |
| 8 import struct | |
| 9 import sys | |
| 10 import warnings | |
| 11 | |
| 12 # Ignore deprecation warnings, they make our output more cluttered. | |
| 13 warnings.filterwarnings("ignore", category=DeprecationWarning) | |
| 14 | |
| 15 if sys.platform == 'win32': | |
| 16 import msvcrt | |
| 17 | |
| 18 | |
| 19 class Error(Exception): | |
| 20 """Error class for this module.""" | |
| 21 | |
| 22 | |
| 23 class OptionError(Error): | |
| 24 """Error for bad command line options.""" | |
| 25 | |
| 26 | |
| 27 class FileMultiplexer(object): | |
| 28 def __init__(self, fd1, fd2) : | |
| 29 self.__fd1 = fd1 | |
| 30 self.__fd2 = fd2 | |
| 31 | |
| 32 def __del__(self) : | |
| 33 if self.__fd1 != sys.stdout and self.__fd1 != sys.stderr: | |
| 34 self.__fd1.close() | |
| 35 if self.__fd2 != sys.stdout and self.__fd2 != sys.stderr: | |
| 36 self.__fd2.close() | |
| 37 | |
| 38 def write(self, text) : | |
| 39 self.__fd1.write(text) | |
| 40 self.__fd2.write(text) | |
| 41 | |
| 42 def flush(self) : | |
| 43 self.__fd1.flush() | |
| 44 self.__fd2.flush() | |
| 45 | |
| 46 | |
| 47 class TestServerRunner(object): | |
| 48 """Runs a test server and communicates with the controlling C++ test code. | |
| 49 | |
| 50 Subclasses should override the create_server method to create their server | |
| 51 object, and the add_options method to add their own options. | |
| 52 """ | |
| 53 | |
| 54 def __init__(self): | |
| 55 self.option_parser = optparse.OptionParser() | |
| 56 self.add_options() | |
| 57 | |
| 58 def main(self): | |
| 59 self.options, self.args = self.option_parser.parse_args() | |
| 60 | |
| 61 logfile = open('testserver.log', 'w') | |
| 62 sys.stderr = FileMultiplexer(sys.stderr, logfile) | |
| 63 if self.options.log_to_console: | |
| 64 sys.stdout = FileMultiplexer(sys.stdout, logfile) | |
| 65 else: | |
| 66 sys.stdout = logfile | |
| 67 | |
| 68 server_data = { | |
| 69 'host': self.options.host, | |
| 70 } | |
| 71 self.server = self.create_server(server_data) | |
| 72 self._notify_startup_complete(server_data) | |
| 73 self.run_server() | |
| 74 | |
| 75 def create_server(self, server_data): | |
| 76 """Creates a server object and returns it. | |
| 77 | |
| 78 Must populate server_data['port'], and can set additional server_data | |
| 79 elements if desired.""" | |
| 80 raise NotImplementedError() | |
| 81 | |
| 82 def run_server(self): | |
| 83 try: | |
| 84 self.server.serve_forever() | |
| 85 except KeyboardInterrupt: | |
| 86 print 'shutting down server' | |
| 87 self.server.stop = True | |
| 88 | |
| 89 def add_options(self): | |
| 90 self.option_parser.add_option('--startup-pipe', type='int', | |
| 91 dest='startup_pipe', | |
| 92 help='File handle of pipe to parent process') | |
| 93 self.option_parser.add_option('--log-to-console', action='store_const', | |
| 94 const=True, default=False, | |
| 95 dest='log_to_console', | |
| 96 help='Enables or disables sys.stdout logging ' | |
| 97 'to the console.') | |
| 98 self.option_parser.add_option('--port', default=0, type='int', | |
| 99 help='Port used by the server. If ' | |
| 100 'unspecified, the server will listen on an ' | |
| 101 'ephemeral port.') | |
| 102 self.option_parser.add_option('--host', default='127.0.0.1', | |
| 103 dest='host', | |
| 104 help='Hostname or IP upon which the server ' | |
| 105 'will listen. Client connections will also ' | |
| 106 'only be allowed from this address.') | |
| 107 | |
| 108 def _notify_startup_complete(self, server_data): | |
| 109 # Notify the parent that we've started. (BaseServer subclasses | |
| 110 # bind their sockets on construction.) | |
| 111 if self.options.startup_pipe is not None: | |
| 112 server_data_json = json.dumps(server_data) | |
| 113 server_data_len = len(server_data_json) | |
| 114 print 'sending server_data: %s (%d bytes)' % ( | |
| 115 server_data_json, server_data_len) | |
| 116 if sys.platform == 'win32': | |
| 117 fd = msvcrt.open_osfhandle(self.options.startup_pipe, 0) | |
| 118 else: | |
| 119 fd = self.options.startup_pipe | |
| 120 startup_pipe = os.fdopen(fd, "w") | |
| 121 # First write the data length as an unsigned 4-byte value. This | |
| 122 # is _not_ using network byte ordering since the other end of the | |
| 123 # pipe is on the same machine. | |
| 124 startup_pipe.write(struct.pack('=L', server_data_len)) | |
| 125 startup_pipe.write(server_data_json) | |
| 126 startup_pipe.close() | |
| OLD | NEW |