Nathaniel Manista | ae4fbcd | 2015-09-23 16:29:44 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 2 | |
Jan Tattermusch | 9c7aefb | 2017-06-07 22:43:39 +0200 | [diff] [blame] | 3 | # Copyright 2015 gRPC authors. |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 4 | # |
Jan Tattermusch | 9c7aefb | 2017-06-07 22:43:39 +0200 | [diff] [blame] | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 8 | # |
Jan Tattermusch | 9c7aefb | 2017-06-07 22:43:39 +0200 | [diff] [blame] | 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 10 | # |
Jan Tattermusch | 9c7aefb | 2017-06-07 22:43:39 +0200 | [diff] [blame] | 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 16 | |
Craig Tiller | 94640a3 | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 17 | import argparse |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 18 | import datetime |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 19 | import os |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 20 | import re |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 21 | import sys |
| 22 | import subprocess |
| 23 | |
| 24 | # find our home |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 25 | ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..')) |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 26 | os.chdir(ROOT) |
| 27 | |
Craig Tiller | 94640a3 | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 28 | # parse command line |
| 29 | argp = argparse.ArgumentParser(description='copyright checker') |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 30 | argp.add_argument( |
| 31 | '-o', '--output', default='details', choices=['list', 'details']) |
| 32 | argp.add_argument('-s', '--skips', default=0, action='store_const', const=1) |
| 33 | argp.add_argument('-a', '--ancient', default=0, action='store_const', const=1) |
| 34 | argp.add_argument('--precommit', default=False, action='store_true') |
Craig Tiller | 94640a3 | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 35 | args = argp.parse_args() |
| 36 | |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 37 | # open the license text |
Jan Tattermusch | 312ea4a | 2017-06-07 06:44:47 +0200 | [diff] [blame] | 38 | with open('NOTICE.txt') as f: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 39 | LICENSE_NOTICE = f.read().splitlines() |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 40 | |
| 41 | # license format by file extension |
| 42 | # key is the file extension, value is a format string |
| 43 | # that given a line of license text, returns what should |
| 44 | # be in the file |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 45 | LICENSE_PREFIX = { |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 46 | '.bat': r'@rem\s*', |
| 47 | '.c': r'\s*(?://|\*)\s*', |
| 48 | '.cc': r'\s*(?://|\*)\s*', |
| 49 | '.h': r'\s*(?://|\*)\s*', |
| 50 | '.m': r'\s*\*\s*', |
Muxi Yan | 44c4c34 | 2018-01-05 17:25:54 -0800 | [diff] [blame] | 51 | '.mm': r'\s*\*\s*', |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 52 | '.php': r'\s*\*\s*', |
| 53 | '.js': r'\s*\*\s*', |
| 54 | '.py': r'#\s*', |
| 55 | '.pyx': r'#\s*', |
| 56 | '.pxd': r'#\s*', |
| 57 | '.pxi': r'#\s*', |
| 58 | '.rb': r'#\s*', |
| 59 | '.sh': r'#\s*', |
| 60 | '.proto': r'//\s*', |
| 61 | '.cs': r'//\s*', |
| 62 | '.mak': r'#\s*', |
| 63 | 'Makefile': r'#\s*', |
| 64 | 'Dockerfile': r'#\s*', |
| 65 | 'BUILD': r'#\s*', |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Nathaniel Manista | d0386f9 | 2016-12-29 17:42:03 +0000 | [diff] [blame] | 68 | _EXEMPT = frozenset(( |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 69 | # Generated protocol compiler output. |
| 70 | 'examples/python/helloworld/helloworld_pb2.py', |
| 71 | 'examples/python/helloworld/helloworld_pb2_grpc.py', |
| 72 | 'examples/python/multiplex/helloworld_pb2.py', |
| 73 | 'examples/python/multiplex/helloworld_pb2_grpc.py', |
| 74 | 'examples/python/multiplex/route_guide_pb2.py', |
| 75 | 'examples/python/multiplex/route_guide_pb2_grpc.py', |
| 76 | 'examples/python/route_guide/route_guide_pb2.py', |
| 77 | 'examples/python/route_guide/route_guide_pb2_grpc.py', |
| 78 | 'src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h', |
| 79 | 'src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.c', |
David Garcia Quintas | f3955ba | 2018-06-21 15:15:10 -0700 | [diff] [blame] | 80 | 'src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.h', |
| 81 | 'src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.c', |
| 82 | 'src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.h', |
| 83 | 'src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.c', |
Yihua Zhang | 04fb58e | 2018-03-08 06:49:24 -0800 | [diff] [blame] | 84 | 'src/core/tsi/alts/handshaker/altscontext.pb.h', |
| 85 | 'src/core/tsi/alts/handshaker/altscontext.pb.c', |
| 86 | 'src/core/tsi/alts/handshaker/handshaker.pb.h', |
| 87 | 'src/core/tsi/alts/handshaker/handshaker.pb.c', |
| 88 | 'src/core/tsi/alts/handshaker/transport_security_common.pb.h', |
| 89 | 'src/core/tsi/alts/handshaker/transport_security_common.pb.c', |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 90 | 'src/cpp/server/health/health.pb.h', |
| 91 | 'src/cpp/server/health/health.pb.c', |
Nathaniel Manista | d0386f9 | 2016-12-29 17:42:03 +0000 | [diff] [blame] | 92 | |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 93 | # An older file originally from outside gRPC. |
| 94 | 'src/php/tests/bootstrap.php', |
| 95 | # census.proto copied from github |
| 96 | 'tools/grpcz/census.proto', |
| 97 | # status.proto copied from googleapis |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 98 | 'src/proto/grpc/status/status.proto', |
Eric Gribkoff | a32a4c5 | 2018-01-25 13:16:41 -0800 | [diff] [blame] | 99 | |
Eric Gribkoff | f9fb3eb | 2018-04-03 16:21:37 -0700 | [diff] [blame] | 100 | # Gradle wrappers used to build for Android |
Eric Gribkoff | a32a4c5 | 2018-01-25 13:16:41 -0800 | [diff] [blame] | 101 | 'examples/android/helloworld/gradlew.bat', |
Eric Gribkoff | f9fb3eb | 2018-04-03 16:21:37 -0700 | [diff] [blame] | 102 | 'src/android/test/interop/gradlew.bat', |
Jan Tattermusch | 85b0052 | 2018-08-01 10:26:00 +0200 | [diff] [blame] | 103 | |
| 104 | # Designer-generated source |
| 105 | 'examples/csharp/HelloworldXamarin/Droid/Resources/Resource.designer.cs', |
| 106 | 'examples/csharp/HelloworldXamarin/iOS/ViewController.designer.cs', |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 107 | )) |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 108 | |
Carl Mastrangelo | ee7c776 | 2018-05-03 10:25:24 -0700 | [diff] [blame] | 109 | RE_YEAR = r'Copyright (?P<first_year>[0-9]+\-)?(?P<last_year>[0-9]+) ([Tt]he )?gRPC [Aa]uthors(\.|)' |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 110 | RE_LICENSE = dict( |
| 111 | (k, r'\n'.join(LICENSE_PREFIX[k] + |
| 112 | (RE_YEAR if re.search(RE_YEAR, line) else re.escape(line)) |
| 113 | for line in LICENSE_NOTICE)) |
| 114 | for k, v in LICENSE_PREFIX.iteritems()) |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 115 | |
murgatroid99 | f656f18 | 2016-02-09 10:56:25 -0800 | [diff] [blame] | 116 | if args.precommit: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 117 | FILE_LIST_COMMAND = 'git status -z | grep -Poz \'(?<=^[MARC][MARCD ] )[^\s]+\'' |
murgatroid99 | f656f18 | 2016-02-09 10:56:25 -0800 | [diff] [blame] | 118 | else: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 119 | FILE_LIST_COMMAND = 'git ls-tree -r --name-only -r HEAD | ' \ |
| 120 | 'grep -v ^third_party/ |' \ |
| 121 | 'grep -v "\(ares_config.h\|ares_build.h\)"' |
| 122 | |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 123 | |
| 124 | def load(name): |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 125 | with open(name) as f: |
| 126 | return f.read() |
| 127 | |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 128 | |
David Garcia Quintas | fa58786 | 2016-01-12 15:31:40 -0800 | [diff] [blame] | 129 | def save(name, text): |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 130 | with open(name, 'w') as f: |
| 131 | f.write(text) |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 132 | |
Jan Tattermusch | d2da5aa | 2017-06-19 10:05:18 +0200 | [diff] [blame] | 133 | |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 134 | assert (re.search(RE_LICENSE['Makefile'], load('Makefile'))) |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 135 | |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 136 | |
Craig Tiller | 94640a3 | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 137 | def log(cond, why, filename): |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 138 | if not cond: return |
| 139 | if args.output == 'details': |
| 140 | print '%s: %s' % (why, filename) |
| 141 | else: |
| 142 | print filename |
Craig Tiller | 94640a3 | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 143 | |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 144 | |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 145 | # scan files, validate the text |
Craig Tiller | 750d3ed | 2016-01-12 09:38:25 -0800 | [diff] [blame] | 146 | ok = True |
murgatroid99 | c58a781 | 2016-02-09 16:33:17 -0800 | [diff] [blame] | 147 | filename_list = [] |
| 148 | try: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 149 | filename_list = subprocess.check_output( |
| 150 | FILE_LIST_COMMAND, shell=True).splitlines() |
murgatroid99 | c58a781 | 2016-02-09 16:33:17 -0800 | [diff] [blame] | 151 | except subprocess.CalledProcessError: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 152 | sys.exit(0) |
murgatroid99 | c58a781 | 2016-02-09 16:33:17 -0800 | [diff] [blame] | 153 | |
| 154 | for filename in filename_list: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 155 | if filename in _EXEMPT: |
| 156 | continue |
| 157 | ext = os.path.splitext(filename)[1] |
| 158 | base = os.path.basename(filename) |
| 159 | if ext in RE_LICENSE: |
| 160 | re_license = RE_LICENSE[ext] |
| 161 | elif base in RE_LICENSE: |
| 162 | re_license = RE_LICENSE[base] |
| 163 | else: |
| 164 | log(args.skips, 'skip', filename) |
| 165 | continue |
| 166 | try: |
| 167 | text = load(filename) |
| 168 | except: |
| 169 | continue |
| 170 | m = re.search(re_license, text) |
| 171 | if m: |
| 172 | pass |
Matt Kwong | a110c74 | 2017-09-22 14:31:22 -0700 | [diff] [blame] | 173 | elif 'DO NOT EDIT' not in text and filename not in [ |
| 174 | 'src/boringssl/err_data.c', 'src/boringssl/crypto_test_data.cc' |
| 175 | ]: |
ncteisen | 7a2be20 | 2017-12-11 16:49:19 -0800 | [diff] [blame] | 176 | log(1, 'copyright missing', filename) |
| 177 | ok = False |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 178 | |
| 179 | sys.exit(0 if ok else 1) |