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 | |
Craig Tiller | 6e2cf6b | 2016-03-31 07:46:10 -0700 | [diff] [blame] | 3 | # Copyright 2015, Google Inc. |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 4 | # All rights reserved. |
| 5 | # |
| 6 | # Redistribution and use in source and binary forms, with or without |
| 7 | # modification, are permitted provided that the following conditions are |
| 8 | # met: |
| 9 | # |
| 10 | # * Redistributions of source code must retain the above copyright |
| 11 | # notice, this list of conditions and the following disclaimer. |
| 12 | # * Redistributions in binary form must reproduce the above |
| 13 | # copyright notice, this list of conditions and the following disclaimer |
| 14 | # in the documentation and/or other materials provided with the |
| 15 | # distribution. |
| 16 | # * Neither the name of Google Inc. nor the names of its |
| 17 | # contributors may be used to endorse or promote products derived from |
| 18 | # this software without specific prior written permission. |
| 19 | # |
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
Craig Tiller | 94640a3 | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 32 | import argparse |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 33 | import datetime |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 34 | import os |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 35 | import re |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 36 | import sys |
| 37 | import subprocess |
| 38 | |
| 39 | # find our home |
| 40 | ROOT = os.path.abspath( |
| 41 | os.path.join(os.path.dirname(sys.argv[0]), '../..')) |
| 42 | os.chdir(ROOT) |
| 43 | |
Craig Tiller | 94640a3 | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 44 | # parse command line |
| 45 | argp = argparse.ArgumentParser(description='copyright checker') |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 46 | argp.add_argument('-o', '--output', |
| 47 | default='details', |
Craig Tiller | 94640a3 | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 48 | choices=['list', 'details']) |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 49 | argp.add_argument('-s', '--skips', |
Craig Tiller | 94640a3 | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 50 | default=0, |
| 51 | action='store_const', |
| 52 | const=1) |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 53 | argp.add_argument('-a', '--ancient', |
| 54 | default=0, |
| 55 | action='store_const', |
| 56 | const=1) |
David Garcia Quintas | fa58786 | 2016-01-12 15:31:40 -0800 | [diff] [blame] | 57 | argp.add_argument('-f', '--fix', |
| 58 | default=False, |
| 59 | action='store_true'); |
murgatroid99 | f656f18 | 2016-02-09 10:56:25 -0800 | [diff] [blame] | 60 | argp.add_argument('--precommit', |
| 61 | default=False, |
| 62 | action='store_true') |
Craig Tiller | 94640a3 | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 63 | args = argp.parse_args() |
| 64 | |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 65 | # open the license text |
| 66 | with open('LICENSE') as f: |
| 67 | LICENSE = f.read().splitlines() |
| 68 | |
| 69 | # license format by file extension |
| 70 | # key is the file extension, value is a format string |
| 71 | # that given a line of license text, returns what should |
| 72 | # be in the file |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 73 | LICENSE_PREFIX = { |
Jan Tattermusch | fdb8931 | 2016-05-13 10:38:34 -0700 | [diff] [blame^] | 74 | '.bat': r'@rem\s*', |
Craig Tiller | 2d50f37 | 2016-01-28 11:48:18 -0800 | [diff] [blame] | 75 | '.c': r'\s*(?://|\*)\s*', |
| 76 | '.cc': r'\s*(?://|\*)\s*', |
| 77 | '.h': r'\s*(?://|\*)\s*', |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 78 | '.m': r'\s*\*\s*', |
| 79 | '.php': r'\s*\*\s*', |
| 80 | '.js': r'\s*\*\s*', |
| 81 | '.py': r'#\s*', |
Craig Tiller | befa1ff | 2016-01-05 07:54:11 -0800 | [diff] [blame] | 82 | '.pyx': r'#\s*', |
| 83 | '.pxd': r'#\s*', |
| 84 | '.pxi': r'#\s*', |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 85 | '.rb': r'#\s*', |
| 86 | '.sh': r'#\s*', |
| 87 | '.proto': r'//\s*', |
| 88 | '.cs': r'//\s*', |
| 89 | '.mak': r'#\s*', |
| 90 | 'Makefile': r'#\s*', |
| 91 | 'Dockerfile': r'#\s*', |
| 92 | 'LICENSE': '', |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Craig Tiller | 897e4fe | 2015-12-22 15:03:40 -0800 | [diff] [blame] | 95 | KNOWN_BAD = set([ |
| 96 | 'src/php/tests/bootstrap.php', |
| 97 | ]) |
| 98 | |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 99 | |
David Garcia Quintas | fa58786 | 2016-01-12 15:31:40 -0800 | [diff] [blame] | 100 | RE_YEAR = r'Copyright (?P<first_year>[0-9]+\-)?(?P<last_year>[0-9]+), Google Inc\.' |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 101 | RE_LICENSE = dict( |
| 102 | (k, r'\n'.join( |
| 103 | LICENSE_PREFIX[k] + |
| 104 | (RE_YEAR if re.search(RE_YEAR, line) else re.escape(line)) |
| 105 | for line in LICENSE)) |
| 106 | for k, v in LICENSE_PREFIX.iteritems()) |
| 107 | |
murgatroid99 | f656f18 | 2016-02-09 10:56:25 -0800 | [diff] [blame] | 108 | if args.precommit: |
murgatroid99 | 028bd45 | 2016-03-16 16:25:42 -0700 | [diff] [blame] | 109 | FILE_LIST_COMMAND = 'git status -z | grep -Poz \'(?<=^[MARC][MARCD ] )[^\s]+\'' |
murgatroid99 | f656f18 | 2016-02-09 10:56:25 -0800 | [diff] [blame] | 110 | else: |
| 111 | FILE_LIST_COMMAND = 'git ls-tree -r --name-only -r HEAD | grep -v ^third_party/' |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 112 | |
| 113 | def load(name): |
| 114 | with open(name) as f: |
David Garcia Quintas | 68a1686 | 2016-01-14 10:56:48 -0800 | [diff] [blame] | 115 | return f.read() |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 116 | |
David Garcia Quintas | fa58786 | 2016-01-12 15:31:40 -0800 | [diff] [blame] | 117 | def save(name, text): |
| 118 | with open(name, 'w') as f: |
| 119 | f.write(text) |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 120 | |
| 121 | assert(re.search(RE_LICENSE['LICENSE'], load('LICENSE'))) |
| 122 | assert(re.search(RE_LICENSE['Makefile'], load('Makefile'))) |
| 123 | |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 124 | |
Craig Tiller | 94640a3 | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 125 | def log(cond, why, filename): |
| 126 | if not cond: return |
| 127 | if args.output == 'details': |
| 128 | print '%s: %s' % (why, filename) |
| 129 | else: |
| 130 | print filename |
| 131 | |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 132 | |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 133 | # scan files, validate the text |
Craig Tiller | 750d3ed | 2016-01-12 09:38:25 -0800 | [diff] [blame] | 134 | ok = True |
murgatroid99 | c58a781 | 2016-02-09 16:33:17 -0800 | [diff] [blame] | 135 | filename_list = [] |
| 136 | try: |
| 137 | filename_list = subprocess.check_output(FILE_LIST_COMMAND, |
| 138 | shell=True).splitlines() |
| 139 | except subprocess.CalledProcessError: |
| 140 | sys.exit(0) |
| 141 | |
| 142 | for filename in filename_list: |
Craig Tiller | 897e4fe | 2015-12-22 15:03:40 -0800 | [diff] [blame] | 143 | if filename in KNOWN_BAD: continue |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 144 | ext = os.path.splitext(filename)[1] |
Craig Tiller | 6ce372c | 2015-02-20 09:44:12 -0800 | [diff] [blame] | 145 | base = os.path.basename(filename) |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 146 | if ext in RE_LICENSE: |
| 147 | re_license = RE_LICENSE[ext] |
| 148 | elif base in RE_LICENSE: |
| 149 | re_license = RE_LICENSE[base] |
Craig Tiller | 6ce372c | 2015-02-20 09:44:12 -0800 | [diff] [blame] | 150 | else: |
Craig Tiller | 94640a3 | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 151 | log(args.skips, 'skip', filename) |
Craig Tiller | f222a4a | 2015-02-16 12:28:10 -0800 | [diff] [blame] | 152 | continue |
Craig Tiller | 94d04a5 | 2016-01-20 10:58:23 -0800 | [diff] [blame] | 153 | try: |
| 154 | text = load(filename) |
| 155 | except: |
| 156 | continue |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 157 | m = re.search(re_license, text) |
| 158 | if m: |
Craig Tiller | 6e2cf6b | 2016-03-31 07:46:10 -0700 | [diff] [blame] | 159 | pass |
Craig Tiller | de3da74 | 2016-01-04 14:41:25 -0800 | [diff] [blame] | 160 | elif 'DO NOT EDIT' not in text and 'AssemblyInfo.cs' not in filename and filename != 'src/boringssl/err_data.c': |
| 161 | log(1, 'copyright missing', filename) |
| 162 | ok = False |
| 163 | |
| 164 | sys.exit(0 if ok else 1) |