• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5import os
6
7from devil import devil_env
8from devil.android import device_blacklist
9from devil.android import device_errors
10from devil.android import device_utils
11
12
13def AddEnvironmentArguments(parser):
14  """Adds environment-specific arguments to the provided parser."""
15  parser.add_argument(
16      '--adb-path', type=os.path.realpath,
17      help='Path to the adb binary')
18
19
20def InitializeEnvironment(args):
21  devil_dynamic_config = devil_env.EmptyConfig()
22  if args.adb_path:
23    devil_dynamic_config['dependencies'].update(
24        devil_env.LocalConfigItem(
25            'adb', devil_env.GetPlatform(), args.adb_path))
26
27  devil_env.config.Initialize(configs=[devil_dynamic_config])
28
29
30def AddDeviceArguments(parser):
31  """Adds device and blacklist arguments to the provided parser."""
32  parser.add_argument(
33      '-d', '--device', dest='devices', action='append',
34      help='Serial number of the Android device to use. (default: use all)')
35  parser.add_argument('--blacklist-file', help='Device blacklist JSON file.')
36
37
38def GetDevices(requested_devices, blacklist_file):
39  """Gets a list of healthy devices matching the given parameters."""
40  if not isinstance(blacklist_file, device_blacklist.Blacklist):
41    blacklist_file = (device_blacklist.Blacklist(blacklist_file)
42                      if blacklist_file
43                      else None)
44
45  devices = device_utils.DeviceUtils.HealthyDevices(blacklist_file)
46  if not devices:
47    raise device_errors.NoDevicesError()
48  elif requested_devices:
49    requested = set(requested_devices)
50    available = set(str(d) for d in devices)
51    missing = requested.difference(available)
52    if missing:
53      raise device_errors.DeviceUnreachableError(next(iter(missing)))
54    return sorted(device_utils.DeviceUtils(d)
55                  for d in available.intersection(requested))
56  else:
57    return devices
58
59