Add ICU message format support
[chromium-blink-merge.git] / build / android / pylib / flag_changer.py
bloba613f6a3abdc1ef1569309d73aebea2b8d03cfdc
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
3 # found in the LICENSE file.
5 import logging
7 import pylib.device.device_utils
9 from pylib.device import device_errors
12 class FlagChanger(object):
13 """Changes the flags Chrome runs with.
15 There are two different use cases for this file:
16 * Flags are permanently set by calling Set().
17 * Flags can be temporarily set for a particular set of unit tests. These
18 tests should call Restore() to revert the flags to their original state
19 once the tests have completed.
20 """
22 def __init__(self, device, cmdline_file):
23 """Initializes the FlagChanger and records the original arguments.
25 Args:
26 device: A DeviceUtils instance.
27 cmdline_file: Path to the command line file on the device.
28 """
29 self._device = device
30 self._cmdline_file = cmdline_file
32 # Save the original flags.
33 try:
34 self._orig_line = self._device.ReadFile(self._cmdline_file).strip()
35 except device_errors.CommandFailedError:
36 self._orig_line = ''
38 # Parse out the flags into a list to facilitate adding and removing flags.
39 self._current_flags = self._TokenizeFlags(self._orig_line)
41 def Get(self):
42 """Returns list of current flags."""
43 return self._current_flags
45 def Set(self, flags):
46 """Replaces all flags on the current command line with the flags given.
48 Args:
49 flags: A list of flags to set, eg. ['--single-process'].
50 """
51 if flags:
52 assert flags[0] != 'chrome'
54 self._current_flags = flags
55 self._UpdateCommandLineFile()
57 def AddFlags(self, flags):
58 """Appends flags to the command line if they aren't already there.
60 Args:
61 flags: A list of flags to add on, eg. ['--single-process'].
62 """
63 if flags:
64 assert flags[0] != 'chrome'
66 # Avoid appending flags that are already present.
67 for flag in flags:
68 if flag not in self._current_flags:
69 self._current_flags.append(flag)
70 self._UpdateCommandLineFile()
72 def RemoveFlags(self, flags):
73 """Removes flags from the command line, if they exist.
75 Args:
76 flags: A list of flags to remove, eg. ['--single-process']. Note that we
77 expect a complete match when removing flags; if you want to remove
78 a switch with a value, you must use the exact string used to add
79 it in the first place.
80 """
81 if flags:
82 assert flags[0] != 'chrome'
84 for flag in flags:
85 if flag in self._current_flags:
86 self._current_flags.remove(flag)
87 self._UpdateCommandLineFile()
89 def Restore(self):
90 """Restores the flags to their original state."""
91 self._current_flags = self._TokenizeFlags(self._orig_line)
92 self._UpdateCommandLineFile()
94 def _UpdateCommandLineFile(self):
95 """Writes out the command line to the file, or removes it if empty."""
96 logging.info('Current flags: %s', self._current_flags)
97 # Root is not required to write to /data/local/tmp/.
98 use_root = '/data/local/tmp/' not in self._cmdline_file
99 if self._current_flags:
100 # The first command line argument doesn't matter as we are not actually
101 # launching the chrome executable using this command line.
102 cmd_line = ' '.join(['_'] + self._current_flags)
103 self._device.WriteFile(
104 self._cmdline_file, cmd_line, as_root=use_root)
105 file_contents = self._device.ReadFile(
106 self._cmdline_file, as_root=use_root).rstrip()
107 assert file_contents == cmd_line, (
108 'Failed to set the command line file at %s' % self._cmdline_file)
109 else:
110 self._device.RunShellCommand('rm ' + self._cmdline_file,
111 as_root=use_root)
112 assert not self._device.FileExists(self._cmdline_file), (
113 'Failed to remove the command line file at %s' % self._cmdline_file)
115 @staticmethod
116 def _TokenizeFlags(line):
117 """Changes the string containing the command line into a list of flags.
119 Follows similar logic to CommandLine.java::tokenizeQuotedArguments:
120 * Flags are split using whitespace, unless the whitespace is within a
121 pair of quotation marks.
122 * Unlike the Java version, we keep the quotation marks around switch
123 values since we need them to re-create the file when new flags are
124 appended.
126 Args:
127 line: A string containing the entire command line. The first token is
128 assumed to be the program name.
130 if not line:
131 return []
133 tokenized_flags = []
134 current_flag = ""
135 within_quotations = False
137 # Move through the string character by character and build up each flag
138 # along the way.
139 for c in line.strip():
140 if c is '"':
141 if len(current_flag) > 0 and current_flag[-1] == '\\':
142 # Last char was a backslash; pop it, and treat this " as a literal.
143 current_flag = current_flag[0:-1] + '"'
144 else:
145 within_quotations = not within_quotations
146 current_flag += c
147 elif not within_quotations and (c is ' ' or c is '\t'):
148 if current_flag is not "":
149 tokenized_flags.append(current_flag)
150 current_flag = ""
151 else:
152 current_flag += c
154 # Tack on the last flag.
155 if not current_flag:
156 if within_quotations:
157 logging.warn('Unterminated quoted argument: ' + line)
158 else:
159 tokenized_flags.append(current_flag)
161 # Return everything but the program name.
162 return tokenized_flags[1:]