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