xref: /titanic_52/usr/src/tools/onbld/Checks/Copyright.py (revision fb2a9bae0030340ad72b9c26ba1ffee2ee3cafec)
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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25#
26
27#
28# Make sure there is a correctly formed copyright message containing
29# the current year.
30#
31# We treat extant but incorrect copyrights of known format as present
32# for the purposes of the "no copyright found" messages to avoid
33# treating every otherwise incorrect copyright as also not present
34#
35
36import time, re, sys
37
38def err(stream, msg, fname, line=None):
39	if line:
40		stream.write("%s: %d: %s\n" % (fname, line, msg))
41	else:
42		stream.write("%s: %s\n" % (fname, msg))
43
44# pre-2002 copyright with '(c)'
45oldcopyright = re.compile(r'Copyright \(c\) .* Sun Microsystems, Inc\.')
46
47# pre-2002 copyright with 'by'
48oldcopyright1 = re.compile(r'Copyright .* by Sun Microsystems, Inc\.')
49
50# last valid Sun copyright
51suncopyright = re.compile(r'Copyright ([\d, -]+) Sun Microsystems, Inc\.' +
52			  r'(\s+)(All rights reserved\.)?')
53
54# old, check to make sure no longer present
55licterms = 'Use is subject to license terms.'
56
57# initial Oracle copyright
58goodcopyright = re.compile(r'Copyright \(c\) (\d\d\d\d, )?(\d\d\d\d)(,)? ' +
59			   r'Oracle and/or its affiliates\.(\s+)' +
60			   r'All rights reserved\.')
61
62def copyright(fh, filename=None, output=sys.stderr):
63	ret = lineno = rights = 0
64	check_license = False
65
66	if not filename:
67		filename = fh.name
68
69	for line in fh:
70		lineno += 1
71
72		if check_license:
73			check_license = False
74			if licterms in line:
75				err(output, "old '%s' message found" % licterms,
76				    filename, lineno)
77				ret = 1
78				continue
79
80		if oldcopyright.search(line):
81			err(output, "ancient Sun copyright", filename,
82			    lineno)
83			rights += 1
84			ret = 1
85			check_license = True
86			continue
87		elif oldcopyright1.search(line):
88			err(output, "pre-2002 Sun copyright", filename, lineno)
89			rights += 1
90			ret = 1
91			check_license = True
92			continue
93		elif suncopyright.search(line):
94			err(output, "old Sun copyright", filename, lineno)
95			rights += 1
96			ret = 1
97			check_license = True
98			continue
99
100		#
101		# group 1 = optional initial year
102		# group 2 = current year
103		# group 3 = comma after current year
104		# group 4 = spacing between phrases
105		#
106		match = goodcopyright.search(line)
107		if match:
108			# only check for the old license message on the line
109			# following a copyright match
110			check_license = True
111			rights += 1
112
113			year = time.strftime('%Y')
114			if match.group(2) != year:
115				err(output, "wrong copyright year %s, should "
116				    "be %s" %
117				    (match.group(2), year), filename, lineno)
118				ret = 1
119
120			if match.group(3) != ',':
121				err(output, "need comma after current year",
122				    filename, lineno)
123				ret = 1
124
125			if match.group(4) != ' ':
126				err(output, "need one space between copyright "
127				    "and all rights reserved phrases",
128				    filename, lineno)
129				ret = 1
130
131	if rights == 0:
132		err(output, "no copyright message found", filename)
133		ret = 1
134
135	return ret
136