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 ones are key: |
9 CHROME_FLAGS: Chrome options to make it work with Web Page Replay. | 9 CHROME_FLAGS: Chrome options to make it work with Web Page Replay. |
10 ReplayServer: a class to start/stop Web Page Replay. | 10 ReplayServer: a class to start/stop Web Page Replay. |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 WPR_ARCHIVE_PATH: path to alternate archive file (e.g. '/tmp/foo.wpr'). | 70 WPR_ARCHIVE_PATH: path to alternate archive file (e.g. '/tmp/foo.wpr'). |
71 WPR_RECORD: if set, puts Web Page Replay in record mode instead of replay. | 71 WPR_RECORD: if set, puts Web Page Replay in record mode instead of replay. |
72 WPR_REPLAY_DIR: path to alternate Web Page Replay source. | 72 WPR_REPLAY_DIR: path to alternate Web Page Replay source. |
73 """ | 73 """ |
74 def __init__(self, archive_path, replay_options=None, replay_dir=None, | 74 def __init__(self, archive_path, replay_options=None, replay_dir=None, |
75 log_path=None): | 75 log_path=None): |
76 """Initialize ReplayServer. | 76 """Initialize ReplayServer. |
77 | 77 |
78 Args: | 78 Args: |
79 archive_path: a path to a specific WPR archive (required). | 79 archive_path: a path to a specific WPR archive (required). |
80 replay_options: a list of options strings to forward to replay.py. | 80 replay_options: an iterable of options strings to forward to replay.py. |
81 replay_dir: directory that has replay.py and related modules. | 81 replay_dir: directory that has replay.py and related modules. |
82 log_path: a path to a log file. | 82 log_path: a path to a log file. |
83 """ | 83 """ |
84 self.archive_path = os.environ.get('WPR_ARCHIVE_PATH', archive_path) | 84 self.archive_path = os.environ.get('WPR_ARCHIVE_PATH', archive_path) |
85 self.replay_options = replay_options or [] | 85 self.replay_options = list(replay_options or ()) |
86 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) |
87 self.log_path = log_path or LOG_PATH | 87 self.log_path = log_path or LOG_PATH |
88 | 88 |
89 if 'WPR_RECORD' in os.environ and '--record' not in self.replay_options: | 89 if 'WPR_RECORD' in os.environ and '--record' not in self.replay_options: |
90 self.replay_options.append('--record') | 90 self.replay_options.append('--record') |
91 self.is_record_mode = '--record' in self.replay_options | 91 self.is_record_mode = '--record' in self.replay_options |
92 self._AddDefaultReplayOptions() | 92 self._AddDefaultReplayOptions() |
93 | 93 |
94 self.replay_py = os.path.join(self.replay_dir, 'replay.py') | 94 self.replay_py = os.path.join(self.replay_dir, 'replay.py') |
95 | 95 |
96 if self.is_record_mode: | 96 if self.is_record_mode: |
97 self._CheckPath('archive directory', os.path.dirname(self.archive_path)) | 97 self._CheckPath('archive directory', os.path.dirname(self.archive_path)) |
98 elif not os.path.exists(self.archive_path): | 98 elif not os.path.exists(self.archive_path): |
99 self._CheckPath('archive file', self.archive_path) | 99 self._CheckPath('archive file', self.archive_path) |
100 self._CheckPath('replay script', self.replay_py) | 100 self._CheckPath('replay script', self.replay_py) |
101 | 101 |
102 self.log_fh = None | 102 self.log_fh = None |
103 self.replay_process = None | 103 self.replay_process = None |
104 | 104 |
105 def _AddDefaultReplayOptions(self): | 105 def _AddDefaultReplayOptions(self): |
106 """Set WPR command-line options. Can be overridden if needed.""" | 106 """Set WPR command-line options. Can be overridden if needed.""" |
107 self.replay_options += [ | 107 self.replay_options += [ |
108 '--port', str(HTTP_PORT), | 108 '--port', str(HTTP_PORT), |
109 '--ssl_port', str(HTTPS_PORT), | 109 '--ssl_port', str(HTTPS_PORT), |
110 '--use_closest_match', | 110 '--use_closest_match', |
111 '--no-dns_forwarding', | 111 '--no-dns_forwarding', |
112 # '--net', 'fios', # TODO(slamm): Add traffic shaping (requires root). | |
113 ] | 112 ] |
114 | 113 |
115 def _CheckPath(self, label, path): | 114 def _CheckPath(self, label, path): |
116 if not os.path.exists(path): | 115 if not os.path.exists(path): |
117 raise ReplayNotFoundError(label, path) | 116 raise ReplayNotFoundError(label, path) |
118 | 117 |
119 def _OpenLogFile(self): | 118 def _OpenLogFile(self): |
120 log_dir = os.path.dirname(self.log_path) | 119 log_dir = os.path.dirname(self.log_path) |
121 if not os.path.exists(log_dir): | 120 if not os.path.exists(log_dir): |
122 os.makedirs(log_dir) | 121 os.makedirs(log_dir) |
(...skipping 23 matching lines...) Expand all Loading... |
146 ReplayNotStartedError if Replay start-up fails. | 145 ReplayNotStartedError if Replay start-up fails. |
147 """ | 146 """ |
148 cmd_line = [self.replay_py] | 147 cmd_line = [self.replay_py] |
149 cmd_line.extend(self.replay_options) | 148 cmd_line.extend(self.replay_options) |
150 cmd_line.append(self.archive_path) | 149 cmd_line.append(self.archive_path) |
151 self.log_fh = self._OpenLogFile() | 150 self.log_fh = self._OpenLogFile() |
152 logging.debug('Starting Web-Page-Replay: %s', cmd_line) | 151 logging.debug('Starting Web-Page-Replay: %s', cmd_line) |
153 self.replay_process = subprocess.Popen( | 152 self.replay_process = subprocess.Popen( |
154 cmd_line, stdout=self.log_fh, stderr=subprocess.STDOUT) | 153 cmd_line, stdout=self.log_fh, stderr=subprocess.STDOUT) |
155 if not self.IsStarted(): | 154 if not self.IsStarted(): |
| 155 log = open(self.log_path).read() |
156 raise ReplayNotStartedError( | 156 raise ReplayNotStartedError( |
157 'Web Page Replay failed to start. See the log file: ' + self.log_name) | 157 'Web Page Replay failed to start. Log output:\n%s' % log) |
158 | 158 |
159 def StopServer(self): | 159 def StopServer(self): |
160 """Stop Web Page Replay.""" | 160 """Stop Web Page Replay.""" |
161 if self.replay_process: | 161 if self.replay_process: |
162 logging.debug('Stopping Web-Page-Replay') | 162 logging.debug('Stopping Web-Page-Replay') |
163 # Use a SIGINT here so that it can do graceful cleanup. | 163 # Use a SIGINT here so that it can do graceful cleanup. |
164 # Otherwise, we will leave subprocesses hanging. | 164 # Otherwise, we will leave subprocesses hanging. |
165 self.replay_process.send_signal(signal.SIGINT) | 165 self.replay_process.send_signal(signal.SIGINT) |
166 self.replay_process.wait() | 166 self.replay_process.wait() |
167 if self.log_fh: | 167 if self.log_fh: |
168 self.log_fh.close() | 168 self.log_fh.close() |
169 | 169 |
170 def __enter__(self): | 170 def __enter__(self): |
171 """Add support for with-statement.""" | 171 """Add support for with-statement.""" |
172 self.StartServer() | 172 self.StartServer() |
173 return self | 173 return self |
174 | 174 |
175 def __exit__(self, unused_exc_type, unused_exc_val, unused_exc_tb): | 175 def __exit__(self, unused_exc_type, unused_exc_val, unused_exc_tb): |
176 """Add support for with-statement.""" | 176 """Add support for with-statement.""" |
177 self.StopServer() | 177 self.StopServer() |
OLD | NEW |