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