OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """A class to help start/stop the PyWebSocket server used by layout tests.""" | 6 """A class to help start/stop the PyWebSocket server used by layout tests.""" |
7 | 7 |
8 | 8 |
9 import logging | 9 import logging |
10 import optparse | 10 import optparse |
11 import os | 11 import os |
12 import subprocess | 12 import subprocess |
13 import sys | 13 import sys |
14 import tempfile | 14 import tempfile |
15 import time | 15 import time |
16 | 16 |
17 import path_utils | 17 import path_utils |
18 import platform_utils | 18 import platform_utils |
19 import http_server | 19 import http_server |
20 | 20 |
21 # So we can import httpd_utils below to make ui_tests happy. | 21 # So we can import httpd_utils below to make ui_tests happy. |
22 sys.path.append(path_utils.PathFromBase('tools', 'python')) | 22 sys.path.append(path_utils.PathFromBase('tools', 'python')) |
23 import google.httpd_utils | 23 import google.httpd_utils |
24 | 24 |
25 _WS_LOG_PREFIX = 'pywebsocket.ws.log-' | 25 _WS_LOG_PREFIX = 'pywebsocket.ws.log-' |
26 _WSS_LOG_PREFIX = 'pywebsocket.wss.log-' | 26 _WSS_LOG_PREFIX = 'pywebsocket.wss.log-' |
27 | 27 |
28 _DEFAULT_WS_PORT = 8880 | 28 _DEFAULT_WS_PORT = 8880 |
29 _DEFAULT_WSS_PORT = 9323 | 29 _DEFAULT_WSS_PORT = 9323 |
30 _DEFAULT_ROOT = '.' | |
31 | 30 |
32 | 31 |
33 def RemoveLogFiles(folder, starts_with): | 32 def RemoveLogFiles(folder, starts_with): |
34 files = os.listdir(folder) | 33 files = os.listdir(folder) |
35 for file in files: | 34 for file in files: |
36 if file.startswith(starts_with) : | 35 if file.startswith(starts_with) : |
37 full_path = os.path.join(folder, file) | 36 full_path = os.path.join(folder, file) |
38 os.remove(full_path) | 37 os.remove(full_path) |
39 | 38 |
40 | 39 |
41 class PyWebSocketNotStarted(Exception): | 40 class PyWebSocketNotStarted(Exception): |
42 pass | 41 pass |
43 | 42 |
44 | 43 |
45 class PyWebSocket(http_server.Lighttpd): | 44 class PyWebSocket(http_server.Lighttpd): |
46 def __init__(self, output_dir, port=_DEFAULT_WS_PORT, | 45 def __init__(self, output_dir, port=_DEFAULT_WS_PORT, |
47 root=_DEFAULT_ROOT, | |
48 use_tls=False, | 46 use_tls=False, |
49 private_key=http_server.Lighttpd._pem_file, | 47 private_key=http_server.Lighttpd._pem_file, |
50 certificate=http_server.Lighttpd._pem_file): | 48 certificate=http_server.Lighttpd._pem_file): |
51 """Args: | 49 """Args: |
52 output_dir: the absolute path to the layout test result directory | 50 output_dir: the absolute path to the layout test result directory |
53 """ | 51 """ |
54 self._output_dir = output_dir | 52 self._output_dir = output_dir |
55 self._process = None | 53 self._process = None |
56 self._port = port | 54 self._port = port |
57 self._root = root | |
58 self._use_tls = use_tls | 55 self._use_tls = use_tls |
59 self._private_key = private_key | 56 self._private_key = private_key |
60 self._certificate = certificate | 57 self._certificate = certificate |
61 if self._port: | 58 if self._port: |
62 self._port = int(self._port) | 59 self._port = int(self._port) |
63 if self._use_tls: | 60 if self._use_tls: |
64 self._server_name = 'PyWebSocket(Secure)' | 61 self._server_name = 'PyWebSocket(Secure)' |
65 else: | 62 else: |
66 self._server_name = 'PyWebSocket' | 63 self._server_name = 'PyWebSocket' |
67 | 64 |
68 # Webkit tests | 65 # Webkit tests |
69 try: | 66 try: |
70 self._webkit_tests = path_utils.PathFromBase( | 67 self._web_socket_tests = path_utils.PathFromBase( |
71 'third_party', 'WebKit', 'LayoutTests', 'websocket', 'tests') | 68 'third_party', 'WebKit', 'LayoutTests', 'websocket', 'tests') |
| 69 self._layout_tests = path_utils.PathFromBase( |
| 70 'third_party', 'WebKit', 'LayoutTests') |
72 except path_utils.PathNotFound: | 71 except path_utils.PathNotFound: |
73 self._webkit_tests = None | 72 self._web_socket_tests = None |
74 | 73 |
75 def Start(self): | 74 def Start(self): |
76 if not self._webkit_tests: | 75 if not self._web_socket_tests: |
77 logging.info('No need to start %s server.' % self._server_name) | 76 logging.info('No need to start %s server.' % self._server_name) |
78 return | 77 return |
79 if self.IsRunning(): | 78 if self.IsRunning(): |
80 raise PyWebSocketNotStarted('%s is already running.' % self._server_name) | 79 raise PyWebSocketNotStarted('%s is already running.' % self._server_name) |
81 | 80 |
82 time_str = time.strftime('%d%b%Y-%H%M%S') | 81 time_str = time.strftime('%d%b%Y-%H%M%S') |
83 if self._use_tls: | 82 if self._use_tls: |
84 log_prefix = _WSS_LOG_PREFIX | 83 log_prefix = _WSS_LOG_PREFIX |
85 else: | 84 else: |
86 log_prefix = _WS_LOG_PREFIX | 85 log_prefix = _WS_LOG_PREFIX |
87 log_file_name = log_prefix + time_str + '.txt' | 86 log_file_name = log_prefix + time_str + '.txt' |
88 error_log = os.path.join(self._output_dir, log_file_name) | 87 error_log = os.path.join(self._output_dir, log_file_name) |
89 | 88 |
90 # Remove old log files. We only need to keep the last ones. | 89 # Remove old log files. We only need to keep the last ones. |
91 RemoveLogFiles(self._output_dir, log_prefix) | 90 RemoveLogFiles(self._output_dir, log_prefix) |
92 | 91 |
93 python_interp = sys.executable | 92 python_interp = sys.executable |
94 pywebsocket_base = path_utils.PathFromBase('third_party', 'pywebsocket') | 93 pywebsocket_base = path_utils.PathFromBase('third_party', 'pywebsocket') |
95 pywebsocket_script = path_utils.PathFromBase( | 94 pywebsocket_script = path_utils.PathFromBase( |
96 'third_party', 'pywebsocket', 'mod_pywebsocket', 'standalone.py') | 95 'third_party', 'pywebsocket', 'mod_pywebsocket', 'standalone.py') |
97 start_cmd = [ | 96 start_cmd = [ |
98 python_interp, pywebsocket_script, | 97 python_interp, pywebsocket_script, |
99 '-p', str(self._port), | 98 '-p', str(self._port), |
100 '-d', self._webkit_tests, | 99 '-d', self._layout_tests, |
| 100 '-s', self._web_socket_tests, |
101 ] | 101 ] |
102 if self._use_tls: | 102 if self._use_tls: |
103 start_cmd.extend(['-t', '-k', self._private_key, | 103 start_cmd.extend(['-t', '-k', self._private_key, |
104 '-c', self._certificate]) | 104 '-c', self._certificate]) |
105 | 105 |
106 # Put the cygwin directory first in the path to find cygwin1.dll | 106 # Put the cygwin directory first in the path to find cygwin1.dll |
107 env = os.environ | 107 env = os.environ |
108 if sys.platform in ('cygwin', 'win32'): | 108 if sys.platform in ('cygwin', 'win32'): |
109 env['PATH'] = '%s;%s' % ( | 109 env['PATH'] = '%s;%s' % ( |
110 path_utils.PathFromBase('third_party', 'cygwin', 'bin'), | 110 path_utils.PathFromBase('third_party', 'cygwin', 'bin'), |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 # Wait a bit to make sure the ports are free'd up | 150 # Wait a bit to make sure the ports are free'd up |
151 time.sleep(2) | 151 time.sleep(2) |
152 | 152 |
153 | 153 |
154 if '__main__' == __name__: | 154 if '__main__' == __name__: |
155 # Provide some command line params for starting the PyWebSocket server | 155 # Provide some command line params for starting the PyWebSocket server |
156 # manually. | 156 # manually. |
157 option_parser = optparse.OptionParser() | 157 option_parser = optparse.OptionParser() |
158 option_parser.add_option('-p', '--port', dest='port', | 158 option_parser.add_option('-p', '--port', dest='port', |
159 default=None, help='Port to listen on') | 159 default=None, help='Port to listen on') |
160 option_parser.add_option('-r', '--root', dest='root', default='.', | |
161 help='Absolute path to DocumentRoot') | |
162 option_parser.add_option('-t', '--tls', dest='use_tls', action='store_true', | 160 option_parser.add_option('-t', '--tls', dest='use_tls', action='store_true', |
163 default=False, help='use TLS (wss://)') | 161 default=False, help='use TLS (wss://)') |
164 option_parser.add_option('-k', '--private_key', dest='private_key', | 162 option_parser.add_option('-k', '--private_key', dest='private_key', |
165 default='', help='TLS private key file.') | 163 default='', help='TLS private key file.') |
166 option_parser.add_option('-c', '--certificate', dest='certificate', | 164 option_parser.add_option('-c', '--certificate', dest='certificate', |
167 default='', help='TLS certificate file.') | 165 default='', help='TLS certificate file.') |
168 options, args = option_parser.parse_args() | 166 options, args = option_parser.parse_args() |
169 | 167 |
170 if not options.port: | 168 if not options.port: |
171 if options.use_tls: | 169 if options.use_tls: |
172 options.port = _DEFAULT_WSS_PORT | 170 options.port = _DEFAULT_WSS_PORT |
173 else: | 171 else: |
174 options.port = _DEFAULT_WS_PORT | 172 options.port = _DEFAULT_WS_PORT |
175 | 173 |
176 kwds = {'port':options.port, | 174 kwds = {'port':options.port, |
177 'root':options.root, | |
178 'use_tls':options.use_tls} | 175 'use_tls':options.use_tls} |
179 if options.private_key: | 176 if options.private_key: |
180 kwds['private_key'] = options.private_key | 177 kwds['private_key'] = options.private_key |
181 if options.certificate: | 178 if options.certificate: |
182 kwds['certificate'] = options.certificate | 179 kwds['certificate'] = options.certificate |
183 | 180 |
184 pywebsocket = PyWebSocket(tempfile.gettempdir(), **kwds) | 181 pywebsocket = PyWebSocket(tempfile.gettempdir(), **kwds) |
185 pywebsocket.Start() | 182 pywebsocket.Start() |
OLD | NEW |