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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 | 135 |
136 # Add comments at the top. | 136 # Add comments at the top. |
137 for comment in new_comments: | 137 for comment in new_comments: |
138 enum_node.appendChild(comment) | 138 enum_node.appendChild(comment) |
139 | 139 |
140 # Add in the new enums. | 140 # Add in the new enums. |
141 for value in sorted(new_item_nodes.iterkeys()): | 141 for value in sorted(new_item_nodes.iterkeys()): |
142 enum_node.appendChild(new_item_nodes[value]) | 142 enum_node.appendChild(new_item_nodes[value]) |
143 | 143 |
144 | 144 |
145 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | 145 def _GetOldAndUpdatedXml(histograms_path, histogram_enum_name, |
146 source_enum_path): | 146 source_enum_values, source_enum_path): |
Ilya Sherman
2016/03/02 23:18:46
Please add documentation for this function.
suzyh_UTC10 (ex-contributor)
2016/03/02 23:49:16
Done.
| |
147 """Updates |histogram_enum_name| enum in histograms.xml file with values | 147 Log('Reading existing histograms from "{0}".'.format(histograms_path)) |
148 from the {value: 'key'} dictionary |source_enum_values|. A comment is added | 148 with open(histograms_path, 'rb') as f: |
149 to histograms.xml citing that the values in |histogram_enum_name| were | |
150 sourced from |source_enum_path|. | |
151 """ | |
152 HISTOGRAMS_PATH = path_util.GetHistogramsFile() | |
153 | |
154 Log('Reading existing histograms from "{0}".'.format(HISTOGRAMS_PATH)) | |
155 with open(HISTOGRAMS_PATH, 'rb') as f: | |
156 histograms_doc = minidom.parse(f) | 149 histograms_doc = minidom.parse(f) |
157 f.seek(0) | 150 f.seek(0) |
158 xml = f.read() | 151 xml = f.read() |
159 | 152 |
160 Log('Comparing histograms enum with new enum definition.') | 153 Log('Comparing histograms enum with new enum definition.') |
161 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values, | 154 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values, |
162 source_enum_path, histograms_doc) | 155 source_enum_path, histograms_doc) |
163 | 156 |
164 Log('Writing out new histograms file.') | |
165 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) | 157 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) |
158 | |
Ilya Sherman
2016/03/02 23:18:46
Optional nit: I'd omit this newline.
suzyh_UTC10 (ex-contributor)
2016/03/02 23:49:17
Done.
| |
159 return (xml, new_xml) | |
160 | |
161 | |
162 def HistogramNeedsUpdate(histogram_enum_name, source_enum_path, start_marker, | |
163 end_marker): | |
164 """Reads a C++ enum from a .h file and does a dry run of updating | |
165 histograms.xml to match. Returns true if the histograms.xml file would be | |
166 changed. | |
167 | |
168 Args: | |
169 histogram_enum_name: The name of the XML <enum> attribute to update. | |
170 source_enum_path: A unix-style path, relative to src/, giving | |
171 the C++ header file from which to read the enum. | |
172 start_marker: A regular expression that matches the start of the C++ enum. | |
173 end_marker: A regular expression that matches the end of the C++ enum. | |
174 """ | |
175 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) | |
176 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, | |
177 end_marker) | |
178 | |
179 HISTOGRAMS_PATH = path_util.GetHistogramsFile() | |
180 (xml, new_xml) = _GetOldAndUpdatedXml(HISTOGRAMS_PATH, histogram_enum_name, | |
181 source_enum_values, source_enum_path) | |
182 return xml != new_xml | |
183 | |
184 | |
185 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | |
186 source_enum_path): | |
187 """Updates |histogram_enum_name| enum in histograms.xml file with values | |
188 from the {value: 'key'} dictionary |source_enum_values|. A comment is added | |
189 to histograms.xml citing that the values in |histogram_enum_name| were | |
190 sourced from |source_enum_path|. | |
191 """ | |
192 HISTOGRAMS_PATH = path_util.GetHistogramsFile() | |
Ilya Sherman
2016/03/02 23:18:46
nit: I think this can be moved into _GetOldAndUpda
suzyh_UTC10 (ex-contributor)
2016/03/02 23:27:10
I contemplated this, but then _GetOldAndUpdatedXml
Ilya Sherman
2016/03/02 23:35:31
Ah, I missed that. I think it would be fine to pr
suzyh_UTC10 (ex-contributor)
2016/03/02 23:49:17
Done.
| |
193 | |
194 (xml, new_xml) = _GetOldAndUpdatedXml(HISTOGRAMS_PATH, histogram_enum_name, | |
195 source_enum_values, source_enum_path) | |
166 if not diff_util.PromptUserToAcceptDiff( | 196 if not diff_util.PromptUserToAcceptDiff( |
167 xml, new_xml, 'Is the updated version acceptable?'): | 197 xml, new_xml, 'Is the updated version acceptable?'): |
168 Log('Cancelled.') | 198 Log('Cancelled.') |
169 return | 199 return |
170 | 200 |
171 with open(HISTOGRAMS_PATH, 'wb') as f: | 201 with open(HISTOGRAMS_PATH, 'wb') as f: |
172 f.write(new_xml) | 202 f.write(new_xml) |
173 | 203 |
174 Log('Done.') | 204 Log('Done.') |
175 | 205 |
176 | 206 |
177 def UpdateHistogramEnum(histogram_enum_name, source_enum_path, | 207 def UpdateHistogramEnum(histogram_enum_name, source_enum_path, |
178 start_marker, end_marker): | 208 start_marker, end_marker): |
179 """Reads a C++ enum from a .h file and updates histograms.xml to match. | 209 """Reads a C++ enum from a .h file and updates histograms.xml to match. |
180 | 210 |
181 Args: | 211 Args: |
182 histogram_enum_name: The name of the XML <enum> attribute to update. | 212 histogram_enum_name: The name of the XML <enum> attribute to update. |
183 source_enum_path: A unix-style path, relative to src/, giving | 213 source_enum_path: A unix-style path, relative to src/, giving |
184 the C++ header file from which to read the enum. | 214 the C++ header file from which to read the enum. |
185 start_marker: A regular expression that matches the start of the C++ enum. | 215 start_marker: A regular expression that matches the start of the C++ enum. |
186 end_marker: A regular expression that matches the end of the C++ enum. | 216 end_marker: A regular expression that matches the end of the C++ enum. |
187 """ | 217 """ |
188 | 218 |
189 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) | 219 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) |
190 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, | 220 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, |
191 end_marker) | 221 end_marker) |
192 | 222 |
193 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | 223 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, |
194 source_enum_path) | 224 source_enum_path) |
OLD | NEW |