1# Copyright 2015 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"""This module wraps Android's split-select tool."""
6
7from devil.android.sdk import build_tools
8from devil.utils import cmd_helper
9from devil.utils import lazy
10
11
12_split_select_path = lazy.WeakConstant(
13    lambda: build_tools.GetPath('split-select'))
14
15
16def _RunSplitSelectCmd(args):
17  """Runs a split-select command.
18
19  Args:
20    args: A list of arguments for split-select.
21
22  Returns:
23    The output of the command.
24  """
25  cmd = [_split_select_path.read()] + args
26  status, output = cmd_helper.GetCmdStatusAndOutput(cmd)
27  if status != 0:
28    raise Exception('Failed running command "%s" with output "%s".' %
29                    (' '.join(cmd), output))
30  return output
31
32
33def _SplitConfig(device, allow_cached_props=False):
34  """Returns a config specifying which APK splits are required by the device.
35
36  Args:
37    device: A DeviceUtils object.
38    allow_cached_props: Whether to use cached values for device properties.
39  """
40  return ('%s-r%s-%s:%s' %
41          (device.GetLanguage(cache=allow_cached_props),
42           device.GetCountry(cache=allow_cached_props),
43           device.screen_density,
44           device.product_cpu_abi))
45
46
47def SelectSplits(device, base_apk, split_apks, allow_cached_props=False):
48  """Determines which APK splits the device requires.
49
50  Args:
51    device: A DeviceUtils object.
52    base_apk: The path of the base APK.
53    split_apks: A list of paths of APK splits.
54    allow_cached_props: Whether to use cached values for device properties.
55
56  Returns:
57    The list of APK splits that the device requires.
58  """
59  config = _SplitConfig(device, allow_cached_props=allow_cached_props)
60  args = ['--target', config, '--base', base_apk]
61  for split in split_apks:
62    args.extend(['--split', split])
63  return _RunSplitSelectCmd(args).splitlines()
64