| 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 |
| 11 import os | 11 import os |
| 12 import re | 12 import re |
| 13 import sys | 13 import sys |
| 14 | 14 |
| 15 from xml.dom import minidom | 15 from xml.dom import minidom |
| 16 | 16 |
| 17 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) | 17 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) |
| 18 import diff_util | 18 import diff_util |
| 19 import path_util | 19 import path_util |
| 20 | 20 |
| 21 import print_style | 21 import print_style |
| 22 | 22 |
| 23 |
| 24 HISTOGRAMS_PATH = path_util.GetHistogramsFile() |
| 25 |
| 26 |
| 23 class UserError(Exception): | 27 class UserError(Exception): |
| 24 def __init__(self, message): | 28 def __init__(self, message): |
| 25 Exception.__init__(self, message) | 29 Exception.__init__(self, message) |
| 26 | 30 |
| 27 @property | 31 @property |
| 28 def message(self): | 32 def message(self): |
| 29 return self.args[0] | 33 return self.args[0] |
| 30 | 34 |
| 31 | 35 |
| 32 def Log(message): | 36 def Log(message): |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 | 139 |
| 136 # Add comments at the top. | 140 # Add comments at the top. |
| 137 for comment in new_comments: | 141 for comment in new_comments: |
| 138 enum_node.appendChild(comment) | 142 enum_node.appendChild(comment) |
| 139 | 143 |
| 140 # Add in the new enums. | 144 # Add in the new enums. |
| 141 for value in sorted(new_item_nodes.iterkeys()): | 145 for value in sorted(new_item_nodes.iterkeys()): |
| 142 enum_node.appendChild(new_item_nodes[value]) | 146 enum_node.appendChild(new_item_nodes[value]) |
| 143 | 147 |
| 144 | 148 |
| 145 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | 149 def _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, |
| 146 source_enum_path): | 150 source_enum_path): |
| 147 """Updates |histogram_enum_name| enum in histograms.xml file with values | 151 """Reads old histogram from |histogram_enum_name| from |HISTOGRAMS_PATH|, and |
| 148 from the {value: 'key'} dictionary |source_enum_values|. A comment is added | 152 calculates new histogram from |source_enum_values| from |source_enum_path|, |
| 149 to histograms.xml citing that the values in |histogram_enum_name| were | 153 and returns both in XML format. |
| 150 sourced from |source_enum_path|. | |
| 151 """ | 154 """ |
| 152 HISTOGRAMS_PATH = path_util.GetHistogramsFile() | |
| 153 | |
| 154 Log('Reading existing histograms from "{0}".'.format(HISTOGRAMS_PATH)) | 155 Log('Reading existing histograms from "{0}".'.format(HISTOGRAMS_PATH)) |
| 155 with open(HISTOGRAMS_PATH, 'rb') as f: | 156 with open(HISTOGRAMS_PATH, 'rb') as f: |
| 156 histograms_doc = minidom.parse(f) | 157 histograms_doc = minidom.parse(f) |
| 157 f.seek(0) | 158 f.seek(0) |
| 158 xml = f.read() | 159 xml = f.read() |
| 159 | 160 |
| 160 Log('Comparing histograms enum with new enum definition.') | 161 Log('Comparing histograms enum with new enum definition.') |
| 161 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values, | 162 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values, |
| 162 source_enum_path, histograms_doc) | 163 source_enum_path, histograms_doc) |
| 163 | 164 |
| 164 Log('Writing out new histograms file.') | |
| 165 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) | 165 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) |
| 166 return (xml, new_xml) |
| 167 |
| 168 |
| 169 def HistogramNeedsUpdate(histogram_enum_name, source_enum_path, start_marker, |
| 170 end_marker): |
| 171 """Reads a C++ enum from a .h file and does a dry run of updating |
| 172 histograms.xml to match. Returns true if the histograms.xml file would be |
| 173 changed. |
| 174 |
| 175 Args: |
| 176 histogram_enum_name: The name of the XML <enum> attribute to update. |
| 177 source_enum_path: A unix-style path, relative to src/, giving |
| 178 the C++ header file from which to read the enum. |
| 179 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. |
| 181 """ |
| 182 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) |
| 183 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, |
| 184 end_marker) |
| 185 |
| 186 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, |
| 187 source_enum_path) |
| 188 return xml != new_xml |
| 189 |
| 190 |
| 191 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, |
| 192 source_enum_path): |
| 193 """Updates |histogram_enum_name| enum in histograms.xml file with values |
| 194 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 |
| 196 sourced from |source_enum_path|. |
| 197 """ |
| 198 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, |
| 199 source_enum_path) |
| 166 if not diff_util.PromptUserToAcceptDiff( | 200 if not diff_util.PromptUserToAcceptDiff( |
| 167 xml, new_xml, 'Is the updated version acceptable?'): | 201 xml, new_xml, 'Is the updated version acceptable?'): |
| 168 Log('Cancelled.') | 202 Log('Cancelled.') |
| 169 return | 203 return |
| 170 | 204 |
| 171 with open(HISTOGRAMS_PATH, 'wb') as f: | 205 with open(HISTOGRAMS_PATH, 'wb') as f: |
| 172 f.write(new_xml) | 206 f.write(new_xml) |
| 173 | 207 |
| 174 Log('Done.') | 208 Log('Done.') |
| 175 | 209 |
| 176 | 210 |
| 177 def UpdateHistogramEnum(histogram_enum_name, source_enum_path, | 211 def UpdateHistogramEnum(histogram_enum_name, source_enum_path, |
| 178 start_marker, end_marker): | 212 start_marker, end_marker): |
| 179 """Reads a C++ enum from a .h file and updates histograms.xml to match. | 213 """Reads a C++ enum from a .h file and updates histograms.xml to match. |
| 180 | 214 |
| 181 Args: | 215 Args: |
| 182 histogram_enum_name: The name of the XML <enum> attribute to update. | 216 histogram_enum_name: The name of the XML <enum> attribute to update. |
| 183 source_enum_path: A unix-style path, relative to src/, giving | 217 source_enum_path: A unix-style path, relative to src/, giving |
| 184 the C++ header file from which to read the enum. | 218 the C++ header file from which to read the enum. |
| 185 start_marker: A regular expression that matches the start of the C++ enum. | 219 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. | 220 end_marker: A regular expression that matches the end of the C++ enum. |
| 187 """ | 221 """ |
| 188 | 222 |
| 189 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) | 223 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) |
| 190 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, | 224 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, |
| 191 end_marker) | 225 end_marker) |
| 192 | 226 |
| 193 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | 227 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, |
| 194 source_enum_path) | 228 source_enum_path) |
| OLD | NEW |