Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | # found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | import constants |
michael@0 | 6 | import traceback |
michael@0 | 7 | import warnings |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | # Location where chrome reads command line flags from |
michael@0 | 11 | CHROME_COMMAND_FILE = constants.TEST_EXECUTABLE_DIR + '/chrome-command-line' |
michael@0 | 12 | |
michael@0 | 13 | class FlagChanger(object): |
michael@0 | 14 | """Changes the flags Chrome runs with. |
michael@0 | 15 | |
michael@0 | 16 | There are two different use cases for this file: |
michael@0 | 17 | * Flags are permanently set by calling Set(). |
michael@0 | 18 | * Flags can be temporarily set for a particular set of unit tests. These |
michael@0 | 19 | tests should call Restore() to revert the flags to their original state |
michael@0 | 20 | once the tests have completed. |
michael@0 | 21 | """ |
michael@0 | 22 | |
michael@0 | 23 | def __init__(self, android_cmd): |
michael@0 | 24 | self._android_cmd = android_cmd |
michael@0 | 25 | |
michael@0 | 26 | # Save the original flags. |
michael@0 | 27 | self._orig_line = self._android_cmd.GetFileContents(CHROME_COMMAND_FILE) |
michael@0 | 28 | if self._orig_line: |
michael@0 | 29 | self._orig_line = self._orig_line[0].strip() |
michael@0 | 30 | |
michael@0 | 31 | # Parse out the flags into a list to facilitate adding and removing flags. |
michael@0 | 32 | self._current_flags = self._TokenizeFlags(self._orig_line) |
michael@0 | 33 | |
michael@0 | 34 | def Get(self): |
michael@0 | 35 | """Returns list of current flags.""" |
michael@0 | 36 | return self._current_flags |
michael@0 | 37 | |
michael@0 | 38 | def Set(self, flags): |
michael@0 | 39 | """Replaces all flags on the current command line with the flags given. |
michael@0 | 40 | |
michael@0 | 41 | Args: |
michael@0 | 42 | flags: A list of flags to set, eg. ['--single-process']. |
michael@0 | 43 | """ |
michael@0 | 44 | if flags: |
michael@0 | 45 | assert flags[0] != 'chrome' |
michael@0 | 46 | |
michael@0 | 47 | self._current_flags = flags |
michael@0 | 48 | self._UpdateCommandLineFile() |
michael@0 | 49 | |
michael@0 | 50 | def AddFlags(self, flags): |
michael@0 | 51 | """Appends flags to the command line if they aren't already there. |
michael@0 | 52 | |
michael@0 | 53 | Args: |
michael@0 | 54 | flags: A list of flags to add on, eg. ['--single-process']. |
michael@0 | 55 | """ |
michael@0 | 56 | if flags: |
michael@0 | 57 | assert flags[0] != 'chrome' |
michael@0 | 58 | |
michael@0 | 59 | # Avoid appending flags that are already present. |
michael@0 | 60 | for flag in flags: |
michael@0 | 61 | if flag not in self._current_flags: |
michael@0 | 62 | self._current_flags.append(flag) |
michael@0 | 63 | self._UpdateCommandLineFile() |
michael@0 | 64 | |
michael@0 | 65 | def RemoveFlags(self, flags): |
michael@0 | 66 | """Removes flags from the command line, if they exist. |
michael@0 | 67 | |
michael@0 | 68 | Args: |
michael@0 | 69 | flags: A list of flags to remove, eg. ['--single-process']. Note that we |
michael@0 | 70 | expect a complete match when removing flags; if you want to remove |
michael@0 | 71 | a switch with a value, you must use the exact string used to add |
michael@0 | 72 | it in the first place. |
michael@0 | 73 | """ |
michael@0 | 74 | if flags: |
michael@0 | 75 | assert flags[0] != 'chrome' |
michael@0 | 76 | |
michael@0 | 77 | for flag in flags: |
michael@0 | 78 | if flag in self._current_flags: |
michael@0 | 79 | self._current_flags.remove(flag) |
michael@0 | 80 | self._UpdateCommandLineFile() |
michael@0 | 81 | |
michael@0 | 82 | def Restore(self): |
michael@0 | 83 | """Restores the flags to their original state.""" |
michael@0 | 84 | self._current_flags = self._TokenizeFlags(self._orig_line) |
michael@0 | 85 | self._UpdateCommandLineFile() |
michael@0 | 86 | |
michael@0 | 87 | def _UpdateCommandLineFile(self): |
michael@0 | 88 | """Writes out the command line to the file, or removes it if empty.""" |
michael@0 | 89 | print "Current flags: ", self._current_flags |
michael@0 | 90 | |
michael@0 | 91 | if self._current_flags: |
michael@0 | 92 | self._android_cmd.SetFileContents(CHROME_COMMAND_FILE, |
michael@0 | 93 | 'chrome ' + |
michael@0 | 94 | ' '.join(self._current_flags)) |
michael@0 | 95 | else: |
michael@0 | 96 | self._android_cmd.RunShellCommand('rm ' + CHROME_COMMAND_FILE) |
michael@0 | 97 | |
michael@0 | 98 | def _TokenizeFlags(self, line): |
michael@0 | 99 | """Changes the string containing the command line into a list of flags. |
michael@0 | 100 | |
michael@0 | 101 | Follows similar logic to CommandLine.java::tokenizeQuotedArguments: |
michael@0 | 102 | * Flags are split using whitespace, unless the whitespace is within a |
michael@0 | 103 | pair of quotation marks. |
michael@0 | 104 | * Unlike the Java version, we keep the quotation marks around switch |
michael@0 | 105 | values since we need them to re-create the file when new flags are |
michael@0 | 106 | appended. |
michael@0 | 107 | |
michael@0 | 108 | Args: |
michael@0 | 109 | line: A string containing the entire command line. The first token is |
michael@0 | 110 | assumed to be the program name. |
michael@0 | 111 | """ |
michael@0 | 112 | if not line: |
michael@0 | 113 | return [] |
michael@0 | 114 | |
michael@0 | 115 | tokenized_flags = [] |
michael@0 | 116 | current_flag = "" |
michael@0 | 117 | within_quotations = False |
michael@0 | 118 | |
michael@0 | 119 | # Move through the string character by character and build up each flag |
michael@0 | 120 | # along the way. |
michael@0 | 121 | for c in line.strip(): |
michael@0 | 122 | if c is '"': |
michael@0 | 123 | if len(current_flag) > 0 and current_flag[-1] == '\\': |
michael@0 | 124 | # Last char was a backslash; pop it, and treat this " as a literal. |
michael@0 | 125 | current_flag = current_flag[0:-1] + '"' |
michael@0 | 126 | else: |
michael@0 | 127 | within_quotations = not within_quotations |
michael@0 | 128 | current_flag += c |
michael@0 | 129 | elif not within_quotations and (c is ' ' or c is '\t'): |
michael@0 | 130 | if current_flag is not "": |
michael@0 | 131 | tokenized_flags.append(current_flag) |
michael@0 | 132 | current_flag = "" |
michael@0 | 133 | else: |
michael@0 | 134 | current_flag += c |
michael@0 | 135 | |
michael@0 | 136 | # Tack on the last flag. |
michael@0 | 137 | if not current_flag: |
michael@0 | 138 | if within_quotations: |
michael@0 | 139 | warnings.warn("Unterminated quoted string: " + current_flag) |
michael@0 | 140 | else: |
michael@0 | 141 | tokenized_flags.append(current_flag) |
michael@0 | 142 | |
michael@0 | 143 | # Return everything but the program name. |
michael@0 | 144 | return tokenized_flags[1:] |