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 """Provides utilities for testing Chrome on Android on AVD's (i.e. emulators). | |
7 | |
8 The purpose of this script is to automate everything from downloading the SDK, | |
9 system images and KVM packages to creating and running emulators (i.e. Android | |
10 Virutal Devices) with GPU acceleration and KVM support. | |
11 """ | |
12 | |
13 | |
14 import getpass | |
15 import logging | |
16 import optparse | |
17 import os | |
18 import pexpect | |
19 import subprocess | |
20 import shutil | |
21 import sys | |
22 | |
23 | |
24 def RunCommand(args): | |
25 """Execute a command and return stdout and stderr. | |
26 | |
27 Args: | |
28 args: list of the command and the args to the command. | |
29 | |
30 Returns: | |
31 (output, stderr): stdout and stderr | |
32 """ | |
33 logging.info('Running command: %s' % args) | |
34 proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
35 output, stderr = proc.communicate() | |
36 return (str(output).strip(), stderr) | |
37 | |
38 | |
39 def CheckSDK(): | |
40 """Check if SDK already installed. | |
41 | |
42 Returns: | |
43 true if android_tools directory exists in current directory. | |
44 """ | |
45 return os.path.exists(os.path.join(os.getcwd(), 'android_tools')) | |
46 | |
47 | |
48 def CheckImages(): | |
49 """Check if Android system images have been installed. | |
50 | |
51 Returns: | |
52 true if android_tools/sdk/system-images directory exists. | |
53 """ | |
54 return os.path.exists(os.path.join(os.getcwd(), 'android_tools', 'sdk', | |
55 'system-images')) | |
56 | |
57 | |
58 def GetSDK(): | |
59 """Checks out android_tools from the chromium git repo.""" | |
60 if CheckSDK(): | |
61 logging.info('android_tools directory already exists (not checking out).') | |
62 else: | |
63 user = getpass.getuser() | |
64 repo_url = 'ssh://%s@gerrit.chromium.org:29418/android_tools.git' % user | |
65 logging.info('Checking out SDK: git clone %s ...', repo_url) | |
66 (output, stderr) = RunCommand(['git', 'clone', repo_url]) | |
67 if stderr: | |
68 logging.critical('ERROR: unable to checkout android_tools: %s', output) | |
69 logging.info('Successfully checked out SDK. Open tools/android and \ | |
70 download system images.') | |
71 | |
72 | |
73 def InstallKVM(): | |
74 """Installs KVM packages.""" | |
75 (output, stderr) = RunCommand(['sudo', 'apt-get', 'install', 'kvm']) | |
76 if stderr: | |
77 logging.critical('ERROR: Did not install KVM. Make sure Intel KVM is \ | |
78 enabled in BIOS.') | |
79 print output | |
80 (output, stderr) = RunCommand(['sudo', 'modprobe', 'kvm-intel']) | |
81 if stderr: | |
82 logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure \ | |
83 Intel KVM is enabled in BIOS.') | |
84 print output | |
85 # Now check to ensure KVM acceleration can be used. | |
86 (output, stderr) = RunCommand(['kvm-ok']) | |
87 if stderr: | |
88 logging.critical('ERROR: Can not use KVM acceleration. Make sure Intel KVM \ | |
89 is enabled in BIOS.') | |
90 print output | |
91 | |
92 | |
93 def CopySystemImages(): | |
94 """Copies system images from navabi shared directory into sdk.""" | |
navabi
2013/03/05 08:09:49
Put this in public place instead of my shared dire
| |
95 if CheckImages(): | |
96 logging.info('system-images directory already exists.') | |
97 else: | |
98 logging.info('Copying system-images directory into sdk directory') | |
99 # TODO(navabi): Put in public place so non-googlers can download | |
100 shutil.copytree('/home/navabi/www/system-images', | |
101 os.path.join(os.getcwd(), | |
102 'android_tools/sdk/system-images')) | |
103 | |
104 | |
105 def CreateAVD(avd_name, abi): | |
106 """Creates and calls a bash script to call android create avd.""" | |
107 logging.info('Creating AVD with name %s...', avd_name) | |
108 android_bin = os.path.join(os.getcwd(), 'android_tools', 'sdk', 'tools', | |
109 'android') | |
110 avd_dir = os.path.join(os.getcwd(), 'avds', avd_name) | |
111 | |
112 create_cmd = ' '.join([android_bin, 'create', 'avd', '-n', avd_name, '-t', | |
113 '1', '--abi', abi, '-p', avd_dir, '--force']) | |
114 logging.info('Running command: %s' % create_cmd) | |
115 | |
116 create = pexpect.spawn(create_cmd) | |
117 # TODO(navabi): use expect and sendline to specify all custom options | |
118 # create.interact() | |
119 # Read and print the Android version info | |
120 print create.readline() | |
121 | |
122 # Define answers to AVD hardware customization options. Empty string indicates | |
123 # default option is used. | |
124 answers = ['yes', '', '', '', '', '', '', '128', '', '', '', '', '', '', '', | |
125 '', '', '', '', '', '', '', '', 'yes', '', '', '', '', '', '', '', | |
126 '', '', '', '128', '', '', '', '', '', '', '', '', '', '', ''] | |
127 | |
128 for answer in answers: | |
129 create.expect(r'\]') | |
130 create.sendline(answer) | |
131 # print create.before + ']' | |
132 | |
133 create.expect(pexpect.EOF) | |
134 # Print hardware configuration for created AVD | |
135 print create.before | |
136 | |
137 | |
138 def main(argv): | |
139 # Run script from parent directory of chrome checkout, because we do not want | |
140 # to put SDK and system images into chrome checkout. | |
141 chrome_root = os.environ.get('CHROME_SRC') | |
142 new_cwd = os.path.join(os.getcwd(), '..', '..', '..', '..') | |
navabi
2013/03/06 01:30:27
Fix: This should be set not relative to os.getcwd(
| |
143 if chrome_root: | |
144 new_cwd = os.path.join(chrome_root, '..', '..') | |
145 else: | |
146 logging.warning('CHROME_SRC not set (run envsetup.sh). Using relative path \ | |
147 from script location instead.') | |
148 | |
149 os.chdir(new_cwd) | |
150 | |
151 # Need to set ANDROID_SDK_ROOT to find AVD's in case it is set by envsetup.sh | |
152 emulator_sdk = os.path.join(new_cwd, 'android_tools', 'sdk') | |
153 os.environ['ANDROID_SDK_ROOT'] = "%s:$PATH" % emulator_sdk | |
154 | |
155 # Parse aguments after the python script | |
156 args = argv[1:] | |
157 opt_parser = optparse.OptionParser(description='AVD script.') | |
158 | |
159 opt_parser.add_option('--get-sdk', action='store_true', default=False, | |
160 help='download the sdk and install images') | |
161 opt_parser.add_option('--download-images', action='store_true', default=False, | |
162 help='install Android system images') | |
163 | |
164 opt_parser.add_option('--install-kvm', action='store_true', default=False, | |
165 help='install Intel KVM assuming enabled in BIOS.') | |
166 | |
167 opt_parser.add_option('--new-avd', help='create new AVD with given name.') | |
168 opt_parser.add_option('--abi', help='specify abi version for AVD') | |
169 | |
170 opt_parser.add_option('--list-avds', action='store_true', default=False, | |
171 help='list AVDs created by this script.') | |
172 | |
173 opt_parser.add_option('--start-avd', | |
174 help='start an AVD that has been created.') | |
175 | |
176 options, _ = opt_parser.parse_args(args) | |
177 | |
178 logging.basicConfig(level=logging.INFO, | |
179 format='# %(asctime)-15s: %(message)s') | |
180 logging.root.setLevel(logging.INFO) | |
181 | |
182 if options.get_sdk: | |
183 # If we are downloading the SDK, also go ahead and install system images | |
184 GetSDK() | |
185 CopySystemImages() | |
186 elif options.install_kvm: | |
187 InstallKVM() | |
188 elif options.download_images: | |
189 CopySystemImages() | |
190 elif options.list_avds: | |
191 (output, _) = RunCommand(['ls', 'avds/']) | |
192 print output | |
193 elif options.new_avd: | |
194 if not CheckSDK(): | |
195 logging.error('Can not create AVD. First get sdk (i.e. --get-sdk).') | |
196 elif not CheckImages(): | |
197 logging.error('Can not create AVD. First download system images (i.e. \ | |
198 --download-images).') | |
199 elif options.new_avd is None: | |
200 logging.error('Please specify name of AVD (e.g. --create-avd myavd).') | |
201 elif options.abi != 'arm' and options.abi != 'x86': | |
202 logging.error('Specify --abi=ABI (Valid ABIs: arm, x86)') | |
203 else: | |
204 if options.abi == 'arm': | |
205 options.abi = 'armeabi-v7a' | |
206 CreateAVD(options.new_avd, options.abi) | |
207 elif options.start_avd: | |
208 emulator_bin = os.path.join(os.getcwd(), 'android_tools', 'sdk', 'tools', | |
209 'emulator') | |
210 (output, stderr) = RunCommand([emulator_bin, '-avd', options.start_avd, | |
211 '-gpu', 'on', '-qemu', '-m', '128']) | |
212 print stderr | |
213 print output | |
214 else: | |
215 logging.info('Run script with --help for options.') | |
216 | |
217 | |
218 if __name__ == '__main__': | |
219 sys.exit(main(sys.argv)) | |
OLD | NEW |