OLD | NEW |
---|---|
1 # Copyright 2013 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 ExtensionFunctions enum in histograms.xml file with values read from | |
6 extension_function_histogram_value.h. | |
7 | |
8 If the file was pretty-printed, the updated version is pretty-printed too. | |
9 """ | |
10 | |
11 import logging | 5 import logging |
12 import os | 6 import print_style |
13 import re | 7 import re |
14 import sys | 8 import sys |
15 | 9 |
16 from xml.dom import minidom | 10 from xml.dom import minidom |
17 import print_style | 11 from diffutil import PromptUserToAcceptDiff |
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 | 12 |
32 class UserError(Exception): | 13 class UserError(Exception): |
33 def __init__(self, message): | 14 def __init__(self, message): |
34 Exception.__init__(self, message) | 15 Exception.__init__(self, message) |
35 | 16 |
36 @property | 17 @property |
37 def message(self): | 18 def message(self): |
38 return self.args[0] | 19 return self.args[0] |
39 | 20 |
21 def Log(message): | |
22 logging.info(message) | |
23 | |
40 def ExtractRegexGroup(line, regex): | 24 def ExtractRegexGroup(line, regex): |
41 m = re.match(regex, line) | 25 m = re.match(regex, line) |
42 if m: | 26 if m: |
43 return m.group(1) | 27 return m.group(1) |
44 else: | 28 else: |
45 return None | 29 return None |
46 | 30 |
Ilya Sherman
2014/03/14 23:19:35
nit: Please leave two blank lines between top-leve
| |
47 | 31 def ReadHistogramValues(filename, start_marker, end_marker): |
48 def ReadHistogramValues(filename): | 32 '''Reads in values from |filename|, returning a list of (label, value) pairs |
49 """Returns a list of pairs (label, value) corresponding to HistogramValue. | 33 corresponding to the enum framed by |start_marker| and |end_marker|. |
50 | 34 ''' |
Ilya Sherman
2014/03/14 23:19:35
nit: Double quotes for this sort of comment string
| |
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 | 35 # Read the file as a list of lines |
56 with open(filename) as f: | 36 with open(filename) as f: |
57 content = f.readlines() | 37 content = f.readlines() |
58 | 38 |
59 # Locate the enum definition and collect all entries in it | 39 # Locate the enum definition and collect all entries in it |
60 inside_enum = False # We haven't found the enum definition yet | 40 inside_enum = False # We haven't found the enum definition yet |
61 result = [] | 41 result = [] |
62 for line in content: | 42 for line in content: |
63 line = line.strip() | 43 line = line.strip() |
64 if inside_enum: | 44 if inside_enum: |
65 # Exit condition: we reached last enum value | 45 # Exit condition: we reached last enum value |
66 if re.match(ENUM_END_MARKER, line): | 46 if re.match(end_marker, line): |
67 inside_enum = False | 47 inside_enum = False |
68 else: | 48 else: |
69 # Inside enum: generate new xml entry | 49 # Inside enum: generate new xml entry |
70 label = ExtractRegexGroup(line.strip(), "^([\w]+)") | 50 label = ExtractRegexGroup(line.strip(), "^([\w]+)") |
71 if label: | 51 if label: |
72 result.append((label, enum_value)) | 52 result.append((label, enum_value)) |
73 enum_value += 1 | 53 enum_value += 1 |
74 else: | 54 else: |
75 if re.match(ENUM_START_MARKER, line): | 55 if re.match(start_marker, line): |
76 inside_enum = True | 56 inside_enum = True |
77 enum_value = 0 # Start at 'UNKNOWN' | 57 enum_value = 0 # Start at 'UNKNOWN' |
78 return result | 58 return result |
79 | 59 |
60 def UpdateHistogramDefinitions(histogram_enum_name, source_enum_values, | |
61 source_enum_path, document, | |
62 labelTransformer): | |
63 '''Sets the children of <enum name=|histogram_enum_name| ...> node in | |
64 |document| to values generated from (label, value) pairs contained in | |
65 |source_enum_values|. | |
80 | 66 |
81 def UpdateHistogramDefinitions(histogram_values, document): | 67 |labelTransformer| is a function used to transform labels from the |
82 """Sets the children of <enum name="ExtensionFunctions" ...> node in | 68 (label, value) pairs into a different format or style. Since label is |
83 |document| to values generated from policy ids contained in | 69 a string, defaulting labelTransformer to str() effectively does nothing. |
84 |policy_templates|. | 70 ''' |
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. | 71 # Find ExtensionFunctions enum. |
94 for enum_node in document.getElementsByTagName('enum'): | 72 for enum_node in document.getElementsByTagName('enum'): |
95 if enum_node.attributes['name'].value == ENUM_NAME: | 73 if enum_node.attributes['name'].value == histogram_enum_name: |
96 extension_functions_enum_node = enum_node | 74 histogram_enum_node = enum_node |
97 break | 75 break |
98 else: | 76 else: |
99 raise UserError('No policy enum node found') | 77 raise UserError('No {0} enum node found'.format(histogram_enum_name)) |
100 | 78 |
101 # Remove existing values. | 79 # Remove existing values. |
102 while extension_functions_enum_node.hasChildNodes(): | 80 while histogram_enum_node.hasChildNodes(): |
103 extension_functions_enum_node.removeChild( | 81 histogram_enum_node.removeChild(histogram_enum_node.lastChild) |
104 extension_functions_enum_node.lastChild) | |
105 | 82 |
106 # Add a "Generated from (...)" comment | 83 # Add a "Generated from (...)" comment |
107 comment = ' Generated from {0} '.format( | 84 comment = ' Generated from {0} '.format(source_enum_path) |
108 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH) | 85 histogram_enum_node.appendChild(document.createComment(comment)) |
109 extension_functions_enum_node.appendChild(document.createComment(comment)) | |
110 | 86 |
111 # Add values generated from policy templates. | 87 # Add values generated from policy templates. |
112 for (label, value) in histogram_values: | 88 for (label, value) in source_enum_values: |
113 node = document.createElement('int') | 89 node = document.createElement('int') |
114 node.attributes['value'] = str(value) | 90 node.attributes['value'] = str(value) |
115 node.attributes['label'] = label | 91 node.attributes['label'] = labelTransformer(label) |
116 extension_functions_enum_node.appendChild(node) | 92 histogram_enum_node.appendChild(node) |
117 | 93 |
118 def Log(message): | 94 def UpdateHistogramEnum(histogram_enum_name, source_enum_path, |
119 logging.info(message) | 95 start_marker, end_marker, labelTransformer=str): |
96 '''Updates |histogram_enum_name| enum in histograms.xml file with values | |
97 read from |source_enum_path|, where |start_marker| and |end_marker| indicate | |
98 the beginning and end of the source enum definition, respectively. | |
120 | 99 |
121 def main(): | 100 |labelTransformer| is a function whose usage is described in |
101 UpdateHistogramDefinitions. | |
102 ''' | |
103 HISTOGRAMS_PATH = 'histograms.xml' | |
104 | |
122 if len(sys.argv) > 1: | 105 if len(sys.argv) > 1: |
123 print >>sys.stderr, 'No arguments expected!' | 106 print >>sys.stderr, 'No arguments expected!' |
124 sys.stderr.write(__doc__) | 107 sys.stderr.write(__doc__) |
125 sys.exit(1) | 108 sys.exit(1) |
126 | 109 |
127 Log('Reading histogram enum definition from "%s".' | 110 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) |
128 % (EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH)) | 111 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, |
129 histogram_values = ReadHistogramValues( | 112 end_marker) |
130 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH) | |
131 | 113 |
132 Log('Reading existing histograms from "%s".' % (HISTOGRAMS_PATH)) | 114 Log('Reading existing histograms from "{0}".'.format(HISTOGRAMS_PATH)) |
133 with open(HISTOGRAMS_PATH, 'rb') as f: | 115 with open(HISTOGRAMS_PATH, 'rb') as f: |
134 histograms_doc = minidom.parse(f) | 116 histograms_doc = minidom.parse(f) |
135 f.seek(0) | 117 f.seek(0) |
136 xml = f.read() | 118 xml = f.read() |
137 | 119 |
138 Log('Comparing histograms enum with new enum definition.') | 120 Log('Comparing histograms enum with new enum definition.') |
139 UpdateHistogramDefinitions(histogram_values, histograms_doc) | 121 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values, |
122 source_enum_path, histograms_doc, labelTransformer) | |
140 | 123 |
141 Log('Writing out new histograms file.') | 124 Log('Writing out new histograms file.') |
142 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) | 125 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) |
143 | |
144 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): | 126 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): |
145 with open(HISTOGRAMS_PATH, 'wb') as f: | 127 with open(HISTOGRAMS_PATH, 'wb') as f: |
146 f.write(new_xml) | 128 f.write(new_xml) |
147 | 129 |
148 Log('Done.') | 130 Log('Done.') |
149 | |
150 | |
151 if __name__ == '__main__': | |
152 main() | |
OLD | NEW |