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