|
Packit |
fd8b60 |
# Copyright (C) 2011 by the Massachusetts Institute of Technology.
|
|
Packit |
fd8b60 |
# All rights reserved.
|
|
Packit |
fd8b60 |
#
|
|
Packit |
fd8b60 |
# Export of this software from the United States of America may
|
|
Packit |
fd8b60 |
# require a specific license from the United States Government.
|
|
Packit |
fd8b60 |
# It is the responsibility of any person or organization contemplating
|
|
Packit |
fd8b60 |
# export to obtain such a license before exporting.
|
|
Packit |
fd8b60 |
#
|
|
Packit |
fd8b60 |
# WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
|
|
Packit |
fd8b60 |
# distribute this software and its documentation for any purpose and
|
|
Packit |
fd8b60 |
# without fee is hereby granted, provided that the above copyright
|
|
Packit |
fd8b60 |
# notice appear in all copies and that both that copyright notice and
|
|
Packit |
fd8b60 |
# this permission notice appear in supporting documentation, and that
|
|
Packit |
fd8b60 |
# the name of M.I.T. not be used in advertising or publicity pertaining
|
|
Packit |
fd8b60 |
# to distribution of the software without specific, written prior
|
|
Packit |
fd8b60 |
# permission. Furthermore if you modify this software you must label
|
|
Packit |
fd8b60 |
# your software as modified software and not distribute it in such a
|
|
Packit |
fd8b60 |
# fashion that it might be confused with the original M.I.T. software.
|
|
Packit |
fd8b60 |
# M.I.T. makes no representations about the suitability of
|
|
Packit |
fd8b60 |
# this software for any purpose. It is provided "as is" without express
|
|
Packit |
fd8b60 |
# or implied warranty.
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
# This program is intended to be used by "make check-copyright". It
|
|
Packit |
fd8b60 |
# checks for violations of the coding standards related to copyright
|
|
Packit |
fd8b60 |
# and license statements in source code comments.
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
import os
|
|
Packit |
fd8b60 |
import sys
|
|
Packit |
fd8b60 |
import re
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
def warn(fname, ln, msg):
|
|
Packit |
fd8b60 |
print('%s: %d: %s' % (fname, ln + 1, msg))
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
def indicates_license(line):
|
|
Packit |
fd8b60 |
return 'Copyright' in line or 'COPYRIGHT' in line or 'License' in line
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
# Check a comment for boilerplate violations. Return true if the comment
|
|
Packit |
fd8b60 |
# is a license statement.
|
|
Packit |
fd8b60 |
def check_comment(comment, fname, ln, code_seen, nonlicense_seen):
|
|
Packit |
fd8b60 |
text_seen = False
|
|
Packit |
fd8b60 |
is_license = False
|
|
Packit |
fd8b60 |
for line in comment:
|
|
Packit |
fd8b60 |
if not is_license and indicates_license(line):
|
|
Packit |
fd8b60 |
is_license = True
|
|
Packit |
fd8b60 |
if text_seen:
|
|
Packit |
fd8b60 |
warn(fname, ln, 'License begins after first line of comment')
|
|
Packit |
fd8b60 |
elif code_seen:
|
|
Packit |
fd8b60 |
warn(fname, ln, 'License after code')
|
|
Packit |
fd8b60 |
elif nonlicense_seen:
|
|
Packit |
fd8b60 |
warn(fname, ln, 'License after non-license comments')
|
|
Packit |
fd8b60 |
break
|
|
Packit |
fd8b60 |
# DB2 licenses start with '/*-' and we don't want to change them.
|
|
Packit |
fd8b60 |
if line != '' and line != '-':
|
|
Packit |
fd8b60 |
text_seen = True
|
|
Packit |
fd8b60 |
return is_license
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
def check_file(lines, fname):
|
|
Packit |
fd8b60 |
# Skip emacs mode line if present.
|
|
Packit |
fd8b60 |
ln = 0
|
|
Packit |
fd8b60 |
if '-*- mode: c;' in lines[ln]:
|
|
Packit |
fd8b60 |
ln += 1
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
# Check filename comment if present.
|
|
Packit |
fd8b60 |
m = re.match(r'/\* ([^ ]*)( - .*)? \*/', lines[ln])
|
|
Packit |
fd8b60 |
if m:
|
|
Packit |
fd8b60 |
if m.group(1) != fname:
|
|
Packit |
fd8b60 |
warn(fname, ln, 'Wrong filename in comment')
|
|
Packit |
fd8b60 |
ln += 1
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
# Scan for license statements.
|
|
Packit |
fd8b60 |
in_comment = False
|
|
Packit |
fd8b60 |
code_seen = False
|
|
Packit |
fd8b60 |
nonlicense_seen = False
|
|
Packit |
fd8b60 |
for line in lines[ln:]:
|
|
Packit |
fd8b60 |
# Strip out whitespace and comments contained within a line.
|
|
Packit |
fd8b60 |
if not in_comment:
|
|
Packit |
fd8b60 |
line = re.sub(r'/\*.*?\*/', '', line)
|
|
Packit |
fd8b60 |
line = line.strip()
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if not in_comment and '/*' in line:
|
|
Packit |
fd8b60 |
(line, sep, comment_part) = line.partition('/*')
|
|
Packit |
fd8b60 |
comment = [comment_part.strip()]
|
|
Packit |
fd8b60 |
comment_starts_at = ln
|
|
Packit |
fd8b60 |
in_comment = True
|
|
Packit |
fd8b60 |
elif in_comment and '*/' not in line:
|
|
Packit |
fd8b60 |
comment.append(line.lstrip('*').lstrip())
|
|
Packit |
fd8b60 |
elif in_comment:
|
|
Packit |
fd8b60 |
(comment_part, sep, line) = line.partition('*/')
|
|
Packit |
fd8b60 |
comment.append(comment_part.strip())
|
|
Packit |
fd8b60 |
is_license = check_comment(comment, fname, comment_starts_at,
|
|
Packit |
fd8b60 |
code_seen, nonlicense_seen)
|
|
Packit |
fd8b60 |
nonlicense_seen = nonlicense_seen or not is_license
|
|
Packit |
fd8b60 |
in_comment = False
|
|
Packit |
fd8b60 |
elif line.strip() != '':
|
|
Packit |
fd8b60 |
code_seen = True
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
ln += 1
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
for fname in sys.argv[1:]:
|
|
Packit |
fd8b60 |
if fname.startswith('./'):
|
|
Packit |
fd8b60 |
fname = fname[2:]
|
|
Packit |
fd8b60 |
f = open(fname)
|
|
Packit |
fd8b60 |
lines = f.readlines()
|
|
Packit |
fd8b60 |
f.close()
|
|
Packit |
fd8b60 |
check_file(lines, fname)
|