#!/usr/pkg/bin/python """test and debug python function libraries use this for debugging instead of having so much debug code in each script.""" Copyright = """ pytest.py - test and debug routines for python scripts/libs Copyright (C) 2004 John Comeau This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. """ errormessage = "Not all needed libraries found, upgrade or check path" try: True # not defined in older Python releases except: True, False = 1, 0 try: import sys, os, types, re, pwd sys.path.append(os.path.join(pwd.getpwuid(os.geteuid())[5], 'lib', 'python')) from com.jcomeau import gpl, jclicense import imp except: try: sys.stderr.write("%s\n" % errormessage) except: print errormessage raise def DebugPrint(*whatever): if whatever: sys.stderr.write("%s\n" % repr(whatever)) return True # so programs can test if debugging enabled def smarteval(args): """any numbers, lists, or tuples in arglist will be eval'ed as such... to avoid this behavior, double quote them so: '"[]"' """ for arg in range(len(args)): try: args[arg] = eval(args[arg]) except: pass return tuple(args) def main(): """invoke program, and optionally function, of module being debugged to debug program myprog.py, simply ./pytest myprog arg1 arg2 to debug function myfunc in myprog, ./pytest myprog.myfunc arg1 arg2 program's main routine must be main() """ if len(sys.argv) < 2: sys.stderr.write("%s\n" % "Usage: pytest MODULE[.FUNCTION] [ARG [...]]") sys.exit(0) sys.argv.pop(0) # we want program under test to think it's boss program = os.path.split(sys.argv[0])[1] try: function = os.path.splitext(program)[1][1:] # in case testing single function except: function = None module = os.path.splitext(program)[0] DebugPrint('module is %s' % module) sys.argv[0] = module sys.path.insert(0, '.') # search 'this' directory first (file, pathname, description) = imp.find_module(module) DebugPrint(file, pathname, description) try: testmodule = imp.load_module(module, file, pathname, description) finally: if file: file.close() #DebugPrint('dir(testmodule)', dir(testmodule)) testmodule.DebugPrint = DebugPrint # overwrite stub function if present sys.stderr.write('Testing program "%s", module "%s", function "%s"\n' % ( program, module, function)) if function is not None and len(function): print repr(eval("testmodule.%s%s" % (function, smarteval(sys.argv[1:])))) else: print repr(eval("testmodule.%s%s" % (module, smarteval(sys.argv[1:])))) if __name__ == '__main__': # if this program was imported by another, the above test will fail, # and this following code won't be used... main()