9f9230833b50b8271840dc2c12bd1e94d9df7d12Alexander Pyhalov#!@PYTHON@
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# CDDL HEADER START
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# The contents of this file are subject to the terms of the
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# Common Development and Distribution License (the "License").
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# You may not use this file except in compliance with the License.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# or http://www.opensolaris.org/os/licensing.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# See the License for the specific language governing permissions
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# and limitations under the License.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# When distributing Covered Code, include this CDDL HEADER in each
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# If applicable, add the following below this CDDL HEADER, with the
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# fields enclosed by brackets "[]" replaced with your own identifying
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# information: Portions Copyright [yyyy] [name of copyright owner]
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# CDDL HEADER END
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
87ab362234f761757d96ff1a758ab5c3bd85ed83Richard Lowe# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# Check for valid CDDL blocks in source files.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonimport sys, os, getopt, fnmatch
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
87ab362234f761757d96ff1a758ab5c3bd85ed83Richard Lowesys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "lib",
87ab362234f761757d96ff1a758ab5c3bd85ed83Richard Lowe "python%d.%d" % sys.version_info[:2]))
87ab362234f761757d96ff1a758ab5c3bd85ed83Richard Lowe
87ab362234f761757d96ff1a758ab5c3bd85ed83Richard Lowe# Allow running from the source tree, using the modules in the source tree
87ab362234f761757d96ff1a758ab5c3bd85ed83Richard Lowesys.path.insert(2, os.path.join(os.path.dirname(__file__), '..'))
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonfrom onbld.Checks.Cddl import cddlchk
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonclass ExceptionList(object):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson def __init__(self):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson self.dirs = []
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson self.files = []
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson self.extensions = []
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson def load(self, exfile):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson fh = None
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson try:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson fh = open(exfile, 'r')
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson except IOError, e:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson sys.stderr.write('Failed to open exception list: '
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson '%s: %s\n' % (e.filename, e.strerror))
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson sys.exit(2)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson for line in fh:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson line = line.strip()
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if line.strip().endswith('/'):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson self.dirs.append(line[0:-1])
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson elif line.startswith('*.'):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson self.extensions.append(line)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson else:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson self.files.append(line)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson fh.close()
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson def match(self, filename):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if os.path.isdir(filename):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson return filename in self.dirs
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson else:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if filename in self.files:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson return True
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson for pat in self.extensions:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if fnmatch.fnmatch(filename, pat):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson return True
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson def __contains__(self, elt):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson return self.match(elt)
e7aca7349385967a98ed221ad54db99998d477e8Richard Lowe
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsondef usage():
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson progname = os.path.split(sys.argv[0])[1]
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson sys.stderr.write('''Usage: %s [-av] [-x exceptions] paths...
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson -a check that all the specified files have a CDDL block.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson -v report on all files, not just those with errors.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson -x exceptions load an exceptions file
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson''' % progname)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson sys.exit(2)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsondef check(filename, opts):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson try:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson fh = open(filename, 'r')
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson except IOError, e:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson sys.stderr.write("failed to open '%s': %s\n" %
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson (e.filename, e.strerror))
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson return 1
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson else:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson return cddlchk(fh, verbose=opts['verbose'],
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson lenient=opts['lenient'],
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson output=sys.stdout)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsondef walker(opts, dirname, fnames):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson for f in fnames:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson path = os.path.join(dirname, f)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if not os.path.isdir(path):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if not path in opts['exclude']:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson opts['status'] |= check(path, opts)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson else:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if path in opts['exclude']:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson fnames.remove(f)
e7aca7349385967a98ed221ad54db99998d477e8Richard Lowe
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsondef walkpath(path, opts):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if os.path.isdir(path):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson os.path.walk(path, walker, opts)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson else:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if not path in opts['exclude']:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson opts['status'] |= check(path, opts)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsondef main(args):
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson options = {
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson 'status': 0,
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson 'lenient': True,
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson 'verbose': False,
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson 'exclude': ExceptionList()
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson }
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson try:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson opts, args = getopt.getopt(sys.argv[1:], 'avx:')
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson except getopt.GetoptError:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson usage()
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson sys.exit(2)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson for opt, arg in opts:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if opt == '-a':
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson options['lenient'] = False
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson elif opt == '-v':
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson options['verbose'] = True
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson elif opt == '-x':
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson options['exclude'].load(arg)
e7aca7349385967a98ed221ad54db99998d477e8Richard Lowe
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson for path in args:
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson walkpath(path, options)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson return options['status']
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonif __name__ == '__main__':
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson sys.exit(main(sys.argv[1:]))