• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2013 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"""Brings in Chrome Android's android_commands module, which itself is a
6thin(ish) wrapper around adb."""
7
8import logging
9import os
10import shutil
11import stat
12
13from telemetry.core import platform
14from telemetry.core import util
15from telemetry.util import support_binaries
16
17# This is currently a thin wrapper around Chrome Android's
18# build scripts, located in chrome/build/android. This file exists mainly to
19# deal with locating the module.
20
21util.AddDirToPythonPath(util.GetChromiumSrcDir(), 'build', 'android')
22from pylib import android_commands  # pylint: disable=F0401
23from pylib import constants  # pylint: disable=F0401
24try:
25  from pylib import ports  # pylint: disable=F0401
26except Exception:
27  ports = None
28from pylib.device import device_utils  # pylint: disable=F0401
29
30
31def IsAndroidSupported():
32  return device_utils != None
33
34
35def GetAttachedDevices():
36  """Returns a list of attached, online android devices.
37
38  If a preferred device has been set with ANDROID_SERIAL, it will be first in
39  the returned list."""
40  return android_commands.GetAttachedDevices()
41
42
43def AllocateTestServerPort():
44  return ports.AllocateTestServerPort()
45
46
47def ResetTestServerPortAllocation():
48  return ports.ResetTestServerPortAllocation()
49
50
51class AdbCommands(object):
52  """A thin wrapper around ADB"""
53
54  def __init__(self, device):
55    self._device = device_utils.DeviceUtils(device)
56    self._device_serial = device
57
58  def device_serial(self):
59    return self._device_serial
60
61  def device(self):
62    return self._device
63
64  def __getattr__(self, name):
65    """Delegate all unknown calls to the underlying AndroidCommands object."""
66    return getattr(self._device.old_interface, name)
67
68  def Forward(self, local, remote):
69    ret = self._device.old_interface.Adb().SendCommand(
70        'forward %s %s' % (local, remote))
71    assert ret == ''
72
73  def IsUserBuild(self):
74    return self._device.GetProp('ro.build.type') == 'user'
75
76
77def GetBuildTypeOfPath(path):
78  if not path:
79    return None
80  for build_dir, build_type in util.GetBuildDirectories():
81    if os.path.join(build_dir, build_type) in path:
82      return build_type
83  return None
84
85
86def SetupPrebuiltTools(adb):
87  """Some of the android pylib scripts we depend on are lame and expect
88  binaries to be in the out/ directory. So we copy any prebuilt binaries there
89  as a prereq."""
90
91  # TODO(bulach): Build the targets for x86/mips.
92  device_tools = [
93    'file_poller',
94    'forwarder_dist/device_forwarder',
95    'md5sum_dist/md5sum_bin',
96    'purge_ashmem',
97    'run_pie',
98  ]
99
100  host_tools = [
101    'bitmaptools',
102    'md5sum_bin_host',
103  ]
104
105  if platform.GetHostPlatform().GetOSName() == 'linux':
106    host_tools.append('host_forwarder')
107
108  has_device_prebuilt = adb.device().GetProp('ro.product.cpu.abi').startswith(
109      'armeabi')
110  if not has_device_prebuilt:
111    return all([support_binaries.FindLocallyBuiltPath(t) for t in device_tools])
112
113  build_type = None
114  for t in device_tools + host_tools:
115    executable = os.path.basename(t)
116    locally_built_path = support_binaries.FindLocallyBuiltPath(t)
117    if not build_type:
118      build_type = GetBuildTypeOfPath(locally_built_path) or 'Release'
119      constants.SetBuildType(build_type)
120    dest = os.path.join(constants.GetOutDirectory(), t)
121    if not locally_built_path:
122      logging.info('Setting up prebuilt %s', dest)
123      if not os.path.exists(os.path.dirname(dest)):
124        os.makedirs(os.path.dirname(dest))
125      platform_name = ('android' if t in device_tools else
126                       platform.GetHostPlatform().GetOSName())
127      prebuilt_path = support_binaries.FindPath(executable, platform_name)
128      if not prebuilt_path or not os.path.exists(prebuilt_path):
129        raise NotImplementedError("""
130%s must be checked into cloud storage.
131Instructions:
132http://www.chromium.org/developers/telemetry/upload_to_cloud_storage
133""" % t)
134      shutil.copyfile(prebuilt_path, dest)
135      os.chmod(dest, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
136  return True
137