OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import logging | 5 import logging |
6 | 6 |
7 import pylib.android_commands | 7 import pylib.android_commands |
8 import pylib.device.device_utils | 8 import pylib.device.device_utils |
9 | 9 |
10 from pylib.device import device_errors | |
11 | |
12 | 10 |
13 class FlagChanger(object): | 11 class FlagChanger(object): |
14 """Changes the flags Chrome runs with. | 12 """Changes the flags Chrome runs with. |
15 | 13 |
16 There are two different use cases for this file: | 14 There are two different use cases for this file: |
17 * Flags are permanently set by calling Set(). | 15 * Flags are permanently set by calling Set(). |
18 * Flags can be temporarily set for a particular set of unit tests. These | 16 * Flags can be temporarily set for a particular set of unit tests. These |
19 tests should call Restore() to revert the flags to their original state | 17 tests should call Restore() to revert the flags to their original state |
20 once the tests have completed. | 18 once the tests have completed. |
21 """ | 19 """ |
22 | 20 |
23 def __init__(self, device, cmdline_file): | 21 def __init__(self, device, cmdline_file): |
24 """Initializes the FlagChanger and records the original arguments. | 22 """Initializes the FlagChanger and records the original arguments. |
25 | 23 |
26 Args: | 24 Args: |
27 device: A DeviceUtils instance. | 25 device: A DeviceUtils instance. |
28 cmdline_file: Path to the command line file on the device. | 26 cmdline_file: Path to the command line file on the device. |
29 """ | 27 """ |
30 # TODO(jbudorick) Remove once telemetry switches over. | 28 # TODO(jbudorick) Remove once telemetry switches over. |
31 if isinstance(device, pylib.android_commands.AndroidCommands): | 29 if isinstance(device, pylib.android_commands.AndroidCommands): |
32 device = pylib.device.device_utils.DeviceUtils(device) | 30 device = pylib.device.device_utils.DeviceUtils(device) |
33 self._device = device | 31 self._device = device |
34 self._cmdline_file = cmdline_file | 32 self._cmdline_file = cmdline_file |
35 | 33 |
36 # Save the original flags. | 34 # Save the original flags. |
37 try: | 35 self._orig_line = self._device.ReadFile(self._cmdline_file) |
38 self._orig_line = self._device.ReadFile(self._cmdline_file).strip() | 36 if self._orig_line: |
39 except device_errors.CommandFailedError: | 37 self._orig_line = self._orig_line[0].strip() |
40 self._orig_line = '' | |
41 | 38 |
42 # Parse out the flags into a list to facilitate adding and removing flags. | 39 # Parse out the flags into a list to facilitate adding and removing flags. |
43 self._current_flags = self._TokenizeFlags(self._orig_line) | 40 self._current_flags = self._TokenizeFlags(self._orig_line) |
44 | 41 |
45 def Get(self): | 42 def Get(self): |
46 """Returns list of current flags.""" | 43 """Returns list of current flags.""" |
47 return self._current_flags | 44 return self._current_flags |
48 | 45 |
49 def Set(self, flags): | 46 def Set(self, flags): |
50 """Replaces all flags on the current command line with the flags given. | 47 """Replaces all flags on the current command line with the flags given. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 logging.info('Current flags: %s', self._current_flags) | 97 logging.info('Current flags: %s', self._current_flags) |
101 # Root is not required to write to /data/local/tmp/. | 98 # Root is not required to write to /data/local/tmp/. |
102 use_root = '/data/local/tmp/' not in self._cmdline_file | 99 use_root = '/data/local/tmp/' not in self._cmdline_file |
103 if self._current_flags: | 100 if self._current_flags: |
104 # The first command line argument doesn't matter as we are not actually | 101 # The first command line argument doesn't matter as we are not actually |
105 # launching the chrome executable using this command line. | 102 # launching the chrome executable using this command line. |
106 cmd_line = ' '.join(['_'] + self._current_flags) | 103 cmd_line = ' '.join(['_'] + self._current_flags) |
107 self._device.WriteFile( | 104 self._device.WriteFile( |
108 self._cmdline_file, cmd_line, as_root=use_root) | 105 self._cmdline_file, cmd_line, as_root=use_root) |
109 file_contents = self._device.ReadFile( | 106 file_contents = self._device.ReadFile( |
110 self._cmdline_file, as_root=use_root).rstrip() | 107 self._cmdline_file, as_root=use_root) |
111 assert file_contents == cmd_line, ( | 108 assert len(file_contents) == 1 and file_contents[0] == cmd_line, ( |
112 'Failed to set the command line file at %s' % self._cmdline_file) | 109 'Failed to set the command line file at %s' % self._cmdline_file) |
113 else: | 110 else: |
114 self._device.RunShellCommand('rm ' + self._cmdline_file, | 111 self._device.RunShellCommand('rm ' + self._cmdline_file, |
115 as_root=use_root) | 112 as_root=use_root) |
116 assert not self._device.FileExists(self._cmdline_file), ( | 113 assert not self._device.FileExists(self._cmdline_file), ( |
117 'Failed to remove the command line file at %s' % self._cmdline_file) | 114 'Failed to remove the command line file at %s' % self._cmdline_file) |
118 | 115 |
119 @staticmethod | 116 @staticmethod |
120 def _TokenizeFlags(line): | 117 def _TokenizeFlags(line): |
121 """Changes the string containing the command line into a list of flags. | 118 """Changes the string containing the command line into a list of flags. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 | 154 |
158 # Tack on the last flag. | 155 # Tack on the last flag. |
159 if not current_flag: | 156 if not current_flag: |
160 if within_quotations: | 157 if within_quotations: |
161 logging.warn('Unterminated quoted argument: ' + line) | 158 logging.warn('Unterminated quoted argument: ' + line) |
162 else: | 159 else: |
163 tokenized_flags.append(current_flag) | 160 tokenized_flags.append(current_flag) |
164 | 161 |
165 # Return everything but the program name. | 162 # Return everything but the program name. |
166 return tokenized_flags[1:] | 163 return tokenized_flags[1:] |
OLD | NEW |