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