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 | |
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 action_name = match.group(2) | |
Alexei Svitkine (slow)
2014/11/15 01:07:48
Hmm, maybe name this metric_name rather than actio
gayane -on leave until 09-2017
2014/11/18 18:18:57
Done.
| |
37 is_boolean = IsBoolean(f.NewContents(), action_name) | |
38 # Search for the matched user action name in |current_actions|. | |
39 if not IsActionPresent(current_actions, action_name, is_boolean): | |
40 return [output_api.PresubmitPromptWarning( | |
41 'File %s line %d: %s is missing in ' | |
42 'tools/metrics/actions/actions.xml. Please run ' | |
43 'tools/metrics/actions/extract_actions.py to update.' | |
44 % (f.LocalPath(), line_num, action_name), [])] | |
45 return [] | |
46 | |
47 | |
48 def IsActionPresent(current_actions, action_name, is_boolean): | |
49 """Checks if action_name is defined in the actions file. | |
50 | |
51 This function tries to match the action definition for the provided action | |
52 name if it is not boolean action. If it is boolean then it tries to match | |
53 first the disabled action. In case of success it contiunes to search for | |
54 enabled one. | |
Alexei Svitkine (slow)
2014/11/15 01:07:48
You can make this shorter by describing the purpos
gayane -on leave until 09-2017
2014/11/18 18:18:57
Done.
| |
55 | |
56 Args: | |
57 current_actions: The content of the actions.xml file. | |
58 action_name: The action_name for which the check should be done. | |
59 is_boolean: The identifer for the action type. | |
Alexei Svitkine (slow)
2014/11/15 01:07:48
What's an identifier for the action type? It would
gayane -on leave until 09-2017
2014/11/18 18:18:57
Done.
| |
60 """ | |
61 if not is_boolean: | |
62 action = 'name="{0}"'.format(action_name) | |
63 return action in current_actions | |
64 | |
65 action_disabled = 'name="{0}_Disabled"'.format(action_name) | |
66 action_enabled = 'name="{0}_Enabled"'.format(action_name) | |
67 | |
68 pos = current_actions.find(action_disabled) | |
69 return pos >= 0 and current_actions.find(action_enabled, pos) >= 0 | |
Alexei Svitkine (slow)
2014/11/15 01:07:48
Why does "action in current_actions" work for the
gayane -on leave until 09-2017
2014/11/18 18:18:57
Done.
| |
70 | |
71 | |
72 def IsBoolean(new_content_lines, action_name): | |
73 """Check whether action defined in the changed code is boolean or not. | |
74 | |
75 This function first tries to match the HTML tag in which the action has been | |
76 mention. Afterwards it tries to match boolean indicators from the remaining | |
77 text ot the HTML tag. | |
78 | |
79 Args: | |
80 new_content_lines: List of changed lines. | |
81 action_name: Action name for which the check should be done. | |
82 """ | |
83 new_content = '\n'.join(new_content_lines) | |
84 | |
85 html_element_re = r'<(.*?)(^|\s+)metric\s*=\s*"%s"(.*?)>' % (action_name) | |
86 type_re = r'datatype="boolean"|type="checkbox"|type="radio".*?'\ | |
87 'value=("true"|"false")' | |
88 | |
89 match = re.search(html_element_re, new_content) | |
90 if match: | |
91 left_content = match.group(1) | |
92 right_content = match.group(3) | |
93 if re.search(type_re, left_content) or re.search(type_re, right_content): | |
94 return True | |
95 return False | |
96 | |
97 | |
98 def CheckChangeOnUpload(input_api, output_api): | |
99 return CheckUserActionUpdate(input_api, output_api, ACTION_XML_PATH) | |
100 | |
101 | |
102 def CheckChangeOnCommit(input_api, output_api): | |
103 return CheckUserActionUpdate(input_api, output_api, ACTION_XML_PATH) | |
OLD | NEW |