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