Chromium Code Reviews| 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 43% |
| copy from tools/metrics/histograms/update_extension_functions.py |
| copy to tools/metrics/histograms/update_histogram_enum.py |
| index 0783f32642f140467918c25420150b5354aa83bf..2f8784e5ab5c80630018175124468eabad405276 100644 |
| --- a/tools/metrics/histograms/update_extension_functions.py |
| +++ b/tools/metrics/histograms/update_histogram_enum.py |
| @@ -1,33 +1,14 @@ |
| -# 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. |
| - |
| -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 +18,20 @@ 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 ReadHistogramValues(filename): |
| - """Returns a list of pairs (label, value) corresponding to HistogramValue. |
| +def Log(message): |
| + logging.info(message) |
| - 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: |
| + return m.group(1) |
| + else: |
| + return None |
|
Ilya Sherman
2014/03/14 23:19:35
nit: Please leave two blank lines between top-leve
|
| +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|. |
| + ''' |
|
Ilya Sherman
2014/03/14 23:19:35
nit: Double quotes for this sort of comment string
|
| # Read the file as a list of lines |
| with open(filename) as f: |
| content = f.readlines() |
| @@ -63,7 +43,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 +52,79 @@ 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, |
| + labelTransformer): |
| + '''Sets the children of <enum name=|histogram_enum_name| ...> node in |
| + |document| to values generated from (label, value) pairs contained in |
| + |source_enum_values|. |
| + |
| + |labelTransformer| is a function used to transform labels from the |
| + (label, value) pairs into a different format or style. Since label is |
| + a string, defaulting labelTransformer to str() effectively does nothing. |
| + ''' |
| # 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) |
| + node.attributes['label'] = labelTransformer(label) |
| + histogram_enum_node.appendChild(node) |
| -def Log(message): |
| - logging.info(message) |
| +def UpdateHistogramEnum(histogram_enum_name, source_enum_path, |
| + start_marker, end_marker, labelTransformer=str): |
| + '''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. |
| + |
| + |labelTransformer| is a function whose usage is described in |
| + UpdateHistogramDefinitions. |
| + ''' |
| + HISTOGRAMS_PATH = 'histograms.xml' |
| -def main(): |
| 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, labelTransformer) |
| 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() |