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