Chromium Code Reviews| Index: tools/metrics/histograms/update_extension_permission.py |
| diff --git a/tools/metrics/histograms/update_extension_functions.py b/tools/metrics/histograms/update_extension_permission.py |
| similarity index 72% |
| copy from tools/metrics/histograms/update_extension_functions.py |
| copy to tools/metrics/histograms/update_extension_permission.py |
| index 1925c6e4e4ea7d707f5cbe53202c790ff206a4bf..a9afe4eb3c7022c6f7fc2944104747308aadee7a 100644 |
| --- a/tools/metrics/histograms/update_extension_functions.py |
| +++ b/tools/metrics/histograms/update_extension_permission.py |
| @@ -1,12 +1,12 @@ |
| -# 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 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
|
| +permission_message.h. |
| If the file was pretty-printed, the updated version is pretty-printed too. |
| -""" |
| +''' |
| import logging |
| import re |
| @@ -18,12 +18,12 @@ from diffutil import PromptUserToAcceptDiff |
| from pretty_print import PrettyPrintNode |
| HISTOGRAMS_PATH = 'histograms.xml' |
| -ENUM_NAME = 'ExtensionFunctions' |
| +PERMISSION_MESSAGE_PATH = \ |
| + '../../../extensions/common/permissions/permission_message.h' |
| -EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH = \ |
| - '../../../chrome/browser/extensions/extension_function_histogram_value.h' |
| -ENUM_START_MARKER = "^enum HistogramValue {" |
| -ENUM_END_MARKER = "^ENUM_BOUNDARY" |
| +ENUM_NAME = 'ExtensionPermission' |
| +ENUM_START_MARKER = "^enum ID {" |
| +ENUM_END_MARKER = "^kEnumBoundary" |
| class UserError(Exception): |
| @@ -34,6 +34,11 @@ class UserError(Exception): |
| def message(self): |
| return self.args[0] |
| +def StyleLabel(label): |
| + '''Transforms labels of form "kLabelName" to "LABEL_NAME" |
| + ''' |
| + 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
|
| + |
| def ExtractRegexGroup(line, regex): |
| m = re.match(regex, line) |
| if m: |
| @@ -45,8 +50,8 @@ def ExtractRegexGroup(line, regex): |
| def ReadHistogramValues(filename): |
| """Returns a list of pairs (label, value) corresponding to HistogramValue. |
| - Reads the extension_functions_histogram_value.h file, locates the |
| - HistogramValue enum definition and returns a pair for each entry. |
| + Reads the permission_message.h file, locates the |
| + ID enum definition and returns a pair for each entry. |
| """ |
| # Read the file as a list of lines |
| @@ -66,7 +71,7 @@ def ReadHistogramValues(filename): |
| # Inside enum: generate new xml entry |
| label = ExtractRegexGroup(line.strip(), "^([\w]+)") |
| if label: |
| - result.append((label, enum_value)) |
| + result.append((StyleLabel(label), enum_value)) |
| enum_value += 1 |
| else: |
| if re.match(ENUM_START_MARKER, line): |
| @@ -76,9 +81,9 @@ def ReadHistogramValues(filename): |
| 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|. |
| + """Sets the children of <enum name="ExtensionPermission" ...> node in |
| + |document| to values generated from (label, value) pairs contained in |
| + |histogram_values|. |
| Args: |
| histogram_values: A list of pairs (label, value) defining each extension |
| @@ -90,27 +95,27 @@ def UpdateHistogramDefinitions(histogram_values, document): |
| # Find ExtensionFunctions enum. |
| for enum_node in document.getElementsByTagName('enum'): |
| if enum_node.attributes['name'].value == ENUM_NAME: |
| - extension_functions_enum_node = enum_node |
| + ID_enum_node = enum_node |
| break |
| else: |
| raise UserError('No policy enum node found') |
| # Remove existing values. |
| - while extension_functions_enum_node.hasChildNodes(): |
| - extension_functions_enum_node.removeChild( |
| - extension_functions_enum_node.lastChild) |
| + while ID_enum_node.hasChildNodes(): |
| + ID_enum_node.removeChild( |
| + ID_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)) |
| + PERMISSION_MESSAGE_PATH) |
| + ID_enum_node.appendChild(document.createComment(comment)) |
| # Add values generated from policy templates. |
| for (label, value) in histogram_values: |
| node = document.createElement('int') |
| node.attributes['value'] = str(value) |
| node.attributes['label'] = label |
| - extension_functions_enum_node.appendChild(node) |
| + ID_enum_node.appendChild(node) |
| def Log(message): |
| logging.info(message) |
| @@ -122,9 +127,9 @@ def main(): |
| sys.exit(1) |
| Log('Reading histogram enum definition from "%s".' |
| - % (EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH)) |
| + % (PERMISSION_MESSAGE_PATH)) |
| histogram_values = ReadHistogramValues( |
| - EXTENSION_FUNCTIONS_HISTOGRAM_VALUE_PATH) |
| + PERMISSION_MESSAGE_PATH) |
| Log('Reading existing histograms from "%s".' % (HISTOGRAMS_PATH)) |
| with open(HISTOGRAMS_PATH, 'rb') as f: |