Chromium Code Reviews| Index: build/android/emulator.py |
| diff --git a/build/android/emulator.py b/build/android/emulator.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..9751c6e1451b25f5a53016225e244bd0b14dd780 |
| --- /dev/null |
| +++ b/build/android/emulator.py |
| @@ -0,0 +1,84 @@ |
| +#!/usr/bin/python |
| +# Copyright (c) 2011 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. |
| + |
| +"""Provides an interface start and stop Android emulator. |
|
Nirnimesh
2011/10/19 22:01:47
start -> to start
michaelbai
2011/10/19 23:17:15
Done.
|
| + |
| +Assumes system environment ANDROID_NDK_ROOT has been set. |
| + |
| + Emulator: The class provides the methods to launch/shutdown the emulator with |
| + the android virtual device named 'buildbot' . |
| +""" |
| + |
| +import logging |
| +import os |
| +import subprocess |
| +import sys |
| + |
| +import android_commands |
| + |
| + |
| +def _get_available_port(): |
|
Nirnimesh
2011/10/19 22:01:47
_GetAvailablePort
michaelbai
2011/10/19 23:17:15
Done.
|
| + """Returns an available TCP port for the console.""" |
| + used_ports = [] |
| + emulators = android_commands.GetEmulators() |
| + for emulator in emulators: |
| + used_ports.append(emulator.split('-')[1]) |
| + # The port must be an even number between 5554 and 5584. |
| + for port in range(5554, 5585, 2): |
| + if str(port) not in used_ports: |
| + return port |
| + |
| + |
| +class Emulator(object): |
| + """Provides the methods to lanuch/shutdown the emulator. |
| + |
| + The emulator has the android virtual device named 'buildbot'. |
| + |
| + The emulator could use any even TCP port between 5554 and 5584 for the |
| + console communication, and this port will be part of the device name like |
| + 'emulator-5554'. Assume it is always True, as the device name is the id of |
| + emulator managed in this class. |
| + |
| + Attributes: |
| + emulator: Path of Android's emulator tool. |
| + popen: Popen object of the running emulator process. |
| + device: Device name of this emulator. |
| + """ |
| + |
| + def __init__(self): |
| + try: |
| + android_sdk_root = os.environ['ANDROID_SDK_ROOT'] |
| + except KeyError: |
| + logging.critical('The ANDROID_SDK_ROOT must be set to run the test on ' |
| + 'emulator.') |
| + raise Exception |
|
Nirnimesh
2011/10/19 22:01:47
Why not just raise the current exception?
raise
michaelbai
2011/10/19 23:17:15
Done.
|
| + self.emulator = os.path.join(android_sdk_root, 'tools', 'emulator') |
| + self.popen = None |
| + self.device = None |
| + |
| + def launch(self): |
|
Nirnimesh
2011/10/19 22:01:47
Style guide requires all non-trivial method names
michaelbai
2011/10/19 23:17:15
Done.
|
| + """Launches the emulator and waits for package manager to startup. |
| + |
| + If fails, an exception will be raised. |
| + """ |
| + port = _get_available_port() |
| + self.device = "emulator-%d" % port |
| + self.popen = subprocess.Popen(args=[self.emulator, '-avd', 'buildbot', |
| + '-port', str(port)]) |
| + # This will not return until device's package manager starts up or an |
| + # exception is raised. |
| + android_commands.AndroidCommands(self.device, True) |
| + |
| + def shutdown(self): |
|
Nirnimesh
2011/10/19 22:01:47
Shutdown
michaelbai
2011/10/19 23:17:15
Done.
|
| + """Shuts down the process started by launch.""" |
| + self.popen.terminate() |
| + |
| + |
| +def main(argv): |
| + Emulator().launch() |
| + |
| + |
| +if __name__ == '__main__': |
| + main(sys.argv) |