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 """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 subprocess | |
16 import shutil | |
pasko-google - do not use
2013/03/21 12:08:37
import order needs tiny treatment
navabi
2013/03/26 21:27:30
Done.
| |
17 import sys | |
18 | |
19 | |
20 # From the Android Developer's website. | |
21 SDK_BASE_URL = 'http://dl.google.com/android/adt' | |
22 SDK_ZIP = 'adt-bundle-linux-x86_64-20130219.zip' | |
23 | |
24 # From the Intel website: | |
25 # http://software.intel.com/en-us/articles/intel-eula-x86-android-4-2-jelly-bean -bin | |
26 X86_IMG_URL = 'http://download-software.intel.com/sites/landingpage/android/sysi mg_x86-17_r01.zip' | |
27 | |
28 # Android API level | |
29 API_TARGET = 'android-17' | |
30 | |
31 | |
32 def RunCommand(args): | |
33 """Execute a command and return stdout and stderr. | |
34 | |
35 Args: | |
36 args: list of the command and the args to the command. | |
37 | |
38 Returns: | |
39 (output, stderr, rc): stdout and stderr and return code | |
40 """ | |
41 logging.info('Running command: %s' % args) | |
42 proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
43 output, stderr = proc.communicate() | |
44 rc = proc.returncode | |
45 return (str(output).strip(), stderr, rc) | |
46 | |
47 | |
48 def CheckSDK(): | |
49 """Check if SDK is already installed. | |
50 | |
51 Returns: | |
52 true if android_tools directory exists in current directory. | |
53 """ | |
54 return os.path.exists(os.path.join(os.getcwd(), 'android_tools')) | |
55 | |
56 | |
57 def CheckX86Image(): | |
58 """Check if Android system images have been installed. | |
59 | |
60 Returns: | |
61 true if android_tools/sdk/system-images directory exists. | |
62 """ | |
63 return os.path.exists(os.path.join(os.getcwd(), 'android_tools', 'sdk', | |
64 'system-images', API_TARGET, 'x86')) | |
65 | |
66 | |
67 def CheckKVM(): | |
68 """Check if KVM is enabled. | |
69 | |
70 Returns: | |
71 true if kvm-ok returns 0 (already enabled) | |
72 """ | |
73 (_, _, rc) = RunCommand(['kvm-ok']) | |
74 return not rc | |
75 | |
76 | |
77 def GetSDK(): | |
78 """Download the SDK and unzip in android_tools directory. | |
79 | |
80 Returns: | |
81 true if SDK successfully downloaded, false otherwise | |
82 """ | |
83 logging.info('Download Android SDK.') | |
84 sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP) | |
85 try: | |
86 # RunCommand(['curl', '-o', '/tmp/sdk.zip', sdk_url]) | |
pasko-google - do not use
2013/03/21 12:08:37
please don't forget to uncomment before committing
navabi
2013/03/26 21:27:30
Done.
| |
87 (_, stderr, rc) = RunCommand(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/']) | |
pasko-google - do not use
2013/03/21 12:08:37
/tmp/sdk.zip is never removed, should probably do
navabi
2013/03/26 21:27:30
Done.
| |
88 if rc: | |
89 print stderr | |
90 raise | |
91 # Get the name of the sub-directory that everything will be extracted to. | |
92 dirname, _ = os.path.splitext(SDK_ZIP) | |
93 zip_dir = '/tmp/%s' % dirname | |
94 # Move the extracted directory to the current working directory. | |
95 shutil.move(zip_dir, 'android_tools') | |
96 except: | |
97 logging.critical('ERROR: Could not download Android SDK.') | |
98 return False | |
99 return True | |
100 | |
101 | |
102 def InstallKVM(): | |
103 """Installs KVM packages.""" | |
104 (output, stderr, rc) = RunCommand(['sudo', 'apt-get', 'install', 'kvm']) | |
105 if rc: | |
106 logging.critical('ERROR: Did not install KVM. Make sure Intel KVM is \ | |
pasko-google - do not use
2013/03/21 12:08:37
This is not necessarily Intel, there is analogous
digit1
2013/03/21 12:48:53
This, also the BIOS will have no mention of KVM, w
navabi
2013/03/26 21:27:30
Done.
| |
107 enabled in BIOS.') | |
108 print '%s\n%s' % output, stderr | |
109 (output, stderr, rc) = RunCommand(['sudo', 'modprobe', 'kvm-intel']) | |
pasko-google - do not use
2013/03/21 12:08:37
please put a TODO(navabi): Use modprobe kvm-amd on
navabi
2013/03/26 21:27:30
Done.
| |
110 if rc: | |
111 logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure \ | |
112 Intel KVM is enabled in BIOS.') | |
pasko-google - do not use
2013/03/21 12:08:37
please no carriage return escaping here and below,
navabi
2013/03/26 21:27:30
Done.
| |
113 print '%s\n%s' % output, stderr | |
114 # Now check to ensure KVM acceleration can be used. | |
115 (output, stderr, rc) = RunCommand(['kvm-ok']) | |
116 if rc: | |
117 logging.critical('ERROR: Can not use KVM acceleration. Make sure Intel KVM \ | |
118 is enabled in BIOS.') | |
pasko-google - do not use
2013/03/21 12:08:37
same thing about replacing 'Intel KVM' with 'hardw
navabi
2013/03/26 21:27:30
Done.
| |
119 print '%s\n%s' % output, stderr | |
120 | |
121 | |
122 def GetX86Image(): | |
123 """Download x86 system image from Intel's website. | |
124 | |
125 Returns: | |
126 true if x86 system image successfully downloaded, false otherwise | |
127 """ | |
128 logging.info('Download x86 system image directory into sdk directory.') | |
129 try: | |
130 RunCommand(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL]) | |
pasko-google - do not use
2013/03/21 12:08:37
please also remove /tmp/x86_img.zip:
finally:
os
navabi
2013/03/26 21:27:30
Done.
| |
131 (_, stderr, rc) = RunCommand(['unzip', '-o', '/tmp/x86_img.zip', '-d', | |
132 '/tmp/']) | |
133 if rc: | |
134 print stderr | |
135 raise | |
136 sys_imgs = os.path.join(os.getcwd(), 'android_tools', 'sdk', | |
137 'system-images', API_TARGET, 'x86') | |
138 shutil.move('/tmp/x86', sys_imgs) | |
139 except: | |
140 logging.critical('ERROR: Could not download x86 image.') | |
141 return False | |
142 return True | |
143 | |
144 | |
145 def main(argv): | |
146 # Run script from parent directory of chrome checkout, because we do not want | |
147 # to put SDK and system images into chrome checkout. | |
148 new_cwd = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', | |
149 '..', '..') | |
150 os.chdir(new_cwd) | |
151 | |
152 # The location of the SDK used to launch the emulator | |
153 emulator_sdk = os.path.join(new_cwd, 'android_tools', 'sdk') | |
154 os.environ['ANDROID_SDK_ROOT'] = emulator_sdk | |
155 | |
156 logging.basicConfig(level=logging.INFO, | |
157 format='# %(asctime)-15s: %(message)s') | |
158 logging.root.setLevel(logging.INFO) | |
159 | |
160 # Calls below will download emulator SDK and/or system images only if needed. | |
161 if CheckSDK(): | |
162 logging.info('android_tools directory already exists (not downloading).') | |
163 elif not GetSDK(): | |
164 # Can not continue without downloading SDK. | |
165 return 1 | |
166 if CheckX86Image(): | |
167 logging.info('system-images directory already exists.') | |
168 else: | |
169 GetX86Image() | |
170 | |
171 # Make sure KVM packages are installed and enabled. | |
172 if CheckKVM(): | |
pasko-google - do not use
2013/03/21 12:08:37
I would like to move it to the first part of work
navabi
2013/03/26 21:27:30
Since the script currently only support Intel virt
| |
173 logging.info('KVM already installed and enabled.') | |
174 else: | |
175 InstallKVM() | |
176 | |
177 | |
178 if __name__ == '__main__': | |
179 sys.exit(main(sys.argv)) | |
OLD | NEW |