Index: scripts/slave/webpagereplay.py |
=================================================================== |
--- scripts/slave/webpagereplay.py (revision 0) |
+++ scripts/slave/webpagereplay.py (revision 0) |
@@ -0,0 +1,65 @@ |
+# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import logging |
+import os |
+import signal |
+import subprocess |
+import time |
+import urllib |
+ |
+class ReplayLauncher: |
+ HTTP_PORT = 8080 |
+ HTTPS_PORT = 8413 |
+ UP_URL = 'http://localhost:%s/web-page-replay-generate-200' % HTTP_PORT |
+ |
+ def __init__(self, replay_dir, data_dir, log_dir): |
+ self.replay_dir = replay_dir |
+ self.data_dir = data_dir |
+ self.log_dir = log_dir |
+ self.log_fh = None |
+ self.proxy_process = None |
+ |
+ def StartServer(self): |
+ logging.debug('Starting Web-Page-Replay') |
+ cmd_line = [ |
+ os.path.join(self.replay_dir, 'replay.py'), |
+ '--no-dns_forwarding', |
+ '--port', str(self.HTTP_PORT), |
+ '--ssl_port', str(self.HTTPS_PORT), |
+ # TODO(slamm): Add traffic shaping (requires root): '--net', 'fios', |
+ os.path.join(self.data_dir, '2012Q2', 'data.wpr') |
+ ] |
+ if not os.path.exists(self.log_dir): |
+ os.makedirs(self.log_dir) |
+ log_name = os.path.join(self.log_dir, 'log.txt') |
+ self.log_fh = open(log_name, 'w') |
+ self.proxy_process = subprocess.Popen( |
+ cmd_line, stdout=self.log_fh, stderr=subprocess.STDOUT) |
+ if not self.IsStarted(): |
+ raise Exception('Web Page Replay failed to start. See the log file: ' + |
+ log_name) |
+ |
+ def IsStarted(self): |
+ for _ in range(5): |
+ if self.proxy_process.poll() is not None: |
+ # The process has exited. |
+ break |
+ try: |
+ if 200 == urllib.urlopen(self.UP_URL, None, {}).getcode(): |
+ return True |
+ except IOError: |
+ time.sleep(1) |
+ self.StopServer() |
+ return False |
+ |
+ def StopServer(self): |
+ if self.proxy_process: |
+ logging.debug('Stopping Web-Page-Replay') |
+ # Use a SIGINT here so that it can do graceful cleanup. |
+ # Otherwise, we will leave subprocesses hanging. |
+ self.proxy_process.send_signal(signal.SIGINT) |
+ self.proxy_process.wait() |
+ if self.log_fh: |
+ self.log_fh.close() |