1#!/usr/bin/env python
2#
3# Copyright (C) 2015 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17"""Installs an SDK and creates emulators for buildbot testing."""
18from __future__ import print_function
19
20import os
21import platform
22import shutil
23import subprocess
24
25
26DEVICES = {
27    '10': ['x86'],
28    '16': ['armeabi-v7a', 'mips', 'x86'],
29    '23': ['armeabi-v7a', 'x86', 'x86_64'],
30}
31
32
33def get_sdk():
34    os_name = platform.system().lower()
35    ext = 'tgz'
36    if os_name == 'darwin':
37        os_name = 'macosx'
38        ext = 'zip'
39
40    url = 'http://dl.google.com/android/android-sdk_r24.3.4-{}.{}'.format(
41        os_name, ext)
42
43    package_name = os.path.basename(url)
44    if os.path.exists(package_name):
45        os.remove(package_name)
46
47    sdk_dir = 'android-sdk-' + os_name
48    if os.path.exists(sdk_dir):
49        shutil.rmtree(sdk_dir)
50
51    subprocess.check_call(['curl', '-O', url])
52    if ext == 'tgz':
53        subprocess.check_call(['tar', 'xf', package_name])
54    else:
55        subprocess.check_call(['unzip', package_name])
56
57    if os.path.exists(package_name):
58        os.remove(package_name)
59
60    return sdk_dir
61
62
63def install_components(sdk_manager):
64    packages = [
65        'platform-tools',
66    ]
67
68    for api, abis in DEVICES.items():
69        packages.append('android-' + api)
70        for abi in abis:
71            packages.append('sys-img-{}-android-{}'.format(abi, api))
72
73    print('Installing packages:')
74    print('\n'.join(packages))
75
76    filter_arg = ','.join(packages)
77
78    cmd = [
79        sdk_manager, 'update', 'sdk', '--no-ui', '--all', '--filter',
80        filter_arg,
81    ]
82    subprocess.check_call(cmd)
83
84
85def create_devices(sdk_manager):
86    for api, abis in DEVICES.items():
87        for abi in abis:
88            avd_name = '-'.join([abi, api])
89            api_name = 'android-' + api
90
91            print('Creating AVD for {}'.format(avd_name))
92
93            cmd = [
94                sdk_manager, 'create', 'avd', '--force', '--name', avd_name,
95                '--target', api_name, '--abi', abi,
96            ]
97            proc = subprocess.Popen(cmd, stdin=subprocess.PIPE)
98            proc.communicate('no\n')  # No custom hardware profile.
99
100
101def main():
102    os.chdir(os.getenv('HOME'))
103
104    sdk_dir = get_sdk()
105    sdk_manager = os.path.join(sdk_dir, 'tools/android')
106    install_components(sdk_manager)
107    create_devices(sdk_manager)
108
109
110if __name__ == '__main__':
111    main()
112