andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 1 | # Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 2 | # |
| 3 | # Use of this source code is governed by a BSD-style license |
| 4 | # that can be found in the LICENSE file in the root of the source |
| 5 | # tree. An additional intellectual property rights grant can be found |
| 6 | # in the file PATENTS. All contributing project authors may |
| 7 | # be found in the AUTHORS file in the root of the source tree. |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 8 | |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 9 | import os.path |
| 10 | |
| 11 | # All folders in LINT_FOLDERS will be scanned by cpplint by the presubmit |
| 12 | # script. Note that subfolders are not included. |
| 13 | LINT_FOLDERS = ['src/video_engine'] |
| 14 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 15 | def _LicenseHeader(input_api): |
| 16 | """Returns the license header regexp.""" |
kjellander@webrtc.org | 6307dbf | 2012-08-31 07:07:11 +0000 | [diff] [blame] | 17 | # Accept any year number from 2011 to the current year |
| 18 | current_year = int(input_api.time.strftime('%Y')) |
| 19 | allowed_years = (str(s) for s in reversed(xrange(2011, current_year + 1))) |
| 20 | years_re = '(' + '|'.join(allowed_years) + ')' |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 21 | license_header = ( |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 22 | r'.*? Copyright \(c\) %(year)s The WebRTC project authors\. ' |
| 23 | r'All Rights Reserved\.\n' |
| 24 | r'.*?\n' |
| 25 | r'.*? Use of this source code is governed by a BSD-style license\n' |
| 26 | r'.*? that can be found in the LICENSE file in the root of the source\n' |
| 27 | r'.*? tree\. An additional intellectual property rights grant can be ' |
| 28 | r'found\n' |
| 29 | r'.*? in the file PATENTS\. All contributing project authors may\n' |
| 30 | r'.*? be found in the AUTHORS file in the root of the source tree\.\n' |
| 31 | ) % { |
kjellander@webrtc.org | 6307dbf | 2012-08-31 07:07:11 +0000 | [diff] [blame] | 32 | 'year': years_re, |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 33 | } |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 34 | return license_header |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 35 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 36 | def _CheckNoIOStreamInHeaders(input_api, output_api): |
| 37 | """Checks to make sure no .h files include <iostream>.""" |
| 38 | files = [] |
| 39 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 40 | input_api.re.MULTILINE) |
| 41 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 42 | if not f.LocalPath().endswith('.h'): |
| 43 | continue |
| 44 | contents = input_api.ReadFile(f) |
| 45 | if pattern.search(contents): |
| 46 | files.append(f) |
| 47 | |
| 48 | if len(files): |
| 49 | return [ output_api.PresubmitError( |
| 50 | 'Do not #include <iostream> in header files, since it inserts static ' + |
| 51 | 'initialization into every file including the header. Instead, ' + |
| 52 | '#include <ostream>. See http://crbug.com/94794', |
| 53 | files) ] |
| 54 | return [] |
| 55 | |
| 56 | def _CheckNoFRIEND_TEST(input_api, output_api): |
| 57 | """Make sure that gtest's FRIEND_TEST() macro is not used, the |
| 58 | FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be |
| 59 | used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes.""" |
| 60 | problems = [] |
| 61 | |
| 62 | file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h')) |
| 63 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 64 | for line_num, line in f.ChangedContents(): |
| 65 | if 'FRIEND_TEST(' in line: |
| 66 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 67 | |
| 68 | if not problems: |
| 69 | return [] |
| 70 | return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use ' |
| 71 | 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and ' |
| 72 | 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))] |
| 73 | |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 74 | def _IsLintWhitelisted(file_name): |
| 75 | """ Checks if a file is whitelisted for lint check.""" |
| 76 | # TODO(mflodman) Include subfolders in the check. |
| 77 | return (os.path.dirname(file_name) in LINT_FOLDERS) |
| 78 | |
| 79 | def _CheckApprovedFilesLintClean(input_api, output_api, |
| 80 | source_file_filter=None): |
| 81 | """Checks that all new or whitelisted .cc and .h files pass cpplint.py. |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 82 | This check is based on _CheckChangeLintsClean in |
| 83 | depot_tools/presubmit_canned_checks.py but has less filters and only checks |
| 84 | added files.""" |
| 85 | result = [] |
| 86 | |
| 87 | # Initialize cpplint. |
| 88 | import cpplint |
| 89 | # Access to a protected member _XX of a client class |
| 90 | # pylint: disable=W0212 |
| 91 | cpplint._cpplint_state.ResetErrorCounts() |
| 92 | |
| 93 | # Justifications for each filter: |
| 94 | # |
| 95 | # - build/header_guard : WebRTC coding style says they should be prefixed |
| 96 | # with WEBRTC_, which is not possible to configure in |
| 97 | # cpplint.py. |
| 98 | cpplint._SetFilters('-build/header_guard') |
| 99 | |
| 100 | # Use the strictest verbosity level for cpplint.py (level 1) which is the |
| 101 | # default when running cpplint.py from command line. |
| 102 | # To make it possible to work with not-yet-converted code, we're only applying |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 103 | # it to new (or moved/renamed) files and files listed in LINT_FOLDERS. |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 104 | verbosity_level = 1 |
| 105 | files = [] |
| 106 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 107 | # Note that moved/renamed files also count as added for svn. |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 108 | if (f.Action() == 'A' or _IsLintWhitelisted(f.LocalPath())): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 109 | files.append(f.AbsoluteLocalPath()) |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 110 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 111 | for file_name in files: |
| 112 | cpplint.ProcessFile(file_name, verbosity_level) |
| 113 | |
| 114 | if cpplint._cpplint_state.error_count > 0: |
| 115 | if input_api.is_committing: |
| 116 | # TODO(kjellander): Change back to PresubmitError below when we're |
| 117 | # confident with the lint settings. |
| 118 | res_type = output_api.PresubmitPromptWarning |
| 119 | else: |
| 120 | res_type = output_api.PresubmitPromptWarning |
| 121 | result = [res_type('Changelist failed cpplint.py check.')] |
| 122 | |
| 123 | return result |
| 124 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 125 | def _CommonChecks(input_api, output_api): |
| 126 | """Checks common to both upload and commit.""" |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 127 | # TODO(kjellander): Use presubmit_canned_checks.PanProjectChecks too. |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 128 | results = [] |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 129 | results.extend(input_api.canned_checks.CheckLongLines( |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 130 | input_api, output_api)) |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 131 | results.extend(input_api.canned_checks.CheckChangeHasNoTabs( |
| 132 | input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 133 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
| 134 | input_api, output_api)) |
| 135 | results.extend(input_api.canned_checks.CheckChangeTodoHasOwner( |
| 136 | input_api, output_api)) |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 137 | results.extend(_CheckApprovedFilesLintClean(input_api, output_api)) |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 138 | results.extend(input_api.canned_checks.CheckLicense( |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 139 | input_api, output_api, _LicenseHeader(input_api))) |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 140 | results.extend(_CheckNoIOStreamInHeaders(input_api, output_api)) |
| 141 | results.extend(_CheckNoFRIEND_TEST(input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 142 | return results |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 143 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 144 | def CheckChangeOnUpload(input_api, output_api): |
| 145 | results = [] |
| 146 | results.extend(_CommonChecks(input_api, output_api)) |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 147 | return results |
| 148 | |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 149 | def CheckChangeOnCommit(input_api, output_api): |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 150 | results = [] |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 151 | results.extend(_CommonChecks(input_api, output_api)) |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 152 | results.extend(input_api.canned_checks.CheckOwners(input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 153 | results.extend(input_api.canned_checks.CheckChangeWasUploaded( |
| 154 | input_api, output_api)) |
| 155 | results.extend(input_api.canned_checks.CheckChangeHasDescription( |
| 156 | input_api, output_api)) |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 157 | results.extend(input_api.canned_checks.CheckChangeHasBugField( |
| 158 | input_api, output_api)) |
| 159 | results.extend(input_api.canned_checks.CheckChangeHasTestField( |
| 160 | input_api, output_api)) |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 161 | return results |
kjellander@webrtc.org | f3ffcce | 2012-10-31 14:52:21 +0000 | [diff] [blame^] | 162 | |
| 163 | def GetPreferredTrySlaves(project, change): |
| 164 | files = change.LocalPaths() |
| 165 | if not files: |
| 166 | return [] |
| 167 | # Default trybots to run jobs at (if --bot is not specified). |
| 168 | return ['win', 'win_rel', 'mac', 'mac_rel', 'linux', 'linux_rel', |
| 169 | 'android', 'android_ndk'] |