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 module to hold adb specific action implementations."""
13 """Exposes the functionality of the adb tool to the rest of cr.
15 This is intended as the only class in the cr that needs to understand the
16 adb command line, and expose it in neutral form to the rest of the code.
19 # Tracks the set of killed target names, so we don't keep issuing kill
20 # commands that are not going to have any effect.
24 def GetPids(cls, target):
25 """Gets the set of running PIDs that match the specified target."""
27 output = cr.Host.Capture(target, '{CR_ADB}', 'shell', 'ps')
28 pattern = re.compile(r'\S+\s+(\d+)\s+.*{CR_PROCESS}')
29 for line in output.split('\n'):
30 match = re.match(pattern, line)
32 pids.append(match.group(1))
36 def Run(cls, target, arguments):
37 """Invoke a target binary on the device."""
40 '{CR_ADB}', 'shell', 'am', 'start',
48 def Kill(cls, target, _):
49 """Kill all running processes for a target."""
50 target_name = target.build_target
51 if target_name in cls._kills:
52 # already killed this target, do nothing
54 pids = cls.GetPids(target)
56 cr.Host.Execute(target, '{CR_ADB}', 'shell', 'kill', *pids)
58 print target.Substitute('{CR_TARGET_NAME} not running')
59 cls._kills[target_name] = True
62 def Uninstall(cls, target, arguments):
65 '{CR_ADB}', 'uninstall',
71 def Install(cls, target, arguments):
74 '{CR_ADB}', 'install',
80 def Reinstall(cls, target, arguments):
83 '{CR_ADB}', 'install',
91 def AttachGdb(cls, target, arguments):
96 '--symbol-dir=${CR_BUILD_DIR}/lib',
97 '--program-name={CR_TARGET_NAME}',
98 '--package-name={CR_PACKAGE}',
103 class AdbRunner(cr.Runner):
104 """An implementation of cr.Runner for the android platform."""
108 return cr.AndroidPlatform.GetInstance().is_active
110 def Kill(self, context, targets, arguments):
111 for target in targets:
112 Adb.Kill(target, arguments)
114 def Run(self, context, target, arguments):
115 Adb.Run(target, arguments)
117 def Test(self, context, target, arguments):
120 '{CR_TEST_RUNNER}', '{CR_TEST_TYPE}',
121 '-s', '{CR_TARGET_NAME}',
127 class AdbInstaller(cr.Installer):
128 """An implementation of cr.Installer for the android platform."""
132 return cr.AndroidPlatform.GetInstance().is_active
134 def Uninstall(self, context, targets, arguments):
135 for target in targets:
136 Adb.Uninstall(target, arguments)
138 def Install(self, context, targets, arguments):
139 for target in targets:
140 Adb.Install(target, arguments)
142 def Reinstall(self, context, targets, arguments):
143 for target in targets:
144 Adb.Reinstall(target, arguments)