1# Author: Lisandro Dalcin 2# Contact: dalcinl@gmail.com 3import os 4import sys 5import optparse 6import unittest 7 8__unittest = True 9 10components = [ 11 'PETSc', 12] 13 14 15def getoptionparser(): 16 parser = optparse.OptionParser() 17 18 parser.add_option("-q", "--quiet", 19 action="store_const", const=0, dest="verbose", default=1, 20 help="do not print status messages to stdout") 21 parser.add_option("-v", "--verbose", 22 action="store_const", const=2, dest="verbose", default=1, 23 help="print status messages to stdout") 24 parser.add_option("-i", "--include", type="string", 25 action="append", dest="include", default=[], 26 help="include tests matching PATTERN", metavar="PATTERN") 27 parser.add_option("-e", "--exclude", type="string", 28 action="append", dest="exclude", default=[], 29 help="exclude tests matching PATTERN", metavar="PATTERN") 30 parser.add_option("-k", "--pattern", type="string", 31 action="append", dest="patterns", default=[], 32 help="only run tests which match the given substring") 33 parser.add_option("-f", "--failfast", 34 action="store_true", dest="failfast", default=False, 35 help="Stop on first failure") 36 parser.add_option("--no-builddir", 37 action="store_false", dest="builddir", default=True, 38 help="disable testing from build directory") 39 parser.add_option("--path", type="string", 40 action="append", dest="path", default=[], 41 help="prepend PATH to sys.path", metavar="PATH") 42 parser.add_option("--arch", type="string", 43 action="store", dest="arch", default=None, 44 help="use PETSC_ARCH", 45 metavar="PETSC_ARCH") 46 parser.add_option("-s","--summary", 47 action="store_true", dest="summary", default=0, 48 help="print PETSc log summary") 49 parser.add_option("--no-memdebug", 50 action="store_false", dest="memdebug", default=True, 51 help="Do not use PETSc memory debugging") 52 return parser 53 54 55def getbuilddir(): 56 try: 57 try: 58 from setuptools.dist import Distribution 59 except ImportError: 60 from distutils.dist import Distribution 61 try: 62 from setuptools.command.build import build 63 except ImportError: 64 from distutils.command.build import build 65 cmd_obj = build(Distribution()) 66 cmd_obj.finalize_options() 67 return cmd_obj.build_platlib 68 except Exception: 69 return None 70 71 72def getprocessorinfo(): 73 try: 74 name = os.uname()[1] 75 except: 76 import platform 77 name = platform.uname()[1] 78 from petsc4py.PETSc import COMM_WORLD 79 rank = COMM_WORLD.getRank() 80 return (rank, name) 81 82 83def getlibraryinfo(name): 84 modname = "%s4py.%s" % (name.lower(), name) 85 module = __import__(modname, fromlist=[name]) 86 (major, minor, micro), devel = module.Sys.getVersion(devel=True) 87 r = not devel 88 if r: release = 'release' 89 else: release = 'development' 90 arch = module.__arch__ 91 return ( 92 "%s %d.%d.%d %s (conf: '%s')" % 93 (name, major, minor, micro, release, arch) 94 ) 95 96 97def getpythoninfo(): 98 x, y, z = sys.version_info[:3] 99 return ("Python %d.%d.%d (%s)" % (x, y, z, sys.executable)) 100 101 102def getpackageinfo(pkg): 103 try: 104 pkg = __import__(pkg) 105 except ImportError: 106 return None 107 name = pkg.__name__ 108 version = pkg.__version__ 109 path = pkg.__path__[0] 110 return ("%s %s (%s)" % (name, version, path)) 111 112 113def setup_python(options): 114 rootdir = os.path.dirname(os.path.dirname(__file__)) 115 builddir = os.path.join(rootdir, getbuilddir()) 116 if options.builddir and os.path.exists(builddir): 117 sys.path.insert(0, builddir) 118 if options.path: 119 path = options.path[:] 120 path.reverse() 121 for p in path: 122 sys.path.insert(0, p) 123 124 125def setup_unittest(options): 126 from unittest import TestSuite 127 try: 128 from unittest.runner import _WritelnDecorator 129 except ImportError: 130 from unittest import _WritelnDecorator 131 # 132 writeln_orig = _WritelnDecorator.writeln 133 def writeln(self, message=''): 134 try: self.stream.flush() 135 except: pass 136 writeln_orig(self, message) 137 try: self.stream.flush() 138 except: pass 139 _WritelnDecorator.writeln = writeln 140 141 142def import_package(options, pkgname): 143 args = [sys.argv[0]] 144 if options.memdebug: 145 args.append('-malloc') 146 args.append('-malloc_debug') 147 args.append('-malloc_dump') 148 if options.summary: 149 args.append('-log_view') 150 package = __import__(pkgname) 151 package.init(args, arch=options.arch) 152 153 154def print_banner(options): 155 r, n = getprocessorinfo() 156 prefix = "[%d@%s]" % (r, n) 157 158 def writeln(message='', endl='\n'): 159 if message is None: 160 return 161 from petsc4py.PETSc import Sys 162 message = "%s %s" % (prefix, message) 163 Sys.syncPrint(message, endl=endl, flush=True) 164 165 if options.verbose: 166 writeln(getpythoninfo()) 167 writeln(getpackageinfo('numpy')) 168 for entry in components: 169 writeln(getlibraryinfo(entry)) 170 writeln(getpackageinfo('%s4py' % entry)) 171 172 173def load_tests(options, args): 174 from glob import glob 175 import re 176 testsuitedir = os.path.dirname(__file__) 177 sys.path.insert(0, testsuitedir) 178 pattern = 'test_*.py' 179 wildcard = os.path.join(testsuitedir, pattern) 180 testfiles = glob(wildcard) 181 testfiles.sort() 182 testsuite = unittest.TestSuite() 183 testloader = unittest.TestLoader() 184 if options.patterns: 185 testloader.testNamePatterns = [ 186 ('*%s*' % p) if ('*' not in p) else p 187 for p in options.patterns 188 ] 189 include = exclude = None 190 if options.include: 191 include = re.compile('|'.join(options.include)).search 192 if options.exclude: 193 exclude = re.compile('|'.join(options.exclude)).search 194 for testfile in testfiles: 195 filename = os.path.basename(testfile) 196 testname = os.path.splitext(filename)[0] 197 if ((exclude and exclude(testname)) or 198 (include and not include(testname))): 199 continue 200 module = __import__(testname) 201 for arg in args: 202 try: 203 cases = testloader.loadTestsFromNames((arg,), module) 204 testsuite.addTests(cases) 205 except AttributeError: 206 pass 207 if not args: 208 cases = testloader.loadTestsFromModule(module) 209 testsuite.addTests(cases) 210 return testsuite 211 212 213def run_tests(options, testsuite, runner=None): 214 if runner is None: 215 runner = unittest.TextTestRunner(verbosity=options.verbose) 216 runner.failfast = options.failfast 217 result = runner.run(testsuite) 218 return result.wasSuccessful() 219 220 221 222def abort(code=1): 223 os.abort() 224 225 226def shutdown(success): 227 pass 228 229 230def main(args=None): 231 pkgname = '%s4py' % components[-1].lower() 232 parser = getoptionparser() 233 (options, args) = parser.parse_args(args) 234 setup_python(options) 235 setup_unittest(options) 236 import_package(options, pkgname) 237 print_banner(options) 238 testsuite = load_tests(options, args) 239 success = run_tests(options, testsuite) 240 if not success and options.failfast: abort() 241 shutdown(success) 242 return not success 243 244 245if __name__ == '__main__': 246 import sys 247 sys.dont_write_bytecode = True 248 sys.exit(main()) 249