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

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: Fix imports (sadface) 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
13 import re 12 import re
14 import sys 13 import sys
15 14
16 from xml.dom import minidom 15 from xml.dom import minidom
17 16
18 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) 17 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
19 from diff_util import PromptUserToAcceptDiff 18 import diff_util
19 import path_util
20
21 import print_style
20 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(path_util.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 HISTOGRAMS_PATH = path_util.GetHistogramsFile()
147 # file in this directory; factor out into a central location
148 HISTOGRAMS_PATH = 'histograms.xml'
149 153
150 Log('Reading existing histograms from "{0}".'.format(HISTOGRAMS_PATH)) 154 Log('Reading existing histograms from "{0}".'.format(HISTOGRAMS_PATH))
151 with open(HISTOGRAMS_PATH, 'rb') as f: 155 with open(HISTOGRAMS_PATH, 'rb') as f:
152 histograms_doc = minidom.parse(f) 156 histograms_doc = minidom.parse(f)
153 f.seek(0) 157 f.seek(0)
154 xml = f.read() 158 xml = f.read()
155 159
156 Log('Comparing histograms enum with new enum definition.') 160 Log('Comparing histograms enum with new enum definition.')
157 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values, 161 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values,
158 source_enum_path, histograms_doc) 162 source_enum_path, histograms_doc)
159 163
160 Log('Writing out new histograms file.') 164 Log('Writing out new histograms file.')
161 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) 165 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc)
162 if not PromptUserToAcceptDiff( 166 if not diff_util.PromptUserToAcceptDiff(
163 xml, new_xml, 'Is the updated version acceptable?'): 167 xml, new_xml, 'Is the updated version acceptable?'):
164 Log('Cancelled.') 168 Log('Cancelled.')
165 return 169 return
166 170
167 with open(HISTOGRAMS_PATH, 'wb') as f: 171 with open(HISTOGRAMS_PATH, 'wb') as f:
168 f.write(new_xml) 172 f.write(new_xml)
169 173
170 Log('Done.') 174 Log('Done.')
171 175
172 176
173 def UpdateHistogramEnum(histogram_enum_name, source_enum_path, 177 def UpdateHistogramEnum(histogram_enum_name, source_enum_path,
174 start_marker, end_marker): 178 start_marker, end_marker):
175 """Updates |histogram_enum_name| enum in histograms.xml file with values 179 """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 180
177 the beginning and end of the source enum definition, respectively. 181 Args:
182 histogram_enum_name: The name of the XML <enum> attribute to update.
183 source_enum_path: A unix-style path, relative to src/, giving
184 the C++ header file from which to read the enum.
185 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.
178 """ 187 """
179 188
180 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) 189 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path))
181 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, 190 source_enum_values = ReadHistogramValues(source_enum_path, start_marker,
182 end_marker) 191 end_marker)
183 192
184 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, 193 UpdateHistogramFromDict(histogram_enum_name, source_enum_values,
185 source_enum_path) 194 source_enum_path)
OLDNEW
« no previous file with comments | « tools/metrics/histograms/update_extension_permission.py ('k') | tools/metrics/histograms/update_net_error_codes.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698