home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32com_test_GenTestScripts.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  2.2 KB  |  85 lines

  1. #
  2. # Generate scripts needed for serious testing!
  3. #
  4. import win32com, win32com.client.makepy
  5. import win32com.test
  6. import pythoncom
  7. import sys, os
  8.  
  9. genList = [
  10. ("msword8", "{00020905-0000-0000-C000-000000000046}",1033,8,0),
  11. ]
  12.  
  13. genDir = "Generated4Test"
  14. def GetGenPath():
  15.     import win32api
  16.     return os.path.join(win32api.GetFullPathName(win32com.test.__path__[0]), genDir)
  17.  
  18. def GenerateFromRegistered(fname, *loadArgs):
  19. #    tlb = apply(pythoncom.LoadRegTypeLib, loadArgs)
  20.     genPath = GetGenPath()
  21.     try:
  22.         os.stat(genPath)
  23.     except os.error:
  24.         os.mkdir(genPath)
  25.     # Ensure an __init__ exists.
  26.     open(os.path.join(genPath, "__init__.py"), "w").close()
  27.     print fname, ": generating -",
  28.     f = open(os.path.join(genPath, fname + ".py"), "w")
  29.     win32com.client.makepy.GenerateFromTypeLibSpec(loadArgs, f, bQuiet = 1, bGUIProgress = 1, bUnicodeToString=NeedUnicodeConversions)
  30.     f.close()
  31.     print "compiling -",
  32.     fullModName = "win32com.test.%s.%s" % (genDir, fname)
  33.     exec "import " + fullModName
  34.     # Inject the generated module as a top level module.
  35.     sys.modules[fname] = sys.modules[fullModName]
  36.     print "done"
  37.     
  38.     
  39. def GenerateAll():
  40.     for args in genList:
  41.         try:
  42.             apply(GenerateFromRegistered, args)
  43.         except KeyboardInterrupt:
  44.             print "** Interrupted ***"
  45.             break
  46.         except pythoncom.com_error:
  47.             print "** Could not generate test code for ", args[0]
  48.  
  49. def CleanAll():
  50.     print "Cleaning generated test scripts..."
  51.     try: # Clear exceptions!
  52.         1/0
  53.     except:
  54.         pass
  55.     genPath = GetGenPath()
  56.     for args in genList:
  57.         try:
  58.             name = args[0]+".py"
  59.             os.unlink(os.path.join(genPath, name))
  60.         except os.error, details:
  61.             if type(details)==type(()) and details[0]!=2:
  62.                 print "Could not deleted generated", name, details
  63.         try:
  64.             name = args[0]+".pyc"
  65.             os.unlink(os.path.join(genPath, name))
  66.         except os.error, details:
  67.             if type(details)==type(()) and details[0]!=2:
  68.                 print "Could not deleted generated", name, details
  69.         try:
  70.             os.unlink(os.path.join(genPath, "__init__.py"))
  71.         except:
  72.             pass
  73.         try:
  74.             os.unlink(os.path.join(genPath, "__init__.pyc"))
  75.         except:
  76.             pass
  77.     try:
  78.         os.rmdir(genPath)
  79.     except os.error, details:
  80.         print "Could not delete test directory -", details
  81.  
  82. if __name__=='__main__':
  83.     GenerateAll()
  84.     CleanAll()
  85.