| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 """Start and stop Web Page Replay. | 6 """Start and stop Web Page Replay. |
| 7 | 7 |
| 8 Of the public module names, the following one is key: | 8 Of the public module names, the following one is key: |
| 9 ReplayServer: a class to start/stop Web Page Replay. | 9 ReplayServer: a class to start/stop Web Page Replay. |
| 10 """ | 10 """ |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 elif not os.path.exists(self.archive_path): | 109 elif not os.path.exists(self.archive_path): |
| 110 self._CheckPath('archive file', self.archive_path) | 110 self._CheckPath('archive file', self.archive_path) |
| 111 self._CheckPath('replay script', self.replay_py) | 111 self._CheckPath('replay script', self.replay_py) |
| 112 | 112 |
| 113 self.log_fh = None | 113 self.log_fh = None |
| 114 self.replay_process = None | 114 self.replay_process = None |
| 115 | 115 |
| 116 def _AddDefaultReplayOptions(self): | 116 def _AddDefaultReplayOptions(self): |
| 117 """Set WPR command-line options. Can be overridden if needed.""" | 117 """Set WPR command-line options. Can be overridden if needed.""" |
| 118 self.replay_options += [ | 118 self.replay_options += [ |
| 119 '--host', str(self._replay_host), |
| 119 '--port', str(self._http_port), | 120 '--port', str(self._http_port), |
| 120 '--ssl_port', str(self._https_port), | 121 '--ssl_port', str(self._https_port), |
| 121 '--use_closest_match', | 122 '--use_closest_match', |
| 122 '--no-dns_forwarding', | 123 '--no-dns_forwarding', |
| 123 '--log_level', 'warning' | 124 '--log_level', 'warning' |
| 124 ] | 125 ] |
| 125 | 126 |
| 126 def _CheckPath(self, label, path): | 127 def _CheckPath(self, label, path): |
| 127 if not os.path.exists(path): | 128 if not os.path.exists(path): |
| 128 raise ReplayNotFoundError(label, path) | 129 raise ReplayNotFoundError(label, path) |
| 129 | 130 |
| 130 def _OpenLogFile(self): | 131 def _OpenLogFile(self): |
| 131 log_dir = os.path.dirname(self.log_path) | 132 log_dir = os.path.dirname(self.log_path) |
| 132 if not os.path.exists(log_dir): | 133 if not os.path.exists(log_dir): |
| 133 os.makedirs(log_dir) | 134 os.makedirs(log_dir) |
| 134 return open(self.log_path, 'w') | 135 return open(self.log_path, 'w') |
| 135 | 136 |
| 136 def IsStarted(self): | 137 def IsStarted(self): |
| 137 """Checks to see if the server is up and running.""" | 138 """Checks to see if the server is up and running.""" |
| 138 for _ in range(30): | 139 for _ in range(30): |
| 139 if self.replay_process.poll() is not None: | 140 if self.replay_process.poll() is not None: |
| 140 # The process has exited. | 141 # The process has exited. |
| 141 break | 142 break |
| 142 try: | 143 try: |
| 143 up_url = '%s://localhost:%s/web-page-replay-generate-200' | 144 up_url = '%s://%s:%s/web-page-replay-generate-200' |
| 144 http_up_url = up_url % ('http', self._http_port) | 145 http_up_url = up_url % ('http', self._replay_host, self._http_port) |
| 145 https_up_url = up_url % ('https', self._https_port) | 146 https_up_url = up_url % ('https', self._replay_host, self._https_port) |
| 146 if (200 == urllib.urlopen(http_up_url, None, {}).getcode() and | 147 if (200 == urllib.urlopen(http_up_url, None, {}).getcode() and |
| 147 200 == urllib.urlopen(https_up_url, None, {}).getcode()): | 148 200 == urllib.urlopen(https_up_url, None, {}).getcode()): |
| 148 return True | 149 return True |
| 149 except IOError: | 150 except IOError: |
| 150 time.sleep(1) | 151 time.sleep(1) |
| 151 return False | 152 return False |
| 152 | 153 |
| 153 def StartServer(self): | 154 def StartServer(self): |
| 154 """Start Web Page Replay and verify that it started. | 155 """Start Web Page Replay and verify that it started. |
| 155 | 156 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 185 self.log_fh.close() | 186 self.log_fh.close() |
| 186 | 187 |
| 187 def __enter__(self): | 188 def __enter__(self): |
| 188 """Add support for with-statement.""" | 189 """Add support for with-statement.""" |
| 189 self.StartServer() | 190 self.StartServer() |
| 190 return self | 191 return self |
| 191 | 192 |
| 192 def __exit__(self, unused_exc_type, unused_exc_val, unused_exc_tb): | 193 def __exit__(self, unused_exc_type, unused_exc_val, unused_exc_tb): |
| 193 """Add support for with-statement.""" | 194 """Add support for with-statement.""" |
| 194 self.StopServer() | 195 self.StopServer() |
| OLD | NEW |