blob: 55cec938c9034c173da4fc76871fef0498b31b27 [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',
80 'src/cpp/server/health/health.pb.h',
81 'src/cpp/server/health/health.pb.c',
Nathaniel Manistad0386f92016-12-29 17:42:03 +000082
ncteisen7a2be202017-12-11 16:49:19 -080083 # An older file originally from outside gRPC.
84 'src/php/tests/bootstrap.php',
85 # census.proto copied from github
86 'tools/grpcz/census.proto',
87 # status.proto copied from googleapis
Mehrdad Afshari87cd9942018-01-02 14:40:00 -080088 'src/proto/grpc/status/status.proto',
89))
Craig Tiller1a61b172015-02-16 11:53:47 -080090
Jan Tattermusch312ea4a2017-06-07 06:44:47 +020091RE_YEAR = r'Copyright (?P<first_year>[0-9]+\-)?(?P<last_year>[0-9]+) gRPC authors.'
Mehrdad Afshari87cd9942018-01-02 14:40:00 -080092RE_LICENSE = dict(
93 (k, r'\n'.join(LICENSE_PREFIX[k] +
94 (RE_YEAR if re.search(RE_YEAR, line) else re.escape(line))
95 for line in LICENSE_NOTICE))
96 for k, v in LICENSE_PREFIX.iteritems())
Craig Tillerde3da742016-01-04 14:41:25 -080097
murgatroid99f656f182016-02-09 10:56:25 -080098if args.precommit:
ncteisen7a2be202017-12-11 16:49:19 -080099 FILE_LIST_COMMAND = 'git status -z | grep -Poz \'(?<=^[MARC][MARCD ] )[^\s]+\''
murgatroid99f656f182016-02-09 10:56:25 -0800100else:
ncteisen7a2be202017-12-11 16:49:19 -0800101 FILE_LIST_COMMAND = 'git ls-tree -r --name-only -r HEAD | ' \
102 'grep -v ^third_party/ |' \
103 'grep -v "\(ares_config.h\|ares_build.h\)"'
104
Craig Tillerde3da742016-01-04 14:41:25 -0800105
106def load(name):
ncteisen7a2be202017-12-11 16:49:19 -0800107 with open(name) as f:
108 return f.read()
109
Craig Tillerde3da742016-01-04 14:41:25 -0800110
David Garcia Quintasfa587862016-01-12 15:31:40 -0800111def save(name, text):
ncteisen7a2be202017-12-11 16:49:19 -0800112 with open(name, 'w') as f:
113 f.write(text)
Craig Tillerde3da742016-01-04 14:41:25 -0800114
Jan Tattermuschd2da5aa2017-06-19 10:05:18 +0200115
ncteisen7a2be202017-12-11 16:49:19 -0800116assert (re.search(RE_LICENSE['Makefile'], load('Makefile')))
Craig Tillerde3da742016-01-04 14:41:25 -0800117
Craig Tiller1a61b172015-02-16 11:53:47 -0800118
Craig Tiller94640a32015-02-17 20:42:05 -0800119def log(cond, why, filename):
ncteisen7a2be202017-12-11 16:49:19 -0800120 if not cond: return
121 if args.output == 'details':
122 print '%s: %s' % (why, filename)
123 else:
124 print filename
Craig Tiller94640a32015-02-17 20:42:05 -0800125
Craig Tillerde3da742016-01-04 14:41:25 -0800126
Craig Tiller1a61b172015-02-16 11:53:47 -0800127# scan files, validate the text
Craig Tiller750d3ed2016-01-12 09:38:25 -0800128ok = True
murgatroid99c58a7812016-02-09 16:33:17 -0800129filename_list = []
130try:
ncteisen7a2be202017-12-11 16:49:19 -0800131 filename_list = subprocess.check_output(
132 FILE_LIST_COMMAND, shell=True).splitlines()
murgatroid99c58a7812016-02-09 16:33:17 -0800133except subprocess.CalledProcessError:
ncteisen7a2be202017-12-11 16:49:19 -0800134 sys.exit(0)
murgatroid99c58a7812016-02-09 16:33:17 -0800135
136for filename in filename_list:
ncteisen7a2be202017-12-11 16:49:19 -0800137 if filename in _EXEMPT:
138 continue
139 ext = os.path.splitext(filename)[1]
140 base = os.path.basename(filename)
141 if ext in RE_LICENSE:
142 re_license = RE_LICENSE[ext]
143 elif base in RE_LICENSE:
144 re_license = RE_LICENSE[base]
145 else:
146 log(args.skips, 'skip', filename)
147 continue
148 try:
149 text = load(filename)
150 except:
151 continue
152 m = re.search(re_license, text)
153 if m:
154 pass
Matt Kwonga110c742017-09-22 14:31:22 -0700155 elif 'DO NOT EDIT' not in text and filename not in [
156 'src/boringssl/err_data.c', 'src/boringssl/crypto_test_data.cc'
157 ]:
ncteisen7a2be202017-12-11 16:49:19 -0800158 log(1, 'copyright missing', filename)
159 ok = False
Craig Tillerde3da742016-01-04 14:41:25 -0800160
161sys.exit(0 if ok else 1)