| 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 | 18 sys.path.append(os.path.join(os.path.dirname(__file__), '..')) # tools/metrics |
| 19 import print_style | 19 from common.diff_util import PromptUserToAcceptDiff |
| 20 from common import path_util |
| 21 from histograms import print_style |
| 20 | 22 |
| 21 # Import the metrics/common module. | 23 HISTOGRAMS_PATH = path_util.GetHistogramsFile() |
| 22 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) | |
| 23 from diff_util import PromptUserToAcceptDiff | |
| 24 | |
| 25 HISTOGRAMS_PATH = 'histograms.xml' | |
| 26 ENUM_NAME = 'MappedEditingCommands' | 24 ENUM_NAME = 'MappedEditingCommands' |
| 27 | 25 |
| 28 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH = \ | 26 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\[\] = {" | 27 ENUM_START_MARKER = "^ static const CommandEntry commands\[\] = {" |
| 31 ENUM_END_MARKER = "^ };" | 28 ENUM_END_MARKER = "^ };" |
| 32 | 29 |
| 33 | 30 |
| 34 class UserError(Exception): | 31 class UserError(Exception): |
| 35 def __init__(self, message): | 32 def __init__(self, message): |
| 36 Exception.__init__(self, message) | 33 Exception.__init__(self, message) |
| 37 | 34 |
| 38 @property | 35 @property |
| 39 def message(self): | 36 def message(self): |
| 40 return self.args[0] | 37 return self.args[0] |
| 41 | 38 |
| 42 | 39 |
| 43 def ReadHistogramValues(filename): | 40 def ReadHistogramValues(filename): |
| 44 """Returns a list of pairs (label, value) corresponding to HistogramValue. | 41 """Returns a list of pairs (label, value) corresponding to HistogramValue. |
| 45 | 42 |
| 46 Reads the EditorCommand.cpp file, locates the | 43 Reads the EditorCommand.cpp file, locates the |
| 47 HistogramValue enum definition and returns a pair for each entry. | 44 HistogramValue enum definition and returns a pair for each entry. |
| 48 """ | 45 """ |
| 49 | 46 |
| 50 # Read the file as a list of lines | 47 # Read the file as a list of lines |
| 51 with open(filename) as f: | 48 with open(path_util.GetInputFile(filename)) as f: |
| 52 content = f.readlines() | 49 content = f.readlines() |
| 53 | 50 |
| 54 # Locate the enum definition and collect all entries in it | 51 # Locate the enum definition and collect all entries in it |
| 55 inside_enum = False # We haven't found the enum definition yet | 52 inside_enum = False # We haven't found the enum definition yet |
| 56 result = [] | 53 result = [] |
| 57 for line in content: | 54 for line in content: |
| 58 if inside_enum: | 55 if inside_enum: |
| 59 # Exit condition: we reached last enum value | 56 # Exit condition: we reached last enum value |
| 60 if re.match(ENUM_END_MARKER, line): | 57 if re.match(ENUM_END_MARKER, line): |
| 61 inside_enum = False | 58 inside_enum = False |
| (...skipping 28 matching lines...) Expand all Loading... |
| 90 break | 87 break |
| 91 else: | 88 else: |
| 92 raise UserError('No policy enum node found') | 89 raise UserError('No policy enum node found') |
| 93 | 90 |
| 94 # Remove existing values. | 91 # Remove existing values. |
| 95 while extension_functions_enum_node.hasChildNodes(): | 92 while extension_functions_enum_node.hasChildNodes(): |
| 96 extension_functions_enum_node.removeChild( | 93 extension_functions_enum_node.removeChild( |
| 97 extension_functions_enum_node.lastChild) | 94 extension_functions_enum_node.lastChild) |
| 98 | 95 |
| 99 # Add a "Generated from (...)" comment | 96 # Add a "Generated from (...)" comment |
| 100 comment = ' Generated from {0} '.format( | 97 comment = ' Generated from {0} '.format(EDITOR_COMMAND_CPP) |
| 101 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH) | |
| 102 extension_functions_enum_node.appendChild(document.createComment(comment)) | 98 extension_functions_enum_node.appendChild(document.createComment(comment)) |
| 103 | 99 |
| 104 # Add values generated from policy templates. | 100 # Add values generated from policy templates. |
| 105 for (label, value) in histogram_values: | 101 for (label, value) in histogram_values: |
| 106 node = document.createElement('int') | 102 node = document.createElement('int') |
| 107 node.attributes['value'] = str(value) | 103 node.attributes['value'] = str(value) |
| 108 node.attributes['label'] = label | 104 node.attributes['label'] = label |
| 109 extension_functions_enum_node.appendChild(node) | 105 extension_functions_enum_node.appendChild(node) |
| 110 | 106 |
| 107 |
| 111 def Log(message): | 108 def Log(message): |
| 112 logging.info(message) | 109 logging.info(message) |
| 113 | 110 |
| 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 |