Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / build / android / gyp / util / build_device.py
bloba19045be329225b77c10eb36e85fc7a6a4be629a
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.
7 """
9 import logging
10 import os
11 import re
12 import sys
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 devil.android import device_errors
20 from devil.android import device_utils
21 from devil.android.sdk import adb_wrapper
24 def GetAttachedDevices():
25 return [a.GetDeviceSerial()
26 for a in adb_wrapper.AdbWrapper.Devices()]
29 class BuildDevice(object):
30 def __init__(self, configuration):
31 self.id = configuration['id']
32 self.description = configuration['description']
33 self.install_metadata = configuration['install_metadata']
34 self.device = device_utils.DeviceUtils(self.id)
36 def RunShellCommand(self, *args, **kwargs):
37 return self.device.RunShellCommand(*args, **kwargs)
39 def PushChangedFiles(self, *args, **kwargs):
40 return self.device.PushChangedFiles(*args, **kwargs)
42 def GetSerialNumber(self):
43 return self.id
45 def Install(self, *args, **kwargs):
46 return self.device.Install(*args, **kwargs)
48 def InstallSplitApk(self, *args, **kwargs):
49 return self.device.InstallSplitApk(*args, **kwargs)
51 def GetInstallMetadata(self, apk_package):
52 """Gets the metadata on the device for the apk_package apk."""
53 # Matches lines like:
54 # -rw-r--r-- system system 7376582 2013-04-19 16:34 \
55 # org.chromium.chrome.apk
56 # -rw-r--r-- system system 7376582 2013-04-19 16:34 \
57 # org.chromium.chrome-1.apk
58 apk_matcher = lambda s: re.match('.*%s(-[0-9]*)?.apk$' % apk_package, s)
59 matches = filter(apk_matcher, self.install_metadata)
60 return matches[0] if matches else None
63 def GetConfigurationForDevice(device_id):
64 device = device_utils.DeviceUtils(device_id)
65 configuration = None
66 has_root = False
67 is_online = device.IsOnline()
68 if is_online:
69 cmd = 'ls -l /data/app; getprop ro.build.description'
70 cmd_output = device.RunShellCommand(cmd)
71 has_root = not 'Permission denied' in cmd_output[0]
72 if not has_root:
73 # Disable warning log messages from EnableRoot()
74 logging.getLogger().disabled = True
75 try:
76 device.EnableRoot()
77 has_root = True
78 except device_errors.CommandFailedError:
79 has_root = False
80 finally:
81 logging.getLogger().disabled = False
82 cmd_output = device.RunShellCommand(cmd)
84 configuration = {
85 'id': device_id,
86 'description': cmd_output[-1],
87 'install_metadata': cmd_output[:-1],
89 return configuration, is_online, has_root
92 def WriteConfigurations(configurations, path):
93 # Currently we only support installing to the first device.
94 build_utils.WriteJson(configurations[:1], path, only_if_changed=True)
97 def ReadConfigurations(path):
98 return build_utils.ReadJson(path)
101 def GetBuildDevice(configurations):
102 assert len(configurations) == 1
103 return BuildDevice(configurations[0])
106 def GetBuildDeviceFromPath(path):
107 configurations = ReadConfigurations(path)
108 if len(configurations) > 0:
109 return GetBuildDevice(ReadConfigurations(path))
110 return None