Peter Wen | b51e454 | 2021-06-30 17:42:57 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame] | 2 | # Copyright 2015 The Chromium Authors |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 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 | |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 8 | |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 9 | import argparse |
Nate Fischer | 5ead9a8a | 2019-02-08 02:20:27 | [diff] [blame] | 10 | import logging |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 11 | import sys |
| 12 | |
Nate Fischer | 8f2418c5 | 2019-07-09 22:03:46 | [diff] [blame] | 13 | import devil_chromium |
jbudorick | d28554a | 2016-01-11 16:22:59 | [diff] [blame] | 14 | |
Nate Fischer | 5ead9a8a | 2019-02-08 02:20:27 | [diff] [blame] | 15 | from devil.android import device_errors |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 16 | from devil.android import device_utils |
perezju | 9f5c3463 | 2017-01-20 07:42:28 | [diff] [blame] | 17 | from devil.android import flag_changer |
Nate Fischer | 5ead9a8a | 2019-02-08 02:20:27 | [diff] [blame] | 18 | from devil.android.tools import script_common |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 19 | from devil.utils import cmd_helper |
Nate Fischer | 5ead9a8a | 2019-02-08 02:20:27 | [diff] [blame] | 20 | from devil.utils import logging_common |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 21 | |
| 22 | |
Nate Fischer | 236f923 | 2019-04-17 19:37:31 | [diff] [blame] | 23 | def CheckBuildTypeSupportsFlags(device, command_line_flags_file): |
| 24 | is_webview = command_line_flags_file == 'webview-command-line' |
| 25 | if device.IsUserBuild() and is_webview: |
| 26 | raise device_errors.CommandFailedError( |
| 27 | 'WebView only respects flags on a userdebug or eng device, yours ' |
| 28 | 'is a user build.', device) |
Yoshisato Yanagisawa | 0f42f10 | 2021-11-30 14:04:37 | [diff] [blame] | 29 | if device.IsUserBuild(): |
Nate Fischer | 236f923 | 2019-04-17 19:37:31 | [diff] [blame] | 30 | logging.warning( |
| 31 | 'Your device (%s) is a user build; Chrome may or may not pick up ' |
| 32 | 'your commandline flags. Check your ' |
| 33 | '"command_line_on_non_rooted_enabled" preference, or switch ' |
| 34 | 'devices.', device) |
| 35 | |
| 36 | |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 37 | def main(): |
| 38 | parser = argparse.ArgumentParser(description=__doc__) |
perezju | 9f5c3463 | 2017-01-20 07:42:28 | [diff] [blame] | 39 | parser.usage = '''%(prog)s --name FILENAME [--device SERIAL] [flags...] |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 40 | |
| 41 | No flags: Prints existing command-line file. |
| 42 | Empty string: Deletes command-line file. |
| 43 | Otherwise: Writes command-line file. |
| 44 | |
| 45 | ''' |
perezju | 9f5c3463 | 2017-01-20 07:42:28 | [diff] [blame] | 46 | parser.add_argument('--name', required=True, |
| 47 | help='Name of file where to store flags on the device.') |
sanfin | 24d7960 | 2016-03-02 18:32:10 | [diff] [blame] | 48 | parser.add_argument('-e', '--executable', dest='executable', default='chrome', |
perezju | 9f5c3463 | 2017-01-20 07:42:28 | [diff] [blame] | 49 | help='(deprecated) No longer used.') |
Nate Fischer | 5ead9a8a | 2019-02-08 02:20:27 | [diff] [blame] | 50 | script_common.AddEnvironmentArguments(parser) |
| 51 | script_common.AddDeviceArguments(parser) |
| 52 | logging_common.AddLoggingArguments(parser) |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 53 | |
Nate Fischer | 5ead9a8a | 2019-02-08 02:20:27 | [diff] [blame] | 54 | args, remote_args = parser.parse_known_args() |
Nate Fischer | 8f2418c5 | 2019-07-09 22:03:46 | [diff] [blame] | 55 | devil_chromium.Initialize(adb_path=args.adb_path) |
Nate Fischer | 5ead9a8a | 2019-02-08 02:20:27 | [diff] [blame] | 56 | logging_common.InitializeLogging(args) |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 57 | |
agrieve | bb574a0 | 2016-04-18 20:02:48 | [diff] [blame] | 58 | devices = device_utils.DeviceUtils.HealthyDevices(device_arg=args.devices, |
| 59 | default_retries=0) |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 60 | all_devices = device_utils.DeviceUtils.parallel(devices) |
| 61 | |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 62 | if not remote_args: |
perezju | 9f5c3463 | 2017-01-20 07:42:28 | [diff] [blame] | 63 | # No args == do not update, just print flags. |
| 64 | remote_args = None |
| 65 | action = '' |
| 66 | elif len(remote_args) == 1 and not remote_args[0]: |
| 67 | # Single empty string arg == delete flags |
| 68 | remote_args = [] |
| 69 | action = 'Deleted command line file. ' |
| 70 | else: |
Bryce Thomas | 786bb13 | 2023-07-18 01:21:21 | [diff] [blame] | 71 | if remote_args[0] == '--': |
| 72 | remote_args.pop(0) |
perezju | 9f5c3463 | 2017-01-20 07:42:28 | [diff] [blame] | 73 | action = 'Wrote command line file. ' |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 74 | |
perezju | 9f5c3463 | 2017-01-20 07:42:28 | [diff] [blame] | 75 | def update_flags(device): |
Nate Fischer | 236f923 | 2019-04-17 19:37:31 | [diff] [blame] | 76 | CheckBuildTypeSupportsFlags(device, args.name) |
perezju | 9f5c3463 | 2017-01-20 07:42:28 | [diff] [blame] | 77 | changer = flag_changer.FlagChanger(device, args.name) |
| 78 | if remote_args is not None: |
| 79 | flags = changer.ReplaceFlags(remote_args) |
| 80 | else: |
| 81 | flags = changer.GetCurrentFlags() |
| 82 | return (device, device.build_description, flags) |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 83 | |
perezju | 9f5c3463 | 2017-01-20 07:42:28 | [diff] [blame] | 84 | updated_values = all_devices.pMap(update_flags).pGet(None) |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 85 | |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 86 | print('%sCurrent flags (in %s):' % (action, args.name)) |
perezju | 9f5c3463 | 2017-01-20 07:42:28 | [diff] [blame] | 87 | for d, desc, flags in updated_values: |
| 88 | if flags: |
| 89 | # Shell-quote flags for easy copy/paste as new args on the terminal. |
| 90 | quoted_flags = ' '.join(cmd_helper.SingleQuote(f) for f in sorted(flags)) |
| 91 | else: |
| 92 | quoted_flags = '( empty )' |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 93 | print(' %s (%s): %s' % (d, desc, quoted_flags)) |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 94 | |
agrieve | d7c8cc0 | 2015-11-24 15:23:18 | [diff] [blame] | 95 | return 0 |
| 96 | |
| 97 | |
| 98 | if __name__ == '__main__': |
| 99 | sys.exit(main()) |