Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 """Updates MappedEditingCommands enum in histograms.xml file with values read | 5 """Updates MappedEditingCommands enum in histograms.xml file with values read |
| 6 from EditorCommand.cpp. | 6 from EditorCommand.cpp. |
| 7 | 7 |
| 8 If the file was pretty-printed, the updated version is pretty-printed too. | 8 If the file was pretty-printed, the updated version is pretty-printed too. |
| 9 """ | 9 """ |
| 10 | 10 |
| 11 import logging | 11 import logging |
| 12 import os | 12 import os |
| 13 import re | 13 import re |
| 14 import sys | 14 import sys |
| 15 | 15 |
| 16 from xml.dom import minidom | 16 from xml.dom import minidom |
| 17 | 17 |
| 18 from diffutil import PromptUserToAcceptDiff | |
| 19 import print_style | 18 import print_style |
| 19 import histograms_path | |
|
Ilya Sherman
2015/05/27 00:12:24
nit: Alphabetize?
ncarter (slow)
2015/05/29 23:01:10
Done.
| |
| 20 | 20 |
| 21 # Import the metrics/common module. | 21 # Import the metrics/common module. |
| 22 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) | 22 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) |
| 23 from diff_util import PromptUserToAcceptDiff | 23 from diff_util import PromptUserToAcceptDiff |
| 24 | 24 |
| 25 HISTOGRAMS_PATH = 'histograms.xml' | 25 HISTOGRAMS_PATH = histograms_path.GetHistogramsFile() |
| 26 ENUM_NAME = 'MappedEditingCommands' | 26 ENUM_NAME = 'MappedEditingCommands' |
| 27 | 27 |
| 28 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH = \ | 28 EDITOR_COMMAND_CPP = 'third_party/WebKit/Source/core/editing/EditorCommand.cpp' |
| 29 '../../../third_party/WebKit/Source/core/editing/EditorCommand.cpp' | |
| 30 ENUM_START_MARKER = "^ static const CommandEntry commands\[\] = {" | 29 ENUM_START_MARKER = "^ static const CommandEntry commands\[\] = {" |
| 31 ENUM_END_MARKER = "^ };" | 30 ENUM_END_MARKER = "^ };" |
| 32 | 31 |
| 33 | 32 |
| 34 class UserError(Exception): | 33 class UserError(Exception): |
| 35 def __init__(self, message): | 34 def __init__(self, message): |
| 36 Exception.__init__(self, message) | 35 Exception.__init__(self, message) |
| 37 | 36 |
| 38 @property | 37 @property |
| 39 def message(self): | 38 def message(self): |
| 40 return self.args[0] | 39 return self.args[0] |
| 41 | 40 |
| 42 | 41 |
| 43 def ReadHistogramValues(filename): | 42 def ReadHistogramValues(filename): |
| 44 """Returns a list of pairs (label, value) corresponding to HistogramValue. | 43 """Returns a list of pairs (label, value) corresponding to HistogramValue. |
| 45 | 44 |
| 46 Reads the EditorCommand.cpp file, locates the | 45 Reads the EditorCommand.cpp file, locates the |
| 47 HistogramValue enum definition and returns a pair for each entry. | 46 HistogramValue enum definition and returns a pair for each entry. |
| 48 """ | 47 """ |
| 49 | 48 |
| 50 # Read the file as a list of lines | 49 # Read the file as a list of lines |
| 51 with open(filename) as f: | 50 with open(histograms_path.GetInputFile(filename)) as f: |
| 52 content = f.readlines() | 51 content = f.readlines() |
| 53 | 52 |
| 54 # Locate the enum definition and collect all entries in it | 53 # Locate the enum definition and collect all entries in it |
| 55 inside_enum = False # We haven't found the enum definition yet | 54 inside_enum = False # We haven't found the enum definition yet |
| 56 result = [] | 55 result = [] |
| 57 for line in content: | 56 for line in content: |
| 58 if inside_enum: | 57 if inside_enum: |
| 59 # Exit condition: we reached last enum value | 58 # Exit condition: we reached last enum value |
| 60 if re.match(ENUM_END_MARKER, line): | 59 if re.match(ENUM_END_MARKER, line): |
| 61 inside_enum = False | 60 inside_enum = False |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 90 break | 89 break |
| 91 else: | 90 else: |
| 92 raise UserError('No policy enum node found') | 91 raise UserError('No policy enum node found') |
| 93 | 92 |
| 94 # Remove existing values. | 93 # Remove existing values. |
| 95 while extension_functions_enum_node.hasChildNodes(): | 94 while extension_functions_enum_node.hasChildNodes(): |
| 96 extension_functions_enum_node.removeChild( | 95 extension_functions_enum_node.removeChild( |
| 97 extension_functions_enum_node.lastChild) | 96 extension_functions_enum_node.lastChild) |
| 98 | 97 |
| 99 # Add a "Generated from (...)" comment | 98 # Add a "Generated from (...)" comment |
| 100 comment = ' Generated from {0} '.format( | 99 comment = ' Generated from {0} '.format(EDITOR_COMMAND_CPP) |
| 101 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH) | |
| 102 extension_functions_enum_node.appendChild(document.createComment(comment)) | 100 extension_functions_enum_node.appendChild(document.createComment(comment)) |
| 103 | 101 |
| 104 # Add values generated from policy templates. | 102 # Add values generated from policy templates. |
| 105 for (label, value) in histogram_values: | 103 for (label, value) in histogram_values: |
| 106 node = document.createElement('int') | 104 node = document.createElement('int') |
| 107 node.attributes['value'] = str(value) | 105 node.attributes['value'] = str(value) |
| 108 node.attributes['label'] = label | 106 node.attributes['label'] = label |
| 109 extension_functions_enum_node.appendChild(node) | 107 extension_functions_enum_node.appendChild(node) |
| 110 | 108 |
| 111 def Log(message): | 109 def Log(message): |
| 112 logging.info(message) | 110 logging.info(message) |
| 113 | 111 |
| 114 def main(): | 112 def main(): |
| 115 if len(sys.argv) > 1: | 113 if len(sys.argv) > 1: |
| 116 print >>sys.stderr, 'No arguments expected!' | 114 print >>sys.stderr, 'No arguments expected!' |
| 117 sys.stderr.write(__doc__) | 115 sys.stderr.write(__doc__) |
| 118 sys.exit(1) | 116 sys.exit(1) |
| 119 | 117 |
| 120 Log('Reading histogram enum definition from "%s".' | 118 Log('Reading histogram enum definition from "%s".' % EDITOR_COMMAND_CPP) |
| 121 % (EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH)) | 119 histogram_values = ReadHistogramValues(EDITOR_COMMAND_CPP) |
| 122 histogram_values = ReadHistogramValues( | |
| 123 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH) | |
| 124 | 120 |
| 125 Log('Reading existing histograms from "%s".' % (HISTOGRAMS_PATH)) | 121 Log('Reading existing histograms from "%s".' % (HISTOGRAMS_PATH)) |
| 126 with open(HISTOGRAMS_PATH, 'rb') as f: | 122 with open(HISTOGRAMS_PATH, 'rb') as f: |
| 127 histograms_doc = minidom.parse(f) | 123 histograms_doc = minidom.parse(f) |
| 128 f.seek(0) | 124 f.seek(0) |
| 129 xml = f.read() | 125 xml = f.read() |
| 130 | 126 |
| 131 Log('Comparing histograms enum with new enum definition.') | 127 Log('Comparing histograms enum with new enum definition.') |
| 132 UpdateHistogramDefinitions(histogram_values, histograms_doc) | 128 UpdateHistogramDefinitions(histogram_values, histograms_doc) |
| 133 | 129 |
| 134 Log('Writing out new histograms file.') | 130 Log('Writing out new histograms file.') |
| 135 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) | 131 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) |
| 136 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): | 132 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): |
| 137 with open(HISTOGRAMS_PATH, 'wb') as f: | 133 with open(HISTOGRAMS_PATH, 'wb') as f: |
| 138 f.write(new_xml) | 134 f.write(new_xml) |
| 139 | 135 |
| 140 Log('Done.') | 136 Log('Done.') |
| 141 | 137 |
| 142 | 138 |
| 143 if __name__ == '__main__': | 139 if __name__ == '__main__': |
| 144 main() | 140 main() |
| OLD | NEW |