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.
10 from telemetry.core import util
12 class TemporaryHTTPServer(object):
13 def __init__(self, browser_backend, paths):
16 self._forwarder = None
17 self._host_port = util.GetAvailableLocalPort()
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',
32 cmd.extend(self._paths)
33 env = os.environ.copy()
34 env['PYTHONPATH'] = os.path.abspath(os.path.dirname(__file__))
35 self._server = subprocess.Popen(cmd, cwd=self._base_dir,
36 env=env, stdout=self._devnull, stderr=self._devnull)
38 self._forwarder = browser_backend.CreateForwarder(
39 util.PortPair(self._host_port,
40 browser_backend.GetRemotePort(self._host_port)))
43 return not socket.socket().connect_ex(('localhost', self._host_port))
44 util.WaitFor(IsServerUp, 10)
53 def __exit__(self, *args):
61 self._forwarder.Close()
62 self._forwarder = None
72 return self._forwarder.url
74 def UrlOf(self, path):
75 relative_path = os.path.relpath(path, self._base_dir)
76 # Preserve trailing slash or backslash.
77 # It doesn't matter in a file path, but it does matter in a URL.
78 if path.endswith(os.sep) or (os.altsep and path.endswith(os.altsep)):
80 return urlparse.urljoin(self.url, relative_path.replace(os.sep, '/'))