Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
|
Alexei Svitkine (slow)
2014/11/15 00:15:47
Nit: In new files, we don't include the (c) - plea
gayane -on leave until 09-2017
2014/11/15 00:50:33
Done.
| |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 """Presubmit script for HTML files in chrome/browser/resources. | |
| 5 | |
| 6 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts | |
| 7 for more details about the presubmit API built into gcl. | |
| 8 """ | |
| 9 | |
| 10 import re | |
| 11 | |
| 12 ACTION_XML_PATH = '../../../tools/metrics/actions/actions.xml' | |
| 13 | |
| 14 def CheckUserActionUpdate(input_api, output_api, action_xml_path): | |
| 15 """Checks if any new user action has been added.""" | |
| 16 if any('actions.xml' == input_api.os_path.basename(f.LocalPath()) for f in | |
| 17 input_api.AffectedFiles()): | |
| 18 # If actions.xml is already included in the changelist, the PRESUBMIT | |
| 19 # for actions.xml will do a more complete presubmit check. | |
| 20 return [] | |
| 21 | |
| 22 file_filter = lambda f: f.LocalPath().endswith(('.html')) | |
| 23 action_re = r'(^|\s+)metric\s*=\s*"([^ ]*)"' | |
| 24 current_actions = None | |
| 25 for f in input_api.AffectedFiles(file_filter=file_filter): | |
| 26 for line_num, line in f.ChangedContents(): | |
| 27 match = input_api.re.search(action_re, line) | |
| 28 if match: | |
| 29 # Loads contents in tools/metrics/actions/actions.xml to memory. It's | |
| 30 # loaded only once. | |
| 31 if not current_actions: | |
| 32 with open(action_xml_path) as actions_f: | |
| 33 current_actions = actions_f.read() | |
| 34 action_name = match.group(2) | |
| 35 | |
| 36 # Search for the matched user action name in |current_actions|. | |
| 37 if not IsActionPresent(current_actions, action_name, | |
| 38 IsBoolean(f.NewContents(), action_name)): | |
|
Alexei Svitkine (slow)
2014/11/15 00:15:47
Nit: If you make is_boolean a local var above, doe
gayane -on leave until 09-2017
2014/11/15 00:50:33
Done.
| |
| 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, action_name), [])] | |
| 44 return [] | |
| 45 | |
| 46 | |
| 47 def IsActionPresent(current_actions, action_name, is_boolean): | |
|
Alexei Svitkine (slow)
2014/11/15 00:15:47
Please add some short comments above these methods
gayane -on leave until 09-2017
2014/11/15 00:50:33
Done.
| |
| 48 if not is_boolean: | |
| 49 action = 'name="{0}"'.format(action_name) | |
| 50 return action in current_actions | |
| 51 else: | |
|
Alexei Svitkine (slow)
2014/11/15 00:15:47
Nit: if there's an early return, remove the else a
gayane -on leave until 09-2017
2014/11/15 00:50:33
Done.
| |
| 52 action_disabled = 'name="{0}_Disabled"'.format(action_name) | |
| 53 action_enabled = 'name="{0}_Enabled"'.format(action_name) | |
| 54 | |
| 55 pos = current_actions.find(action_disabled) | |
| 56 return pos >= 0 and current_actions.find(action_enabled, pos) >= 0 | |
| 57 | |
| 58 | |
| 59 def IsBoolean(new_content_lines, action_name): | |
| 60 new_content = '\n'.join(new_content_lines) | |
| 61 | |
| 62 html_element_re = r'<(.*?)(^|\s+)metric\s*=\s*"%s"(.*?)>' % (action_name) | |
| 63 type_re = r'datatype="boolean"|type="checkbox"|type="radio".*?'\ | |
| 64 'value=("true"|"false")' | |
| 65 | |
| 66 match = re.search(html_element_re, new_content) | |
| 67 if match: | |
| 68 left_content = match.group(1) | |
| 69 right_content = match.group(3) | |
| 70 if re.search(type_re, left_content) or re.search(type_re, right_content): | |
| 71 return True | |
| 72 return False | |
| 73 | |
| 74 | |
| 75 def CheckChangeOnUpload(input_api, output_api): | |
| 76 return CheckUserActionUpdate(input_api, output_api, ACTION_XML_PATH) | |
| 77 | |
| 78 | |
| 79 def CheckChangeOnCommit(input_api, output_api): | |
| 80 return CheckUserActionUpdate(input_api, output_api, ACTION_XML_PATH) | |
| OLD | NEW |