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