OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import logging |
| 6 import os |
| 7 import signal |
| 8 import subprocess |
| 9 import time |
| 10 import urllib |
| 11 |
| 12 class ReplayLauncher: |
| 13 HTTP_PORT = 8080 |
| 14 HTTPS_PORT = 8413 |
| 15 UP_URL = 'http://localhost:%s/web-page-replay-generate-200' % HTTP_PORT |
| 16 |
| 17 def __init__(self, replay_dir, data_dir, log_dir): |
| 18 self.replay_dir = replay_dir |
| 19 self.data_dir = data_dir |
| 20 self.log_dir = log_dir |
| 21 self.log_fh = None |
| 22 self.proxy_process = None |
| 23 |
| 24 def StartServer(self): |
| 25 logging.debug('Starting Web-Page-Replay') |
| 26 cmd_line = [ |
| 27 os.path.join(self.replay_dir, 'replay.py'), |
| 28 '--no-dns_forwarding', |
| 29 '--port', str(self.HTTP_PORT), |
| 30 '--ssl_port', str(self.HTTPS_PORT), |
| 31 # TODO(slamm): Add traffic shaping (requires root): '--net', 'fios', |
| 32 os.path.join(self.data_dir, '2012Q2', 'data.wpr') |
| 33 ] |
| 34 if not os.path.exists(self.log_dir): |
| 35 os.makedirs(self.log_dir) |
| 36 log_name = os.path.join(self.log_dir, 'log.txt') |
| 37 self.log_fh = open(log_name, 'w') |
| 38 self.proxy_process = subprocess.Popen( |
| 39 cmd_line, stdout=self.log_fh, stderr=subprocess.STDOUT) |
| 40 if not self.IsStarted(): |
| 41 raise Exception('Web Page Replay failed to start. See the log file: ' + |
| 42 log_name) |
| 43 |
| 44 def IsStarted(self): |
| 45 for _ in range(5): |
| 46 if self.proxy_process.poll() is not None: |
| 47 # The process has exited. |
| 48 break |
| 49 try: |
| 50 if 200 == urllib.urlopen(self.UP_URL, None, {}).getcode(): |
| 51 return True |
| 52 except IOError: |
| 53 time.sleep(1) |
| 54 self.StopServer() |
| 55 return False |
| 56 |
| 57 def StopServer(self): |
| 58 if self.proxy_process: |
| 59 logging.debug('Stopping Web-Page-Replay') |
| 60 # Use a SIGINT here so that it can do graceful cleanup. |
| 61 # Otherwise, we will leave subprocesses hanging. |
| 62 self.proxy_process.send_signal(signal.SIGINT) |
| 63 self.proxy_process.wait() |
| 64 if self.log_fh: |
| 65 self.log_fh.close() |
OLD | NEW |