| 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 import os | |
| 5 import subprocess | |
| 6 | |
| 7 class FileServer(object): | |
| 8 def __init__(self, path, port=8000): | |
| 9 self._server = None | |
| 10 self._devnull = None | |
| 11 self._path = path | |
| 12 self._port = port | |
| 13 | |
| 14 assert os.path.exists(path) | |
| 15 assert os.path.isdir(path) | |
| 16 | |
| 17 def __enter__(self): | |
| 18 self._devnull = open(os.devnull, 'w') | |
| 19 self._server = subprocess.Popen( | |
| 20 ['python', '-m', 'SimpleHTTPServer', str(self._port)], | |
| 21 cwd=self._path, | |
| 22 stdout=self._devnull, stderr=self._devnull) | |
| 23 return self | |
| 24 | |
| 25 @property | |
| 26 def url(self): | |
| 27 return 'http://localhost:%d' % self._port | |
| 28 | |
| 29 def __exit__(self, *args): | |
| 30 if self._server: | |
| 31 self._server.kill() | |
| 32 self._server = None | |
| 33 if self._devnull: | |
| 34 self._devnull.close() | |
| 35 self._devnull = None | |
| 36 | |
| 37 def __del__(self): | |
| 38 if self._server: | |
| 39 self._server.kill() | |
| 40 if self._devnull: | |
| 41 self._devnull.close() | |
| 42 self._devnull = None | |
| OLD | NEW |