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.
11 from telemetry.core import util
13 class TemporaryHTTPServer(object):
14 def __init__(self, browser_backend, paths):
17 self._forwarder = None
20 assert os.path.exists(path), '%s does not exist.' % path
21 self._paths = set(map(os.path.realpath, paths))
23 common_prefix = os.path.commonprefix(self._paths)
24 if os.path.isdir(common_prefix):
25 self._base_dir = common_prefix
27 self._base_dir = os.path.dirname(common_prefix)
29 self._devnull = open(os.devnull, 'w')
30 cmd = [sys.executable, '-m', 'memory_cache_http_server']
31 cmd.extend(self._paths)
32 env = os.environ.copy()
33 env['PYTHONPATH'] = os.path.abspath(os.path.dirname(__file__))
34 self._server = subprocess.Popen(cmd, cwd=self._base_dir,
35 env=env, stdout=subprocess.PIPE, stderr=self._devnull)
38 '.*(?P<protocol>HTTPS?) server started on (?P<host>.*):(?P<port>\d+)')
39 while self._server.poll() == None:
40 m = port_re.match(self._server.stdout.readline())
42 port = int(m.group('port'))
45 self._forwarder = browser_backend.CreateForwarder(
46 util.PortPair(port, browser_backend.GetRemotePort(port)))
49 return not socket.socket().connect_ex(('localhost', port))
50 util.WaitFor(IsServerUp, 10)
59 def __exit__(self, *args):
67 self._forwarder.Close()
68 self._forwarder = None
70 # TODO(tonyg): Should this block until it goes away?
79 return self._forwarder.url
81 def UrlOf(self, path):
82 relative_path = os.path.relpath(path, self._base_dir)
83 # Preserve trailing slash or backslash.
84 # It doesn't matter in a file path, but it does matter in a URL.
85 if path.endswith(os.sep) or (os.altsep and path.endswith(os.altsep)):
87 return urlparse.urljoin(self.url, relative_path.replace(os.sep, '/'))