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