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.
5 """ A simple device interface for build steps.
14 from util import build_utils
16 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..', '..')
17 sys.path.append(BUILD_ANDROID_DIR)
19 from pylib import android_commands
20 from pylib.device import device_utils
22 GetAttachedDevices = android_commands.GetAttachedDevices
25 class BuildDevice(object):
26 def __init__(self, configuration):
27 self.id = configuration['id']
28 self.description = configuration['description']
29 self.install_metadata = configuration['install_metadata']
30 self.device = device_utils.DeviceUtils(self.id)
32 def RunShellCommand(self, *args, **kwargs):
33 return self.device.old_interface.RunShellCommand(*args, **kwargs)
35 def PushIfNeeded(self, *args, **kwargs):
36 return self.device.old_interface.PushIfNeeded(*args, **kwargs)
38 def GetSerialNumber(self):
41 def Install(self, *args, **kwargs):
42 return self.device.old_interface.Install(*args, **kwargs)
44 def GetInstallMetadata(self, apk_package):
45 """Gets the metadata on the device for the apk_package apk."""
47 # -rw-r--r-- system system 7376582 2013-04-19 16:34 \
48 # org.chromium.chrome.shell.apk
49 # -rw-r--r-- system system 7376582 2013-04-19 16:34 \
50 # org.chromium.chrome.shell-1.apk
51 apk_matcher = lambda s: re.match('.*%s(-[0-9]*)?.apk$' % apk_package, s)
52 matches = filter(apk_matcher, self.install_metadata)
53 return matches[0] if matches else None
56 def GetConfigurationForDevice(device_id):
57 device = device_utils.DeviceUtils(device_id)
60 is_online = device.old_interface.IsOnline()
62 cmd = 'ls -l /data/app; getprop ro.build.description'
63 cmd_output = device.old_interface.RunShellCommand(cmd)
64 has_root = not 'Permission denied' in cmd_output[0]
66 # Disable warning log messages from EnableAdbRoot()
67 logging.getLogger().disabled = True
68 has_root = device.old_interface.EnableAdbRoot()
69 logging.getLogger().disabled = False
70 cmd_output = device.old_interface.RunShellCommand(cmd)
74 'description': cmd_output[-1],
75 'install_metadata': cmd_output[:-1],
77 return configuration, is_online, has_root
80 def WriteConfigurations(configurations, path):
81 # Currently we only support installing to the first device.
82 build_utils.WriteJson(configurations[:1], path, only_if_changed=True)
85 def ReadConfigurations(path):
86 return build_utils.ReadJson(path)
89 def GetBuildDevice(configurations):
90 assert len(configurations) == 1
91 return BuildDevice(configurations[0])
94 def GetBuildDeviceFromPath(path):
95 configurations = ReadConfigurations(path)
96 if len(configurations) > 0:
97 return GetBuildDevice(ReadConfigurations(path))