[email protected] | 516dce2 | 2014-05-01 18:31:53 | [diff] [blame] | 1 | # Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 54caefd | 2012-02-15 06:18:56 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
[email protected] | 516dce2 | 2014-05-01 18:31:53 | [diff] [blame] | 5 | """Presubmit script for Chromium browser code. |
| 6 | |
daniimms | 4193020 | 2015-04-13 15:59:50 | [diff] [blame] | 7 | This script currently checks HTML/CSS/JS files in resources/ and ui/webui/. |
[email protected] | 54caefd | 2012-02-15 06:18:56 | [diff] [blame] | 8 | |
| 9 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
tfarina | 78bb92f4 | 2015-01-31 00:20:48 | [diff] [blame] | 10 | for more details about the presubmit API built into depot_tools, and see |
[email protected] | 54caefd | 2012-02-15 06:18:56 | [diff] [blame] | 11 | http://www.chromium.org/developers/web-development-style-guide for the rules |
[email protected] | 516dce2 | 2014-05-01 18:31:53 | [diff] [blame] | 12 | checked for here. |
[email protected] | 54caefd | 2012-02-15 06:18:56 | [diff] [blame] | 13 | """ |
| 14 | |
[email protected] | 54caefd | 2012-02-15 06:18:56 | [diff] [blame] | 15 | def CheckChangeOnUpload(input_api, output_api): |
| 16 | return _CommonChecks(input_api, output_api) |
| 17 | |
| 18 | |
| 19 | def CheckChangeOnCommit(input_api, output_api): |
| 20 | return _CommonChecks(input_api, output_api) |
| 21 | |
dougt | b53dc59 | 2017-02-01 16:25:00 | [diff] [blame] | 22 | def _RunHistogramChecks(input_api, output_api, histogram_name): |
| 23 | try: |
| 24 | # Setup sys.path so that we can call histrogram code |
| 25 | import sys |
| 26 | original_sys_path = sys.path |
| 27 | sys.path = sys.path + [input_api.os_path.join( |
| 28 | input_api.change.RepositoryRoot(), |
| 29 | 'tools', 'metrics', 'histograms')] |
| 30 | |
alexmos | 7f7bae6 | 2017-04-05 21:15:04 | [diff] [blame^] | 31 | results = [] |
| 32 | |
dougt | b53dc59 | 2017-02-01 16:25:00 | [diff] [blame] | 33 | import presubmit_bad_message_reasons |
alexmos | 7f7bae6 | 2017-04-05 21:15:04 | [diff] [blame^] | 34 | results.extend(presubmit_bad_message_reasons.PrecheckBadMessage(input_api, |
| 35 | output_api, histogram_name)) |
| 36 | |
| 37 | import presubmit_scheme_histograms |
| 38 | results.extend(presubmit_scheme_histograms. |
| 39 | PrecheckShouldAllowOpenURLEnums(input_api, output_api)) |
| 40 | |
| 41 | return results |
dougt | b53dc59 | 2017-02-01 16:25:00 | [diff] [blame] | 42 | except: |
| 43 | return [output_api.PresubmitError('Could not verify histogram!')] |
| 44 | finally: |
| 45 | sys.path = original_sys_path |
| 46 | |
[email protected] | 54caefd | 2012-02-15 06:18:56 | [diff] [blame] | 47 | |
| 48 | def _CommonChecks(input_api, output_api): |
| 49 | """Checks common to both upload and commit.""" |
| 50 | results = [] |
[email protected] | 54caefd | 2012-02-15 06:18:56 | [diff] [blame] | 51 | |
| 52 | path = input_api.os_path |
[email protected] | 516dce2 | 2014-05-01 18:31:53 | [diff] [blame] | 53 | cwd = input_api.PresubmitLocalPath() |
| 54 | resources = path.join(cwd, 'resources') |
| 55 | webui = path.join(cwd, 'ui', 'webui') |
| 56 | |
[email protected] | b334713 | 2012-03-22 03:36:00 | [diff] [blame] | 57 | affected_files = (f.AbsoluteLocalPath() for f in input_api.AffectedFiles()) |
dbeam | a7b29129 | 2015-07-20 21:53:48 | [diff] [blame] | 58 | |
| 59 | would_affect_tests = [ |
[email protected] | 516dce2 | 2014-05-01 18:31:53 | [diff] [blame] | 60 | path.join(cwd, 'PRESUBMIT.py'), |
| 61 | path.join(cwd, 'test_presubmit.py'), |
dbeam | a7b29129 | 2015-07-20 21:53:48 | [diff] [blame] | 62 | ] |
| 63 | would_affect_tests += input_api.glob(path.join(cwd, 'web_dev_style', '*.py')) |
| 64 | |
[email protected] | b334713 | 2012-03-22 03:36:00 | [diff] [blame] | 65 | if any(f for f in affected_files if f in would_affect_tests): |
[email protected] | 516dce2 | 2014-05-01 18:31:53 | [diff] [blame] | 66 | tests = [path.join(cwd, 'test_presubmit.py')] |
[email protected] | 54caefd | 2012-02-15 06:18:56 | [diff] [blame] | 67 | results.extend( |
| 68 | input_api.canned_checks.RunUnitTests(input_api, output_api, tests)) |
| 69 | |
| 70 | import sys |
| 71 | old_path = sys.path |
| 72 | |
| 73 | try: |
[email protected] | 516dce2 | 2014-05-01 18:31:53 | [diff] [blame] | 74 | sys.path = [cwd] + old_path |
estade | 422789b6 | 2014-09-06 01:33:32 | [diff] [blame] | 75 | from web_dev_style import (resource_checker, css_checker, html_checker, |
| 76 | js_checker) |
[email protected] | 54caefd | 2012-02-15 06:18:56 | [diff] [blame] | 77 | |
[email protected] | 516dce2 | 2014-05-01 18:31:53 | [diff] [blame] | 78 | search_dirs = (resources, webui) |
[email protected] | ffbeebf | 2013-03-21 04:20:33 | [diff] [blame] | 79 | def _html_css_js_resource(p): |
[email protected] | 516dce2 | 2014-05-01 18:31:53 | [diff] [blame] | 80 | return p.endswith(('.html', '.css', '.js')) and p.startswith(search_dirs) |
[email protected] | a1ce93f | 2012-04-04 06:14:49 | [diff] [blame] | 81 | |
tsergeant | cf585f1 | 2016-09-23 02:35:48 | [diff] [blame] | 82 | def _vulcanized_resource(p): |
| 83 | return p.endswith(('vulcanized.html', 'crisper.js')) |
| 84 | |
| 85 | BLACKLIST = [ |
| 86 | 'chrome/browser/resources/pdf/index.html', |
| 87 | 'chrome/browser/resources/pdf/index.js' |
| 88 | ] |
[email protected] | ffbeebf | 2013-03-21 04:20:33 | [diff] [blame] | 89 | def is_resource(maybe_resource): |
[email protected] | d260060 | 2014-02-19 00:09:19 | [diff] [blame] | 90 | return (maybe_resource.LocalPath() not in BLACKLIST and |
tsergeant | cf585f1 | 2016-09-23 02:35:48 | [diff] [blame] | 91 | not _vulcanized_resource(maybe_resource.LocalPath()) and |
[email protected] | d260060 | 2014-02-19 00:09:19 | [diff] [blame] | 92 | _html_css_js_resource(maybe_resource.AbsoluteLocalPath())) |
[email protected] | ffbeebf | 2013-03-21 04:20:33 | [diff] [blame] | 93 | |
[email protected] | 633c60c | 2014-07-24 03:19:23 | [diff] [blame] | 94 | results.extend(resource_checker.ResourceChecker( |
| 95 | input_api, output_api, file_filter=is_resource).RunChecks()) |
[email protected] | ffbeebf | 2013-03-21 04:20:33 | [diff] [blame] | 96 | results.extend(css_checker.CSSChecker( |
[email protected] | 92d6804 | 2013-04-30 17:49:34 | [diff] [blame] | 97 | input_api, output_api, file_filter=is_resource).RunChecks()) |
estade | 422789b6 | 2014-09-06 01:33:32 | [diff] [blame] | 98 | results.extend(html_checker.HtmlChecker( |
| 99 | input_api, output_api, file_filter=is_resource).RunChecks()) |
[email protected] | ffbeebf | 2013-03-21 04:20:33 | [diff] [blame] | 100 | results.extend(js_checker.JSChecker( |
| 101 | input_api, output_api, file_filter=is_resource).RunChecks()) |
dougt | b53dc59 | 2017-02-01 16:25:00 | [diff] [blame] | 102 | results.extend(_RunHistogramChecks(input_api, output_api, |
| 103 | "BadMessageReasonChrome")) |
[email protected] | 54caefd | 2012-02-15 06:18:56 | [diff] [blame] | 104 | finally: |
| 105 | sys.path = old_path |
| 106 | |
| 107 | return results |