xref: /titanic_53/usr/src/tools/scripts/mapfilechk.py (revision e7aca7349385967a98ed221ad54db99998d477e8)
1*e7aca734SRichard Lowe#! /usr/bin/python2.4
2bfed486aSAli Bahrami#
3bfed486aSAli Bahrami# CDDL HEADER START
4bfed486aSAli Bahrami#
5bfed486aSAli Bahrami# The contents of this file are subject to the terms of the
6bfed486aSAli Bahrami# Common Development and Distribution License (the "License").
7bfed486aSAli Bahrami# You may not use this file except in compliance with the License.
8bfed486aSAli Bahrami#
9bfed486aSAli Bahrami# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10bfed486aSAli Bahrami# or http://www.opensolaris.org/os/licensing.
11bfed486aSAli Bahrami# See the License for the specific language governing permissions
12bfed486aSAli Bahrami# and limitations under the License.
13bfed486aSAli Bahrami#
14bfed486aSAli Bahrami# When distributing Covered Code, include this CDDL HEADER in each
15bfed486aSAli Bahrami# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16bfed486aSAli Bahrami# If applicable, add the following below this CDDL HEADER, with the
17bfed486aSAli Bahrami# fields enclosed by brackets "[]" replaced with your own identifying
18bfed486aSAli Bahrami# information: Portions Copyright [yyyy] [name of copyright owner]
19bfed486aSAli Bahrami#
20bfed486aSAli Bahrami# CDDL HEADER END
21bfed486aSAli Bahrami#
22bfed486aSAli Bahrami
23bfed486aSAli Bahrami#
24bfed486aSAli Bahrami# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25bfed486aSAli Bahrami# Use is subject to license terms.
26bfed486aSAli Bahrami#
27bfed486aSAli Bahrami
28bfed486aSAli Bahrami#
29bfed486aSAli Bahrami# Check for valid link-editor mapfile comment blocks in source files.
30bfed486aSAli Bahrami#
31bfed486aSAli Bahrami
32bfed486aSAli Bahramiimport sys, os, getopt, fnmatch
33bfed486aSAli Bahrami
34bfed486aSAli Bahramisys.path.append(os.path.join(os.path.dirname(__file__), '../lib/python'))
35bfed486aSAli Bahramisys.path.append(os.path.join(os.path.dirname(__file__), '..'))
36bfed486aSAli Bahrami
37bfed486aSAli Bahramifrom onbld.Checks.Mapfile import mapfilechk
38bfed486aSAli Bahrami
39bfed486aSAli Bahramiclass ExceptionList(object):
40bfed486aSAli Bahrami	def __init__(self):
41bfed486aSAli Bahrami		self.dirs = []
42bfed486aSAli Bahrami		self.files = []
43bfed486aSAli Bahrami		self.extensions = []
44bfed486aSAli Bahrami
45bfed486aSAli Bahrami	def load(self, exfile):
46bfed486aSAli Bahrami		fh = None
47bfed486aSAli Bahrami		try:
48bfed486aSAli Bahrami			fh = open(exfile, 'r')
49bfed486aSAli Bahrami		except IOError, e:
50bfed486aSAli Bahrami			sys.stderr.write('Failed to open exception list: '
51bfed486aSAli Bahrami					 '%s: %s\n' % (e.filename, e.strerror))
52bfed486aSAli Bahrami			sys.exit(2)
53bfed486aSAli Bahrami
54bfed486aSAli Bahrami		for line in fh:
55bfed486aSAli Bahrami			line = line.strip()
56bfed486aSAli Bahrami
57bfed486aSAli Bahrami			if line.strip().endswith('/'):
58bfed486aSAli Bahrami				self.dirs.append(line[0:-1])
59bfed486aSAli Bahrami			elif line.startswith('*.'):
60bfed486aSAli Bahrami				self.extensions.append(line)
61bfed486aSAli Bahrami			else:
62bfed486aSAli Bahrami				self.files.append(line)
63bfed486aSAli Bahrami
64bfed486aSAli Bahrami		fh.close()
65bfed486aSAli Bahrami
66bfed486aSAli Bahrami	def match(self, filename):
67bfed486aSAli Bahrami		if os.path.isdir(filename):
68bfed486aSAli Bahrami			return filename in self.dirs
69bfed486aSAli Bahrami		else:
70bfed486aSAli Bahrami			if filename in self.files:
71bfed486aSAli Bahrami				return True
72bfed486aSAli Bahrami
73bfed486aSAli Bahrami			for pat in self.extensions:
74bfed486aSAli Bahrami				if fnmatch.fnmatch(filename, pat):
75bfed486aSAli Bahrami					return True
76bfed486aSAli Bahrami
77bfed486aSAli Bahrami	def __contains__(self, elt):
78bfed486aSAli Bahrami		return self.match(elt)
79bfed486aSAli Bahrami
80bfed486aSAli Bahramidef usage():
81bfed486aSAli Bahrami	progname = os.path.split(sys.argv[0])[1]
82bfed486aSAli Bahrami	sys.stderr.write('''Usage: %s [-v] [-x exceptions] paths...
83bfed486aSAli Bahrami        -v		report on all files, not just those with errors.
84bfed486aSAli Bahrami        -x exceptions	load an exceptions file
85bfed486aSAli Bahrami''' % progname)
86bfed486aSAli Bahrami	sys.exit(2)
87bfed486aSAli Bahrami
88bfed486aSAli Bahrami
89bfed486aSAli Bahramidef check(filename, opts):
90bfed486aSAli Bahrami	try:
91bfed486aSAli Bahrami		fh = open(filename, 'r')
92bfed486aSAli Bahrami	except IOError, e:
93bfed486aSAli Bahrami		sys.stderr.write("failed to open '%s': %s\n" %
94bfed486aSAli Bahrami				 (e.filename, e.strerror))
95bfed486aSAli Bahrami		return 1
96bfed486aSAli Bahrami	else:
97bfed486aSAli Bahrami		return mapfilechk(fh, verbose=opts['verbose'],
98bfed486aSAli Bahrami			       output=sys.stdout)
99bfed486aSAli Bahrami
100bfed486aSAli Bahramidef walker(opts, dirname, fnames):
101bfed486aSAli Bahrami	for f in fnames:
102bfed486aSAli Bahrami		path = os.path.join(dirname, f)
103bfed486aSAli Bahrami
104bfed486aSAli Bahrami		if not os.path.isdir(path):
105bfed486aSAli Bahrami			if not path in opts['exclude']:
106bfed486aSAli Bahrami				opts['status'] |= check(path, opts)
107bfed486aSAli Bahrami		else:
108bfed486aSAli Bahrami			if path in opts['exclude']:
109bfed486aSAli Bahrami				fnames.remove(f)
110bfed486aSAli Bahrami
111bfed486aSAli Bahramidef walkpath(path, opts):
112bfed486aSAli Bahrami	if os.path.isdir(path):
113bfed486aSAli Bahrami		os.path.walk(path, walker, opts)
114bfed486aSAli Bahrami	else:
115bfed486aSAli Bahrami		if not path in opts['exclude']:
116bfed486aSAli Bahrami			opts['status'] |= check(path, opts)
117bfed486aSAli Bahrami
118bfed486aSAli Bahramidef main(args):
119bfed486aSAli Bahrami	options = {
120bfed486aSAli Bahrami		'status': 0,
121bfed486aSAli Bahrami		'verbose': False,
122bfed486aSAli Bahrami		'exclude': ExceptionList()
123bfed486aSAli Bahrami	}
124bfed486aSAli Bahrami
125bfed486aSAli Bahrami	try:
126bfed486aSAli Bahrami		opts, args = getopt.getopt(sys.argv[1:], 'avx:')
127bfed486aSAli Bahrami	except getopt.GetoptError:
128bfed486aSAli Bahrami		usage()
129bfed486aSAli Bahrami		sys.exit(2)
130bfed486aSAli Bahrami
131bfed486aSAli Bahrami	for opt, arg in opts:
132bfed486aSAli Bahrami		if opt == '-v':
133bfed486aSAli Bahrami			options['verbose'] = True
134bfed486aSAli Bahrami		elif opt == '-x':
135bfed486aSAli Bahrami			options['exclude'].load(arg)
136bfed486aSAli Bahrami
137bfed486aSAli Bahrami	for path in args:
138bfed486aSAli Bahrami		walkpath(path, options)
139bfed486aSAli Bahrami
140bfed486aSAli Bahrami	return options['status']
141bfed486aSAli Bahrami
142bfed486aSAli Bahramiif __name__ == '__main__':
143bfed486aSAli Bahrami	sys.exit(main(sys.argv[1:]))
144