OLD | NEW |
---|---|
(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 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.
| |
12 | |
13 ACTION_XML_PATH = '../../../tools/metrics/actions/actions.xml' | |
14 | |
15 def CheckUserActionUpdate(input_api, output_api, action_xml_path): | |
16 """Checks if any new user action has been added.""" | |
17 if any('actions.xml' == input_api.os_path.basename(f.LocalPath()) for f in | |
18 input_api.AffectedFiles()): | |
19 # If actions.xml is already included in the changelist, the PRESUBMIT | |
20 # for actions.xml will do a more complete presubmit check. | |
21 return [] | |
22 | |
23 file_filter = lambda f: f.LocalPath().endswith(('.html')) | |
24 action_re = r'(^|\s+)metric\s*=\s*"([^ ]*)"' | |
25 current_actions = None | |
26 for f in input_api.AffectedFiles(file_filter=file_filter): | |
27 for line_num, line in f.ChangedContents(): | |
28 match = input_api.re.search(action_re, line) | |
29 if match: | |
30 # Loads contents in tools/metrics/actions/actions.xml to memory. It's | |
31 # loaded only once. | |
32 if not current_actions: | |
33 with open(action_xml_path) as actions_f: | |
34 current_actions = actions_f.read() | |
35 | |
36 metric_name = match.group(2) | |
37 is_boolean = IsBoolean(f.NewContents(), metric_name) | |
38 | |
39 # Search for the matched user action name in |current_actions|. | |
40 if not IsActionPresent(current_actions, metric_name, is_boolean): | |
41 return [output_api.PresubmitPromptWarning( | |
42 'File %s line %d: %s is missing in ' | |
43 'tools/metrics/actions/actions.xml. Please run ' | |
44 'tools/metrics/actions/extract_actions.py to update.' | |
45 % (f.LocalPath(), line_num, metric_name), [])] | |
46 return [] | |
47 | |
48 | |
49 def IsActionPresent(current_actions, metric_name, is_boolean): | |
50 """Checks if metric_name is defined in the actions file. | |
51 | |
52 Checks whether there's matching entries in an actions.xml file for the given | |
53 |metric_name|, depending on whether it is a boolean action. | |
54 | |
55 Args: | |
56 current_actions: The content of the actions.xml file. | |
57 metric_name: The name for which the check should be done. | |
58 is_boolean: Whether the action comes from a boolean control. | |
59 """ | |
60 if not is_boolean: | |
61 action = 'name="{0}"'.format(metric_name) | |
62 return action in current_actions | |
63 | |
64 action_disabled = 'name="{0}_Disabled"'.format(metric_name) | |
65 action_enabled = 'name="{0}_Enabled"'.format(metric_name) | |
66 | |
67 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.
| |
68 action_enabled in current_actions | |
69 | |
70 | |
71 def IsBoolean(new_content_lines, metric_name): | |
72 """Check whether action defined in the changed code is boolean or not. | |
73 | |
74 Checks whether the action comes from boolean control based on the HTML | |
75 elements attributes. | |
76 | |
77 Args: | |
78 new_content_lines: List of changed lines. | |
79 metric_name: The name for which the check should be done. | |
80 """ | |
81 new_content = '\n'.join(new_content_lines) | |
82 | |
83 html_element_re = r'<(.*?)(^|\s+)metric\s*=\s*"%s"(.*?)>' % (metric_name) | |
84 type_re = r'datatype="boolean"|type="checkbox"|type="radio".*?'\ | |
85 'value=("true"|"false")' | |
86 | |
87 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.
| |
88 if match: | |
89 left_content = match.group(1) | |
90 right_content = match.group(3) | |
91 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.
| |
92 return True | |
93 return False | |
94 | |
95 | |
96 def CheckChangeOnUpload(input_api, output_api): | |
97 return CheckUserActionUpdate(input_api, output_api, ACTION_XML_PATH) | |
98 | |
99 | |
100 def CheckChangeOnCommit(input_api, output_api): | |
101 return CheckUserActionUpdate(input_api, output_api, ACTION_XML_PATH) | |
OLD | NEW |