adb_command_line.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env vpython3
  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. """Utility for reading / writing command-line flag files on device(s)."""
  6. from __future__ import print_function
  7. import argparse
  8. import logging
  9. import sys
  10. import devil_chromium
  11. from devil.android import device_errors
  12. from devil.android import device_utils
  13. from devil.android import flag_changer
  14. from devil.android.tools import script_common
  15. from devil.utils import cmd_helper
  16. from devil.utils import logging_common
  17. def CheckBuildTypeSupportsFlags(device, command_line_flags_file):
  18. is_webview = command_line_flags_file == 'webview-command-line'
  19. if device.IsUserBuild() and is_webview:
  20. raise device_errors.CommandFailedError(
  21. 'WebView only respects flags on a userdebug or eng device, yours '
  22. 'is a user build.', device)
  23. if device.IsUserBuild():
  24. logging.warning(
  25. 'Your device (%s) is a user build; Chrome may or may not pick up '
  26. 'your commandline flags. Check your '
  27. '"command_line_on_non_rooted_enabled" preference, or switch '
  28. 'devices.', device)
  29. def main():
  30. parser = argparse.ArgumentParser(description=__doc__)
  31. parser.usage = '''%(prog)s --name FILENAME [--device SERIAL] [flags...]
  32. No flags: Prints existing command-line file.
  33. Empty string: Deletes command-line file.
  34. Otherwise: Writes command-line file.
  35. '''
  36. parser.add_argument('--name', required=True,
  37. help='Name of file where to store flags on the device.')
  38. parser.add_argument('-e', '--executable', dest='executable', default='chrome',
  39. help='(deprecated) No longer used.')
  40. script_common.AddEnvironmentArguments(parser)
  41. script_common.AddDeviceArguments(parser)
  42. logging_common.AddLoggingArguments(parser)
  43. args, remote_args = parser.parse_known_args()
  44. devil_chromium.Initialize(adb_path=args.adb_path)
  45. logging_common.InitializeLogging(args)
  46. devices = device_utils.DeviceUtils.HealthyDevices(device_arg=args.devices,
  47. default_retries=0)
  48. all_devices = device_utils.DeviceUtils.parallel(devices)
  49. if not remote_args:
  50. # No args == do not update, just print flags.
  51. remote_args = None
  52. action = ''
  53. elif len(remote_args) == 1 and not remote_args[0]:
  54. # Single empty string arg == delete flags
  55. remote_args = []
  56. action = 'Deleted command line file. '
  57. else:
  58. action = 'Wrote command line file. '
  59. def update_flags(device):
  60. CheckBuildTypeSupportsFlags(device, args.name)
  61. changer = flag_changer.FlagChanger(device, args.name)
  62. if remote_args is not None:
  63. flags = changer.ReplaceFlags(remote_args)
  64. else:
  65. flags = changer.GetCurrentFlags()
  66. return (device, device.build_description, flags)
  67. updated_values = all_devices.pMap(update_flags).pGet(None)
  68. print('%sCurrent flags (in %s):' % (action, args.name))
  69. for d, desc, flags in updated_values:
  70. if flags:
  71. # Shell-quote flags for easy copy/paste as new args on the terminal.
  72. quoted_flags = ' '.join(cmd_helper.SingleQuote(f) for f in sorted(flags))
  73. else:
  74. quoted_flags = '( empty )'
  75. print(' %s (%s): %s' % (d, desc, quoted_flags))
  76. return 0
  77. if __name__ == '__main__':
  78. sys.exit(main())