1*6066d16eSPiotr Jasiukajtis#!/usr/bin/python2.6 2cdf0c1d5Smjnelson# 3cdf0c1d5Smjnelson# CDDL HEADER START 4cdf0c1d5Smjnelson# 5cdf0c1d5Smjnelson# The contents of this file are subject to the terms of the 6cdf0c1d5Smjnelson# Common Development and Distribution License (the "License"). 7cdf0c1d5Smjnelson# You may not use this file except in compliance with the License. 8cdf0c1d5Smjnelson# 9cdf0c1d5Smjnelson# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10cdf0c1d5Smjnelson# or http://www.opensolaris.org/os/licensing. 11cdf0c1d5Smjnelson# See the License for the specific language governing permissions 12cdf0c1d5Smjnelson# and limitations under the License. 13cdf0c1d5Smjnelson# 14cdf0c1d5Smjnelson# When distributing Covered Code, include this CDDL HEADER in each 15cdf0c1d5Smjnelson# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16cdf0c1d5Smjnelson# If applicable, add the following below this CDDL HEADER, with the 17cdf0c1d5Smjnelson# fields enclosed by brackets "[]" replaced with your own identifying 18cdf0c1d5Smjnelson# information: Portions Copyright [yyyy] [name of copyright owner] 19cdf0c1d5Smjnelson# 20cdf0c1d5Smjnelson# CDDL HEADER END 21cdf0c1d5Smjnelson# 22cdf0c1d5Smjnelson 23cdf0c1d5Smjnelson# 2487ab3622SRichard Lowe# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 25cdf0c1d5Smjnelson# 26cdf0c1d5Smjnelson 27cdf0c1d5Smjnelson# 28cdf0c1d5Smjnelson# Check for valid CDDL blocks in source files. 29cdf0c1d5Smjnelson# 30cdf0c1d5Smjnelson 31cdf0c1d5Smjnelsonimport sys, os, getopt, fnmatch 32cdf0c1d5Smjnelson 3387ab3622SRichard Lowesys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "lib", 3487ab3622SRichard Lowe "python%d.%d" % sys.version_info[:2])) 3587ab3622SRichard Lowe 3687ab3622SRichard Lowe# Allow running from the source tree, using the modules in the source tree 3787ab3622SRichard Lowesys.path.insert(2, os.path.join(os.path.dirname(__file__), '..')) 38cdf0c1d5Smjnelson 39cdf0c1d5Smjnelsonfrom onbld.Checks.Cddl import cddlchk 40cdf0c1d5Smjnelson 41cdf0c1d5Smjnelsonclass ExceptionList(object): 42cdf0c1d5Smjnelson def __init__(self): 43cdf0c1d5Smjnelson self.dirs = [] 44cdf0c1d5Smjnelson self.files = [] 45cdf0c1d5Smjnelson self.extensions = [] 46cdf0c1d5Smjnelson 47cdf0c1d5Smjnelson def load(self, exfile): 48cdf0c1d5Smjnelson fh = None 49cdf0c1d5Smjnelson try: 50cdf0c1d5Smjnelson fh = open(exfile, 'r') 51cdf0c1d5Smjnelson except IOError, e: 52cdf0c1d5Smjnelson sys.stderr.write('Failed to open exception list: ' 53cdf0c1d5Smjnelson '%s: %s\n' % (e.filename, e.strerror)) 54cdf0c1d5Smjnelson sys.exit(2) 55cdf0c1d5Smjnelson 56cdf0c1d5Smjnelson for line in fh: 57cdf0c1d5Smjnelson line = line.strip() 58cdf0c1d5Smjnelson 59cdf0c1d5Smjnelson if line.strip().endswith('/'): 60cdf0c1d5Smjnelson self.dirs.append(line[0:-1]) 61cdf0c1d5Smjnelson elif line.startswith('*.'): 62cdf0c1d5Smjnelson self.extensions.append(line) 63cdf0c1d5Smjnelson else: 64cdf0c1d5Smjnelson self.files.append(line) 65cdf0c1d5Smjnelson 66cdf0c1d5Smjnelson fh.close() 67cdf0c1d5Smjnelson 68cdf0c1d5Smjnelson def match(self, filename): 69cdf0c1d5Smjnelson if os.path.isdir(filename): 70cdf0c1d5Smjnelson return filename in self.dirs 71cdf0c1d5Smjnelson else: 72cdf0c1d5Smjnelson if filename in self.files: 73cdf0c1d5Smjnelson return True 74cdf0c1d5Smjnelson 75cdf0c1d5Smjnelson for pat in self.extensions: 76cdf0c1d5Smjnelson if fnmatch.fnmatch(filename, pat): 77cdf0c1d5Smjnelson return True 78cdf0c1d5Smjnelson 79cdf0c1d5Smjnelson def __contains__(self, elt): 80cdf0c1d5Smjnelson return self.match(elt) 81cdf0c1d5Smjnelson 82cdf0c1d5Smjnelsondef usage(): 83cdf0c1d5Smjnelson progname = os.path.split(sys.argv[0])[1] 84cdf0c1d5Smjnelson sys.stderr.write('''Usage: %s [-av] [-x exceptions] paths... 85cdf0c1d5Smjnelson -a check that all the specified files have a CDDL block. 86cdf0c1d5Smjnelson -v report on all files, not just those with errors. 87cdf0c1d5Smjnelson -x exceptions load an exceptions file 88cdf0c1d5Smjnelson''' % progname) 89cdf0c1d5Smjnelson sys.exit(2) 90cdf0c1d5Smjnelson 91cdf0c1d5Smjnelson 92cdf0c1d5Smjnelsondef check(filename, opts): 93cdf0c1d5Smjnelson try: 94cdf0c1d5Smjnelson fh = open(filename, 'r') 95cdf0c1d5Smjnelson except IOError, e: 96cdf0c1d5Smjnelson sys.stderr.write("failed to open '%s': %s\n" % 97cdf0c1d5Smjnelson (e.filename, e.strerror)) 98cdf0c1d5Smjnelson return 1 99cdf0c1d5Smjnelson else: 100cdf0c1d5Smjnelson return cddlchk(fh, verbose=opts['verbose'], 101cdf0c1d5Smjnelson lenient=opts['lenient'], 102cdf0c1d5Smjnelson output=sys.stdout) 103cdf0c1d5Smjnelson 104cdf0c1d5Smjnelsondef walker(opts, dirname, fnames): 105cdf0c1d5Smjnelson for f in fnames: 106cdf0c1d5Smjnelson path = os.path.join(dirname, f) 107cdf0c1d5Smjnelson 108cdf0c1d5Smjnelson if not os.path.isdir(path): 109cdf0c1d5Smjnelson if not path in opts['exclude']: 110cdf0c1d5Smjnelson opts['status'] |= check(path, opts) 111cdf0c1d5Smjnelson else: 112cdf0c1d5Smjnelson if path in opts['exclude']: 113cdf0c1d5Smjnelson fnames.remove(f) 114cdf0c1d5Smjnelson 115cdf0c1d5Smjnelsondef walkpath(path, opts): 116cdf0c1d5Smjnelson if os.path.isdir(path): 117cdf0c1d5Smjnelson os.path.walk(path, walker, opts) 118cdf0c1d5Smjnelson else: 119cdf0c1d5Smjnelson if not path in opts['exclude']: 120cdf0c1d5Smjnelson opts['status'] |= check(path, opts) 121cdf0c1d5Smjnelson 122cdf0c1d5Smjnelsondef main(args): 123cdf0c1d5Smjnelson options = { 124cdf0c1d5Smjnelson 'status': 0, 125cdf0c1d5Smjnelson 'lenient': True, 126cdf0c1d5Smjnelson 'verbose': False, 127cdf0c1d5Smjnelson 'exclude': ExceptionList() 128cdf0c1d5Smjnelson } 129cdf0c1d5Smjnelson 130cdf0c1d5Smjnelson try: 131cdf0c1d5Smjnelson opts, args = getopt.getopt(sys.argv[1:], 'avx:') 132cdf0c1d5Smjnelson except getopt.GetoptError: 133cdf0c1d5Smjnelson usage() 134cdf0c1d5Smjnelson sys.exit(2) 135cdf0c1d5Smjnelson 136cdf0c1d5Smjnelson for opt, arg in opts: 137cdf0c1d5Smjnelson if opt == '-a': 138cdf0c1d5Smjnelson options['lenient'] = False 139cdf0c1d5Smjnelson elif opt == '-v': 140cdf0c1d5Smjnelson options['verbose'] = True 141cdf0c1d5Smjnelson elif opt == '-x': 142cdf0c1d5Smjnelson options['exclude'].load(arg) 143cdf0c1d5Smjnelson 144cdf0c1d5Smjnelson for path in args: 145cdf0c1d5Smjnelson walkpath(path, options) 146cdf0c1d5Smjnelson 147cdf0c1d5Smjnelson return options['status'] 148cdf0c1d5Smjnelson 149cdf0c1d5Smjnelsonif __name__ == '__main__': 150cdf0c1d5Smjnelson sys.exit(main(sys.argv[1:])) 151