| OLD | NEW |
| 1 #!/bin/env python | 1 #!/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 a local apache http server.""" | 6 """A class to help start/stop a local apache http server.""" |
| 7 | 7 |
| 8 import logging | 8 import logging |
| 9 import optparse | 9 import optparse |
| 10 import os | 10 import os |
| 11 import subprocess | 11 import subprocess |
| 12 import sys | 12 import sys |
| 13 import time | 13 import time |
| 14 import urllib | 14 import urllib |
| 15 | 15 |
| 16 import google.path_utils | 16 import google.path_utils |
| 17 import google.platform_utils | 17 import google.platform_utils |
| 18 | 18 |
| 19 class HttpdNotStarted(Exception): pass | 19 class HttpdNotStarted(Exception): pass |
| 20 | 20 |
| 21 def UrlIsAlive(url): |
| 22 """Checks to see if we get an http response from |url|. |
| 23 We poll the url 5 times with a 1 second delay. If we don't |
| 24 get a reply in that time, we give up and assume the httpd |
| 25 didn't start properly. |
| 26 |
| 27 Args: |
| 28 url: The URL to check. |
| 29 Return: |
| 30 True if the url is alive. |
| 31 """ |
| 32 wait_time = 5 |
| 33 while wait_time > 0: |
| 34 try: |
| 35 response = urllib.urlopen(url) |
| 36 # Server is up and responding. |
| 37 return True |
| 38 except IOError: |
| 39 pass |
| 40 wait_time -= 1 |
| 41 # Wait a second and try again. |
| 42 time.sleep(1) |
| 43 |
| 44 return False |
| 45 |
| 21 def ApacheConfigDir(start_dir): | 46 def ApacheConfigDir(start_dir): |
| 22 """Returns a path to the directory holding the Apache config files.""" | 47 """Returns a path to the directory holding the Apache config files.""" |
| 23 return google.path_utils.FindUpward(start_dir, 'tools', 'python', | 48 return google.path_utils.FindUpward(start_dir, 'tools', 'python', |
| 24 'google', 'httpd_config') | 49 'google', 'httpd_config') |
| 25 | 50 |
| 26 | 51 |
| 27 def GetCygserverPath(start_dir, apache2=False): | 52 def GetCygserverPath(start_dir, apache2=False): |
| 28 """Returns the path to the directory holding cygserver.exe file.""" | 53 """Returns the path to the directory holding cygserver.exe file.""" |
| 29 cygserver_path = None | 54 cygserver_path = None |
| 30 if apache2: | 55 if apache2: |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 cygbin = google.path_utils.FindUpward(cygserver_exe, 'third_party', | 140 cygbin = google.path_utils.FindUpward(cygserver_exe, 'third_party', |
| 116 'cygwin', 'bin') | 141 'cygwin', 'bin') |
| 117 env = os.environ | 142 env = os.environ |
| 118 env['PATH'] += ";" + cygbin | 143 env['PATH'] += ";" + cygbin |
| 119 subprocess.Popen(cygserver_exe, env=env) | 144 subprocess.Popen(cygserver_exe, env=env) |
| 120 logging.info('Starting http server') | 145 logging.info('Starting http server') |
| 121 self._http_server_proc = subprocess.Popen(self._start_command) | 146 self._http_server_proc = subprocess.Popen(self._start_command) |
| 122 | 147 |
| 123 # Ensure that the server is running on all the desired ports. | 148 # Ensure that the server is running on all the desired ports. |
| 124 for port in self._port_list: | 149 for port in self._port_list: |
| 125 if not self._UrlIsAlive('http://127.0.0.1:%s/' % str(port)): | 150 if not UrlIsAlive('http://127.0.0.1:%s/' % str(port)): |
| 126 raise HttpdNotStarted('Failed to start httpd on port %s' % str(port)) | 151 raise HttpdNotStarted('Failed to start httpd on port %s' % str(port)) |
| 127 | 152 |
| 128 def _UrlIsAlive(self, url): | |
| 129 """Checks to see if we get an http response from |url|. | |
| 130 We poll the url 5 times with a 1 second delay. If we don't | |
| 131 get a reply in that time, we give up and assume the httpd | |
| 132 didn't start properly. | |
| 133 | |
| 134 Args: | |
| 135 url: The URL to check. | |
| 136 Return: | |
| 137 True if the url is alive. | |
| 138 """ | |
| 139 wait_time = 5 | |
| 140 while wait_time > 0: | |
| 141 try: | |
| 142 response = urllib.urlopen(url) | |
| 143 # Server is up and responding. | |
| 144 return True | |
| 145 except IOError: | |
| 146 pass | |
| 147 wait_time -= 1 | |
| 148 # Wait a second and try again. | |
| 149 time.sleep(1) | |
| 150 | |
| 151 return False | |
| 152 | |
| 153 def StopServer(self, force=False): | 153 def StopServer(self, force=False): |
| 154 """If we started an httpd.exe process, or if force is True, call | 154 """If we started an httpd.exe process, or if force is True, call |
| 155 self._stop_command (passed in on init so it can be platform-dependent). | 155 self._stop_command (passed in on init so it can be platform-dependent). |
| 156 This will presumably kill it, and may also kill any other httpd.exe | 156 This will presumably kill it, and may also kill any other httpd.exe |
| 157 processes that are running. | 157 processes that are running. |
| 158 """ | 158 """ |
| 159 if force or self._http_server_proc: | 159 if force or self._http_server_proc: |
| 160 logging.info('Stopping http server') | 160 logging.info('Stopping http server') |
| 161 kill_proc = subprocess.Popen(self._stop_command, | 161 kill_proc = subprocess.Popen(self._stop_command, |
| 162 stdout=subprocess.PIPE, | 162 stdout=subprocess.PIPE, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 187 | 187 |
| 188 document_root = None | 188 document_root = None |
| 189 if options.root: | 189 if options.root: |
| 190 document_root = options.root | 190 document_root = options.root |
| 191 | 191 |
| 192 if 'start' == options.server: | 192 if 'start' == options.server: |
| 193 StartServer(document_root, apache2=options.apache2) | 193 StartServer(document_root, apache2=options.apache2) |
| 194 else: | 194 else: |
| 195 StopServers(apache2=options.apache2) | 195 StopServers(apache2=options.apache2) |
| 196 | 196 |
| OLD | NEW |