aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/distrib/check_copyright.py
blob: 6ecacede71a6bcc14bf70dcac941289cea53ee5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python2.7

# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import datetime
import os
import re
import sys
import subprocess

# find our home
ROOT = os.path.abspath(
    os.path.join(os.path.dirname(sys.argv[0]), '../..'))
os.chdir(ROOT)

# parse command line
argp = argparse.ArgumentParser(description='copyright checker')
argp.add_argument('-o', '--output',
                  default='details',
                  choices=['list', 'details'])
argp.add_argument('-s', '--skips',
                  default=0,
                  action='store_const',
                  const=1)
argp.add_argument('-a', '--ancient',
                  default=0,
                  action='store_const',
                  const=1)
argp.add_argument('--precommit',
                  default=False,
                  action='store_true')
args = argp.parse_args()

# open the license text
with open('NOTICE.txt') as f:
  LICENSE_NOTICE = f.read().splitlines()


# license format by file extension
# key is the file extension, value is a format string
# that given a line of license text, returns what should
# be in the file
LICENSE_PREFIX = {
  '.bat':       r'@rem\s*',
  '.c':         r'\s*(?://|\*)\s*',
  '.cc':        r'\s*(?://|\*)\s*',
  '.h':         r'\s*(?://|\*)\s*',
  '.m':         r'\s*\*\s*',
  '.php':       r'\s*\*\s*',
  '.js':        r'\s*\*\s*',
  '.py':        r'#\s*',
  '.pyx':       r'#\s*',
  '.pxd':       r'#\s*',
  '.pxi':       r'#\s*',
  '.rb':        r'#\s*',
  '.sh':        r'#\s*',
  '.proto':     r'//\s*',
  '.cs':        r'//\s*',
  '.mak':       r'#\s*',
  'Makefile':   r'#\s*',
  'Dockerfile': r'#\s*',
  'BUILD':      r'#\s*',
}

_EXEMPT = frozenset((
  # Generated protocol compiler output.
  'examples/python/helloworld/helloworld_pb2.py',
  'examples/python/helloworld/helloworld_pb2_grpc.py',
  'examples/python/multiplex/helloworld_pb2.py',
  'examples/python/multiplex/helloworld_pb2_grpc.py',
  'examples/python/multiplex/route_guide_pb2.py',
  'examples/python/multiplex/route_guide_pb2_grpc.py',
  'examples/python/route_guide/route_guide_pb2.py',
  'examples/python/route_guide/route_guide_pb2_grpc.py',

  'src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h',
  'src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.c',
  'src/cpp/server/health/health.pb.h',
  'src/cpp/server/health/health.pb.c',

  # An older file originally from outside gRPC.
  'src/php/tests/bootstrap.php',
  # census.proto copied from github
  'tools/grpcz/census.proto',
  # status.proto copied from googleapis
  'src/proto/grpc/status/status.proto',
))


RE_YEAR = r'Copyright (?P<first_year>[0-9]+\-)?(?P<last_year>[0-9]+) gRPC authors.'
RE_LICENSE = dict(
    (k, r'\n'.join(
        LICENSE_PREFIX[k] +
        (RE_YEAR if re.search(RE_YEAR, line) else re.escape(line))
        for line in LICENSE_NOTICE))
     for k, v in LICENSE_PREFIX.iteritems())

if args.precommit:
  FILE_LIST_COMMAND = 'git status -z | grep -Poz \'(?<=^[MARC][MARCD ] )[^\s]+\''
else:
  FILE_LIST_COMMAND = 'git ls-tree -r --name-only -r HEAD | ' \
                      'grep -v ^third_party/ |' \
                      'grep -v "\(ares_config.h\|ares_build.h\)"'

def load(name):
  with open(name) as f:
    return f.read()

def save(name, text):
  with open(name, 'w') as f:
    f.write(text)


assert(re.search(RE_LICENSE['Makefile'], load('Makefile')))


def log(cond, why, filename):
  if not cond: return
  if args.output == 'details':
    print '%s: %s' % (why, filename)
  else:
    print filename


# scan files, validate the text
ok = True
filename_list = []
try:
  filename_list = subprocess.check_output(FILE_LIST_COMMAND,
                                          shell=True).splitlines()
except subprocess.CalledProcessError:
  sys.exit(0)

for filename in filename_list:
  if filename in _EXEMPT:
    continue
  ext = os.path.splitext(filename)[1]
  base = os.path.basename(filename)
  if ext in RE_LICENSE:
    re_license = RE_LICENSE[ext]
  elif base in RE_LICENSE:
    re_license = RE_LICENSE[base]
  else:
    log(args.skips, 'skip', filename)
    continue
  try:
    text = load(filename)
  except:
    continue
  m = re.search(re_license, text)
  if m:
    pass
  elif 'DO NOT EDIT' not in text and filename != 'src/boringssl/err_data.c':
    log(1, 'copyright missing', filename)
    ok = False

sys.exit(0 if ok else 1)