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