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