Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: tools/metrics/histograms/update_histogram_enum.py

Issue 1143323006: Histograms.xml python script housekeeping (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bad_message
Patch Set: Split off the extensions permission weirdness. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 print_style 12 import print_style
13 import re 13 import re
14 import sys 14 import sys
15 15
16 from xml.dom import minidom 16 from xml.dom import minidom
17 17
18 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) 18 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
19 from diff_util import PromptUserToAcceptDiff 19 from diff_util import PromptUserToAcceptDiff
20 20
21 import histograms_path
22
21 class UserError(Exception): 23 class UserError(Exception):
22 def __init__(self, message): 24 def __init__(self, message):
23 Exception.__init__(self, message) 25 Exception.__init__(self, message)
24 26
25 @property 27 @property
26 def message(self): 28 def message(self):
27 return self.args[0] 29 return self.args[0]
28 30
29 31
30 def Log(message): 32 def Log(message):
31 logging.info(message) 33 logging.info(message)
32 34
33 35
34 def ReadHistogramValues(filename, start_marker, end_marker): 36 def ReadHistogramValues(filename, start_marker, end_marker):
35 """Reads in values from |filename|, returning a dictionary mapping value to 37 """Returns a dictionary of enum values, read from a C++ file.
36 label corresponding to the enum framed by |start_marker| and |end_marker|. 38
39 Args:
40 filename: The unix-style path (relative to src/) of the file to open.
41 start_marker: A regex that signifies the start of the enum values.
42 end_marker: A regex that signifies the end of the enum values.
37 """ 43 """
38 # Read the file as a list of lines 44 # Read the file as a list of lines
39 with open(filename) as f: 45 with open(histograms_path.GetInputFile(filename)) as f:
40 content = f.readlines() 46 content = f.readlines()
41 47
42 START_REGEX = re.compile(start_marker) 48 START_REGEX = re.compile(start_marker)
43 ITEM_REGEX = re.compile(r'^(\w+)') 49 ITEM_REGEX = re.compile(r'^(\w+)')
44 ITEM_REGEX_WITH_INIT = re.compile(r'(\w+)\s*=\s*(\d+)') 50 ITEM_REGEX_WITH_INIT = re.compile(r'(\w+)\s*=\s*(\d+)')
45 END_REGEX = re.compile(end_marker) 51 END_REGEX = re.compile(end_marker)
46 52
47 # Locate the enum definition and collect all entries in it 53 # Locate the enum definition and collect all entries in it
48 inside_enum = False # We haven't found the enum definition yet 54 inside_enum = False # We haven't found the enum definition yet
49 result = {} 55 result = {}
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 enum_node.appendChild(new_item_nodes[value]) 142 enum_node.appendChild(new_item_nodes[value])
137 143
138 144
139 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, 145 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values,
140 source_enum_path): 146 source_enum_path):
141 """Updates |histogram_enum_name| enum in histograms.xml file with values 147 """Updates |histogram_enum_name| enum in histograms.xml file with values
142 from the {value: 'key'} dictionary |source_enum_values|. A comment is added 148 from the {value: 'key'} dictionary |source_enum_values|. A comment is added
143 to histograms.xml citing that the values in |histogram_enum_name| were 149 to histograms.xml citing that the values in |histogram_enum_name| were
144 sourced from |source_enum_path|. 150 sourced from |source_enum_path|.
145 """ 151 """
146 # TODO(ahernandez.miralles): The line below is present in nearly every 152
147 # file in this directory; factor out into a central location 153 HISTOGRAMS_PATH = histograms_path.GetHistogramsFile()
148 HISTOGRAMS_PATH = 'histograms.xml'
149 154
150 Log('Reading existing histograms from "{0}".'.format(HISTOGRAMS_PATH)) 155 Log('Reading existing histograms from "{0}".'.format(HISTOGRAMS_PATH))
151 with open(HISTOGRAMS_PATH, 'rb') as f: 156 with open(HISTOGRAMS_PATH, 'rb') as f:
152 histograms_doc = minidom.parse(f) 157 histograms_doc = minidom.parse(f)
153 f.seek(0) 158 f.seek(0)
154 xml = f.read() 159 xml = f.read()
155 160
156 Log('Comparing histograms enum with new enum definition.') 161 Log('Comparing histograms enum with new enum definition.')
157 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values, 162 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values,
158 source_enum_path, histograms_doc) 163 source_enum_path, histograms_doc)
159 164
160 Log('Writing out new histograms file.') 165 Log('Writing out new histograms file.')
161 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) 166 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc)
162 if not PromptUserToAcceptDiff( 167 if not PromptUserToAcceptDiff(
163 xml, new_xml, 'Is the updated version acceptable?'): 168 xml, new_xml, 'Is the updated version acceptable?'):
164 Log('Cancelled.') 169 Log('Cancelled.')
165 return 170 return
166 171
167 with open(HISTOGRAMS_PATH, 'wb') as f: 172 with open(HISTOGRAMS_PATH, 'wb') as f:
168 f.write(new_xml) 173 f.write(new_xml)
169 174
170 Log('Done.') 175 Log('Done.')
171 176
172 177
173 def UpdateHistogramEnum(histogram_enum_name, source_enum_path, 178 def UpdateHistogramEnum(histogram_enum_name, source_enum_path,
174 start_marker, end_marker): 179 start_marker, end_marker):
175 """Updates |histogram_enum_name| enum in histograms.xml file with values 180 """Reads a C++ enum from a .h file and updates histograms.xml to match.
176 read from |source_enum_path|, where |start_marker| and |end_marker| indicate 181
177 the beginning and end of the source enum definition, respectively. 182 Args:
183 histogram_enum_name: The name of the XML <enum> attribute to update.
184 source_enum_path: A unix-style path, relative to src/, giving
185 the C++ header file from which to read the enum.
186 start_marker: A regular expression that matches the start of the C++ enum.
187 end_marker: A regular expression that matches the end of the C++ enum.
178 """ 188 """
179 189
180 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) 190 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path))
181 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, 191 source_enum_values = ReadHistogramValues(source_enum_path, start_marker,
182 end_marker) 192 end_marker)
183 193
184 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, 194 UpdateHistogramFromDict(histogram_enum_name, source_enum_values,
185 source_enum_path) 195 source_enum_path)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698