adb_run_shell_cmd.py revision 47f0f1e200da8a481462f364f822c98fe1b1cd5b
1#!/usr/bin/env python
2# Copyright 2015 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import argparse
7import json
8import sys
9
10from devil.android import device_blacklist
11from devil.android import device_utils
12from devil.utils import run_tests_helper
13
14
15def main():
16  parser = argparse.ArgumentParser(
17      'Run an adb shell command on selected devices')
18  parser.add_argument('cmd', help='Adb shell command to run.', nargs="+")
19  parser.add_argument('-d', '--device', action='append', dest='devices',
20                      default=[],
21                      help='Device to run cmd on. Runs on all devices if not '
22                           'specified. Set multiple times for multiple devices')
23  parser.add_argument('-v', '--verbose', default=0, action='count',
24                      help='Verbose level (multiple times for more)')
25  parser.add_argument('--blacklist-file', help='Device blacklist file.')
26  parser.add_argument('--as-root', action='store_true', help='Run as root.')
27  parser.add_argument('--json-output',
28                      help='File to dump json output to.')
29  args = parser.parse_args()
30  run_tests_helper.SetLogLevel(args.verbose)
31
32  args.blacklist_file = device_blacklist.Blacklist(
33      args.blacklist_file) if args.blacklist_file else None
34  devices = device_utils.DeviceUtils.HealthyDevices(
35      blacklist=args.blacklist_file, device_arg=args.devices)
36
37  p_out = (device_utils.DeviceUtils.parallel(devices).RunShellCommand(
38      args.cmd, large_output=True, as_root=args.as_root, check_return=True)
39      .pGet(None))
40
41  data = {}
42  for device, output in zip(devices, p_out):
43    for line in output:
44      print '%s: %s' % (device, line)
45    data[str(device)] = output
46
47  if args.json_output:
48    with open(args.json_output, 'w') as f:
49      json.dump(data, f)
50
51  return 0
52
53
54if __name__ == '__main__':
55  sys.exit(main())
56