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

Side by Side Diff: chrome/browser/resources/PRESUBMIT.py

Issue 719463003: Presubmit checks for user actions intorduced in HTML files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments for mock classes Created 6 years 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
« no previous file with comments | « PRESUBMIT_test_mocks.py ('k') | chrome/browser/resources/PRESUBMIT_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 """Presubmit script for HTML files in chrome/browser/resources.
6
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into gcl.
9 """
10
11 ACTION_XML_PATH = '../../../tools/metrics/actions/actions.xml'
12
13 def CheckUserActionUpdate(input_api, output_api, action_xml_path):
14 """Checks if any new user action has been added."""
15 if any('actions.xml' == input_api.os_path.basename(f.LocalPath()) for f in
16 input_api.AffectedFiles()):
17 # If actions.xml is already included in the changelist, the PRESUBMIT
18 # for actions.xml will do a more complete presubmit check.
19 return []
20
21 file_filter = lambda f: f.LocalPath().endswith('.html')
22 action_re = r'(^|\s+)metric\s*=\s*"([^ ]*)"'
23 current_actions = None
24 for f in input_api.AffectedFiles(file_filter=file_filter):
25 for line_num, line in f.ChangedContents():
26 match = input_api.re.search(action_re, line)
27 if match:
28 # Loads contents in tools/metrics/actions/actions.xml to memory. It's
29 # loaded only once.
30 if not current_actions:
31 with open(action_xml_path) as actions_f:
32 current_actions = actions_f.read()
33
34 metric_name = match.group(2)
35 is_boolean = IsBoolean(f.NewContents(), metric_name, input_api)
36
37 # Search for the matched user action name in |current_actions|.
38 if not IsActionPresent(current_actions, metric_name, is_boolean):
39 return [output_api.PresubmitPromptWarning(
40 'File %s line %d: %s is missing in '
41 'tools/metrics/actions/actions.xml. Please run '
42 'tools/metrics/actions/extract_actions.py to update.'
43 % (f.LocalPath(), line_num, metric_name), [])]
44 return []
45
46
47 def IsActionPresent(current_actions, metric_name, is_boolean):
48 """Checks if metric_name is defined in the actions file.
49
50 Checks whether there's matching entries in an actions.xml file for the given
51 |metric_name|, depending on whether it is a boolean action.
52
53 Args:
54 current_actions: The content of the actions.xml file.
55 metric_name: The name for which the check should be done.
56 is_boolean: Whether the action comes from a boolean control.
57 """
58 if not is_boolean:
59 action = 'name="{0}"'.format(metric_name)
60 return action in current_actions
61
62 action_disabled = 'name="{0}_Disabled"'.format(metric_name)
63 action_enabled = 'name="{0}_Enabled"'.format(metric_name)
64
65 return (action_disabled in current_actions and
66 action_enabled in current_actions)
67
68
69 def IsBoolean(new_content_lines, metric_name, input_api):
70 """Check whether action defined in the changed code is boolean or not.
71
72 Checks whether the action comes from boolean control based on the HTML
73 elements attributes.
74
75 Args:
76 new_content_lines: List of changed lines.
77 metric_name: The name for which the check should be done.
78 """
79 new_content = '\n'.join(new_content_lines)
80
81 html_element_re = r'<(.*?)(^|\s+)metric\s*=\s*"%s"(.*?)>' % (metric_name)
82 type_re = (r'datatype="boolean"|type="checkbox"|type="radio".*?'
83 'value=("true"|"false")')
84
85 match = input_api.re.search(html_element_re, new_content)
86 return (match and
87 any(input_api.re.search(type_re, match.group(i)) for i in (1, 3)))
88
89
90 def CheckChangeOnUpload(input_api, output_api):
91 return CheckUserActionUpdate(input_api, output_api, ACTION_XML_PATH)
92
93
94 def CheckChangeOnCommit(input_api, output_api):
95 return CheckUserActionUpdate(input_api, output_api, ACTION_XML_PATH)
OLDNEW
« no previous file with comments | « PRESUBMIT_test_mocks.py ('k') | chrome/browser/resources/PRESUBMIT_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698