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 os
9import sys
10
11if __name__ == '__main__':
12  sys.path.append(
13      os.path.abspath(os.path.join(os.path.dirname(__file__),
14                                   '..', '..', '..')))
15
16from devil.android import device_utils
17from devil.android.tools import script_common
18from devil.utils import logging_common
19
20
21def main():
22  parser = argparse.ArgumentParser(
23      'Run an adb shell command on selected devices')
24  parser.add_argument('cmd', help='Adb shell command to run.', nargs="+")
25  logging_common.AddLoggingArguments(parser)
26  script_common.AddDeviceArguments(parser)
27  script_common.AddEnvironmentArguments(parser)
28  parser.add_argument('--as-root', action='store_true', help='Run as root.')
29  parser.add_argument('--json-output',
30                      help='File to dump json output to.')
31  args = parser.parse_args()
32
33  logging_common.InitializeLogging(args)
34  script_common.InitializeEnvironment(args)
35
36  devices = script_common.GetDevices(args.devices, args.blacklist_file)
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