1*e7aca734SRichard Lowe#! /usr/bin/python2.4 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# 24*e7aca734SRichard Lowe# Copyright 2009 Sun Microsystems, Inc. All rights reserved. 25cdf0c1d5Smjnelson# Use is subject to license terms. 26cdf0c1d5Smjnelson# 27cdf0c1d5Smjnelson 28cdf0c1d5Smjnelson# 29cdf0c1d5Smjnelson# Check for valid CDDL blocks in source files. 30cdf0c1d5Smjnelson# 31cdf0c1d5Smjnelson 32cdf0c1d5Smjnelsonimport sys, os, getopt, fnmatch 33cdf0c1d5Smjnelson 34cdf0c1d5Smjnelsonsys.path.append(os.path.join(os.path.dirname(__file__), '../lib/python')) 35cdf0c1d5Smjnelsonsys.path.append(os.path.join(os.path.dirname(__file__), '..')) 36cdf0c1d5Smjnelson 37cdf0c1d5Smjnelsonfrom onbld.Checks.Cddl import cddlchk 38cdf0c1d5Smjnelson 39cdf0c1d5Smjnelsonclass ExceptionList(object): 40cdf0c1d5Smjnelson def __init__(self): 41cdf0c1d5Smjnelson self.dirs = [] 42cdf0c1d5Smjnelson self.files = [] 43cdf0c1d5Smjnelson self.extensions = [] 44cdf0c1d5Smjnelson 45cdf0c1d5Smjnelson def load(self, exfile): 46cdf0c1d5Smjnelson fh = None 47cdf0c1d5Smjnelson try: 48cdf0c1d5Smjnelson fh = open(exfile, 'r') 49cdf0c1d5Smjnelson except IOError, e: 50cdf0c1d5Smjnelson sys.stderr.write('Failed to open exception list: ' 51cdf0c1d5Smjnelson '%s: %s\n' % (e.filename, e.strerror)) 52cdf0c1d5Smjnelson sys.exit(2) 53cdf0c1d5Smjnelson 54cdf0c1d5Smjnelson for line in fh: 55cdf0c1d5Smjnelson line = line.strip() 56cdf0c1d5Smjnelson 57cdf0c1d5Smjnelson if line.strip().endswith('/'): 58cdf0c1d5Smjnelson self.dirs.append(line[0:-1]) 59cdf0c1d5Smjnelson elif line.startswith('*.'): 60cdf0c1d5Smjnelson self.extensions.append(line) 61cdf0c1d5Smjnelson else: 62cdf0c1d5Smjnelson self.files.append(line) 63cdf0c1d5Smjnelson 64cdf0c1d5Smjnelson fh.close() 65cdf0c1d5Smjnelson 66cdf0c1d5Smjnelson def match(self, filename): 67cdf0c1d5Smjnelson if os.path.isdir(filename): 68cdf0c1d5Smjnelson return filename in self.dirs 69cdf0c1d5Smjnelson else: 70cdf0c1d5Smjnelson if filename in self.files: 71cdf0c1d5Smjnelson return True 72cdf0c1d5Smjnelson 73cdf0c1d5Smjnelson for pat in self.extensions: 74cdf0c1d5Smjnelson if fnmatch.fnmatch(filename, pat): 75cdf0c1d5Smjnelson return True 76cdf0c1d5Smjnelson 77cdf0c1d5Smjnelson def __contains__(self, elt): 78cdf0c1d5Smjnelson return self.match(elt) 79cdf0c1d5Smjnelson 80cdf0c1d5Smjnelsondef usage(): 81cdf0c1d5Smjnelson progname = os.path.split(sys.argv[0])[1] 82cdf0c1d5Smjnelson sys.stderr.write('''Usage: %s [-av] [-x exceptions] paths... 83cdf0c1d5Smjnelson -a check that all the specified files have a CDDL block. 84cdf0c1d5Smjnelson -v report on all files, not just those with errors. 85cdf0c1d5Smjnelson -x exceptions load an exceptions file 86cdf0c1d5Smjnelson''' % progname) 87cdf0c1d5Smjnelson sys.exit(2) 88cdf0c1d5Smjnelson 89cdf0c1d5Smjnelson 90cdf0c1d5Smjnelsondef check(filename, opts): 91cdf0c1d5Smjnelson try: 92cdf0c1d5Smjnelson fh = open(filename, 'r') 93cdf0c1d5Smjnelson except IOError, e: 94cdf0c1d5Smjnelson sys.stderr.write("failed to open '%s': %s\n" % 95cdf0c1d5Smjnelson (e.filename, e.strerror)) 96cdf0c1d5Smjnelson return 1 97cdf0c1d5Smjnelson else: 98cdf0c1d5Smjnelson return cddlchk(fh, verbose=opts['verbose'], 99cdf0c1d5Smjnelson lenient=opts['lenient'], 100cdf0c1d5Smjnelson output=sys.stdout) 101cdf0c1d5Smjnelson 102cdf0c1d5Smjnelsondef walker(opts, dirname, fnames): 103cdf0c1d5Smjnelson for f in fnames: 104cdf0c1d5Smjnelson path = os.path.join(dirname, f) 105cdf0c1d5Smjnelson 106cdf0c1d5Smjnelson if not os.path.isdir(path): 107cdf0c1d5Smjnelson if not path in opts['exclude']: 108cdf0c1d5Smjnelson opts['status'] |= check(path, opts) 109cdf0c1d5Smjnelson else: 110cdf0c1d5Smjnelson if path in opts['exclude']: 111cdf0c1d5Smjnelson fnames.remove(f) 112cdf0c1d5Smjnelson 113cdf0c1d5Smjnelsondef walkpath(path, opts): 114cdf0c1d5Smjnelson if os.path.isdir(path): 115cdf0c1d5Smjnelson os.path.walk(path, walker, opts) 116cdf0c1d5Smjnelson else: 117cdf0c1d5Smjnelson if not path in opts['exclude']: 118cdf0c1d5Smjnelson opts['status'] |= check(path, opts) 119cdf0c1d5Smjnelson 120cdf0c1d5Smjnelsondef main(args): 121cdf0c1d5Smjnelson options = { 122cdf0c1d5Smjnelson 'status': 0, 123cdf0c1d5Smjnelson 'lenient': True, 124cdf0c1d5Smjnelson 'verbose': False, 125cdf0c1d5Smjnelson 'exclude': ExceptionList() 126cdf0c1d5Smjnelson } 127cdf0c1d5Smjnelson 128cdf0c1d5Smjnelson try: 129cdf0c1d5Smjnelson opts, args = getopt.getopt(sys.argv[1:], 'avx:') 130cdf0c1d5Smjnelson except getopt.GetoptError: 131cdf0c1d5Smjnelson usage() 132cdf0c1d5Smjnelson sys.exit(2) 133cdf0c1d5Smjnelson 134cdf0c1d5Smjnelson for opt, arg in opts: 135cdf0c1d5Smjnelson if opt == '-a': 136cdf0c1d5Smjnelson options['lenient'] = False 137cdf0c1d5Smjnelson elif opt == '-v': 138cdf0c1d5Smjnelson options['verbose'] = True 139cdf0c1d5Smjnelson elif opt == '-x': 140cdf0c1d5Smjnelson options['exclude'].load(arg) 141cdf0c1d5Smjnelson 142cdf0c1d5Smjnelson for path in args: 143cdf0c1d5Smjnelson walkpath(path, options) 144cdf0c1d5Smjnelson 145cdf0c1d5Smjnelson return options['status'] 146cdf0c1d5Smjnelson 147cdf0c1d5Smjnelsonif __name__ == '__main__': 148cdf0c1d5Smjnelson sys.exit(main(sys.argv[1:])) 149