xref: /titanic_51/usr/src/tools/scripts/mapfilechk.py (revision 68c47f65208790c466e5e484f2293d3baed71c6a)
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 link-editor mapfile comment 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.Mapfile import mapfilechk
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 [-v] [-x exceptions] paths...
83        -v		report on all files, not just those with errors.
84        -x exceptions	load an exceptions file
85''' % progname)
86	sys.exit(2)
87
88
89def check(filename, opts):
90	try:
91		fh = open(filename, 'r')
92	except IOError, e:
93		sys.stderr.write("failed to open '%s': %s\n" %
94				 (e.filename, e.strerror))
95		return 1
96	else:
97		return mapfilechk(fh, verbose=opts['verbose'],
98			       output=sys.stdout)
99
100def walker(opts, dirname, fnames):
101	for f in fnames:
102		path = os.path.join(dirname, f)
103
104		if not os.path.isdir(path):
105			if not path in opts['exclude']:
106				opts['status'] |= check(path, opts)
107		else:
108			if path in opts['exclude']:
109				fnames.remove(f)
110
111def walkpath(path, opts):
112	if os.path.isdir(path):
113		os.path.walk(path, walker, opts)
114	else:
115		if not path in opts['exclude']:
116			opts['status'] |= check(path, opts)
117
118def main(args):
119	options = {
120		'status': 0,
121		'verbose': False,
122		'exclude': ExceptionList()
123	}
124
125	try:
126		opts, args = getopt.getopt(sys.argv[1:], 'avx:')
127	except getopt.GetoptError:
128		usage()
129		sys.exit(2)
130
131	for opt, arg in opts:
132		if opt == '-v':
133			options['verbose'] = True
134		elif opt == '-x':
135			options['exclude'].load(arg)
136
137	for path in args:
138		walkpath(path, options)
139
140	return options['status']
141
142if __name__ == '__main__':
143	sys.exit(main(sys.argv[1:]))
144