OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2013 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 """Installs deps for using SDK emulator for testing. | |
7 | |
8 The script will download the SDK and system images, if they are not present, and | |
9 install and enable KVM, if virtualization has been enabled in the BIOS. | |
10 """ | |
11 | |
12 | |
13 import logging | |
14 import os | |
15 import shutil | |
16 import subprocess | |
17 import sys | |
18 | |
19 from pylib import constants | |
20 | |
21 # From the Android Developer's website. | |
22 SDK_BASE_URL = 'http://dl.google.com/android/adt' | |
23 SDK_ZIP = 'adt-bundle-linux-x86_64-20130219.zip' | |
24 | |
25 # Android x86 system image from the Intel website: | |
26 # http://software.intel.com/en-us/articles/intel-eula-x86-android-4-2-jelly-bean -bin | |
27 X86_IMG_URL = 'http://download-software.intel.com/sites/landingpage/android/sysi mg_x86-17_r01.zip' | |
28 | |
29 # Android API level | |
30 API_TARGET = 'android-%s' % constants.ANDROID_SDK_VERSION | |
31 | |
32 | |
33 def RunCommand(args): | |
frankf
2013/03/26 21:42:44
You can use pylib.cmd_helper here
navabi
2013/03/26 23:05:38
Done.
| |
34 """Execute a command and return stdout and stderr. | |
35 | |
36 Args: | |
37 args: list of the command and the args to the command. | |
38 | |
39 Returns: | |
40 (output, stderr, rc): stdout and stderr and return code | |
41 """ | |
42 logging.info('Running command: %s' % args) | |
43 proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
44 output, stderr = proc.communicate() | |
45 rc = proc.returncode | |
46 return (str(output).strip(), stderr, rc) | |
47 | |
48 | |
49 def CheckSDK(): | |
50 """Check if SDK is already installed. | |
51 | |
52 Returns: | |
53 true if android_tools directory exists in current directory. | |
54 """ | |
55 return os.path.exists(os.path.join(os.getcwd(), 'android_tools')) | |
56 | |
57 | |
58 def CheckX86Image(): | |
59 """Check if Android system images have been installed. | |
60 | |
61 Returns: | |
62 true if android_tools/sdk/system-images directory exists. | |
63 """ | |
64 return os.path.exists(os.path.join(os.getcwd(), 'android_tools', 'sdk', | |
65 'system-images', API_TARGET, 'x86')) | |
66 | |
67 | |
68 def CheckKVM(): | |
69 """Check if KVM is enabled. | |
70 | |
71 Returns: | |
72 true if kvm-ok returns 0 (already enabled) | |
73 """ | |
74 (_, _, rc) = RunCommand(['kvm-ok']) | |
75 return not rc | |
76 | |
77 | |
78 def GetSDK(): | |
79 """Download the SDK and unzip in android_tools directory. | |
80 | |
81 Returns: | |
82 true if SDK successfully downloaded, false otherwise | |
83 """ | |
84 logging.info('Download Android SDK.') | |
85 sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP) | |
86 try: | |
87 RunCommand(['curl', '-o', '/tmp/sdk.zip', sdk_url]) | |
88 (_, stderr, rc) = RunCommand(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/']) | |
89 # Remove /tmp/sdk.zip | |
90 os.unlink('/tmp/sdk.zip') | |
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 hardware ' | |
110 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' | |
111 'AMD SVM).') | |
112 print '%s\n%s' % output, stderr | |
113 # TODO(navabi): Use modprobe kvm-amd on AMD processors. | |
114 (output, stderr, rc) = RunCommand(['sudo', 'modprobe', 'kvm-intel']) | |
115 if rc: | |
116 logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure ' | |
117 'hardware virtualization is enabled in BIOS.') | |
118 print '%s\n%s' % output, stderr | |
119 # Now check to ensure KVM acceleration can be used. | |
120 (output, stderr, rc) = RunCommand(['kvm-ok']) | |
121 if rc: | |
122 logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware ' | |
123 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' | |
124 'AMD SVM).') | |
125 print '%s\n%s' % output, stderr | |
126 | |
127 | |
128 def GetX86Image(): | |
129 """Download x86 system image from Intel's website. | |
130 | |
131 Returns: | |
132 true if x86 system image successfully downloaded, false otherwise | |
133 """ | |
134 logging.info('Download x86 system image directory into sdk directory.') | |
135 try: | |
136 RunCommand(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL]) | |
137 (_, stderr, rc) = RunCommand(['unzip', '-o', '/tmp/x86_img.zip', '-d', | |
138 '/tmp/']) | |
139 if rc: | |
140 print stderr | |
141 raise | |
142 sys_imgs = os.path.join(os.getcwd(), 'android_tools', 'sdk', | |
143 'system-images', API_TARGET, 'x86') | |
144 shutil.move('/tmp/x86', sys_imgs) | |
145 except: | |
146 logging.critical('ERROR: Could not download x86 image.') | |
147 return False | |
148 finally: | |
149 os.unlink('/tmp/x86_img.zip') | |
150 return True | |
151 | |
152 | |
153 def main(argv): | |
154 # Run script from parent directory of chrome checkout, because we do not want | |
155 # to put SDK and system images into chrome checkout. | |
156 new_cwd = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', | |
157 '..', '..') | |
158 os.chdir(new_cwd) | |
159 | |
160 # The location of the SDK used to launch the emulator | |
161 emulator_sdk = os.path.join(new_cwd, 'android_tools', 'sdk') | |
162 os.environ['ANDROID_SDK_ROOT'] = emulator_sdk | |
163 | |
164 logging.basicConfig(level=logging.INFO, | |
165 format='# %(asctime)-15s: %(message)s') | |
166 logging.root.setLevel(logging.INFO) | |
frankf
2013/03/26 21:42:44
You can use pylib.utils.run_tests_helper for this
navabi
2013/03/26 23:05:38
Done.
| |
167 | |
168 # Calls below will download emulator SDK and/or system images only if needed. | |
169 if CheckSDK(): | |
170 logging.info('android_tools directory already exists (not downloading).') | |
171 elif not GetSDK(): | |
172 # Can not continue without downloading SDK. | |
173 return 1 | |
174 if CheckX86Image(): | |
175 logging.info('system-images directory already exists.') | |
176 else: | |
177 GetX86Image() | |
178 | |
179 # Make sure KVM packages are installed and enabled. | |
180 if CheckKVM(): | |
navabi
2013/03/26 21:35:43
Copied (and elaborated some) from response to pask
pasko-google - do not use
2013/03/27 13:31:16
I am assuming you mean ARM when you are saying "AM
| |
181 logging.info('KVM already installed and enabled.') | |
182 else: | |
183 InstallKVM() | |
184 | |
185 | |
186 if __name__ == '__main__': | |
187 sys.exit(main(sys.argv)) | |
OLD | NEW |