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.
28 # Check for valid CDDL blocks in source files.
31 import sys
, os
, getopt
, fnmatch
33 sys
.path
.insert(1, os
.path
.join(os
.path
.dirname(__file__
), "..", "lib",
34 "python%d.%d" % sys
.version_info
[:2]))
36 # Allow running from the source tree, using the modules in the source tree
37 sys
.path
.insert(2, os
.path
.join(os
.path
.dirname(__file__
), '..'))
39 from onbld
.Checks
.Cddl
import cddlchk
41 class ExceptionList(object):
47 def load(self
, exfile
):
50 fh
= open(exfile
, 'r')
52 sys
.stderr
.write('Failed to open exception list: '
53 '%s: %s\n' % (e
.filename
, e
.strerror
))
59 if line
.strip().endswith('/'):
60 self
.dirs
.append(line
[0:-1])
61 elif line
.startswith('*.'):
62 self
.extensions
.append(line
)
64 self
.files
.append(line
)
68 def match(self
, filename
):
69 if os
.path
.isdir(filename
):
70 return filename
in self
.dirs
72 if filename
in self
.files
:
75 for pat
in self
.extensions
:
76 if fnmatch
.fnmatch(filename
, pat
):
79 def __contains__(self
, elt
):
80 return self
.match(elt
)
83 progname
= os
.path
.split(sys
.argv
[0])[1]
84 sys
.stderr
.write('''Usage: %s [-av] [-x exceptions] paths...
85 -a check that all the specified files have a CDDL block.
86 -v report on all files, not just those with errors.
87 -x exceptions load an exceptions file
92 def check(filename
, opts
):
94 fh
= open(filename
, 'r')
96 sys
.stderr
.write("failed to open '%s': %s\n" %
97 (e
.filename
, e
.strerror
))
100 return cddlchk(fh
, verbose
=opts
['verbose'],
101 lenient
=opts
['lenient'],
104 def walker(opts
, dirname
, fnames
):
106 path
= os
.path
.join(dirname
, f
)
108 if not os
.path
.isdir(path
):
109 if not path
in opts
['exclude']:
110 opts
['status'] |
= check(path
, opts
)
112 if path
in opts
['exclude']:
115 def walkpath(path
, opts
):
116 if os
.path
.isdir(path
):
117 os
.path
.walk(path
, walker
, opts
)
119 if not path
in opts
['exclude']:
120 opts
['status'] |
= check(path
, opts
)
127 'exclude': ExceptionList()
131 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'avx:')
132 except getopt
.GetoptError
:
136 for opt
, arg
in opts
:
138 options
['lenient'] = False
140 options
['verbose'] = True
142 options
['exclude'].load(arg
)
145 walkpath(path
, options
)
147 return options
['status']
149 if __name__
== '__main__':
150 sys
.exit(main(sys
.argv
[1:]))