Chromium Code Reviews| Index: chrome/browser/resources/PRESUBMIT.py |
| diff --git a/chrome/browser/resources/PRESUBMIT.py b/chrome/browser/resources/PRESUBMIT.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6558e012401054b72426440419dea77ba2feca49 |
| --- /dev/null |
| +++ b/chrome/browser/resources/PRESUBMIT.py |
| @@ -0,0 +1,101 @@ |
| +# 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. |
| + |
| +"""Presubmit script for HTML files in chrome/browser/resources. |
| + |
| +See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| +for more details about the presubmit API built into gcl. |
| +""" |
| + |
| +import re |
|
M-A Ruel
2014/11/26 18:15:48
remove
gayane -on leave until 09-2017
2014/11/26 20:28:46
Done.
|
| + |
| +ACTION_XML_PATH = '../../../tools/metrics/actions/actions.xml' |
| + |
| +def CheckUserActionUpdate(input_api, output_api, action_xml_path): |
| + """Checks if any new user action has been added.""" |
| + if any('actions.xml' == input_api.os_path.basename(f.LocalPath()) for f in |
| + input_api.AffectedFiles()): |
| + # If actions.xml is already included in the changelist, the PRESUBMIT |
| + # for actions.xml will do a more complete presubmit check. |
| + return [] |
| + |
| + file_filter = lambda f: f.LocalPath().endswith(('.html')) |
| + action_re = r'(^|\s+)metric\s*=\s*"([^ ]*)"' |
| + current_actions = None |
| + for f in input_api.AffectedFiles(file_filter=file_filter): |
| + for line_num, line in f.ChangedContents(): |
| + match = input_api.re.search(action_re, line) |
| + if match: |
| + # Loads contents in tools/metrics/actions/actions.xml to memory. It's |
| + # loaded only once. |
| + if not current_actions: |
| + with open(action_xml_path) as actions_f: |
| + current_actions = actions_f.read() |
| + |
| + metric_name = match.group(2) |
| + is_boolean = IsBoolean(f.NewContents(), metric_name) |
| + |
| + # Search for the matched user action name in |current_actions|. |
| + if not IsActionPresent(current_actions, metric_name, is_boolean): |
| + return [output_api.PresubmitPromptWarning( |
| + 'File %s line %d: %s is missing in ' |
| + 'tools/metrics/actions/actions.xml. Please run ' |
| + 'tools/metrics/actions/extract_actions.py to update.' |
| + % (f.LocalPath(), line_num, metric_name), [])] |
| + return [] |
| + |
| + |
| +def IsActionPresent(current_actions, metric_name, is_boolean): |
| + """Checks if metric_name is defined in the actions file. |
| + |
| + Checks whether there's matching entries in an actions.xml file for the given |
| + |metric_name|, depending on whether it is a boolean action. |
| + |
| + Args: |
| + current_actions: The content of the actions.xml file. |
| + metric_name: The name for which the check should be done. |
| + is_boolean: Whether the action comes from a boolean control. |
| + """ |
| + if not is_boolean: |
| + action = 'name="{0}"'.format(metric_name) |
| + return action in current_actions |
| + |
| + action_disabled = 'name="{0}_Disabled"'.format(metric_name) |
| + action_enabled = 'name="{0}_Enabled"'.format(metric_name) |
| + |
| + return action_disabled in current_actions and \ |
|
M-A Ruel
2014/11/26 18:15:48
use () instead of \
everywhere (like line 84)
gayane -on leave until 09-2017
2014/11/26 20:28:46
Done.
|
| + action_enabled in current_actions |
| + |
| + |
| +def IsBoolean(new_content_lines, metric_name): |
| + """Check whether action defined in the changed code is boolean or not. |
| + |
| + Checks whether the action comes from boolean control based on the HTML |
| + elements attributes. |
| + |
| + Args: |
| + new_content_lines: List of changed lines. |
| + metric_name: The name for which the check should be done. |
| + """ |
| + new_content = '\n'.join(new_content_lines) |
| + |
| + html_element_re = r'<(.*?)(^|\s+)metric\s*=\s*"%s"(.*?)>' % (metric_name) |
| + type_re = r'datatype="boolean"|type="checkbox"|type="radio".*?'\ |
| + 'value=("true"|"false")' |
| + |
| + match = re.search(html_element_re, new_content) |
|
M-A Ruel
2014/11/26 18:15:48
input_api.re.
gayane -on leave until 09-2017
2014/11/26 20:28:46
Done.
|
| + if match: |
| + left_content = match.group(1) |
| + right_content = match.group(3) |
| + if re.search(type_re, left_content) or re.search(type_re, right_content): |
|
M-A Ruel
2014/11/26 18:15:48
return bool(re.search(type_re, left_content) or re
gayane -on leave until 09-2017
2014/11/26 20:28:46
Done.
|
| + return True |
| + return False |
| + |
| + |
| +def CheckChangeOnUpload(input_api, output_api): |
| + return CheckUserActionUpdate(input_api, output_api, ACTION_XML_PATH) |
| + |
| + |
| +def CheckChangeOnCommit(input_api, output_api): |
| + return CheckUserActionUpdate(input_api, output_api, ACTION_XML_PATH) |