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

Side by Side Diff: tools/metrics/histograms/update_histogram_enum.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: Update directory name Created 6 years, 9 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
(Empty)
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
3 # found in the LICENSE file.
4
5 import logging
6 import re
7 import sys
8
9 from xml.dom import minidom
10 from diffutil import PromptUserToAcceptDiff
11 from pretty_print import PrettyPrintNode
12
13 class UserError(Exception):
14 def __init__(self, message):
15 Exception.__init__(self, message)
16
17 @property
18 def message(self):
19 return self.args[0]
20
21 def Log(message):
22 logging.info(message)
23
24 def ExtractRegexGroup(line, regex):
25 m = re.match(regex, line)
26 if m:
27 return m.group(1)
28 else:
29 return None
30
31 def ReadHistogramValues(filename, start_marker, end_marker):
Jeffrey Yasskin 2014/03/10 20:53:23 Set your --similarity low enough that `git cl uplo
32 '''Reads in values from |filename|, returning a list of (label, value) pairs
33 corresponding to the enum framed by |start_marker| and |end_marker|.
34 '''
35 # Read the file as a list of lines
36 with open(filename) as f:
37 content = f.readlines()
38
39 # Locate the enum definition and collect all entries in it
40 inside_enum = False # We haven't found the enum definition yet
41 result = []
42 for line in content:
43 line = line.strip()
44 if inside_enum:
45 # Exit condition: we reached last enum value
46 if re.match(end_marker, line):
47 inside_enum = False
48 else:
49 # Inside enum: generate new xml entry
50 label = ExtractRegexGroup(line.strip(), "^([\w]+)")
51 if label:
52 result.append((label, enum_value))
53 enum_value += 1
54 else:
55 if re.match(start_marker, line):
56 inside_enum = True
57 enum_value = 0 # Start at 'UNKNOWN'
58 return result
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|.
66
67 |labelTransformer| is a function used to transform labels from the
68 (label, value) pairs into a different format or style. Since label is
69 a string, defaulting labelTransformer to str() effectively does nothing.
70 '''
71 # Find ExtensionFunctions enum.
72 for enum_node in document.getElementsByTagName('enum'):
73 if enum_node.attributes['name'].value == histogram_enum_name:
74 histogram_enum_node = enum_node
75 break
76 else:
77 raise UserError('No {0} enum node found'.format(histogram_enum_name))
78
79 # Remove existing values.
80 while histogram_enum_node.hasChildNodes():
81 histogram_enum_node.removeChild(histogram_enum_node.lastChild)
82
83 # Add a "Generated from (...)" comment
84 comment = ' Generated from {0} '.format(source_enum_path)
85 histogram_enum_node.appendChild(document.createComment(comment))
86
87 # Add values generated from policy templates.
88 for (label, value) in source_enum_values:
89 node = document.createElement('int')
90 node.attributes['value'] = str(value)
91 node.attributes['label'] = labelTransformer(label)
92 histogram_enum_node.appendChild(node)
93
94 def UpdateHistogramEnum(histogram_enum_name, source_enum_path,
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.
99
100 |labelTransformer| is a function whose usage is described in
101 UpdateHistogramDefinitions.
102 '''
103 HISTOGRAMS_PATH = 'histograms.xml'
104
105 if len(sys.argv) > 1:
106 print >>sys.stderr, 'No arguments expected!'
107 sys.stderr.write(__doc__)
108 sys.exit(1)
109
110 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path))
111 source_enum_values = ReadHistogramValues(source_enum_path, start_marker,
112 end_marker)
113
114 Log('Reading existing histograms from "{0}".'.format(HISTOGRAMS_PATH))
115 with open(HISTOGRAMS_PATH, 'rb') as f:
116 histograms_doc = minidom.parse(f)
117 f.seek(0)
118 xml = f.read()
119
120 Log('Comparing histograms enum with new enum definition.')
121 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values,
122 source_enum_path, histograms_doc, labelTransformer)
123
124 Log('Writing out new histograms file.')
125 new_xml = PrettyPrintNode(histograms_doc)
126 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'):
127 with open(HISTOGRAMS_PATH, 'wb') as f:
128 f.write(new_xml)
129
130 Log('Done.')
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698