• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Utility functions for interacting with devices supporting adb commands. The
6functions only work with an Autotest instance setup, e.g., in a lab.
7"""
8
9
10import os
11import logging
12
13import common
14from autotest_lib.client.common_lib import error
15from autotest_lib.client.common_lib.cros import dev_server
16
17
18def install_apk_from_build(host, apk, build_artifact, package_name=None,
19                           force_reinstall=False, build_name=None):
20    """Install the specific apk from given build artifact.
21
22    @param host: An ADBHost object to install apk.
23    @param apk: Name of the apk to install, e.g., sl4a.apk
24    @param build_artifact: Name of the build artifact, e.g., test_zip. Note
25            that it's not the name of the artifact file. Refer to
26            artifact_info.py in devserver code for the mapping between
27            artifact name to a build artifact.
28    @param package_name: Name of the package, e.g., com.android.phone.
29            If package_name is given, it checks if the package exists before
30            trying to install it, unless force_reinstall is set to True.
31    @param force_reinstall: True to reinstall the apk even if it's already
32            installed. Default is set to False.
33    @param build_name: None unless DUT is CrOS with ARC++ container. build_name
34            points to ARC++ build artifacts.
35    """
36    # Check if apk is already installed.
37    if package_name and not force_reinstall:
38        if host.is_apk_installed(package_name):
39            logging.info('Package %s is already installed.', package_name)
40            return
41    if build_name:
42        # Pull devserver_url given ARC++ enabled host
43        host_devserver_url = dev_server.AndroidBuildServer.resolve(build_name,
44                host.hostname).url()
45        # Return devserver_url given Android build path
46        job_repo_url = os.path.join(host_devserver_url, build_name)
47    else:
48        info = host.host_info_store.get()
49        job_repo_url = info.attributes.get(host.job_repo_url_attribute, '')
50    if not job_repo_url:
51        raise error.AutoservError(
52                'The host %s has no attribute %s. `install_apk_from_build` '
53                'only works for test with image specified.' %
54                (host.hostname, host.job_repo_url_attribute))
55    devserver_url = dev_server.AndroidBuildServer.get_server_url(job_repo_url)
56    devserver = dev_server.AndroidBuildServer(devserver_url)
57    build_info = host.get_build_info_from_build_url(job_repo_url)
58    devserver.trigger_download(build_info['target'], build_info['build_id'],
59                               build_info['branch'], build_artifact,
60                               synchronous=True)
61    build_info['os_type'] = 'android'
62    apk_url = devserver.locate_file(apk, build_artifact, None, build_info)
63    logging.debug('Found apk at: %s', apk_url)
64
65    tmp_dir = host.teststation.get_tmp_dir()
66    try:
67        host.download_file(apk_url, apk, tmp_dir)
68        result = host.install_apk(os.path.join(tmp_dir, apk),
69                                  force_reinstall=force_reinstall)
70        logging.debug(result.stdout)
71        if package_name and not host.is_apk_installed(package_name):
72            raise error.AutoservError('No package found with name of %s',
73                                      package_name)
74        logging.info('%s is installed successfully.', apk)
75    finally:
76        host.teststation.run('rm -rf %s' % tmp_dir)
77