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