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