928bd02f8c2f60ff4c716d5248dc4b86f090b898
[platform/framework/web/crosswalk.git] / src / build / android / gyp / util / build_device.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 """ A simple device interface for build steps.
6
7 """
8
9 import logging
10 import os
11 import re
12 import sys
13
14 from util import build_utils
15
16 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..', '..')
17 sys.path.append(BUILD_ANDROID_DIR)
18
19 from pylib import android_commands
20 from pylib.device import device_utils
21
22 GetAttachedDevices = android_commands.GetAttachedDevices
23
24
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)
31
32   def RunShellCommand(self, *args, **kwargs):
33     return self.device.old_interface.RunShellCommand(*args, **kwargs)
34
35   def PushIfNeeded(self, *args, **kwargs):
36     return self.device.old_interface.PushIfNeeded(*args, **kwargs)
37
38   def GetSerialNumber(self):
39     return self.id
40
41   def Install(self, *args, **kwargs):
42     return self.device.old_interface.Install(*args, **kwargs)
43
44   def GetInstallMetadata(self, apk_package):
45     """Gets the metadata on the device for the apk_package apk."""
46     # Matches lines like:
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
54
55
56 def GetConfigurationForDevice(device_id):
57   device = device_utils.DeviceUtils(device_id)
58   configuration = None
59   has_root = False
60   is_online = device.old_interface.IsOnline()
61   if is_online:
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]
65     if not has_root:
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)
71
72     configuration = {
73         'id': device_id,
74         'description': cmd_output[-1],
75         'install_metadata': cmd_output[:-1],
76       }
77   return configuration, is_online, has_root
78
79
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)
83
84
85 def ReadConfigurations(path):
86   return build_utils.ReadJson(path)
87
88
89 def GetBuildDevice(configurations):
90   assert len(configurations) == 1
91   return BuildDevice(configurations[0])
92
93
94 def GetBuildDeviceFromPath(path):
95   configurations = ReadConfigurations(path)
96   if len(configurations) > 0:
97     return GetBuildDevice(ReadConfigurations(path))
98   return None
99