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