• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/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
6"""Utility for reading / writing command-line flag files on device(s)."""
7
8import argparse
9import sys
10
11import devil_chromium
12
13from devil.android import device_utils
14from devil.android import device_errors
15from devil.utils import cmd_helper
16
17
18def main():
19  parser = argparse.ArgumentParser(description=__doc__)
20  parser.usage = '''%(prog)s --device-path PATH [--device SERIAL] [flags...]
21
22No flags: Prints existing command-line file.
23Empty string: Deletes command-line file.
24Otherwise: Writes command-line file.
25
26'''
27  parser.add_argument('-d', '--device', dest='devices', action='append',
28                      default=[], help='Target device serial (repeatable).')
29  parser.add_argument('--device-path', required=True,
30                      help='Remote path to flags file.')
31  parser.add_argument('-e', '--executable', dest='executable', default='chrome',
32                      help='Name of the executable.')
33  args, remote_args = parser.parse_known_args()
34
35  devil_chromium.Initialize()
36
37  as_root = not args.device_path.startswith('/data/local/tmp/')
38
39  devices = device_utils.DeviceUtils.HealthyDevices(device_arg=args.devices,
40                                                    default_retries=0)
41  all_devices = device_utils.DeviceUtils.parallel(devices)
42
43  def print_args():
44    def read_flags(device):
45      try:
46        return device.ReadFile(args.device_path, as_root=as_root).rstrip()
47      except device_errors.AdbCommandFailedError:
48        return ''  # File might not exist.
49
50    descriptions = all_devices.pMap(lambda d: d.build_description).pGet(None)
51    flags = all_devices.pMap(read_flags).pGet(None)
52    for d, desc, flags in zip(devices, descriptions, flags):
53      print '  %s (%s): %r' % (d, desc, flags)
54
55  # No args == print flags.
56  if not remote_args:
57    print 'Existing flags (in %s):' % args.device_path
58    print_args()
59    return 0
60
61  # Empty string arg == delete flags file.
62  if len(remote_args) == 1 and not remote_args[0]:
63    def delete_flags(device):
64      device.RunShellCommand(['rm', '-f', args.device_path], as_root=as_root)
65    all_devices.pMap(delete_flags).pGet(None)
66    print 'Deleted %s' % args.device_path
67    return 0
68
69  # Set flags.
70  quoted_args = ' '.join(cmd_helper.SingleQuote(x) for x in remote_args)
71  flags_str = ' '.join([args.executable, quoted_args])
72
73  def write_flags(device):
74    device.WriteFile(args.device_path, flags_str, as_root=as_root)
75    device.RunShellCommand(['chmod', '0664', args.device_path], as_root=as_root)
76
77  all_devices.pMap(write_flags).pGet(None)
78  print 'Wrote flags to %s' % args.device_path
79  print_args()
80  return 0
81
82
83if __name__ == '__main__':
84  sys.exit(main())
85