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