xref: /onnv-gate/usr/src/tools/onbld/Checks/Copyright.py (revision 12041:03c4bd206296)
17078Smjnelson#! /usr/bin/python
27078Smjnelson#
37078Smjnelson# CDDL HEADER START
47078Smjnelson#
57078Smjnelson# The contents of this file are subject to the terms of the
67078Smjnelson# Common Development and Distribution License (the "License").
77078Smjnelson# You may not use this file except in compliance with the License.
87078Smjnelson#
97078Smjnelson# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107078Smjnelson# or http://www.opensolaris.org/os/licensing.
117078Smjnelson# See the License for the specific language governing permissions
127078Smjnelson# and limitations under the License.
137078Smjnelson#
147078Smjnelson# When distributing Covered Code, include this CDDL HEADER in each
157078Smjnelson# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167078Smjnelson# If applicable, add the following below this CDDL HEADER, with the
177078Smjnelson# fields enclosed by brackets "[]" replaced with your own identifying
187078Smjnelson# information: Portions Copyright [yyyy] [name of copyright owner]
197078Smjnelson#
207078Smjnelson# CDDL HEADER END
217078Smjnelson#
227078Smjnelson
237078Smjnelson#
24*12041SJohn.Beck@Sun.COM# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
257078Smjnelson#
267078Smjnelson
277078Smjnelson#
287078Smjnelson# Make sure there is a correctly formed copyright message containing
297078Smjnelson# the current year.
307078Smjnelson#
317078Smjnelson# We treat extant but incorrect copyrights of known format as present
327078Smjnelson# for the purposes of the "no copyright found" messages to avoid
337078Smjnelson# treating every otherwise incorrect copyright as also not present
347078Smjnelson#
357078Smjnelson
367078Smjnelsonimport time, re, sys
377078Smjnelson
387078Smjnelsondef err(stream, msg, fname, line=None):
397078Smjnelson	if line:
407078Smjnelson		stream.write("%s: %d: %s\n" % (fname, line, msg))
417078Smjnelson	else:
427078Smjnelson		stream.write("%s: %s\n" % (fname, msg))
437078Smjnelson
447078Smjnelson# pre-2002 copyright with '(c)'
457078Smjnelsonoldcopyright = re.compile(r'Copyright \(c\) .* Sun Microsystems, Inc\.')
467078Smjnelson
477078Smjnelson# pre-2002 copyright with 'by'
487078Smjnelsonoldcopyright1 = re.compile(r'Copyright .* by Sun Microsystems, Inc\.')
497078Smjnelson
50*12041SJohn.Beck@Sun.COM# last valid Sun copyright
51*12041SJohn.Beck@Sun.COMsuncopyright = re.compile(r'Copyright ([\d, -]+) Sun Microsystems, Inc\.' +
52*12041SJohn.Beck@Sun.COM			  r'(\s+)(All rights reserved\.)?')
537078Smjnelson
54*12041SJohn.Beck@Sun.COM# old, check to make sure no longer present
557078Smjnelsonlicterms = 'Use is subject to license terms.'
567078Smjnelson
57*12041SJohn.Beck@Sun.COM# initial Oracle copyright
58*12041SJohn.Beck@Sun.COMgoodcopyright = re.compile(r'Copyright \(c\) (\d\d\d\d, )?(\d\d\d\d)(,)? ' +
59*12041SJohn.Beck@Sun.COM			   r'Oracle and/or its affiliates\.(\s+)' +
60*12041SJohn.Beck@Sun.COM			   r'All rights reserved\.')
61*12041SJohn.Beck@Sun.COM
627078Smjnelsondef copyright(fh, filename=None, output=sys.stderr):
637078Smjnelson	ret = lineno = rights = 0
64*12041SJohn.Beck@Sun.COM	check_license = False
657078Smjnelson
667078Smjnelson	if not filename:
677078Smjnelson		filename = fh.name
687078Smjnelson
697078Smjnelson	for line in fh:
707078Smjnelson		lineno += 1
717078Smjnelson
72*12041SJohn.Beck@Sun.COM		if check_license:
73*12041SJohn.Beck@Sun.COM			check_license = False
74*12041SJohn.Beck@Sun.COM			if licterms in line:
75*12041SJohn.Beck@Sun.COM				err(output, "old '%s' message found" % licterms,
767078Smjnelson				    filename, lineno)
777078Smjnelson				ret = 1
787078Smjnelson				continue
797078Smjnelson
807078Smjnelson		if oldcopyright.search(line):
81*12041SJohn.Beck@Sun.COM			err(output, "ancient Sun copyright", filename,
827078Smjnelson			    lineno)
837078Smjnelson			rights += 1
847078Smjnelson			ret = 1
85*12041SJohn.Beck@Sun.COM			check_license = True
86*12041SJohn.Beck@Sun.COM			continue
877078Smjnelson		elif oldcopyright1.search(line):
88*12041SJohn.Beck@Sun.COM			err(output, "pre-2002 Sun copyright", filename, lineno)
897078Smjnelson			rights += 1
907078Smjnelson			ret = 1
91*12041SJohn.Beck@Sun.COM			check_license = True
92*12041SJohn.Beck@Sun.COM			continue
93*12041SJohn.Beck@Sun.COM		elif suncopyright.search(line):
94*12041SJohn.Beck@Sun.COM			err(output, "old Sun copyright", filename, lineno)
95*12041SJohn.Beck@Sun.COM			rights += 1
96*12041SJohn.Beck@Sun.COM			ret = 1
97*12041SJohn.Beck@Sun.COM			check_license = True
98*12041SJohn.Beck@Sun.COM			continue
997078Smjnelson
1007078Smjnelson		#
101*12041SJohn.Beck@Sun.COM		# group 1 = optional initial year
102*12041SJohn.Beck@Sun.COM		# group 2 = current year
103*12041SJohn.Beck@Sun.COM		# group 3 = comma after current year
104*12041SJohn.Beck@Sun.COM		# group 4 = spacing between phrases
1057078Smjnelson		#
1067078Smjnelson		match = goodcopyright.search(line)
1077078Smjnelson		if match:
108*12041SJohn.Beck@Sun.COM			# only check for the old license message on the line
109*12041SJohn.Beck@Sun.COM			# following a copyright match
110*12041SJohn.Beck@Sun.COM			check_license = True
1117078Smjnelson			rights += 1
1127078Smjnelson
1137078Smjnelson			year = time.strftime('%Y')
114*12041SJohn.Beck@Sun.COM			if match.group(2) != year:
1157078Smjnelson				err(output, "wrong copyright year %s, should "
1167078Smjnelson				    "be %s" %
117*12041SJohn.Beck@Sun.COM				    (match.group(2), year), filename, lineno)
1187078Smjnelson				ret = 1
1197078Smjnelson
120*12041SJohn.Beck@Sun.COM			if match.group(3) != ',':
121*12041SJohn.Beck@Sun.COM				err(output, "need comma after current year",
1227078Smjnelson				    filename, lineno)
1237078Smjnelson				ret = 1
124*12041SJohn.Beck@Sun.COM
125*12041SJohn.Beck@Sun.COM			if match.group(4) != ' ':
126*12041SJohn.Beck@Sun.COM				err(output, "need one space between copyright "
1277078Smjnelson				    "and all rights reserved phrases",
1287078Smjnelson				    filename, lineno)
1297078Smjnelson				ret = 1
1307078Smjnelson
1317078Smjnelson	if rights == 0:
1327078Smjnelson		err(output, "no copyright message found", filename)
1337078Smjnelson		ret = 1
1347078Smjnelson
1357078Smjnelson	return ret
136