blob: 7b798d16d459723db5dc2004ffb32bb0c947865e [file] [log] [blame]
Dan Shidb0366c2016-02-19 10:36:18 -08001# 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.
Dan Shidb0366c2016-02-19 10:36:18 -08007"""
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
Dan Shidb0366c2016-02-19 10:36:18 -080016
17
18def install_apk_from_build(host, apk, build_artifact, package_name=None,
Tien Changf420b782016-09-22 14:55:03 -070019 force_reinstall=False, build_name=None):
Dan Shidb0366c2016-02-19 10:36:18 -080020 """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.
Tien Changf420b782016-09-22 14:55:03 -070033 @param build_name: None unless DUT is CrOS with ARC++ container. build_name
34 points to ARC++ build artifacts.
Dan Shidb0366c2016-02-19 10:36:18 -080035 """
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
Tien Changf420b782016-09-22 14:55:03 -070041 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:
Prathmesh Prabhu368abdf2017-02-14 11:23:47 -080048 info = host.host_info_store.get()
49 job_repo_url = info.attributes.get(host.job_repo_url_attribute, '')
Dan Shidb0366c2016-02-19 10:36:18 -080050 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))
xixuan9e2c98d2016-02-26 19:04:53 -080055 devserver_url = dev_server.AndroidBuildServer.get_server_url(job_repo_url)
Dan Shidb0366c2016-02-19 10:36:18 -080056 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'],
Benny Peakebdbea872016-12-07 13:18:09 -080059 build_info['branch'], build_artifact,
60 synchronous=True)
Dan Shidb0366c2016-02-19 10:36:18 -080061 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)