flag_changer.py 4.6 KB

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