| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-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 lighttpd server used by layout tests.""" | 6 """A class to help start/stop the lighttpd 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 shutil | 12 import shutil |
| 13 import subprocess | 13 import subprocess |
| 14 import sys | 14 import sys |
| 15 import tempfile | 15 import tempfile |
| 16 import time | 16 import time |
| 17 import urllib | 17 import urllib |
| 18 | 18 |
| 19 import path_utils | 19 import path_utils |
| 20 import google.httpd_utils |
| 20 | 21 |
| 21 def RemoveLogFiles(folder, starts_with): | 22 def RemoveLogFiles(folder, starts_with): |
| 22 files = os.listdir(folder) | 23 files = os.listdir(folder) |
| 23 for file in files: | 24 for file in files: |
| 24 if file.startswith(starts_with) : | 25 if file.startswith(starts_with) : |
| 25 full_path = os.path.join(folder, file) | 26 full_path = os.path.join(folder, file) |
| 26 os.remove(full_path) | 27 os.remove(full_path) |
| 27 | 28 |
| 28 class HttpdNotStarted(Exception): | |
| 29 pass | |
| 30 | |
| 31 class Lighttpd: | 29 class Lighttpd: |
| 32 # Webkit tests | 30 # Webkit tests |
| 33 try: | 31 try: |
| 34 _webkit_tests = path_utils.PathFromBase('third_party', 'WebKit', | 32 _webkit_tests = path_utils.PathFromBase('third_party', 'WebKit', |
| 35 'LayoutTests', 'http', 'tests') | 33 'LayoutTests', 'http', 'tests') |
| 36 _js_test_resource = path_utils.PathFromBase('third_party', 'WebKit', | 34 _js_test_resource = path_utils.PathFromBase('third_party', 'WebKit', |
| 37 'LayoutTests', 'fast', | 35 'LayoutTests', 'fast', |
| 38 'js', 'resources') | 36 'js', 'resources') |
| 39 except path_utils.PathNotFound: | 37 except path_utils.PathNotFound: |
| 40 # If third_party/WebKit/LayoutTests/http/tests does not exist, assume wekit | 38 # If third_party/WebKit/LayoutTests/http/tests does not exist, assume wekit |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 logging.info('Starting http server') | 211 logging.info('Starting http server') |
| 214 self._process = subprocess.Popen(start_cmd, env=env) | 212 self._process = subprocess.Popen(start_cmd, env=env) |
| 215 | 213 |
| 216 # Wait for server to start. | 214 # Wait for server to start. |
| 217 time.sleep(3) | 215 time.sleep(3) |
| 218 | 216 |
| 219 # Ensure that the server is running on all the desired ports. | 217 # Ensure that the server is running on all the desired ports. |
| 220 for mapping in mappings: | 218 for mapping in mappings: |
| 221 url = 'http%s://127.0.0.1:%d/' % ('sslcert' in mapping and 's' or '', | 219 url = 'http%s://127.0.0.1:%d/' % ('sslcert' in mapping and 's' or '', |
| 222 mapping['port']) | 220 mapping['port']) |
| 223 if not self._UrlIsAlive(url): | 221 if not google.httpd_utils.UrlIsAlive(url): |
| 224 raise HttpdNotStarted('Failed to start httpd on port %s' % | 222 raise google.httpd_utils.HttpdNotStarted('Failed to start httpd on ', |
| 225 str(mapping['port'])) | 223 'port %s' % |
| 224 str(mapping['port'])) |
| 226 | 225 |
| 227 # Our process terminated already | 226 # Our process terminated already |
| 228 if self._process.returncode != None: | 227 if self._process.returncode != None: |
| 229 raise HttpdNotStarted('Failed to start httpd.') | 228 raise google.httpd_utils.HttpdNotStarted('Failed to start httpd.') |
| 230 | |
| 231 def _UrlIsAlive(self, url): | |
| 232 """Checks to see if we get an http response from |url|. | |
| 233 We poll the url 5 times with a 3 second delay. If we don't | |
| 234 get a reply in that time, we give up and assume the httpd | |
| 235 didn't start properly. | |
| 236 | |
| 237 Args: | |
| 238 url: The URL to check. | |
| 239 Return: | |
| 240 True if the url is alive. | |
| 241 """ | |
| 242 attempts = 5 | |
| 243 while attempts > 0: | |
| 244 try: | |
| 245 response = urllib.urlopen(url) | |
| 246 # Server is up and responding. | |
| 247 return True | |
| 248 except IOError: | |
| 249 pass | |
| 250 attempts -= 1 | |
| 251 # Wait 3 seconds and try again. | |
| 252 time.sleep(3) | |
| 253 | |
| 254 return False | |
| 255 | 229 |
| 256 # TODO(deanm): Find a nicer way to shutdown cleanly. Our log files are | 230 # TODO(deanm): Find a nicer way to shutdown cleanly. Our log files are |
| 257 # probably not being flushed, etc... why doesn't our python have os.kill ? | 231 # probably not being flushed, etc... why doesn't our python have os.kill ? |
| 258 def Stop(self, force=False): | 232 def Stop(self, force=False): |
| 259 if not force and not self.IsRunning(): | 233 if not force and not self.IsRunning(): |
| 260 return | 234 return |
| 261 | 235 |
| 262 logging.info('Shutting down http server') | 236 logging.info('Shutting down http server') |
| 263 path_utils.ShutDownHTTPServer(self._process) | 237 path_utils.ShutDownHTTPServer(self._process) |
| 264 | 238 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 raise 'Specifying port requires also a root.' | 270 raise 'Specifying port requires also a root.' |
| 297 httpd = Lighttpd(tempfile.gettempdir(), | 271 httpd = Lighttpd(tempfile.gettempdir(), |
| 298 port=options.port, | 272 port=options.port, |
| 299 root=options.root, | 273 root=options.root, |
| 300 register_cygwin=options.register_cygwin, | 274 register_cygwin=options.register_cygwin, |
| 301 run_background=options.run_background) | 275 run_background=options.run_background) |
| 302 if 'start' == options.server: | 276 if 'start' == options.server: |
| 303 httpd.Start() | 277 httpd.Start() |
| 304 else: | 278 else: |
| 305 httpd.Stop(force=True) | 279 httpd.Stop(force=True) |
| OLD | NEW |