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]
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):
48 def load(self
, exfile
):
51 fh
= open(exfile
, 'r')
53 sys
.stderr
.write('Failed to open exception list: '
54 '%s: %s\n' % (e
.filename
, e
.strerror
))
60 if line
.strip().endswith('/'):
61 self
.dirs
.append(line
[0:-1])
62 elif line
.startswith('*.'):
63 self
.extensions
.append(line
)
65 self
.files
.append(line
)
69 def match(self
, filename
):
70 if os
.path
.isdir(filename
):
71 return filename
in self
.dirs
73 if filename
in self
.files
:
76 for pat
in self
.extensions
:
77 if fnmatch
.fnmatch(filename
, pat
):
80 def __contains__(self
, elt
):
81 return self
.match(elt
)
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
92 def check(filename
, opts
):
94 with io
.open(filename
, encoding
='utf-8',
95 errors
='replace') as fh
:
96 return mapfilechk(fh
, verbose
=opts
['verbose'],
99 sys
.stderr
.write("failed to open '%s': %s\n" %
100 (e
.filename
, e
.strerror
))
103 def walker(opts
, dirname
, 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
)
111 if path
in opts
['exclude']:
114 def walkpath(path
, opts
):
115 if os
.path
.isdir(path
):
116 os
.path
.walk(path
, walker
, opts
)
118 if not path
in opts
['exclude']:
119 opts
['status'] |
= check(path
, opts
)
125 'exclude': ExceptionList()
129 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'avx:')
130 except getopt
.GetoptError
:
134 for opt
, arg
in opts
:
136 options
['verbose'] = True
138 options
['exclude'].load(arg
)
141 walkpath(path
, options
)
143 return options
['status']
145 if __name__
== '__main__':
146 sys
.exit(main(sys
.argv
[1:]))