OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Launches Android Virtual Devices with a set configuration for testing Chrome. | |
7 | |
8 The script will download the SDK and system images, if they are not present, | |
9 install ane enable KVM, if virtualization has been enabled in the BIOS, and then | |
10 launch a specified number of Android Virtual Devices (AVD's). | |
11 """ | |
12 | |
13 | |
14 import logging | |
15 import optparse | |
16 import os | |
17 import subprocess | |
18 import shutil | |
19 import sys | |
20 | |
21 from pylib.utils import emulator | |
22 | |
23 # From the Android Developer's website. | |
24 SDK_BASE_URL = 'http://dl.google.com/android/adt' | |
25 SDK_ZIP = 'adt-bundle-linux-x86_64-20130219.zip' | |
26 | |
27 # From the Intel website: | |
28 # http://software.intel.com/en-us/articles/intel-eula-x86-android-4-2-jelly-bean -bin | |
29 X86_IMG_URL = 'http://download-software.intel.com/sites/landingpage/android/sysi mg_x86-17_r01.zip' | |
30 | |
31 # Android API level | |
32 API_TARGET = 'android-17' | |
33 | |
34 | |
35 def RunCommand(args): | |
36 """Execute a command and return stdout and stderr. | |
37 | |
38 Args: | |
39 args: list of the command and the args to the command. | |
40 | |
41 Returns: | |
42 (output, stderr, rc): stdout and stderr and return code | |
43 """ | |
44 logging.info('Running command: %s' % args) | |
45 proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
46 output, stderr = proc.communicate() | |
47 rc = proc.returncode | |
48 return (str(output).strip(), stderr, rc) | |
49 | |
50 | |
51 def CheckSDK(): | |
52 """Check if SDK already installed. | |
pasko-google - do not use
2013/03/14 15:46:28
s/SDK /SDK is/
navabi
2013/03/20 21:40:16
Done.
| |
53 | |
54 Returns: | |
55 true if android_tools directory exists in current directory. | |
56 """ | |
57 return os.path.exists(os.path.join(os.getcwd(), 'android_tools')) | |
58 | |
59 | |
60 def CheckX86Image(): | |
61 """Check if Android system images have been installed. | |
62 | |
63 Returns: | |
64 true if android_tools/sdk/system-images directory exists. | |
65 """ | |
66 return os.path.exists(os.path.join(os.getcwd(), 'android_tools', 'sdk', | |
67 'system-images', API_TARGET, 'x86')) | |
68 | |
69 | |
70 def CheckKVM(): | |
71 """Check if KVM is enabled. | |
72 | |
73 Returns: | |
74 true if kvm-ok returns 0 (already enabled) | |
75 """ | |
76 (_, _, rc) = RunCommand(['kvm-ok']) | |
77 return not rc | |
78 | |
79 | |
80 def GetSDK(): | |
81 """Download the SDK and unzip in android_tools directory. | |
82 | |
83 Returns: | |
84 true if SDK successfully downloaded, false otherwise | |
85 """ | |
86 logging.info('Download Android SDK.') | |
87 sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP) | |
88 try: | |
89 # RunCommand(['curl', '-o', '/tmp/sdk.zip', sdk_url]) | |
90 (_, stderr, rc) = RunCommand(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/']) | |
91 if rc: | |
92 print stderr | |
93 raise | |
94 # Get the name of the sub-directory that everything will be extracted to. | |
95 dirname, _ = os.path.splitext(SDK_ZIP) | |
96 zip_dir = '/tmp/%s' % dirname | |
97 # Move the extracted directory to the current working directory. | |
98 shutil.move(zip_dir, 'android_tools') | |
99 except: | |
100 logging.critical('ERROR: Could not download Android SDK.') | |
101 return False | |
102 return True | |
103 | |
104 | |
105 def InstallKVM(): | |
106 """Installs KVM packages.""" | |
107 (output, stderr, rc) = RunCommand(['sudo', 'apt-get', 'install', 'kvm']) | |
108 if rc: | |
109 logging.critical('ERROR: Did not install KVM. Make sure Intel KVM is \ | |
110 enabled in BIOS.') | |
111 print '%s\n%s' % output, stderr | |
112 (output, stderr, rc) = RunCommand(['sudo', 'modprobe', 'kvm-intel']) | |
113 if rc: | |
114 logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure \ | |
115 Intel KVM is enabled in BIOS.') | |
116 print '%s\n%s' % output, stderr | |
117 # Now check to ensure KVM acceleration can be used. | |
118 (output, stderr, rc) = RunCommand(['kvm-ok']) | |
119 if rc: | |
120 logging.critical('ERROR: Can not use KVM acceleration. Make sure Intel KVM \ | |
121 is enabled in BIOS.') | |
122 print '%s\n%s' % output, stderr | |
123 | |
124 | |
125 def GetX86Image(): | |
126 """Download x86 system image from Intel's website. | |
127 | |
128 Returns: | |
129 true if x86 system image successfully downloaded, false otherwise | |
130 """ | |
131 logging.info('Download x86 system image directory into sdk directory.') | |
132 try: | |
133 RunCommand(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL]) | |
134 (_, stderr, rc) = RunCommand(['unzip', '-o', '/tmp/x86_img.zip', '-d', | |
135 '/tmp/']) | |
136 if rc: | |
137 print stderr | |
138 raise | |
139 sys_imgs = os.path.join(os.getcwd(), 'android_tools', 'sdk', | |
140 'system-images', API_TARGET, 'x86') | |
141 shutil.move('/tmp/x86', sys_imgs) | |
142 except: | |
143 logging.critical('ERROR: Could not download x86 image.') | |
144 return False | |
145 return True | |
146 | |
147 | |
148 def main(argv): | |
149 # Run script from parent directory of chrome checkout, because we do not want | |
150 # to put SDK and system images into chrome checkout. | |
151 chrome_root = os.environ.get('CHROME_SRC') | |
pasko-google - do not use
2013/03/14 15:46:28
Another possibility is to take the path to the scr
navabi
2013/03/20 21:40:16
Done. The code is already there below with new_cwd
| |
152 new_cwd = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', | |
153 '..', '..') | |
154 if chrome_root: | |
155 new_cwd = os.path.join(chrome_root, '..', '..') | |
156 else: | |
157 logging.critical('Need to run envsetup from src dir (i.e. \ | |
pasko-google - do not use
2013/03/14 15:46:28
more pythonic style is instead of escaping to star
navabi
2013/03/20 21:40:16
Thanks. This has been removed because of the above
| |
158 build/android/envsetup.sh <options>)') | |
159 return 1 | |
160 | |
161 os.chdir(new_cwd) | |
162 | |
163 # Need to set ANDROID_SDK_ROOT to find AVD's in case it is set by envsetup.sh | |
pasko-google - do not use
2013/03/14 15:46:28
should it be set in this script? Does the emulator
navabi
2013/03/20 21:40:16
Yes, the emulator refuses to work without it. The
| |
164 emulator_sdk = os.path.join(new_cwd, 'android_tools', 'sdk') | |
165 os.environ['ANDROID_SDK_ROOT'] = emulator_sdk | |
166 | |
167 # Parse aguments after the python script | |
168 args = argv[1:] | |
169 opt_parser = optparse.OptionParser(description='AVD script.') | |
170 opt_parser.add_option('-n', '--num', dest='emulator_count', | |
171 help='Number of emulators to launch.', | |
172 type='int', default='1') | |
173 opt_parser.add_option('--abi', default='arm', | |
174 help='Platform of emulators to launch.') | |
175 | |
176 options, _ = opt_parser.parse_args(args) | |
pasko-google - do not use
2013/03/14 15:46:28
better to avoid defining a new variable:
options,
navabi
2013/03/20 21:40:16
Done.
| |
177 if options.abi == 'arm': | |
178 options.abi = 'armeabi-v7a' | |
179 | |
180 logging.basicConfig(level=logging.INFO, | |
181 format='# %(asctime)-15s: %(message)s') | |
182 logging.root.setLevel(logging.INFO) | |
183 | |
184 # Calls below will download emulator SDK and/or system images only if needed. | |
185 if CheckSDK(): | |
186 logging.info('android_tools directory already exists (not downloading).') | |
187 elif not GetSDK(): | |
188 # Can not continue without downloading SDK. | |
189 return 1 | |
190 if CheckX86Image(): | |
191 logging.info('system-images directory already exists.') | |
192 else: | |
193 GetX86Image() | |
194 | |
195 # Make sure KVM packages are installed and enabled. | |
196 if CheckKVM(): | |
pasko-google - do not use
2013/03/14 15:46:28
should have something more granular: fail early if
navabi
2013/03/20 21:40:16
Moved installation stuff to different file: instal
| |
197 logging.info('KVM already installed and enabled.') | |
198 else: | |
199 InstallKVM() | |
200 | |
201 emulator.LaunchEmulators(options.emulator_count, options.abi, True) | |
202 | |
203 | |
204 if __name__ == '__main__': | |
205 sys.exit(main(sys.argv)) | |
OLD | NEW |