blob: a42564ac0c1f6c84307c0119d91922e7e7646e89 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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 to start and stop Android emulator.
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 _GetAvailablePort():
"""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
self.emulator = os.path.join(android_sdk_root, 'tools', 'emulator')
self.popen = None
self.device = None
def Launch(self):
"""Launches the emulator and waits for package manager to startup.
If fails, an exception will be raised.
"""
port = _GetAvailablePort()
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):
"""Shuts down the process started by launch."""
self.popen.terminate()
def main(argv):
Emulator().launch()
if __name__ == '__main__':
main(sys.argv)
|