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."""
28 output = cr.Host.Capture('{CR_ADB}', 'shell', 'ps')
29 pattern = re.compile(r'\S+\s+(\d+)\s+.*{CR_PROCESS}')
30 for line in output.split('\n'):
31 match = re.match(pattern, line)
33 pids.append(match.group(1))
37 def Run(cls, target, arguments):
38 """Invoke a target binary on the device."""
41 '{CR_ADB}', 'shell', 'am', 'start',
49 def Kill(cls, target, _):
50 """Kill all running processes for a target."""
51 target_name = target.build_target
52 if target_name in cls._kills:
53 # already killed this target, do nothing
55 pids = cls.GetPids(target)
58 cr.Host.Execute('{CR_ADB}', 'shell', 'kill', *pids)
60 print target.Substitute('{CR_TARGET_NAME} not running')
61 cls._kills[target_name] = True
64 def Uninstall(cls, target, arguments):
67 '{CR_ADB}', 'uninstall',
73 def Install(cls, target, arguments):
76 '{CR_ADB}', 'install',
82 def Reinstall(cls, target, arguments):
85 '{CR_ADB}', 'install',
92 def AttachGdb(cls, target, arguments):
97 '--symbol-dir=${CR_BUILD_DIR}/lib',
98 '--program-name={CR_TARGET_NAME}',
99 '--package-name={CR_PACKAGE}',
104 class AdbRunner(cr.Runner):
105 """An implementation of cr.Runner for the android platform."""
109 return cr.AndroidPlatform.GetInstance().is_active
111 def Kill(self, targets, arguments):
112 for target in targets:
113 Adb.Kill(target, arguments)
115 def Run(self, target, arguments):
116 Adb.Run(target, arguments)
118 def Test(self, target, arguments):
120 test_type = cr.context.Get('CR_TEST_TYPE')
121 if test_type == cr.Target.INSTRUMENTATION_TEST:
122 target_name_flag = '--test-apk'
124 target_name_flag = '-s'
126 '{CR_TEST_RUNNER}', test_type,
127 target_name_flag, '{CR_TARGET_NAME}',
133 class AdbInstaller(cr.Installer):
134 """An implementation of cr.Installer for the android platform."""
138 return cr.AndroidPlatform.GetInstance().is_active
140 def Uninstall(self, targets, arguments):
141 for target in targets:
142 Adb.Uninstall(target, arguments)
144 def Install(self, targets, arguments):
145 for target in targets:
146 Adb.Install(target, arguments)
148 def Reinstall(self, targets, arguments):
149 for target in targets:
150 Adb.Reinstall(target, arguments)