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

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

Issue 170233008: Add presubmit check and automatic update script for ExtensionPermission enum. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 8 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 2013 The Chromium Authors. All rights reserved. 1 # Copyright 2013 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 ExtensionFunctions enum in histograms.xml file with values read from 5 """Updates ExtensionFunctions enum in histograms.xml file with values read from
6 extension_function_histogram_value.h. 6 extension_function_histogram_value.h.
7 7
8 If the file was pretty-printed, the updated version is pretty-printed too. 8 If the file was pretty-printed, the updated version is pretty-printed too.
9 """ 9 """
10 10
11 import logging
12 import os 11 import os
13 import re
14 import sys
15 12
16 from xml.dom import minidom 13 from update_histogram_enum import UpdateHistogramEnum
17 import print_style
18
19 # Import the metrics/common module.
20 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
21 from diff_util import PromptUserToAcceptDiff
22
23 HISTOGRAMS_PATH = 'histograms.xml'
24 ENUM_NAME = 'ExtensionFunctions'
25
26 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH = \
27 '../../../extensions/browser/extension_function_histogram_value.h'
28 ENUM_START_MARKER = "^enum HistogramValue {"
29 ENUM_END_MARKER = "^ENUM_BOUNDARY"
30
31
32 class UserError(Exception):
33 def __init__(self, message):
34 Exception.__init__(self, message)
35
36 @property
37 def message(self):
38 return self.args[0]
39
40 def ExtractRegexGroup(line, regex):
41 m = re.match(regex, line)
42 if m:
43 return m.group(1)
44 else:
45 return None
46
47
48 def ReadHistogramValues(filename):
49 """Returns a list of pairs (label, value) corresponding to HistogramValue.
50
51 Reads the extension_function_histogram_value.h file, locates the
52 HistogramValue enum definition and returns a pair for each entry.
53 """
54
55 # Read the file as a list of lines
56 with open(filename) as f:
57 content = f.readlines()
58
59 # Locate the enum definition and collect all entries in it
60 inside_enum = False # We haven't found the enum definition yet
61 result = []
62 for line in content:
63 line = line.strip()
64 if inside_enum:
65 # Exit condition: we reached last enum value
66 if re.match(ENUM_END_MARKER, line):
67 inside_enum = False
68 else:
69 # Inside enum: generate new xml entry
70 label = ExtractRegexGroup(line.strip(), "^([\w]+)")
71 if label:
72 result.append((label, enum_value))
73 enum_value += 1
74 else:
75 if re.match(ENUM_START_MARKER, line):
76 inside_enum = True
77 enum_value = 0 # Start at 'UNKNOWN'
78 return result
79
80
81 def UpdateHistogramDefinitions(histogram_values, document):
82 """Sets the children of <enum name="ExtensionFunctions" ...> node in
83 |document| to values generated from policy ids contained in
84 |policy_templates|.
85
86 Args:
87 histogram_values: A list of pairs (label, value) defining each extension
88 function
89 document: A minidom.Document object representing parsed histogram
90 definitions XML file.
91
92 """
93 # Find ExtensionFunctions enum.
94 for enum_node in document.getElementsByTagName('enum'):
95 if enum_node.attributes['name'].value == ENUM_NAME:
96 extension_functions_enum_node = enum_node
97 break
98 else:
99 raise UserError('No policy enum node found')
100
101 # Remove existing values.
102 while extension_functions_enum_node.hasChildNodes():
103 extension_functions_enum_node.removeChild(
104 extension_functions_enum_node.lastChild)
105
106 # Add a "Generated from (...)" comment
107 comment = ' Generated from {0} '.format(
108 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH)
109 extension_functions_enum_node.appendChild(document.createComment(comment))
110
111 # Add values generated from policy templates.
112 for (label, value) in histogram_values:
113 node = document.createElement('int')
114 node.attributes['value'] = str(value)
115 node.attributes['label'] = label
116 extension_functions_enum_node.appendChild(node)
117
118 def Log(message):
119 logging.info(message)
120
121 def main():
122 if len(sys.argv) > 1:
123 print >>sys.stderr, 'No arguments expected!'
124 sys.stderr.write(__doc__)
125 sys.exit(1)
126
127 Log('Reading histogram enum definition from "%s".'
128 % (EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH))
129 histogram_values = ReadHistogramValues(
130 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH)
131
132 Log('Reading existing histograms from "%s".' % (HISTOGRAMS_PATH))
133 with open(HISTOGRAMS_PATH, 'rb') as f:
134 histograms_doc = minidom.parse(f)
135 f.seek(0)
136 xml = f.read()
137
138 Log('Comparing histograms enum with new enum definition.')
139 UpdateHistogramDefinitions(histogram_values, histograms_doc)
140
141 Log('Writing out new histograms file.')
142 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc)
143
144 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'):
145 with open(HISTOGRAMS_PATH, 'wb') as f:
146 f.write(new_xml)
147
148 Log('Done.')
149
150 14
151 if __name__ == '__main__': 15 if __name__ == '__main__':
152 main() 16 UpdateHistogramEnum(histogram_enum_name='ExtensionFunctions',
17 source_enum_path=
18 os.path.join('..', '..', '..', 'extensions',
19 'browser',
20 'extension_function_histogram_value.h'),
21 start_marker='^enum HistogramValue {',
22 end_marker='^ENUM_BOUNDARY')
OLDNEW
« no previous file with comments | « tools/metrics/histograms/histograms.xml ('k') | tools/metrics/histograms/update_extension_permission.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698