xref: /titanic_53/usr/src/tools/scripts/cddlchk.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# Check for valid CDDL blocks in source files.
32*cdf0c1d5Smjnelson#
33*cdf0c1d5Smjnelson
34*cdf0c1d5Smjnelsonimport sys, os, getopt, fnmatch
35*cdf0c1d5Smjnelson
36*cdf0c1d5Smjnelsonsys.path.append(os.path.join(os.path.dirname(__file__), '../lib/python'))
37*cdf0c1d5Smjnelsonsys.path.append(os.path.join(os.path.dirname(__file__), '..'))
38*cdf0c1d5Smjnelson
39*cdf0c1d5Smjnelsonfrom onbld.Checks.Cddl import cddlchk
40*cdf0c1d5Smjnelson
41*cdf0c1d5Smjnelsonclass ExceptionList(object):
42*cdf0c1d5Smjnelson	def __init__(self):
43*cdf0c1d5Smjnelson		self.dirs = []
44*cdf0c1d5Smjnelson		self.files = []
45*cdf0c1d5Smjnelson		self.extensions = []
46*cdf0c1d5Smjnelson
47*cdf0c1d5Smjnelson	def load(self, exfile):
48*cdf0c1d5Smjnelson		fh = None
49*cdf0c1d5Smjnelson		try:
50*cdf0c1d5Smjnelson			fh = open(exfile, 'r')
51*cdf0c1d5Smjnelson		except IOError, e:
52*cdf0c1d5Smjnelson			sys.stderr.write('Failed to open exception list: '
53*cdf0c1d5Smjnelson					 '%s: %s\n' % (e.filename, e.strerror))
54*cdf0c1d5Smjnelson			sys.exit(2)
55*cdf0c1d5Smjnelson
56*cdf0c1d5Smjnelson		for line in fh:
57*cdf0c1d5Smjnelson			line = line.strip()
58*cdf0c1d5Smjnelson
59*cdf0c1d5Smjnelson			if line.strip().endswith('/'):
60*cdf0c1d5Smjnelson				self.dirs.append(line[0:-1])
61*cdf0c1d5Smjnelson			elif line.startswith('*.'):
62*cdf0c1d5Smjnelson				self.extensions.append(line)
63*cdf0c1d5Smjnelson			else:
64*cdf0c1d5Smjnelson				self.files.append(line)
65*cdf0c1d5Smjnelson
66*cdf0c1d5Smjnelson		fh.close()
67*cdf0c1d5Smjnelson
68*cdf0c1d5Smjnelson	def match(self, filename):
69*cdf0c1d5Smjnelson		if os.path.isdir(filename):
70*cdf0c1d5Smjnelson			return filename in self.dirs
71*cdf0c1d5Smjnelson		else:
72*cdf0c1d5Smjnelson			if filename in self.files:
73*cdf0c1d5Smjnelson				return True
74*cdf0c1d5Smjnelson
75*cdf0c1d5Smjnelson			for pat in self.extensions:
76*cdf0c1d5Smjnelson				if fnmatch.fnmatch(filename, pat):
77*cdf0c1d5Smjnelson					return True
78*cdf0c1d5Smjnelson
79*cdf0c1d5Smjnelson	def __contains__(self, elt):
80*cdf0c1d5Smjnelson		return self.match(elt)
81*cdf0c1d5Smjnelson
82*cdf0c1d5Smjnelsondef usage():
83*cdf0c1d5Smjnelson	progname = os.path.split(sys.argv[0])[1]
84*cdf0c1d5Smjnelson	sys.stderr.write('''Usage: %s [-av] [-x exceptions] paths...
85*cdf0c1d5Smjnelson        -a		check that all the specified files have a CDDL block.
86*cdf0c1d5Smjnelson        -v		report on all files, not just those with errors.
87*cdf0c1d5Smjnelson        -x exceptions	load an exceptions file
88*cdf0c1d5Smjnelson''' % progname)
89*cdf0c1d5Smjnelson	sys.exit(2)
90*cdf0c1d5Smjnelson
91*cdf0c1d5Smjnelson
92*cdf0c1d5Smjnelsondef check(filename, opts):
93*cdf0c1d5Smjnelson	try:
94*cdf0c1d5Smjnelson		fh = open(filename, 'r')
95*cdf0c1d5Smjnelson	except IOError, e:
96*cdf0c1d5Smjnelson		sys.stderr.write("failed to open '%s': %s\n" %
97*cdf0c1d5Smjnelson				 (e.filename, e.strerror))
98*cdf0c1d5Smjnelson		return 1
99*cdf0c1d5Smjnelson	else:
100*cdf0c1d5Smjnelson		return cddlchk(fh, verbose=opts['verbose'],
101*cdf0c1d5Smjnelson			       lenient=opts['lenient'],
102*cdf0c1d5Smjnelson			       output=sys.stdout)
103*cdf0c1d5Smjnelson
104*cdf0c1d5Smjnelsondef walker(opts, dirname, fnames):
105*cdf0c1d5Smjnelson	for f in fnames:
106*cdf0c1d5Smjnelson		path = os.path.join(dirname, f)
107*cdf0c1d5Smjnelson
108*cdf0c1d5Smjnelson		if not os.path.isdir(path):
109*cdf0c1d5Smjnelson			if not path in opts['exclude']:
110*cdf0c1d5Smjnelson				opts['status'] |= check(path, opts)
111*cdf0c1d5Smjnelson		else:
112*cdf0c1d5Smjnelson			if path in opts['exclude']:
113*cdf0c1d5Smjnelson				fnames.remove(f)
114*cdf0c1d5Smjnelson
115*cdf0c1d5Smjnelsondef walkpath(path, opts):
116*cdf0c1d5Smjnelson	if os.path.isdir(path):
117*cdf0c1d5Smjnelson		os.path.walk(path, walker, opts)
118*cdf0c1d5Smjnelson	else:
119*cdf0c1d5Smjnelson		if not path in opts['exclude']:
120*cdf0c1d5Smjnelson			opts['status'] |= check(path, opts)
121*cdf0c1d5Smjnelson
122*cdf0c1d5Smjnelsondef main(args):
123*cdf0c1d5Smjnelson	options = {
124*cdf0c1d5Smjnelson		'status': 0,
125*cdf0c1d5Smjnelson		'lenient': True,
126*cdf0c1d5Smjnelson		'verbose': False,
127*cdf0c1d5Smjnelson		'exclude': ExceptionList()
128*cdf0c1d5Smjnelson	}
129*cdf0c1d5Smjnelson
130*cdf0c1d5Smjnelson	try:
131*cdf0c1d5Smjnelson		opts, args = getopt.getopt(sys.argv[1:], 'avx:')
132*cdf0c1d5Smjnelson	except getopt.GetoptError:
133*cdf0c1d5Smjnelson		usage()
134*cdf0c1d5Smjnelson		sys.exit(2)
135*cdf0c1d5Smjnelson
136*cdf0c1d5Smjnelson	for opt, arg in opts:
137*cdf0c1d5Smjnelson		if opt == '-a':
138*cdf0c1d5Smjnelson			options['lenient'] = False
139*cdf0c1d5Smjnelson		elif opt == '-v':
140*cdf0c1d5Smjnelson			options['verbose'] = True
141*cdf0c1d5Smjnelson		elif opt == '-x':
142*cdf0c1d5Smjnelson			options['exclude'].load(arg)
143*cdf0c1d5Smjnelson
144*cdf0c1d5Smjnelson	for path in args:
145*cdf0c1d5Smjnelson		walkpath(path, options)
146*cdf0c1d5Smjnelson
147*cdf0c1d5Smjnelson	return options['status']
148*cdf0c1d5Smjnelson
149*cdf0c1d5Smjnelsonif __name__ == '__main__':
150*cdf0c1d5Smjnelson	sys.exit(main(sys.argv[1:]))
151