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