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 ExtensionPermission enum in histograms.xml file with values read from |
Jeffrey Yasskin
2014/02/20 19:07:36
Try to share the code between this file and the on
| |
6 extension_function_histogram_value.h. | 6 permission_message.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 | 11 import logging |
12 import re | 12 import re |
13 import sys | 13 import sys |
14 | 14 |
15 from xml.dom import minidom | 15 from xml.dom import minidom |
16 | 16 |
17 from diffutil import PromptUserToAcceptDiff | 17 from diffutil import PromptUserToAcceptDiff |
18 from pretty_print import PrettyPrintNode | 18 from pretty_print import PrettyPrintNode |
19 | 19 |
20 HISTOGRAMS_PATH = 'histograms.xml' | 20 HISTOGRAMS_PATH = 'histograms.xml' |
21 ENUM_NAME = 'ExtensionFunctions' | 21 PERMISSION_MESSAGE_PATH = \ |
22 '../../../extensions/common/permissions/permission_message.h' | |
22 | 23 |
23 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH = \ | 24 ENUM_NAME = 'ExtensionPermission' |
24 '../../../chrome/browser/extensions/extension_function_histogram_value.h' | 25 ENUM_START_MARKER = "^enum ID {" |
25 ENUM_START_MARKER = "^enum HistogramValue {" | 26 ENUM_END_MARKER = "^kEnumBoundary" |
26 ENUM_END_MARKER = "^ENUM_BOUNDARY" | |
27 | 27 |
28 | 28 |
29 class UserError(Exception): | 29 class UserError(Exception): |
30 def __init__(self, message): | 30 def __init__(self, message): |
31 Exception.__init__(self, message) | 31 Exception.__init__(self, message) |
32 | 32 |
33 @property | 33 @property |
34 def message(self): | 34 def message(self): |
35 return self.args[0] | 35 return self.args[0] |
36 | 36 |
37 def StyleLabel(label): | |
38 '''Transforms labels of form "kLabelName" to "LABEL_NAME" | |
39 ''' | |
40 return label[1] + re.sub('([A-Z])', r'_\1', label[2:]).upper() | |
Jeffrey Yasskin
2014/02/20 19:07:36
Probably assert that label.startswith('k') and lab
| |
41 | |
37 def ExtractRegexGroup(line, regex): | 42 def ExtractRegexGroup(line, regex): |
38 m = re.match(regex, line) | 43 m = re.match(regex, line) |
39 if m: | 44 if m: |
40 return m.group(1) | 45 return m.group(1) |
41 else: | 46 else: |
42 return None | 47 return None |
43 | 48 |
44 | 49 |
45 def ReadHistogramValues(filename): | 50 def ReadHistogramValues(filename): |
46 """Returns a list of pairs (label, value) corresponding to HistogramValue. | 51 """Returns a list of pairs (label, value) corresponding to HistogramValue. |
47 | 52 |
48 Reads the extension_functions_histogram_value.h file, locates the | 53 Reads the permission_message.h file, locates the |
49 HistogramValue enum definition and returns a pair for each entry. | 54 ID enum definition and returns a pair for each entry. |
50 """ | 55 """ |
51 | 56 |
52 # Read the file as a list of lines | 57 # Read the file as a list of lines |
53 with open(filename) as f: | 58 with open(filename) as f: |
54 content = f.readlines() | 59 content = f.readlines() |
55 | 60 |
56 # Locate the enum definition and collect all entries in it | 61 # Locate the enum definition and collect all entries in it |
57 inside_enum = False # We haven't found the enum definition yet | 62 inside_enum = False # We haven't found the enum definition yet |
58 result = [] | 63 result = [] |
59 for line in content: | 64 for line in content: |
60 line = line.strip() | 65 line = line.strip() |
61 if inside_enum: | 66 if inside_enum: |
62 # Exit condition: we reached last enum value | 67 # Exit condition: we reached last enum value |
63 if re.match(ENUM_END_MARKER, line): | 68 if re.match(ENUM_END_MARKER, line): |
64 inside_enum = False | 69 inside_enum = False |
65 else: | 70 else: |
66 # Inside enum: generate new xml entry | 71 # Inside enum: generate new xml entry |
67 label = ExtractRegexGroup(line.strip(), "^([\w]+)") | 72 label = ExtractRegexGroup(line.strip(), "^([\w]+)") |
68 if label: | 73 if label: |
69 result.append((label, enum_value)) | 74 result.append((StyleLabel(label), enum_value)) |
70 enum_value += 1 | 75 enum_value += 1 |
71 else: | 76 else: |
72 if re.match(ENUM_START_MARKER, line): | 77 if re.match(ENUM_START_MARKER, line): |
73 inside_enum = True | 78 inside_enum = True |
74 enum_value = 0 # Start at 'UNKNOWN' | 79 enum_value = 0 # Start at 'UNKNOWN' |
75 return result | 80 return result |
76 | 81 |
77 | 82 |
78 def UpdateHistogramDefinitions(histogram_values, document): | 83 def UpdateHistogramDefinitions(histogram_values, document): |
79 """Sets the children of <enum name="ExtensionFunctions" ...> node in | 84 """Sets the children of <enum name="ExtensionPermission" ...> node in |
80 |document| to values generated from policy ids contained in | 85 |document| to values generated from (label, value) pairs contained in |
81 |policy_templates|. | 86 |histogram_values|. |
82 | 87 |
83 Args: | 88 Args: |
84 histogram_values: A list of pairs (label, value) defining each extension | 89 histogram_values: A list of pairs (label, value) defining each extension |
85 function | 90 function |
86 document: A minidom.Document object representing parsed histogram | 91 document: A minidom.Document object representing parsed histogram |
87 definitions XML file. | 92 definitions XML file. |
88 | 93 |
89 """ | 94 """ |
90 # Find ExtensionFunctions enum. | 95 # Find ExtensionFunctions enum. |
91 for enum_node in document.getElementsByTagName('enum'): | 96 for enum_node in document.getElementsByTagName('enum'): |
92 if enum_node.attributes['name'].value == ENUM_NAME: | 97 if enum_node.attributes['name'].value == ENUM_NAME: |
93 extension_functions_enum_node = enum_node | 98 ID_enum_node = enum_node |
94 break | 99 break |
95 else: | 100 else: |
96 raise UserError('No policy enum node found') | 101 raise UserError('No policy enum node found') |
97 | 102 |
98 # Remove existing values. | 103 # Remove existing values. |
99 while extension_functions_enum_node.hasChildNodes(): | 104 while ID_enum_node.hasChildNodes(): |
100 extension_functions_enum_node.removeChild( | 105 ID_enum_node.removeChild( |
101 extension_functions_enum_node.lastChild) | 106 ID_enum_node.lastChild) |
102 | 107 |
103 # Add a "Generated from (...)" comment | 108 # Add a "Generated from (...)" comment |
104 comment = ' Generated from {0} '.format( | 109 comment = ' Generated from {0} '.format( |
105 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH) | 110 PERMISSION_MESSAGE_PATH) |
106 extension_functions_enum_node.appendChild(document.createComment(comment)) | 111 ID_enum_node.appendChild(document.createComment(comment)) |
107 | 112 |
108 # Add values generated from policy templates. | 113 # Add values generated from policy templates. |
109 for (label, value) in histogram_values: | 114 for (label, value) in histogram_values: |
110 node = document.createElement('int') | 115 node = document.createElement('int') |
111 node.attributes['value'] = str(value) | 116 node.attributes['value'] = str(value) |
112 node.attributes['label'] = label | 117 node.attributes['label'] = label |
113 extension_functions_enum_node.appendChild(node) | 118 ID_enum_node.appendChild(node) |
114 | 119 |
115 def Log(message): | 120 def Log(message): |
116 logging.info(message) | 121 logging.info(message) |
117 | 122 |
118 def main(): | 123 def main(): |
119 if len(sys.argv) > 1: | 124 if len(sys.argv) > 1: |
120 print >>sys.stderr, 'No arguments expected!' | 125 print >>sys.stderr, 'No arguments expected!' |
121 sys.stderr.write(__doc__) | 126 sys.stderr.write(__doc__) |
122 sys.exit(1) | 127 sys.exit(1) |
123 | 128 |
124 Log('Reading histogram enum definition from "%s".' | 129 Log('Reading histogram enum definition from "%s".' |
125 % (EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH)) | 130 % (PERMISSION_MESSAGE_PATH)) |
126 histogram_values = ReadHistogramValues( | 131 histogram_values = ReadHistogramValues( |
127 EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH) | 132 PERMISSION_MESSAGE_PATH) |
128 | 133 |
129 Log('Reading existing histograms from "%s".' % (HISTOGRAMS_PATH)) | 134 Log('Reading existing histograms from "%s".' % (HISTOGRAMS_PATH)) |
130 with open(HISTOGRAMS_PATH, 'rb') as f: | 135 with open(HISTOGRAMS_PATH, 'rb') as f: |
131 histograms_doc = minidom.parse(f) | 136 histograms_doc = minidom.parse(f) |
132 f.seek(0) | 137 f.seek(0) |
133 xml = f.read() | 138 xml = f.read() |
134 | 139 |
135 Log('Comparing histograms enum with new enum definition.') | 140 Log('Comparing histograms enum with new enum definition.') |
136 UpdateHistogramDefinitions(histogram_values, histograms_doc) | 141 UpdateHistogramDefinitions(histogram_values, histograms_doc) |
137 | 142 |
138 Log('Writing out new histograms file.') | 143 Log('Writing out new histograms file.') |
139 new_xml = PrettyPrintNode(histograms_doc) | 144 new_xml = PrettyPrintNode(histograms_doc) |
140 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): | 145 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): |
141 with open(HISTOGRAMS_PATH, 'wb') as f: | 146 with open(HISTOGRAMS_PATH, 'wb') as f: |
142 f.write(new_xml) | 147 f.write(new_xml) |
143 | 148 |
144 Log('Done.') | 149 Log('Done.') |
145 | 150 |
146 | 151 |
147 if __name__ == '__main__': | 152 if __name__ == '__main__': |
148 main() | 153 main() |
OLD | NEW |