blob: 40be7b78c76811633a9e919dc8fefad46fb7f12a [file] [log] [blame]
[email protected]ca8d19842009-02-19 16:33:121#!/usr/bin/python
2# Copyright (c) 2009 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Top-level presubmit script for Chromium.
7
8See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
9details on the presubmit API built into gcl.
10"""
11
[email protected]d5ed8572009-03-15 19:45:5312# Files with these extensions will be considered source files.
[email protected]33478702009-03-05 14:03:1413SOURCE_FILE_EXTENSIONS = [
14 '.c', '.cc', '.cpp', '.h', '.m', '.mm', '.py', '.mk', '.am', '.json',
15]
16EXCLUDED_PATHS = [
17 r"breakpad[\\\/].*",
18 r"chrome[\\\/]Debug[\\\/].*",
[email protected]33478702009-03-05 14:03:1419 r"chrome[\\\/]Release[\\\/].*",
[email protected]04a40f842009-04-01 20:13:0420 r"sconsbuild[\\\/].*",
[email protected]33478702009-03-05 14:03:1421 r"xcodebuild[\\\/].*",
22 r"skia[\\\/].*",
23 r".*third_party[\\\/].*",
24 r"v8[\\\/].*",
25]
[email protected]ca8d19842009-02-19 16:33:1226
27def ReadFile(path):
28 """Given a path, returns the full contents of the file.
[email protected]33478702009-03-05 14:03:1429
[email protected]ca8d19842009-02-19 16:33:1230 Reads files in binary format.
31 """
32 fo = open(path, 'rb')
33 try:
34 contents = fo.read()
35 finally:
36 fo.close()
37 return contents
38
39
[email protected]ca8d19842009-02-19 16:33:1240def CheckChangeOnUpload(input_api, output_api):
[email protected]b4971ce2009-03-05 16:14:5541 # TODO(maruel): max_cols is temporarily disabled. Reenable once the source
42 # tree is in better shape.
[email protected]fe5f57c52009-06-05 14:25:5443 results = []
44 results.extend(LocalChecks(input_api, output_api, max_cols=0))
45 results.extend(input_api.canned_checks.CheckChangeHasBugField(input_api,
46 output_api))
47 results.extend(input_api.canned_checks.CheckChangeHasTestField(input_api,
48 output_api))
49 return results
[email protected]ca8d19842009-02-19 16:33:1250
51
52def CheckChangeOnCommit(input_api, output_api):
[email protected]fe5f57c52009-06-05 14:25:5453 results = []
[email protected]b4971ce2009-03-05 16:14:5554 # TODO(maruel): max_cols is temporarily disabled. Reenable once the source
55 # tree is in better shape.
[email protected]fe5f57c52009-06-05 14:25:5456 results.extend(LocalChecks(input_api, output_api, max_cols=0))
57 results.extend(input_api.canned_checks.CheckChangeHasBugField(input_api,
58 output_api))
59 results.extend(input_api.canned_checks.CheckChangeHasTestField(input_api,
60 output_api))
61 # Make sure the tree is 'open'.
62 results.extend(input_api.canned_checks.CheckTreeIsOpen(
63 input_api, output_api,
64 'http://chromium-status.appspot.com/status', '0'
65 ))
66 return results
[email protected]ca8d19842009-02-19 16:33:1267
68
[email protected]33478702009-03-05 14:03:1469def LocalChecks(input_api, output_api, max_cols=80):
70 """Reports an error if for any source file in SOURCE_FILE_EXTENSIONS:
71 - uses CR (or CRLF)
72 - contains a TAB
73 - has a line that ends with whitespace
74 - contains a line >|max_cols| cols unless |max_cols| is 0.
[email protected]2fcccc72009-03-10 13:55:0975 - File does not end in a newline, or ends in more than one.
[email protected]33478702009-03-05 14:03:1476
77 Note that the whole file is checked, not only the changes.
[email protected]ca8d19842009-02-19 16:33:1278 """
[email protected]db24f362009-03-10 17:13:4279 C_SOURCE_FILE_EXTENSIONS = ('.c', '.cc', '.cpp', '.h', '.inl')
[email protected]ca8d19842009-02-19 16:33:1280 cr_files = []
[email protected]2fcccc72009-03-10 13:55:0981 eof_files = []
[email protected]ca8d19842009-02-19 16:33:1282 results = []
[email protected]33478702009-03-05 14:03:1483 excluded_paths = [input_api.re.compile(x) for x in EXCLUDED_PATHS]
[email protected]933721172009-03-13 04:35:3784 files = input_api.AffectedFiles(include_deletes=False)
[email protected]33478702009-03-05 14:03:1485 for f in files:
[email protected]ca8d19842009-02-19 16:33:1286 path = f.LocalPath()
[email protected]33478702009-03-05 14:03:1487 root, ext = input_api.os_path.splitext(path)
88 # Look for unsupported extensions.
89 if not ext in SOURCE_FILE_EXTENSIONS:
90 continue
91 # Look for excluded paths.
92 found = False
93 for item in excluded_paths:
94 if item.match(path):
95 found = True
96 break
97 if found:
98 continue
99
100 # Need to read the file ourselves since AffectedFile.NewContents()
101 # will normalize line endings.
[email protected]085bdc922009-06-04 00:00:42102 contents = ReadFile(f.AbsoluteLocalPath())
[email protected]33478702009-03-05 14:03:14103 if '\r' in contents:
104 cr_files.append(path)
105
[email protected]2fcccc72009-03-10 13:55:09106 # Check that the file ends in one and only one newline character.
107 if len(contents) > 0 and (contents[-1:] != "\n" or contents[-2:-1] == "\n"):
108 eof_files.append(path)
109
[email protected]33478702009-03-05 14:03:14110 local_errors = []
[email protected]9e930592009-04-26 01:36:48111 # Remove end of line character.
[email protected]33478702009-03-05 14:03:14112 lines = contents.splitlines()
113 line_num = 1
114 for line in lines:
115 if line.endswith(' '):
116 local_errors.append(output_api.PresubmitError(
117 '%s, line %s ends with whitespaces.' %
118 (path, line_num)))
[email protected]db24f362009-03-10 17:13:42119 # Accept lines with http://, https:// and C #define/#pragma/#include to
120 # exceed the max_cols rule.
121 if (max_cols and
122 len(line) > max_cols and
123 not 'http://' in line and
124 not 'https://' in line and
125 not (line[0] == '#' and ext in C_SOURCE_FILE_EXTENSIONS)):
[email protected]33478702009-03-05 14:03:14126 local_errors.append(output_api.PresubmitError(
127 '%s, line %s has %s chars, please reduce to %d chars.' %
128 (path, line_num, len(line), max_cols)))
129 if '\t' in line:
130 local_errors.append(output_api.PresubmitError(
131 "%s, line %s contains a tab character." %
132 (path, line_num)))
133 line_num += 1
134 # Just show the first 5 errors.
135 if len(local_errors) == 6:
136 local_errors.pop()
137 local_errors.append(output_api.PresubmitError("... and more."))
138 break
139 results.extend(local_errors)
140
[email protected]ca8d19842009-02-19 16:33:12141 if cr_files:
142 results.append(output_api.PresubmitError(
143 'Found CR (or CRLF) line ending in these files, please use only LF:',
144 items=cr_files))
[email protected]2fcccc72009-03-10 13:55:09145 if eof_files:
146 results.append(output_api.PresubmitError(
147 'These files should end in one (and only one) newline character:',
148 items=eof_files))
[email protected]ca8d19842009-02-19 16:33:12149 return results