xref: /onnv-gate/usr/src/tools/scripts/mapfilechk.py (revision 12216:5167f302c8cf)
111838SLiane.Praza@Sun.COM#!/usr/bin/python2.4
28744SAli.Bahrami@Sun.COM#
38744SAli.Bahrami@Sun.COM# CDDL HEADER START
48744SAli.Bahrami@Sun.COM#
58744SAli.Bahrami@Sun.COM# The contents of this file are subject to the terms of the
68744SAli.Bahrami@Sun.COM# Common Development and Distribution License (the "License").
78744SAli.Bahrami@Sun.COM# You may not use this file except in compliance with the License.
88744SAli.Bahrami@Sun.COM#
98744SAli.Bahrami@Sun.COM# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
108744SAli.Bahrami@Sun.COM# or http://www.opensolaris.org/os/licensing.
118744SAli.Bahrami@Sun.COM# See the License for the specific language governing permissions
128744SAli.Bahrami@Sun.COM# and limitations under the License.
138744SAli.Bahrami@Sun.COM#
148744SAli.Bahrami@Sun.COM# When distributing Covered Code, include this CDDL HEADER in each
158744SAli.Bahrami@Sun.COM# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
168744SAli.Bahrami@Sun.COM# If applicable, add the following below this CDDL HEADER, with the
178744SAli.Bahrami@Sun.COM# fields enclosed by brackets "[]" replaced with your own identifying
188744SAli.Bahrami@Sun.COM# information: Portions Copyright [yyyy] [name of copyright owner]
198744SAli.Bahrami@Sun.COM#
208744SAli.Bahrami@Sun.COM# CDDL HEADER END
218744SAli.Bahrami@Sun.COM#
228744SAli.Bahrami@Sun.COM
238744SAli.Bahrami@Sun.COM#
24*12216Srichlowe@richlowe.net# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
258744SAli.Bahrami@Sun.COM#
268744SAli.Bahrami@Sun.COM
278744SAli.Bahrami@Sun.COM#
288744SAli.Bahrami@Sun.COM# Check for valid link-editor mapfile comment blocks in source files.
298744SAli.Bahrami@Sun.COM#
308744SAli.Bahrami@Sun.COM
318744SAli.Bahrami@Sun.COMimport sys, os, getopt, fnmatch
328744SAli.Bahrami@Sun.COM
33*12216Srichlowe@richlowe.netsys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "lib",
34*12216Srichlowe@richlowe.net                                "python%d.%d" % sys.version_info[:2]))
35*12216Srichlowe@richlowe.net
36*12216Srichlowe@richlowe.net# Allow running from the source tree, using the modules in the source tree
37*12216Srichlowe@richlowe.netsys.path.insert(2, os.path.join(os.path.dirname(__file__), '..'))
388744SAli.Bahrami@Sun.COM
398744SAli.Bahrami@Sun.COMfrom onbld.Checks.Mapfile import mapfilechk
408744SAli.Bahrami@Sun.COM
418744SAli.Bahrami@Sun.COMclass ExceptionList(object):
428744SAli.Bahrami@Sun.COM	def __init__(self):
438744SAli.Bahrami@Sun.COM		self.dirs = []
448744SAli.Bahrami@Sun.COM		self.files = []
458744SAli.Bahrami@Sun.COM		self.extensions = []
468744SAli.Bahrami@Sun.COM
478744SAli.Bahrami@Sun.COM	def load(self, exfile):
488744SAli.Bahrami@Sun.COM		fh = None
498744SAli.Bahrami@Sun.COM		try:
508744SAli.Bahrami@Sun.COM			fh = open(exfile, 'r')
518744SAli.Bahrami@Sun.COM		except IOError, e:
528744SAli.Bahrami@Sun.COM			sys.stderr.write('Failed to open exception list: '
538744SAli.Bahrami@Sun.COM					 '%s: %s\n' % (e.filename, e.strerror))
548744SAli.Bahrami@Sun.COM			sys.exit(2)
558744SAli.Bahrami@Sun.COM
568744SAli.Bahrami@Sun.COM		for line in fh:
578744SAli.Bahrami@Sun.COM			line = line.strip()
588744SAli.Bahrami@Sun.COM
598744SAli.Bahrami@Sun.COM			if line.strip().endswith('/'):
608744SAli.Bahrami@Sun.COM				self.dirs.append(line[0:-1])
618744SAli.Bahrami@Sun.COM			elif line.startswith('*.'):
628744SAli.Bahrami@Sun.COM				self.extensions.append(line)
638744SAli.Bahrami@Sun.COM			else:
648744SAli.Bahrami@Sun.COM				self.files.append(line)
658744SAli.Bahrami@Sun.COM
668744SAli.Bahrami@Sun.COM		fh.close()
678744SAli.Bahrami@Sun.COM
688744SAli.Bahrami@Sun.COM	def match(self, filename):
698744SAli.Bahrami@Sun.COM		if os.path.isdir(filename):
708744SAli.Bahrami@Sun.COM			return filename in self.dirs
718744SAli.Bahrami@Sun.COM		else:
728744SAli.Bahrami@Sun.COM			if filename in self.files:
738744SAli.Bahrami@Sun.COM				return True
748744SAli.Bahrami@Sun.COM
758744SAli.Bahrami@Sun.COM			for pat in self.extensions:
768744SAli.Bahrami@Sun.COM				if fnmatch.fnmatch(filename, pat):
778744SAli.Bahrami@Sun.COM					return True
788744SAli.Bahrami@Sun.COM
798744SAli.Bahrami@Sun.COM	def __contains__(self, elt):
808744SAli.Bahrami@Sun.COM		return self.match(elt)
8111300Srichlowe@richlowe.net
828744SAli.Bahrami@Sun.COMdef usage():
838744SAli.Bahrami@Sun.COM	progname = os.path.split(sys.argv[0])[1]
848744SAli.Bahrami@Sun.COM	sys.stderr.write('''Usage: %s [-v] [-x exceptions] paths...
858744SAli.Bahrami@Sun.COM        -v		report on all files, not just those with errors.
868744SAli.Bahrami@Sun.COM        -x exceptions	load an exceptions file
878744SAli.Bahrami@Sun.COM''' % progname)
888744SAli.Bahrami@Sun.COM	sys.exit(2)
898744SAli.Bahrami@Sun.COM
908744SAli.Bahrami@Sun.COM
918744SAli.Bahrami@Sun.COMdef check(filename, opts):
928744SAli.Bahrami@Sun.COM	try:
938744SAli.Bahrami@Sun.COM		fh = open(filename, 'r')
948744SAli.Bahrami@Sun.COM	except IOError, e:
958744SAli.Bahrami@Sun.COM		sys.stderr.write("failed to open '%s': %s\n" %
968744SAli.Bahrami@Sun.COM				 (e.filename, e.strerror))
978744SAli.Bahrami@Sun.COM		return 1
988744SAli.Bahrami@Sun.COM	else:
998744SAli.Bahrami@Sun.COM		return mapfilechk(fh, verbose=opts['verbose'],
1008744SAli.Bahrami@Sun.COM			       output=sys.stdout)
1018744SAli.Bahrami@Sun.COM
1028744SAli.Bahrami@Sun.COMdef walker(opts, dirname, fnames):
1038744SAli.Bahrami@Sun.COM	for f in fnames:
1048744SAli.Bahrami@Sun.COM		path = os.path.join(dirname, f)
1058744SAli.Bahrami@Sun.COM
1068744SAli.Bahrami@Sun.COM		if not os.path.isdir(path):
1078744SAli.Bahrami@Sun.COM			if not path in opts['exclude']:
1088744SAli.Bahrami@Sun.COM				opts['status'] |= check(path, opts)
1098744SAli.Bahrami@Sun.COM		else:
1108744SAli.Bahrami@Sun.COM			if path in opts['exclude']:
1118744SAli.Bahrami@Sun.COM				fnames.remove(f)
11211300Srichlowe@richlowe.net
1138744SAli.Bahrami@Sun.COMdef walkpath(path, opts):
1148744SAli.Bahrami@Sun.COM	if os.path.isdir(path):
1158744SAli.Bahrami@Sun.COM		os.path.walk(path, walker, opts)
1168744SAli.Bahrami@Sun.COM	else:
1178744SAli.Bahrami@Sun.COM		if not path in opts['exclude']:
1188744SAli.Bahrami@Sun.COM			opts['status'] |= check(path, opts)
1198744SAli.Bahrami@Sun.COM
1208744SAli.Bahrami@Sun.COMdef main(args):
1218744SAli.Bahrami@Sun.COM	options = {
1228744SAli.Bahrami@Sun.COM		'status': 0,
1238744SAli.Bahrami@Sun.COM		'verbose': False,
1248744SAli.Bahrami@Sun.COM		'exclude': ExceptionList()
1258744SAli.Bahrami@Sun.COM	}
1268744SAli.Bahrami@Sun.COM
1278744SAli.Bahrami@Sun.COM	try:
1288744SAli.Bahrami@Sun.COM		opts, args = getopt.getopt(sys.argv[1:], 'avx:')
1298744SAli.Bahrami@Sun.COM	except getopt.GetoptError:
1308744SAli.Bahrami@Sun.COM		usage()
1318744SAli.Bahrami@Sun.COM		sys.exit(2)
1328744SAli.Bahrami@Sun.COM
1338744SAli.Bahrami@Sun.COM	for opt, arg in opts:
1348744SAli.Bahrami@Sun.COM		if opt == '-v':
1358744SAli.Bahrami@Sun.COM			options['verbose'] = True
1368744SAli.Bahrami@Sun.COM		elif opt == '-x':
1378744SAli.Bahrami@Sun.COM			options['exclude'].load(arg)
13811300Srichlowe@richlowe.net
1398744SAli.Bahrami@Sun.COM	for path in args:
1408744SAli.Bahrami@Sun.COM		walkpath(path, options)
1418744SAli.Bahrami@Sun.COM
1428744SAli.Bahrami@Sun.COM	return options['status']
1438744SAli.Bahrami@Sun.COM
1448744SAli.Bahrami@Sun.COMif __name__ == '__main__':
1458744SAli.Bahrami@Sun.COM	sys.exit(main(sys.argv[1:]))
146