blob: a2181603da04266e759ef7c9a371ffa7312e6c39 [file] [log] [blame]
Peter Wenb51e4542021-06-30 17:42:571#!/usr/bin/env vpython3
Avi Drissman73a09d12022-09-08 20:33:382# Copyright 2015 The Chromium Authors
agrieved7c8cc02015-11-24 15:23:183# 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 Tambre9e24293b2019-05-12 06:11:078
agrieved7c8cc02015-11-24 15:23:189import argparse
Nate Fischer5ead9a8a2019-02-08 02:20:2710import logging
agrieved7c8cc02015-11-24 15:23:1811import sys
12
Nate Fischer8f2418c52019-07-09 22:03:4613import devil_chromium
jbudorickd28554a2016-01-11 16:22:5914
Nate Fischer5ead9a8a2019-02-08 02:20:2715from devil.android import device_errors
agrieved7c8cc02015-11-24 15:23:1816from devil.android import device_utils
perezju9f5c34632017-01-20 07:42:2817from devil.android import flag_changer
Nate Fischer5ead9a8a2019-02-08 02:20:2718from devil.android.tools import script_common
agrieved7c8cc02015-11-24 15:23:1819from devil.utils import cmd_helper
Nate Fischer5ead9a8a2019-02-08 02:20:2720from devil.utils import logging_common
agrieved7c8cc02015-11-24 15:23:1821
22
Nate Fischer236f9232019-04-17 19:37:3123def 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 Yanagisawa0f42f102021-11-30 14:04:3729 if device.IsUserBuild():
Nate Fischer236f9232019-04-17 19:37:3130 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
agrieved7c8cc02015-11-24 15:23:1837def main():
38 parser = argparse.ArgumentParser(description=__doc__)
perezju9f5c34632017-01-20 07:42:2839 parser.usage = '''%(prog)s --name FILENAME [--device SERIAL] [flags...]
agrieved7c8cc02015-11-24 15:23:1840
41No flags: Prints existing command-line file.
42Empty string: Deletes command-line file.
43Otherwise: Writes command-line file.
44
45'''
perezju9f5c34632017-01-20 07:42:2846 parser.add_argument('--name', required=True,
47 help='Name of file where to store flags on the device.')
sanfin24d79602016-03-02 18:32:1048 parser.add_argument('-e', '--executable', dest='executable', default='chrome',
perezju9f5c34632017-01-20 07:42:2849 help='(deprecated) No longer used.')
Nate Fischer5ead9a8a2019-02-08 02:20:2750 script_common.AddEnvironmentArguments(parser)
51 script_common.AddDeviceArguments(parser)
52 logging_common.AddLoggingArguments(parser)
agrieved7c8cc02015-11-24 15:23:1853
Nate Fischer5ead9a8a2019-02-08 02:20:2754 args, remote_args = parser.parse_known_args()
Nate Fischer8f2418c52019-07-09 22:03:4655 devil_chromium.Initialize(adb_path=args.adb_path)
Nate Fischer5ead9a8a2019-02-08 02:20:2756 logging_common.InitializeLogging(args)
agrieved7c8cc02015-11-24 15:23:1857
agrievebb574a02016-04-18 20:02:4858 devices = device_utils.DeviceUtils.HealthyDevices(device_arg=args.devices,
59 default_retries=0)
agrieved7c8cc02015-11-24 15:23:1860 all_devices = device_utils.DeviceUtils.parallel(devices)
61
agrieved7c8cc02015-11-24 15:23:1862 if not remote_args:
perezju9f5c34632017-01-20 07:42:2863 # 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 Thomas786bb132023-07-18 01:21:2171 if remote_args[0] == '--':
72 remote_args.pop(0)
perezju9f5c34632017-01-20 07:42:2873 action = 'Wrote command line file. '
agrieved7c8cc02015-11-24 15:23:1874
perezju9f5c34632017-01-20 07:42:2875 def update_flags(device):
Nate Fischer236f9232019-04-17 19:37:3176 CheckBuildTypeSupportsFlags(device, args.name)
perezju9f5c34632017-01-20 07:42:2877 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)
agrieved7c8cc02015-11-24 15:23:1883
perezju9f5c34632017-01-20 07:42:2884 updated_values = all_devices.pMap(update_flags).pGet(None)
agrieved7c8cc02015-11-24 15:23:1885
Raul Tambre9e24293b2019-05-12 06:11:0786 print('%sCurrent flags (in %s):' % (action, args.name))
perezju9f5c34632017-01-20 07:42:2887 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 Tambre9e24293b2019-05-12 06:11:0793 print(' %s (%s): %s' % (d, desc, quoted_flags))
agrieved7c8cc02015-11-24 15:23:1894
agrieved7c8cc02015-11-24 15:23:1895 return 0
96
97
98if __name__ == '__main__':
99 sys.exit(main())