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