blob: 77f602ad1fc5368c3b8c526c45c8f05b55e577aa [file] [log] [blame]
Nathaniel Manistaae4fbcd2015-09-23 16:29:44 +00001#!/usr/bin/env python2.7
Craig Tillerc2c79212015-02-16 12:00:01 -08002
Jan Tattermusch9c7aefb2017-06-07 22:43:39 +02003# Copyright 2015 gRPC authors.
Craig Tillerc2c79212015-02-16 12:00:01 -08004#
Jan Tattermusch9c7aefb2017-06-07 22:43:39 +02005# 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 Tillerc2c79212015-02-16 12:00:01 -08008#
Jan Tattermusch9c7aefb2017-06-07 22:43:39 +02009# http://www.apache.org/licenses/LICENSE-2.0
Craig Tillerc2c79212015-02-16 12:00:01 -080010#
Jan Tattermusch9c7aefb2017-06-07 22:43:39 +020011# 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 Tillerc2c79212015-02-16 12:00:01 -080016
Craig Tiller94640a32015-02-17 20:42:05 -080017import argparse
Craig Tillerde3da742016-01-04 14:41:25 -080018import datetime
Craig Tiller1a61b172015-02-16 11:53:47 -080019import os
Craig Tillerde3da742016-01-04 14:41:25 -080020import re
Craig Tiller1a61b172015-02-16 11:53:47 -080021import sys
22import subprocess
23
24# find our home
ncteisen7a2be202017-12-11 16:49:19 -080025ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
Craig Tiller1a61b172015-02-16 11:53:47 -080026os.chdir(ROOT)
27
Craig Tiller94640a32015-02-17 20:42:05 -080028# parse command line
29argp = argparse.ArgumentParser(description='copyright checker')
ncteisen7a2be202017-12-11 16:49:19 -080030argp.add_argument(
31 '-o', '--output', default='details', choices=['list', 'details'])
32argp.add_argument('-s', '--skips', default=0, action='store_const', const=1)
33argp.add_argument('-a', '--ancient', default=0, action='store_const', const=1)
34argp.add_argument('--precommit', default=False, action='store_true')
Craig Tiller94640a32015-02-17 20:42:05 -080035args = argp.parse_args()
36
Craig Tiller1a61b172015-02-16 11:53:47 -080037# open the license text
Jan Tattermusch312ea4a2017-06-07 06:44:47 +020038with open('NOTICE.txt') as f:
ncteisen7a2be202017-12-11 16:49:19 -080039 LICENSE_NOTICE = f.read().splitlines()
Craig Tiller1a61b172015-02-16 11:53:47 -080040
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 Tillerde3da742016-01-04 14:41:25 -080045LICENSE_PREFIX = {
ncteisen7a2be202017-12-11 16:49:19 -080046 '.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 Yan44c4c342018-01-05 17:25:54 -080051 '.mm': r'\s*\*\s*',
ncteisen7a2be202017-12-11 16:49:19 -080052 '.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 Tiller1a61b172015-02-16 11:53:47 -080066}
67
Nathaniel Manistad0386f92016-12-29 17:42:03 +000068_EXEMPT = frozenset((
ncteisen7a2be202017-12-11 16:49:19 -080069 # 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 Quintasf3955ba2018-06-21 15:15:10 -070080 '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 Zhang04fb58e2018-03-08 06:49:24 -080084 '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',
ncteisen7a2be202017-12-11 16:49:19 -080090 'src/cpp/server/health/health.pb.h',
91 'src/cpp/server/health/health.pb.c',
Nathaniel Manistad0386f92016-12-29 17:42:03 +000092
ncteisen7a2be202017-12-11 16:49:19 -080093 # 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 Afshari87cd9942018-01-02 14:40:00 -080098 'src/proto/grpc/status/status.proto',
Eric Gribkoffa32a4c52018-01-25 13:16:41 -080099
Eric Gribkofff9fb3eb2018-04-03 16:21:37 -0700100 # Gradle wrappers used to build for Android
Eric Gribkoffa32a4c52018-01-25 13:16:41 -0800101 'examples/android/helloworld/gradlew.bat',
Eric Gribkofff9fb3eb2018-04-03 16:21:37 -0700102 'src/android/test/interop/gradlew.bat',
Jan Tattermusch85b00522018-08-01 10:26:00 +0200103
104 # Designer-generated source
105 'examples/csharp/HelloworldXamarin/Droid/Resources/Resource.designer.cs',
106 'examples/csharp/HelloworldXamarin/iOS/ViewController.designer.cs',
Mehrdad Afshari87cd9942018-01-02 14:40:00 -0800107))
Craig Tiller1a61b172015-02-16 11:53:47 -0800108
Carl Mastrangeloee7c7762018-05-03 10:25:24 -0700109RE_YEAR = r'Copyright (?P<first_year>[0-9]+\-)?(?P<last_year>[0-9]+) ([Tt]he )?gRPC [Aa]uthors(\.|)'
Mehrdad Afshari87cd9942018-01-02 14:40:00 -0800110RE_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 Tillerde3da742016-01-04 14:41:25 -0800115
murgatroid99f656f182016-02-09 10:56:25 -0800116if args.precommit:
ncteisen7a2be202017-12-11 16:49:19 -0800117 FILE_LIST_COMMAND = 'git status -z | grep -Poz \'(?<=^[MARC][MARCD ] )[^\s]+\''
murgatroid99f656f182016-02-09 10:56:25 -0800118else:
ncteisen7a2be202017-12-11 16:49:19 -0800119 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 Tillerde3da742016-01-04 14:41:25 -0800123
124def load(name):
ncteisen7a2be202017-12-11 16:49:19 -0800125 with open(name) as f:
126 return f.read()
127
Craig Tillerde3da742016-01-04 14:41:25 -0800128
David Garcia Quintasfa587862016-01-12 15:31:40 -0800129def save(name, text):
ncteisen7a2be202017-12-11 16:49:19 -0800130 with open(name, 'w') as f:
131 f.write(text)
Craig Tillerde3da742016-01-04 14:41:25 -0800132
Jan Tattermuschd2da5aa2017-06-19 10:05:18 +0200133
ncteisen7a2be202017-12-11 16:49:19 -0800134assert (re.search(RE_LICENSE['Makefile'], load('Makefile')))
Craig Tillerde3da742016-01-04 14:41:25 -0800135
Craig Tiller1a61b172015-02-16 11:53:47 -0800136
Craig Tiller94640a32015-02-17 20:42:05 -0800137def log(cond, why, filename):
ncteisen7a2be202017-12-11 16:49:19 -0800138 if not cond: return
139 if args.output == 'details':
140 print '%s: %s' % (why, filename)
141 else:
142 print filename
Craig Tiller94640a32015-02-17 20:42:05 -0800143
Craig Tillerde3da742016-01-04 14:41:25 -0800144
Craig Tiller1a61b172015-02-16 11:53:47 -0800145# scan files, validate the text
Craig Tiller750d3ed2016-01-12 09:38:25 -0800146ok = True
murgatroid99c58a7812016-02-09 16:33:17 -0800147filename_list = []
148try:
ncteisen7a2be202017-12-11 16:49:19 -0800149 filename_list = subprocess.check_output(
150 FILE_LIST_COMMAND, shell=True).splitlines()
murgatroid99c58a7812016-02-09 16:33:17 -0800151except subprocess.CalledProcessError:
ncteisen7a2be202017-12-11 16:49:19 -0800152 sys.exit(0)
murgatroid99c58a7812016-02-09 16:33:17 -0800153
154for filename in filename_list:
ncteisen7a2be202017-12-11 16:49:19 -0800155 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 Kwonga110c742017-09-22 14:31:22 -0700173 elif 'DO NOT EDIT' not in text and filename not in [
174 'src/boringssl/err_data.c', 'src/boringssl/crypto_test_data.cc'
175 ]:
ncteisen7a2be202017-12-11 16:49:19 -0800176 log(1, 'copyright missing', filename)
177 ok = False
Craig Tillerde3da742016-01-04 14:41:25 -0800178
179sys.exit(0 if ok else 1)