00001 """
00002 runs examples in examples package with text output
00003 """
00004
00005 __copyright__ = """
00006 Copyright 2008 Sean Ross-Ross
00007 """
00008 __license__ = """
00009 This file is part of SLIMpy .
00010
00011 SLIMpy is free software: you can redistribute it and/or modify
00012 it under the terms of the GNU Lesser General Public License as published by
00013 the Free Software Foundation, either version 3 of the License, or
00014 (at your option) any later version.
00015
00016 SLIMpy is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 GNU Lesser General Public License for more details.
00020
00021 You should have received a copy of the GNU Lesser General Public License
00022 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00023 """
00024
00025
00026 from slimpy_base import __file__ as SLIMpyfile
00027 from glob import glob
00028 from os.path import basename, dirname, join
00029 from sys import stdout
00030
00031
00032 def getExampleDirs():
00033 """
00034 returns all of the packages in the example directory
00035 """
00036
00037 dir = dirname( SLIMpyfile )
00038 tpath = join( dir, "examples", "*" )
00039 exampledirs = glob( tpath )
00040
00041 try: exampledirs.remove( join( dir, "examples", "__init__.py" ) )
00042 except: pass
00043 try: exampledirs.remove( join( dir, "examples", "__init__.pyc" ) )
00044 except: pass
00045 return exampledirs
00046
00047 def runexample( name, run, clean ):
00048 """
00049 run an example
00050 @param run: must be a callable object with no
00051 parameters
00052 @param clean: must be a callable object with no
00053 parameters
00054 """
00055 moderrors = 0
00056 print "%(name)20s" % vars(), "...",
00057 stdout.flush()
00058 try:
00059 run()
00060 except EnvironmentError, msg:
00061 print msg, "- Skipping test."
00062
00063 except Exception, msg:
00064 print "ERROR"
00065
00066 moderrors = 1
00067 else:
00068 print "ok"
00069
00070 try:
00071 err = clean()
00072 if err:
00073 print "Error with Clean"
00074 except:
00075 pass
00076 return moderrors
00077
00078 def runExamples():
00079 """
00080 fetch all examples and run them
00081
00082 @postcondition: exceptions NotImplementedError and Environment Error
00083 will not count as errors
00084 """
00085 num = 0
00086 exampledirs = getExampleDirs()
00087
00088 errors = 0
00089
00090 for examples in exampledirs:
00091
00092 moderrors = 0
00093 name = basename( examples )
00094
00095 print "%(name)s ..." %vars() ,
00096 try:
00097 mod = __import__( ".".join( ["SLIMpy.examples", name] ), globals(), locals(), name )
00098 if hasattr(mod, 'canrun'):
00099 mod.canrun()
00100 else:
00101 print mod
00102 raise AttributeError("module has no attr: canrun")
00103 except NotImplementedError,msg:
00104 print msg
00105 except AttributeError, msg:
00106 print msg
00107 moderrors += 1
00108 except:
00109 print "Failed to Import"
00110 moderrors += 1
00111 else:
00112 print
00113 for name, run, clean in mod.get():
00114 num += 1
00115 if run is None:
00116 print "%(name)20s" % vars(), "...", "Error: Could not Import"
00117 moderrors += 1
00118 else:
00119 err = runexample( name, run, clean )
00120 if err:
00121 moderrors += 1
00122 if moderrors:
00123 print " ... Module: FAIL"
00124 errors += moderrors
00125 else:
00126 print " ... Module: PASS"
00127
00128 return errors, num
00129