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