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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 else: | 80 else: |
81 # Enum name is so long that the value wrapped to the next line | 81 # Enum name is so long that the value wrapped to the next line |
82 next_line = next(iterator).strip() | 82 next_line = next(iterator).strip() |
83 enum_value = int(WRAPPED_INIT.match(next_line).group(1)) | 83 enum_value = int(WRAPPED_INIT.match(next_line).group(1)) |
84 else: | 84 else: |
85 m = ITEM_REGEX.match(line) | 85 m = ITEM_REGEX.match(line) |
86 if m: | 86 if m: |
87 label = m.group(1) | 87 label = m.group(1) |
88 else: | 88 else: |
89 continue | 89 continue |
90 if strip_k_prefix: | |
91 assert label.startswith('k'), "Enum " + label + " should start with 'k'." | |
92 label = label[1:] | |
90 # If two enum labels have the same value | 93 # If two enum labels have the same value |
91 if enum_value in result: | 94 if enum_value in result: |
92 return result, (result[enum_value], label) | 95 return result, (result[enum_value], label) |
93 if strip_k_prefix: | |
94 assert label.startswith('k'), "Enum " + label + " should start with 'k'." | |
95 label = label[1:] | |
Łukasz Anforowicz
2017/04/26 22:20:54
Stripping of 'k' prefix needs to happen *before* r
| |
96 result[enum_value] = label | 96 result[enum_value] = label |
97 enum_value += 1 | 97 enum_value += 1 |
98 return result, None | 98 return result, None |
99 | 99 |
100 | 100 |
101 def CreateEnumItemNode(document, value, label): | 101 def CreateEnumItemNode(document, value, label): |
102 """Creates an int element to append to an enum.""" | 102 """Creates an int element to append to an enum.""" |
103 item_node = document.createElement('int') | 103 item_node = document.createElement('int') |
104 item_node.attributes['value'] = str(value) | 104 item_node.attributes['value'] = str(value) |
105 item_node.attributes['label'] = label | 105 item_node.attributes['label'] = label |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 xml = f.read() | 173 xml = f.read() |
174 | 174 |
175 Log('Comparing histograms enum with new enum definition.') | 175 Log('Comparing histograms enum with new enum definition.') |
176 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values, | 176 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values, |
177 source_enum_path, histograms_doc) | 177 source_enum_path, histograms_doc) |
178 | 178 |
179 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) | 179 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) |
180 return (xml, new_xml) | 180 return (xml, new_xml) |
181 | 181 |
182 | 182 |
183 def HistogramNeedsUpdate(histogram_enum_name, source_enum_path, start_marker, | 183 def CheckPresubmitErrors(histogram_enum_name, update_script_name, |
184 source_enum_path, start_marker, | |
184 end_marker, strip_k_prefix = False): | 185 end_marker, strip_k_prefix = False): |
185 """Reads a C++ enum from a .h file and does a dry run of updating | 186 """Reads a C++ enum from a .h file and checks for presubmit violations: |
186 histograms.xml to match. Returns true if the histograms.xml file would be | 187 1. Failure to update histograms.xml to match |
187 changed. | 188 2. Introduction of duplicate values. |
188 | 189 |
189 Args: | 190 Args: |
190 histogram_enum_name: The name of the XML <enum> attribute to update. | 191 histogram_enum_name: The name of the XML <enum> attribute to update. |
192 update_script_name: The name of an update script to run to update the UMA | |
193 mappings for the enum. | |
191 source_enum_path: A unix-style path, relative to src/, giving | 194 source_enum_path: A unix-style path, relative to src/, giving |
192 the C++ header file from which to read the enum. | 195 the C++ header file from which to read the enum. |
193 start_marker: A regular expression that matches the start of the C++ enum. | 196 start_marker: A regular expression that matches the start of the C++ enum. |
194 end_marker: A regular expression that matches the end of the C++ enum. | 197 end_marker: A regular expression that matches the end of the C++ enum. |
195 strip_k_prefix: Set to True if enum values are declared as kFoo and the | 198 strip_k_prefix: Set to True if enum values are declared as kFoo and the |
196 'k' should be stripped. | 199 'k' should be stripped. |
200 | |
201 Returns: | |
202 A string with presubmit failure description, or None (of no failures). | |
Ilya Sherman
2017/04/27 22:28:09
nit: s/of/if
Łukasz Anforowicz
2017/04/27 22:35:41
Done.
| |
197 """ | 203 """ |
198 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) | 204 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) |
199 source_enum_values, duplicated_values = ReadHistogramValues( | 205 source_enum_values, duplicated_values = ReadHistogramValues( |
200 source_enum_path, start_marker, end_marker, strip_k_prefix) | 206 source_enum_path, start_marker, end_marker, strip_k_prefix) |
207 | |
201 if duplicated_values: | 208 if duplicated_values: |
202 return False, duplicated_values | 209 return ('%s enum has been updated and there exists ' |
Ilya Sherman
2017/04/27 22:28:09
nit: s/exists/exist
Łukasz Anforowicz
2017/04/27 22:35:41
Done.
| |
210 'duplicated values between (%s) and (%s)' % (histogram_enum_name, | |
211 duplicated_values[0], | |
212 duplicated_values[1])) | |
203 | 213 |
204 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, | 214 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, |
205 source_enum_path) | 215 source_enum_path) |
206 return xml != new_xml, None | 216 if xml != new_xml: |
217 return ('%s enum has been updated and the UMA mapping needs to be ' | |
218 'regenerated. Please run %s in src/tools/metrics/histograms/ to ' | |
219 'update the mapping.' % (histogram_enum_name, update_script_name)) | |
220 | |
221 return None | |
207 | 222 |
208 | 223 |
209 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | 224 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, |
210 source_enum_path): | 225 source_enum_path): |
211 """Updates |histogram_enum_name| enum in histograms.xml file with values | 226 """Updates |histogram_enum_name| enum in histograms.xml file with values |
212 from the {value: 'key'} dictionary |source_enum_values|. A comment is added | 227 from the {value: 'key'} dictionary |source_enum_values|. A comment is added |
213 to histograms.xml citing that the values in |histogram_enum_name| were | 228 to histograms.xml citing that the values in |histogram_enum_name| were |
214 sourced from |source_enum_path|. | 229 sourced from |source_enum_path|. |
215 """ | 230 """ |
216 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, | 231 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, |
(...skipping 22 matching lines...) Expand all Loading... | |
239 strip_k_prefix: Set to True if enum values are declared as kFoo and the | 254 strip_k_prefix: Set to True if enum values are declared as kFoo and the |
240 'k' should be stripped. | 255 'k' should be stripped. |
241 """ | 256 """ |
242 | 257 |
243 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) | 258 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) |
244 source_enum_values, ignored = ReadHistogramValues(source_enum_path, | 259 source_enum_values, ignored = ReadHistogramValues(source_enum_path, |
245 start_marker, end_marker, strip_k_prefix) | 260 start_marker, end_marker, strip_k_prefix) |
246 | 261 |
247 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | 262 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, |
248 source_enum_path) | 263 source_enum_path) |
OLD | NEW |