16684 Solaris 10 zones and libproc broken by new parameter to fcntl
[illumos-gate.git] / usr / src / tools / scripts / mapfilechk.py
blob662505a6a72240fa4425317d202082e4448a5108
1 #!@TOOLS_PYTHON@ -Es
3 # CDDL HEADER START
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.
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.
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]
20 # CDDL HEADER END
24 # Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25 # Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
29 # Check for valid link-editor mapfile comment blocks in source files.
32 import sys, os, io, getopt, fnmatch
34 sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "lib",
35 "python%d.%d" % sys.version_info[:2]))
37 # Allow running from the source tree, using the modules in the source tree
38 sys.path.insert(2, os.path.join(os.path.dirname(__file__), '..'))
40 from onbld.Checks.Mapfile import mapfilechk
42 class ExceptionList(object):
43 def __init__(self):
44 self.dirs = []
45 self.files = []
46 self.extensions = []
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)
57 for line in fh:
58 line = line.strip()
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)
67 fh.close()
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
76 for pat in self.extensions:
77 if fnmatch.fnmatch(filename, pat):
78 return True
80 def __contains__(self, elt):
81 return self.match(elt)
83 def 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)
92 def 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
103 def walker(opts, dirname, fnames):
104 for f in fnames:
105 path = os.path.join(dirname, f)
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)
114 def 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)
121 def main(args):
122 options = {
123 'status': 0,
124 'verbose': False,
125 'exclude': ExceptionList()
128 try:
129 opts, args = getopt.getopt(sys.argv[1:], 'avx:')
130 except getopt.GetoptError:
131 usage()
132 sys.exit(2)
134 for opt, arg in opts:
135 if opt == '-v':
136 options['verbose'] = True
137 elif opt == '-x':
138 options['exclude'].load(arg)
140 for path in args:
141 walkpath(path, options)
143 return options['status']
145 if __name__ == '__main__':
146 sys.exit(main(sys.argv[1:]))