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 ones are key: | 8 Of the public module names, the following one is key: |
9 CHROME_FLAGS: Chrome options to make it work with Web Page Replay. | |
10 ReplayServer: a class to start/stop Web Page Replay. | 9 ReplayServer: a class to start/stop Web Page Replay. |
11 """ | 10 """ |
12 | 11 |
13 import logging | 12 import logging |
14 import os | 13 import os |
15 import signal | 14 import signal |
16 import subprocess | 15 import subprocess |
17 import sys | 16 import sys |
18 import time | 17 import time |
19 import urllib | 18 import urllib |
20 | 19 |
21 | 20 |
22 HTTP_PORT = 8080 | |
23 HTTPS_PORT = 8413 | |
24 REPLAY_HOST='127.0.0.1' | |
25 CHROME_FLAGS = [ | |
26 '--host-resolver-rules=MAP * %s,EXCLUDE localhost' % REPLAY_HOST, | |
27 '--testing-fixed-http-port=%s' % HTTP_PORT, | |
28 '--testing-fixed-https-port=%s' % HTTPS_PORT, | |
29 '--ignore-certificate-errors', | |
30 ] | |
31 | |
32 _CHROME_BASE_DIR = os.path.abspath(os.path.join( | 21 _CHROME_BASE_DIR = os.path.abspath(os.path.join( |
33 os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, os.pardir)) | 22 os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, os.pardir)) |
34 REPLAY_DIR = os.path.join( | 23 REPLAY_DIR = os.path.join( |
35 _CHROME_BASE_DIR, 'src', 'third_party', 'webpagereplay') | 24 _CHROME_BASE_DIR, 'src', 'third_party', 'webpagereplay') |
36 LOG_PATH = os.path.join( | 25 LOG_PATH = os.path.join( |
37 _CHROME_BASE_DIR, 'src', 'webpagereplay_logs', 'logs.txt') | 26 _CHROME_BASE_DIR, 'src', 'webpagereplay_logs', 'logs.txt') |
38 | 27 |
39 | 28 |
| 29 # Chrome options to make it work with Web Page Replay. |
| 30 def GetChromeFlags(replay_host, http_port, https_port): |
| 31 return [ |
| 32 '--host-resolver-rules=MAP * %s,EXCLUDE localhost' % replay_host, |
| 33 '--testing-fixed-http-port=%s' % http_port, |
| 34 '--testing-fixed-https-port=%s' % https_port, |
| 35 '--ignore-certificate-errors', |
| 36 ] |
| 37 |
40 class ReplayError(Exception): | 38 class ReplayError(Exception): |
41 """Catch-all exception for the module.""" | 39 """Catch-all exception for the module.""" |
42 pass | 40 pass |
43 | 41 |
44 class ReplayNotFoundError(ReplayError): | 42 class ReplayNotFoundError(ReplayError): |
45 def __init__(self, label, path): | 43 def __init__(self, label, path): |
46 self.args = (label, path) | 44 self.args = (label, path) |
47 | 45 |
48 def __str__(self): | 46 def __str__(self): |
49 label, path = self.args | 47 label, path = self.args |
(...skipping 15 matching lines...) Expand all Loading... |
65 Example: | 63 Example: |
66 with ReplayServer(archive_path): | 64 with ReplayServer(archive_path): |
67 self.NavigateToURL(start_url) | 65 self.NavigateToURL(start_url) |
68 self.WaitUntil(...) | 66 self.WaitUntil(...) |
69 | 67 |
70 Environment Variables (for development): | 68 Environment Variables (for development): |
71 WPR_ARCHIVE_PATH: path to alternate archive file (e.g. '/tmp/foo.wpr'). | 69 WPR_ARCHIVE_PATH: path to alternate archive file (e.g. '/tmp/foo.wpr'). |
72 WPR_RECORD: if set, puts Web Page Replay in record mode instead of replay. | 70 WPR_RECORD: if set, puts Web Page Replay in record mode instead of replay. |
73 WPR_REPLAY_DIR: path to alternate Web Page Replay source. | 71 WPR_REPLAY_DIR: path to alternate Web Page Replay source. |
74 """ | 72 """ |
75 def __init__(self, archive_path, replay_options=None, replay_dir=None, | 73 def __init__(self, archive_path, replay_host, http_port, https_port, |
| 74 replay_options=None, replay_dir=None, |
76 log_path=None): | 75 log_path=None): |
77 """Initialize ReplayServer. | 76 """Initialize ReplayServer. |
78 | 77 |
79 Args: | 78 Args: |
80 archive_path: a path to a specific WPR archive (required). | 79 archive_path: a path to a specific WPR archive (required). |
81 replay_options: an iterable of options strings to forward to replay.py. | 80 replay_options: an iterable of options strings to forward to replay.py. |
82 replay_dir: directory that has replay.py and related modules. | 81 replay_dir: directory that has replay.py and related modules. |
83 log_path: a path to a log file. | 82 log_path: a path to a log file. |
84 """ | 83 """ |
85 self.archive_path = os.environ.get('WPR_ARCHIVE_PATH', archive_path) | 84 self.archive_path = os.environ.get('WPR_ARCHIVE_PATH', archive_path) |
86 self.replay_options = list(replay_options or ()) | 85 self.replay_options = list(replay_options or ()) |
87 self.replay_dir = os.environ.get('WPR_REPLAY_DIR', replay_dir or REPLAY_DIR) | 86 self.replay_dir = os.environ.get('WPR_REPLAY_DIR', replay_dir or REPLAY_DIR) |
88 self.log_path = log_path or LOG_PATH | 87 self.log_path = log_path or LOG_PATH |
| 88 self._http_port = http_port |
| 89 self._https_port = https_port |
| 90 self._replay_host = replay_host |
89 | 91 |
90 if 'WPR_RECORD' in os.environ and '--record' not in self.replay_options: | 92 if 'WPR_RECORD' in os.environ and '--record' not in self.replay_options: |
91 self.replay_options.append('--record') | 93 self.replay_options.append('--record') |
92 self.is_record_mode = '--record' in self.replay_options | 94 self.is_record_mode = '--record' in self.replay_options |
93 self._AddDefaultReplayOptions() | 95 self._AddDefaultReplayOptions() |
94 | 96 |
95 self.replay_py = os.path.join(self.replay_dir, 'replay.py') | 97 self.replay_py = os.path.join(self.replay_dir, 'replay.py') |
96 | 98 |
97 if self.is_record_mode: | 99 if self.is_record_mode: |
98 self._CheckPath('archive directory', os.path.dirname(self.archive_path)) | 100 self._CheckPath('archive directory', os.path.dirname(self.archive_path)) |
99 elif not os.path.exists(self.archive_path): | 101 elif not os.path.exists(self.archive_path): |
100 self._CheckPath('archive file', self.archive_path) | 102 self._CheckPath('archive file', self.archive_path) |
101 self._CheckPath('replay script', self.replay_py) | 103 self._CheckPath('replay script', self.replay_py) |
102 | 104 |
103 self.log_fh = None | 105 self.log_fh = None |
104 self.replay_process = None | 106 self.replay_process = None |
105 | 107 |
106 def _AddDefaultReplayOptions(self): | 108 def _AddDefaultReplayOptions(self): |
107 """Set WPR command-line options. Can be overridden if needed.""" | 109 """Set WPR command-line options. Can be overridden if needed.""" |
108 self.replay_options += [ | 110 self.replay_options += [ |
109 '--port', str(HTTP_PORT), | 111 '--port', str(self._http_port), |
110 '--ssl_port', str(HTTPS_PORT), | 112 '--ssl_port', str(self._https_port), |
111 '--use_closest_match', | 113 '--use_closest_match', |
112 '--no-dns_forwarding', | 114 '--no-dns_forwarding', |
113 ] | 115 ] |
114 | 116 |
115 def _CheckPath(self, label, path): | 117 def _CheckPath(self, label, path): |
116 if not os.path.exists(path): | 118 if not os.path.exists(path): |
117 raise ReplayNotFoundError(label, path) | 119 raise ReplayNotFoundError(label, path) |
118 | 120 |
119 def _OpenLogFile(self): | 121 def _OpenLogFile(self): |
120 log_dir = os.path.dirname(self.log_path) | 122 log_dir = os.path.dirname(self.log_path) |
121 if not os.path.exists(log_dir): | 123 if not os.path.exists(log_dir): |
122 os.makedirs(log_dir) | 124 os.makedirs(log_dir) |
123 return open(self.log_path, 'w') | 125 return open(self.log_path, 'w') |
124 | 126 |
125 def IsStarted(self): | 127 def IsStarted(self): |
126 """Checks to see if the server is up and running.""" | 128 """Checks to see if the server is up and running.""" |
127 for _ in range(5): | 129 for _ in range(5): |
128 if self.replay_process.poll() is not None: | 130 if self.replay_process.poll() is not None: |
129 # The process has exited. | 131 # The process has exited. |
130 break | 132 break |
131 try: | 133 try: |
132 up_url = '%s://localhost:%s/web-page-replay-generate-200' | 134 up_url = '%s://localhost:%s/web-page-replay-generate-200' |
133 http_up_url = up_url % ('http', HTTP_PORT) | 135 http_up_url = up_url % ('http', self._http_port) |
134 https_up_url = up_url % ('https', HTTPS_PORT) | 136 https_up_url = up_url % ('https', self._https_port) |
135 if (200 == urllib.urlopen(http_up_url, None, {}).getcode() and | 137 if (200 == urllib.urlopen(http_up_url, None, {}).getcode() and |
136 200 == urllib.urlopen(https_up_url, None, {}).getcode()): | 138 200 == urllib.urlopen(https_up_url, None, {}).getcode()): |
137 return True | 139 return True |
138 except IOError: | 140 except IOError: |
139 time.sleep(1) | 141 time.sleep(1) |
140 return False | 142 return False |
141 | 143 |
142 def StartServer(self): | 144 def StartServer(self): |
143 """Start Web Page Replay and verify that it started. | 145 """Start Web Page Replay and verify that it started. |
144 | 146 |
(...skipping 27 matching lines...) Expand all Loading... |
172 self.log_fh.close() | 174 self.log_fh.close() |
173 | 175 |
174 def __enter__(self): | 176 def __enter__(self): |
175 """Add support for with-statement.""" | 177 """Add support for with-statement.""" |
176 self.StartServer() | 178 self.StartServer() |
177 return self | 179 return self |
178 | 180 |
179 def __exit__(self, unused_exc_type, unused_exc_val, unused_exc_tb): | 181 def __exit__(self, unused_exc_type, unused_exc_val, unused_exc_tb): |
180 """Add support for with-statement.""" | 182 """Add support for with-statement.""" |
181 self.StopServer() | 183 self.StopServer() |
OLD | NEW |