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_errors
21 from pylib
.device
import device_utils
23 GetAttachedDevices
= android_commands
.GetAttachedDevices
26 class BuildDevice(object):
27 def __init__(self
, configuration
):
28 self
.id = configuration
['id']
29 self
.description
= configuration
['description']
30 self
.install_metadata
= configuration
['install_metadata']
31 self
.device
= device_utils
.DeviceUtils(self
.id)
33 def RunShellCommand(self
, *args
, **kwargs
):
34 return self
.device
.RunShellCommand(*args
, **kwargs
)
36 def PushChangedFiles(self
, *args
, **kwargs
):
37 return self
.device
.PushChangedFiles(*args
, **kwargs
)
39 def GetSerialNumber(self
):
42 def Install(self
, *args
, **kwargs
):
43 return self
.device
.Install(*args
, **kwargs
)
45 def GetInstallMetadata(self
, apk_package
):
46 """Gets the metadata on the device for the apk_package apk."""
48 # -rw-r--r-- system system 7376582 2013-04-19 16:34 \
49 # org.chromium.chrome.shell.apk
50 # -rw-r--r-- system system 7376582 2013-04-19 16:34 \
51 # org.chromium.chrome.shell-1.apk
52 apk_matcher
= lambda s
: re
.match('.*%s(-[0-9]*)?.apk$' % apk_package
, s
)
53 matches
= filter(apk_matcher
, self
.install_metadata
)
54 return matches
[0] if matches
else None
57 def GetConfigurationForDevice(device_id
):
58 device
= device_utils
.DeviceUtils(device_id
)
61 is_online
= device
.IsOnline()
63 cmd
= 'ls -l /data/app; getprop ro.build.description'
64 cmd_output
= device
.RunShellCommand(cmd
)
65 has_root
= not 'Permission denied' in cmd_output
[0]
67 # Disable warning log messages from EnableRoot()
68 logging
.getLogger().disabled
= True
72 except device_errors
.CommandFailedError
:
75 logging
.getLogger().disabled
= False
76 cmd_output
= device
.RunShellCommand(cmd
)
80 'description': cmd_output
[-1],
81 'install_metadata': cmd_output
[:-1],
83 return configuration
, is_online
, has_root
86 def WriteConfigurations(configurations
, path
):
87 # Currently we only support installing to the first device.
88 build_utils
.WriteJson(configurations
[:1], path
, only_if_changed
=True)
91 def ReadConfigurations(path
):
92 return build_utils
.ReadJson(path
)
95 def GetBuildDevice(configurations
):
96 assert len(configurations
) == 1
97 return BuildDevice(configurations
[0])
100 def GetBuildDeviceFromPath(path
):
101 configurations
= ReadConfigurations(path
)
102 if len(configurations
) > 0:
103 return GetBuildDevice(ReadConfigurations(path
))