Ryan Tseng | 85ec17e | 2018-09-06 17:10:05 -0700 | [diff] [blame] | 1 | # Copyright (c) 2018 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 | |
btolsch | 9ba2371 | 2019-04-18 16:36:55 -0700 | [diff] [blame] | 6 | def _CheckDeps(input_api, output_api): |
| 7 | results = [] |
| 8 | import sys |
| 9 | original_sys_path = sys.path |
| 10 | try: |
| 11 | sys.path = sys.path + [input_api.os_path.join( |
| 12 | input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')] |
| 13 | import checkdeps |
| 14 | from cpp_checker import CppChecker |
| 15 | from rules import Rule |
| 16 | finally: |
| 17 | sys.path = original_sys_path |
| 18 | |
| 19 | added_includes = [] |
| 20 | for f in input_api.AffectedFiles(): |
| 21 | if CppChecker.IsCppFile(f.LocalPath()): |
| 22 | changed_lines = [line for _, line in f.ChangedContents()] |
| 23 | added_includes.append([f.AbsoluteLocalPath(), changed_lines]) |
| 24 | |
| 25 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
| 26 | violations = deps_checker.CheckAddedCppIncludes(added_includes) |
| 27 | for path, rule_type, rule_description in violations: |
| 28 | relpath = input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 29 | error_description = '%s\n %s' % (relpath, rule_description) |
| 30 | if rule_type == Rule.DISALLOW: |
| 31 | results.append(output_api.PresubmitError(error_description)) |
| 32 | else: |
| 33 | results.append(output_api.PresubmitPromptWarning(error_description)) |
| 34 | return results |
| 35 | |
| 36 | |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 37 | def _CommonChecks(input_api, output_api): |
| 38 | results = [] |
| 39 | # TODO(issues/43): Probably convert this to python so we can give more |
| 40 | # detailed errors. |
| 41 | presubmit_sh_result = input_api.subprocess.call( |
| 42 | input_api.PresubmitLocalPath() + '/PRESUBMIT.sh') |
| 43 | if presubmit_sh_result != 0: |
| 44 | results.append(output_api.PresubmitError('PRESUBMIT.sh failed')) |
btolsch | 9ba2371 | 2019-04-18 16:36:55 -0700 | [diff] [blame] | 45 | results.extend(_CheckDeps(input_api, output_api)) |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 46 | return results |
| 47 | |
| 48 | |
Ryan Tseng | 85ec17e | 2018-09-06 17:10:05 -0700 | [diff] [blame] | 49 | def CheckChangeOnUpload(input_api, output_api): |
btolsch | 333aecd | 2019-04-18 16:21:23 -0700 | [diff] [blame] | 50 | results = [] |
| 51 | results.extend(_CommonChecks(input_api, output_api)) |
| 52 | results.extend( |
| 53 | input_api.canned_checks.CheckChangedLUCIConfigs(input_api, output_api)) |
| 54 | return results |
| 55 | |
| 56 | |
| 57 | def CheckChangeOnCommit(input_api, output_api): |
| 58 | results = [] |
| 59 | results.extend(_CommonChecks(input_api, output_api)) |
| 60 | return results |