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

Unified 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: 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 side-by-side diff with in-line comments
Download patch
Index: tools/metrics/histograms/update_histogram_enum.py
diff --git a/tools/metrics/histograms/update_extension_functions.py b/tools/metrics/histograms/update_histogram_enum.py
similarity index 46%
copy from tools/metrics/histograms/update_extension_functions.py
copy to tools/metrics/histograms/update_histogram_enum.py
index 0783f32642f140467918c25420150b5354aa83bf..ea43a3504916292db83609ab57b8a8c426cead48 100644
--- a/tools/metrics/histograms/update_extension_functions.py
+++ b/tools/metrics/histograms/update_histogram_enum.py
@@ -1,33 +1,19 @@
-# Copyright 2013 The Chromium Authors. All rights reserved.
+# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-"""Updates ExtensionFunctions enum in histograms.xml file with values read from
-extension_function_histogram_value.h.
+"""Updates enums in histograms.xml file with values read from provided C++ enum.
If the file was pretty-printed, the updated version is pretty-printed too.
"""
import logging
-import os
+import print_style
import re
import sys
from xml.dom import minidom
-import print_style
-
-# Import the metrics/common module.
-sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
-from diff_util import PromptUserToAcceptDiff
-
-HISTOGRAMS_PATH = 'histograms.xml'
-ENUM_NAME = 'ExtensionFunctions'
-
-EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH = \
- '../../../extensions/browser/extension_function_histogram_value.h'
-ENUM_START_MARKER = "^enum HistogramValue {"
-ENUM_END_MARKER = "^ENUM_BOUNDARY"
-
+from diffutil import PromptUserToAcceptDiff
class UserError(Exception):
def __init__(self, message):
@@ -37,21 +23,25 @@ class UserError(Exception):
def message(self):
return self.args[0]
-def ExtractRegexGroup(line, regex):
- m = re.match(regex, line)
- if m:
- return m.group(1)
- else:
- return None
+def Log(message):
+ logging.info(message)
-def ReadHistogramValues(filename):
- """Returns a list of pairs (label, value) corresponding to HistogramValue.
- Reads the extension_function_histogram_value.h file, locates the
- HistogramValue enum definition and returns a pair for each entry.
- """
+def ExtractRegexGroup(line, regex):
+ m = re.match(regex, line)
+ if m:
+ # TODO(ahernandez.miralles): This can throw an IndexError
+ # if no groups are present; enclose in try catch block
+ return m.group(1)
+ else:
+ return None
+
+def ReadHistogramValues(filename, start_marker, end_marker):
+ """Reads in values from |filename|, returning a list of (label, value) pairs
+ corresponding to the enum framed by |start_marker| and |end_marker|.
+ """
# Read the file as a list of lines
with open(filename) as f:
content = f.readlines()
@@ -63,7 +53,7 @@ def ReadHistogramValues(filename):
line = line.strip()
if inside_enum:
# Exit condition: we reached last enum value
- if re.match(ENUM_END_MARKER, line):
+ if re.match(end_marker, line):
inside_enum = False
else:
# Inside enum: generate new xml entry
@@ -72,81 +62,75 @@ def ReadHistogramValues(filename):
result.append((label, enum_value))
enum_value += 1
else:
- if re.match(ENUM_START_MARKER, line):
+ if re.match(start_marker, line):
inside_enum = True
enum_value = 0 # Start at 'UNKNOWN'
return result
-def UpdateHistogramDefinitions(histogram_values, document):
- """Sets the children of <enum name="ExtensionFunctions" ...> node in
- |document| to values generated from policy ids contained in
- |policy_templates|.
-
- Args:
- histogram_values: A list of pairs (label, value) defining each extension
- function
- document: A minidom.Document object representing parsed histogram
- definitions XML file.
-
+def UpdateHistogramDefinitions(histogram_enum_name, source_enum_values,
+ source_enum_path, document):
+ """Sets the children of <enum name=|histogram_enum_name| ...> node in
+ |document| to values generated from (label, value) pairs contained in
+ |source_enum_values|.
"""
# Find ExtensionFunctions enum.
for enum_node in document.getElementsByTagName('enum'):
- if enum_node.attributes['name'].value == ENUM_NAME:
- extension_functions_enum_node = enum_node
- break
+ if enum_node.attributes['name'].value == histogram_enum_name:
+ histogram_enum_node = enum_node
+ break
else:
- raise UserError('No policy enum node found')
+ raise UserError('No {0} enum node found'.format(histogram_enum_name))
# Remove existing values.
- while extension_functions_enum_node.hasChildNodes():
- extension_functions_enum_node.removeChild(
- extension_functions_enum_node.lastChild)
+ while histogram_enum_node.hasChildNodes():
+ histogram_enum_node.removeChild(histogram_enum_node.lastChild)
# Add a "Generated from (...)" comment
- comment = ' Generated from {0} '.format(
- EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH)
- extension_functions_enum_node.appendChild(document.createComment(comment))
+ comment = ' Generated from {0} '.format(source_enum_path)
+ histogram_enum_node.appendChild(document.createComment(comment))
# Add values generated from policy templates.
- for (label, value) in histogram_values:
+ for (label, value) in source_enum_values:
node = document.createElement('int')
node.attributes['value'] = str(value)
node.attributes['label'] = label
- extension_functions_enum_node.appendChild(node)
+ histogram_enum_node.appendChild(node)
-def Log(message):
- logging.info(message)
-def main():
+def UpdateHistogramEnum(histogram_enum_name, source_enum_path,
+ start_marker, end_marker):
+ """Updates |histogram_enum_name| enum in histograms.xml file with values
+ read from |source_enum_path|, where |start_marker| and |end_marker| indicate
+ the beginning and end of the source enum definition, respectively.
+ """
+ # TODO(ahernandez.miralles): The line below is present in nearly every
+ # file in this directory; factor out into a central location
+ HISTOGRAMS_PATH = 'histograms.xml'
+
if len(sys.argv) > 1:
print >>sys.stderr, 'No arguments expected!'
sys.stderr.write(__doc__)
sys.exit(1)
- Log('Reading histogram enum definition from "%s".'
- % (EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH))
- histogram_values = ReadHistogramValues(
- EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH)
+ Log('Reading histogram enum definition from "{0}".'.format(source_enum_path))
+ source_enum_values = ReadHistogramValues(source_enum_path, start_marker,
+ end_marker)
- Log('Reading existing histograms from "%s".' % (HISTOGRAMS_PATH))
+ Log('Reading existing histograms from "{0}".'.format(HISTOGRAMS_PATH))
with open(HISTOGRAMS_PATH, 'rb') as f:
histograms_doc = minidom.parse(f)
f.seek(0)
xml = f.read()
Log('Comparing histograms enum with new enum definition.')
- UpdateHistogramDefinitions(histogram_values, histograms_doc)
+ UpdateHistogramDefinitions(histogram_enum_name, source_enum_values,
+ source_enum_path, histograms_doc)
Log('Writing out new histograms file.')
new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc)
-
if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'):
with open(HISTOGRAMS_PATH, 'wb') as f:
f.write(new_xml)
Log('Done.')
-
-
-if __name__ == '__main__':
- main()
« no previous file with comments | « tools/metrics/histograms/update_extension_permission.py ('k') | tools/strict_enum_value_checker/changed_file_1.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698