blob: 3721d61492b7c61a561a0f641959e99cad0289f5 [file] [log] [blame]
Ryan Tseng85ec17e2018-09-06 17:10:05 -07001# 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
btolsch9ba23712019-04-18 16:36:55 -07006def _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
btolsch333aecd2019-04-18 16:21:23 -070037def _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'))
btolsch9ba23712019-04-18 16:36:55 -070045 results.extend(_CheckDeps(input_api, output_api))
btolsch333aecd2019-04-18 16:21:23 -070046 return results
47
48
Ryan Tseng85ec17e2018-09-06 17:10:05 -070049def CheckChangeOnUpload(input_api, output_api):
btolsch333aecd2019-04-18 16:21:23 -070050 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
57def CheckChangeOnCommit(input_api, output_api):
58 results = []
59 results.extend(_CommonChecks(input_api, output_api))
60 return results