GenTestScripts.py 2.5 KB

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