xref: /titanic_41/usr/src/tools/onbld/Checks/Copyright.py (revision cc1a9a89a73172cc2db053635fab3b1b91691657)
1#! /usr/bin/python
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27# ident	"%Z%%M%	%I%	%E% SMI"
28#
29
30#
31# Make sure there is a correctly formed copyright message containing
32# the current year.
33#
34# We treat extant but incorrect copyrights of known format as present
35# for the purposes of the "no copyright found" messages to avoid
36# treating every otherwise incorrect copyright as also not present
37#
38
39import time, re, sys
40
41def err(stream, msg, fname, line=None):
42	if line:
43		stream.write("%s: %d: %s\n" % (fname, line, msg))
44	else:
45		stream.write("%s: %s\n" % (fname, msg))
46
47# pre-2002 copyright with '(c)'
48oldcopyright = re.compile(r'Copyright \(c\) .* Sun Microsystems, Inc\.')
49
50# pre-2002 copyright with 'by'
51oldcopyright1 = re.compile(r'Copyright .* by Sun Microsystems, Inc\.')
52
53# Valid, current copyright
54goodcopyright = re.compile(r'Copyright ([\d, -]+) Sun Microsystems, Inc\.' +
55			   r'(\s+)(All rights reserved\.)?')
56
57licterms = 'Use is subject to license terms.'
58
59def copyright(fh, filename=None, output=sys.stderr):
60	ret = lineno = rights = 0
61	# Are we expecting the license terms message on this line?
62	expecting_license = False
63
64	if not filename:
65		filename = fh.name
66
67	for line in fh:
68		lineno += 1
69
70		if expecting_license:
71			expecting_license = False
72
73			if licterms not in line:
74				err(output, "'%s' message missing" % licterms,
75				    filename, lineno)
76				ret = 1
77				continue
78			elif rights > 0 and ret == 0:
79				return 0
80			continue
81
82		if oldcopyright.search(line):
83			err(output, "old copyright with '(c)'", filename,
84			    lineno)
85			rights += 1
86			ret = 1
87		elif oldcopyright1.search(line):
88			err(output, "old copyright with 'by'", filename, lineno)
89			rights += 1
90			ret = 1
91
92		#
93		# group 1 = year
94		# group 2 = spacing
95		# group 3 = All rights reserved message.
96		#
97		match = goodcopyright.search(line)
98		if match:
99			expecting_license = True
100			rights += 1
101
102			year = time.strftime('%Y')
103			if match.group(1) != year:
104				err(output, "wrong copyright year %s, should "
105				    "be %s" %
106				    (match.group(1), year), filename, lineno)
107				ret = 1
108
109			if not match.group(3):
110				err(output, "'All rights reserved.' message "
111				    "missing",
112				    filename, lineno)
113				ret = 1
114			elif match.group(2) != '  ':
115				err(output, "need two spaces between copyright "
116				    "and all rights reserved phrases",
117				    filename, lineno)
118				ret = 1
119	#
120	# If the last line left us expecting the license message,
121	# we're pretty sure it isn't going to be there.
122	#
123	if expecting_license:
124		err(output, "'Use is subject to license terms.' message "
125		    "missing", filename, lineno)
126		ret = 1
127
128	if rights == 0:
129		err(output, "no copyright message found", filename)
130		ret = 1
131
132	return ret
133