Chromium Code Reviews| Index: build/android/pylib/host_driven/test_server.py |
| diff --git a/build/android/pylib/host_driven/test_server.py b/build/android/pylib/host_driven/test_server.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..db304c624b3fa80a7808c5aaa1b9e7af13acbb66 |
| --- /dev/null |
| +++ b/build/android/pylib/host_driven/test_server.py |
| @@ -0,0 +1,93 @@ |
| +#!/usr/bin/python |
|
frankf
2013/08/30 18:22:43
Remove the shebang, this module is only imported.
nyquist
2013/08/30 21:50:59
Done.
|
| +# Copyright 2013 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. |
| + |
| +"""Python driven test server controller. |
| + |
| +This class controls the startup and shutdown of a python driven test server that |
| +runs in a separate process. |
| + |
| +The server starts up automatically when the object is created. |
| + |
| +After it starts up, it is possible to retreive the port it started on through |
| +accessing the member field |port|. |
| + |
| +For shutting down the server, call TearDown(). |
| +""" |
| + |
| +import subprocess |
| +import os |
| +import os.path |
| + |
| +from pylib import constants |
| + |
| +# NOTE: when adding or modifying these lines, omit any leading slashes! |
| +# Otherwise os.path.join() will (correctly) treat them as absolute paths |
| +# instead of relative paths, and will do nothing. |
| +_PYTHONPATH_DIRS = [ |
| + 'net/tools/testserver/', |
| + 'third_party/', |
| + 'third_party/pyftpdlib/src/', |
| + 'third_party/pywebsocket/src', |
| + 'third_party/tlslite/', |
| +] |
| + |
| +# Python files in these directories are generated as part of the build. |
| +# These dirs are located in out/(Debug|Release) directory. |
| +# The correct path is determined based on the build type. E.g. out/Debug for |
| +# debug builds and out/Release for release builds. |
| +_GENERATED_PYTHONPATH_DIRS = [ |
| + 'pyproto/sync/protocol/', |
| + 'pyproto/' |
| +] |
| + |
| + |
| +class TestServer(object): |
| + """Sets up a Python driven test server on the host machine. |
|
frankf
2013/08/30 18:22:43
s/python driven/host driven/g
nyquist
2013/08/30 21:50:59
Done.
|
| + |
| + For shutting down the server, call TearDown(). |
| + """ |
| + |
| + def __init__(self, shard_index, test_server_port, test_server_path): |
| + """Sets up a Python driven test server on the host machine. |
| + |
| + Args: |
| + shard_index: Index of the current shard. |
| + test_server_port: Port to run the test server on. This is multiplexed with |
| + the shard index. To retrieve the real port access the |
| + member variable |port|. |
| + test_server_path: The path (relative to the root src dir) of the server |
| + |
| + Returns: |
|
frankf
2013/08/30 18:22:43
Update this
nyquist
2013/08/30 21:50:59
Done. Removed "Returns" since this is now a constr
|
| + A tuple where the first element is the port used by the server, and the |
| + second is the mock server that was setup. |
| + """ |
| + self.port = test_server_port + shard_index |
| + |
| + src_dir = constants.DIR_SOURCE_ROOT |
| + # Make dirs into a list of absolute paths. |
| + abs_dirs = [os.path.join(src_dir, d) for d in _PYTHONPATH_DIRS] |
| + # Add the generated python files to the path |
| + abs_dirs.extend([os.path.join(src_dir, 'out', constants.GetBuildType(), d) |
| + for d in _GENERATED_PYTHONPATH_DIRS]) |
| + # Note the colon after $PYTHONPATH. This appends the list of |
|
craigdh
2013/08/30 17:55:50
nit: update comment, or perhaps just remove it?
nyquist
2013/08/30 21:50:59
Done. Removed.
|
| + # _PYTHONPATH_DIRS to the existing PYTHONPATH. |
| + current_python_path = os.environ.get('PYTHONPATH') |
| + extra_python_path = ':'.join(abs_dirs) |
| + if current_python_path: |
| + python_path = current_python_path + ':' + extra_python_path |
| + else: |
| + python_path = extra_python_path |
| + |
| + # NOTE: A separate python process is used to simplify getting the right |
| + # system path for finding includes. |
| + server_flags = ['python', os.path.join(src_dir, test_server_path), |
|
frankf
2013/08/30 18:22:43
server_flags -> cmd
nyquist
2013/08/30 21:50:59
Done.
|
| + '--log-to-console', |
| + ('--host=%s' % constants.TEST_SERVER_HOST), |
| + ('--port=%d' % self.port)] |
| + self._test_server_process = subprocess.Popen( |
| + server_flags, env={'PYTHONPATH': python_path}) |
| + |
| + def TearDown(self): |
| + self._test_server_process.kill() |