Path: blob/main/crypto/krb5/src/util/krb5-check-copyright.py
34889 views
# Copyright (C) 2011 by the Massachusetts Institute of Technology.1# All rights reserved.2#3# Export of this software from the United States of America may4# require a specific license from the United States Government.5# It is the responsibility of any person or organization contemplating6# export to obtain such a license before exporting.7#8# WITHIN THAT CONSTRAINT, permission to use, copy, modify, and9# distribute this software and its documentation for any purpose and10# without fee is hereby granted, provided that the above copyright11# notice appear in all copies and that both that copyright notice and12# this permission notice appear in supporting documentation, and that13# the name of M.I.T. not be used in advertising or publicity pertaining14# to distribution of the software without specific, written prior15# permission. Furthermore if you modify this software you must label16# your software as modified software and not distribute it in such a17# fashion that it might be confused with the original M.I.T. software.18# M.I.T. makes no representations about the suitability of19# this software for any purpose. It is provided "as is" without express20# or implied warranty.2122# This program is intended to be used by "make check-copyright". It23# checks for violations of the coding standards related to copyright24# and license statements in source code comments.2526import os27import sys28import re2930def warn(fname, ln, msg):31print('%s: %d: %s' % (fname, ln + 1, msg))3233def indicates_license(line):34return 'Copyright' in line or 'COPYRIGHT' in line or 'License' in line3536# Check a comment for boilerplate violations. Return true if the comment37# is a license statement.38def check_comment(comment, fname, ln, code_seen, nonlicense_seen):39text_seen = False40is_license = False41for line in comment:42if not is_license and indicates_license(line):43is_license = True44if text_seen:45warn(fname, ln, 'License begins after first line of comment')46elif code_seen:47warn(fname, ln, 'License after code')48elif nonlicense_seen:49warn(fname, ln, 'License after non-license comments')50break51# DB2 licenses start with '/*-' and we don't want to change them.52if line != '' and line != '-':53text_seen = True54return is_license5556def check_file(lines, fname):57# Skip emacs mode line if present.58ln = 059if '-*- mode: c;' in lines[ln]:60ln += 16162# Check filename comment if present.63m = re.match(r'/\* ([^ ]*)( - .*)? \*/', lines[ln])64if m:65if m.group(1) != fname:66warn(fname, ln, 'Wrong filename in comment')67ln += 16869# Scan for license statements.70in_comment = False71code_seen = False72nonlicense_seen = False73for line in lines[ln:]:74# Strip out whitespace and comments contained within a line.75if not in_comment:76line = re.sub(r'/\*.*?\*/', '', line)77line = line.strip()7879if not in_comment and '/*' in line:80(line, sep, comment_part) = line.partition('/*')81comment = [comment_part.strip()]82comment_starts_at = ln83in_comment = True84elif in_comment and '*/' not in line:85comment.append(line.lstrip('*').lstrip())86elif in_comment:87(comment_part, sep, line) = line.partition('*/')88comment.append(comment_part.strip())89is_license = check_comment(comment, fname, comment_starts_at,90code_seen, nonlicense_seen)91nonlicense_seen = nonlicense_seen or not is_license92in_comment = False93elif line.strip() != '':94code_seen = True9596ln += 19798for fname in sys.argv[1:]:99if fname.startswith('./'):100fname = fname[2:]101f = open(fname)102lines = f.readlines()103f.close()104check_file(lines, fname)105106107