1*6066d16eSPiotr Jasiukajtis#!/usr/bin/python2.6 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. 25bfed486aSAli Bahrami# 26bfed486aSAli Bahrami 27bfed486aSAli Bahrami# 28bfed486aSAli Bahrami# Check for valid link-editor mapfile comment blocks in source files. 29bfed486aSAli Bahrami# 30bfed486aSAli Bahrami 31bfed486aSAli Bahramiimport sys, os, getopt, fnmatch 32bfed486aSAli Bahrami 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__), '..')) 38bfed486aSAli Bahrami 39bfed486aSAli Bahramifrom onbld.Checks.Mapfile import mapfilechk 40bfed486aSAli Bahrami 41bfed486aSAli Bahramiclass ExceptionList(object): 42bfed486aSAli Bahrami def __init__(self): 43bfed486aSAli Bahrami self.dirs = [] 44bfed486aSAli Bahrami self.files = [] 45bfed486aSAli Bahrami self.extensions = [] 46bfed486aSAli Bahrami 47bfed486aSAli Bahrami def load(self, exfile): 48bfed486aSAli Bahrami fh = None 49bfed486aSAli Bahrami try: 50bfed486aSAli Bahrami fh = open(exfile, 'r') 51bfed486aSAli Bahrami except IOError, e: 52bfed486aSAli Bahrami sys.stderr.write('Failed to open exception list: ' 53bfed486aSAli Bahrami '%s: %s\n' % (e.filename, e.strerror)) 54bfed486aSAli Bahrami sys.exit(2) 55bfed486aSAli Bahrami 56bfed486aSAli Bahrami for line in fh: 57bfed486aSAli Bahrami line = line.strip() 58bfed486aSAli Bahrami 59bfed486aSAli Bahrami if line.strip().endswith('/'): 60bfed486aSAli Bahrami self.dirs.append(line[0:-1]) 61bfed486aSAli Bahrami elif line.startswith('*.'): 62bfed486aSAli Bahrami self.extensions.append(line) 63bfed486aSAli Bahrami else: 64bfed486aSAli Bahrami self.files.append(line) 65bfed486aSAli Bahrami 66bfed486aSAli Bahrami fh.close() 67bfed486aSAli Bahrami 68bfed486aSAli Bahrami def match(self, filename): 69bfed486aSAli Bahrami if os.path.isdir(filename): 70bfed486aSAli Bahrami return filename in self.dirs 71bfed486aSAli Bahrami else: 72bfed486aSAli Bahrami if filename in self.files: 73bfed486aSAli Bahrami return True 74bfed486aSAli Bahrami 75bfed486aSAli Bahrami for pat in self.extensions: 76bfed486aSAli Bahrami if fnmatch.fnmatch(filename, pat): 77bfed486aSAli Bahrami return True 78bfed486aSAli Bahrami 79bfed486aSAli Bahrami def __contains__(self, elt): 80bfed486aSAli Bahrami return self.match(elt) 81bfed486aSAli Bahrami 82bfed486aSAli Bahramidef usage(): 83bfed486aSAli Bahrami progname = os.path.split(sys.argv[0])[1] 84bfed486aSAli Bahrami sys.stderr.write('''Usage: %s [-v] [-x exceptions] paths... 85bfed486aSAli Bahrami -v report on all files, not just those with errors. 86bfed486aSAli Bahrami -x exceptions load an exceptions file 87bfed486aSAli Bahrami''' % progname) 88bfed486aSAli Bahrami sys.exit(2) 89bfed486aSAli Bahrami 90bfed486aSAli Bahrami 91bfed486aSAli Bahramidef check(filename, opts): 92bfed486aSAli Bahrami try: 93bfed486aSAli Bahrami fh = open(filename, 'r') 94bfed486aSAli Bahrami except IOError, e: 95bfed486aSAli Bahrami sys.stderr.write("failed to open '%s': %s\n" % 96bfed486aSAli Bahrami (e.filename, e.strerror)) 97bfed486aSAli Bahrami return 1 98bfed486aSAli Bahrami else: 99bfed486aSAli Bahrami return mapfilechk(fh, verbose=opts['verbose'], 100bfed486aSAli Bahrami output=sys.stdout) 101bfed486aSAli Bahrami 102bfed486aSAli Bahramidef walker(opts, dirname, fnames): 103bfed486aSAli Bahrami for f in fnames: 104bfed486aSAli Bahrami path = os.path.join(dirname, f) 105bfed486aSAli Bahrami 106bfed486aSAli Bahrami if not os.path.isdir(path): 107bfed486aSAli Bahrami if not path in opts['exclude']: 108bfed486aSAli Bahrami opts['status'] |= check(path, opts) 109bfed486aSAli Bahrami else: 110bfed486aSAli Bahrami if path in opts['exclude']: 111bfed486aSAli Bahrami fnames.remove(f) 112bfed486aSAli Bahrami 113bfed486aSAli Bahramidef walkpath(path, opts): 114bfed486aSAli Bahrami if os.path.isdir(path): 115bfed486aSAli Bahrami os.path.walk(path, walker, opts) 116bfed486aSAli Bahrami else: 117bfed486aSAli Bahrami if not path in opts['exclude']: 118bfed486aSAli Bahrami opts['status'] |= check(path, opts) 119bfed486aSAli Bahrami 120bfed486aSAli Bahramidef main(args): 121bfed486aSAli Bahrami options = { 122bfed486aSAli Bahrami 'status': 0, 123bfed486aSAli Bahrami 'verbose': False, 124bfed486aSAli Bahrami 'exclude': ExceptionList() 125bfed486aSAli Bahrami } 126bfed486aSAli Bahrami 127bfed486aSAli Bahrami try: 128bfed486aSAli Bahrami opts, args = getopt.getopt(sys.argv[1:], 'avx:') 129bfed486aSAli Bahrami except getopt.GetoptError: 130bfed486aSAli Bahrami usage() 131bfed486aSAli Bahrami sys.exit(2) 132bfed486aSAli Bahrami 133bfed486aSAli Bahrami for opt, arg in opts: 134bfed486aSAli Bahrami if opt == '-v': 135bfed486aSAli Bahrami options['verbose'] = True 136bfed486aSAli Bahrami elif opt == '-x': 137bfed486aSAli Bahrami options['exclude'].load(arg) 138bfed486aSAli Bahrami 139bfed486aSAli Bahrami for path in args: 140bfed486aSAli Bahrami walkpath(path, options) 141bfed486aSAli Bahrami 142bfed486aSAli Bahrami return options['status'] 143bfed486aSAli Bahrami 144bfed486aSAli Bahramiif __name__ == '__main__': 145bfed486aSAli Bahrami sys.exit(main(sys.argv[1:])) 146