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 enums in histograms.xml file with values read from provided C++ enum. | 5 """Updates enums in histograms.xml file with values read from provided C++ enum. |
6 | 6 |
7 If the file was pretty-printed, the updated version is pretty-printed too. | 7 If the file was pretty-printed, the updated version is pretty-printed too. |
8 """ | 8 """ |
9 | 9 |
10 import logging | 10 import logging |
(...skipping 20 matching lines...) Expand all Loading... | |
31 @property | 31 @property |
32 def message(self): | 32 def message(self): |
33 return self.args[0] | 33 return self.args[0] |
34 | 34 |
35 | 35 |
36 def Log(message): | 36 def Log(message): |
37 logging.info(message) | 37 logging.info(message) |
38 | 38 |
39 | 39 |
40 def ReadHistogramValues(filename, start_marker, end_marker): | 40 def ReadHistogramValues(filename, start_marker, end_marker): |
41 """Returns a dictionary of enum values, read from a C++ file. | 41 """Returns a dictionary of enum values and a pair of labels that have the same |
42 enum values, read from a C++ file. | |
42 | 43 |
43 Args: | 44 Args: |
44 filename: The unix-style path (relative to src/) of the file to open. | 45 filename: The unix-style path (relative to src/) of the file to open. |
45 start_marker: A regex that signifies the start of the enum values. | 46 start_marker: A regex that signifies the start of the enum values. |
46 end_marker: A regex that signifies the end of the enum values. | 47 end_marker: A regex that signifies the end of the enum values. |
47 """ | 48 """ |
48 # Read the file as a list of lines | 49 # Read the file as a list of lines |
49 with open(path_util.GetInputFile(filename)) as f: | 50 with open(path_util.GetInputFile(filename)) as f: |
50 content = f.readlines() | 51 content = f.readlines() |
51 | 52 |
(...skipping 16 matching lines...) Expand all Loading... | |
68 m = ITEM_REGEX_WITH_INIT.match(line) | 69 m = ITEM_REGEX_WITH_INIT.match(line) |
69 if m: | 70 if m: |
70 enum_value = int(m.group(2)) | 71 enum_value = int(m.group(2)) |
71 label = m.group(1) | 72 label = m.group(1) |
72 else: | 73 else: |
73 m = ITEM_REGEX.match(line) | 74 m = ITEM_REGEX.match(line) |
74 if m: | 75 if m: |
75 label = m.group(1) | 76 label = m.group(1) |
76 else: | 77 else: |
77 continue | 78 continue |
79 # If two enum labels have the same value | |
80 if enum_value in result: | |
81 return result, (result[enum_value], label) | |
78 result[enum_value] = label | 82 result[enum_value] = label |
79 enum_value += 1 | 83 enum_value += 1 |
80 else: | 84 else: |
81 if START_REGEX.match(line): | 85 if START_REGEX.match(line): |
82 inside_enum = True | 86 inside_enum = True |
83 enum_value = 0 | 87 enum_value = 0 |
84 return result | 88 return result, None |
85 | 89 |
86 | 90 |
87 def CreateEnumItemNode(document, value, label): | 91 def CreateEnumItemNode(document, value, label): |
88 """Creates an int element to append to an enum.""" | 92 """Creates an int element to append to an enum.""" |
89 item_node = document.createElement('int') | 93 item_node = document.createElement('int') |
90 item_node.attributes['value'] = str(value) | 94 item_node.attributes['value'] = str(value) |
91 item_node.attributes['label'] = label | 95 item_node.attributes['label'] = label |
92 return item_node | 96 return item_node |
93 | 97 |
94 | 98 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 changed. | 177 changed. |
174 | 178 |
175 Args: | 179 Args: |
176 histogram_enum_name: The name of the XML <enum> attribute to update. | 180 histogram_enum_name: The name of the XML <enum> attribute to update. |
177 source_enum_path: A unix-style path, relative to src/, giving | 181 source_enum_path: A unix-style path, relative to src/, giving |
178 the C++ header file from which to read the enum. | 182 the C++ header file from which to read the enum. |
179 start_marker: A regular expression that matches the start of the C++ enum. | 183 start_marker: A regular expression that matches the start of the C++ enum. |
180 end_marker: A regular expression that matches the end of the C++ enum. | 184 end_marker: A regular expression that matches the end of the C++ enum. |
181 """ | 185 """ |
182 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) | 186 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) |
183 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, | 187 source_enum_values, duplicated_values = ReadHistogramValues( |
184 end_marker) | 188 source_enum_path, start_marker, end_marker) |
189 if duplicated_values is not None: | |
190 return False, duplicated_values | |
Dirk Pranke
2017/04/06 15:19:37
Nit:
if duplicated_values:
return False
| |
185 | 191 |
186 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, | 192 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, |
187 source_enum_path) | 193 source_enum_path) |
188 return xml != new_xml | 194 return xml != new_xml, None |
189 | 195 |
190 | 196 |
191 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | 197 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, |
192 source_enum_path): | 198 source_enum_path): |
193 """Updates |histogram_enum_name| enum in histograms.xml file with values | 199 """Updates |histogram_enum_name| enum in histograms.xml file with values |
194 from the {value: 'key'} dictionary |source_enum_values|. A comment is added | 200 from the {value: 'key'} dictionary |source_enum_values|. A comment is added |
195 to histograms.xml citing that the values in |histogram_enum_name| were | 201 to histograms.xml citing that the values in |histogram_enum_name| were |
196 sourced from |source_enum_path|. | 202 sourced from |source_enum_path|. |
197 """ | 203 """ |
198 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, | 204 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, |
(...skipping 20 matching lines...) Expand all Loading... | |
219 start_marker: A regular expression that matches the start of the C++ enum. | 225 start_marker: A regular expression that matches the start of the C++ enum. |
220 end_marker: A regular expression that matches the end of the C++ enum. | 226 end_marker: A regular expression that matches the end of the C++ enum. |
221 """ | 227 """ |
222 | 228 |
223 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) | 229 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) |
224 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, | 230 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, |
225 end_marker) | 231 end_marker) |
226 | 232 |
227 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | 233 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, |
228 source_enum_path) | 234 source_enum_path) |
OLD | NEW |