setuptestframework.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/python2
  2. # Configure this in order to run the testcases.
  3. "setuptestframework.py v 2.6.0.8"
  4. import os
  5. import sys
  6. import tempfile
  7. import shutil
  8. try:
  9. OSErrors = (WindowsError, OSError)
  10. except NameError: # not running on Windows
  11. OSErrors = OSError
  12. def maketemp():
  13. temphome = tempfile.gettempdir()
  14. tempdir = os.path.join(temphome, 'adodbapi_test')
  15. try: os.mkdir(tempdir)
  16. except: pass
  17. return tempdir
  18. def _cleanup_function(testfolder, mdb_name):
  19. try: os.unlink(os.path.join(testfolder, mdb_name))
  20. except: pass # mdb database not present
  21. try:
  22. shutil.rmtree(testfolder)
  23. print(' cleaned up folder', testfolder)
  24. except: pass # test package not present
  25. def getcleanupfunction():
  26. return _cleanup_function
  27. def find_ado_path():
  28. adoName = os.path.normpath(os.getcwd() + '/../../adodbapi.py')
  29. adoPackage = os.path.dirname(adoName)
  30. return adoPackage
  31. # make a new package directory for the test copy of ado
  32. def makeadopackage(testfolder):
  33. adoName = os.path.normpath(os.getcwd() + '/../adodbapi.py')
  34. adoPath = os.path.dirname(adoName)
  35. if os.path.exists(adoName):
  36. newpackage = os.path.join(testfolder,'adodbapi')
  37. try:
  38. os.mkdir(newpackage)
  39. except OSErrors:
  40. print('*Note: temporary adodbapi package already exists: may be two versions running?')
  41. for f in os.listdir(adoPath):
  42. if f.endswith('.py'):
  43. shutil.copy(os.path.join(adoPath, f), newpackage)
  44. if sys.version_info >= (3,0): # only when running Py3.n
  45. save = sys.stdout
  46. sys.stdout = None
  47. from lib2to3.main import main # use 2to3 to make test package
  48. main("lib2to3.fixes",args=['-n','-w', newpackage])
  49. sys.stdout = save
  50. return testfolder
  51. else:
  52. raise EnvironmentError('Connot find source of adodbapi to test.')
  53. def makemdb(testfolder, mdb_name):
  54. # following setup code borrowed from pywin32 odbc test suite
  55. # kindly contributed by Frank Millman.
  56. import os
  57. _accessdatasource = os.path.join(testfolder, mdb_name)
  58. if os.path.isfile(_accessdatasource):
  59. print('using JET database=', _accessdatasource)
  60. else:
  61. try:
  62. from win32com.client.gencache import EnsureDispatch
  63. from win32com.client import constants
  64. win32 = True
  65. except ImportError: #perhaps we are running IronPython
  66. win32 = False #iron Python
  67. try:
  68. from System import Activator, Type
  69. except:
  70. pass
  71. # Create a brand-new database - what is the story with these?
  72. dbe = None
  73. for suffix in (".36", ".35", ".30"):
  74. try:
  75. if win32:
  76. dbe = EnsureDispatch("DAO.DBEngine" + suffix)
  77. else:
  78. type= Type.GetTypeFromProgID("DAO.DBEngine" + suffix)
  79. dbe = Activator.CreateInstance(type)
  80. break
  81. except:
  82. pass
  83. if dbe:
  84. print(' ...Creating ACCESS db at '+_accessdatasource)
  85. if win32:
  86. workspace = dbe.Workspaces(0)
  87. newdb = workspace.CreateDatabase(_accessdatasource,
  88. constants.dbLangGeneral,
  89. constants.dbVersion40)
  90. else:
  91. newdb = dbe.CreateDatabase(_accessdatasource,';LANGID=0x0409;CP=1252;COUNTRY=0')
  92. newdb.Close()
  93. else:
  94. print(' ...copying test ACCESS db to '+_accessdatasource)
  95. mdbName = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'examples', 'test.mdb'))
  96. import shutil
  97. shutil.copy(mdbName, _accessdatasource)
  98. return _accessdatasource
  99. if __name__ == "__main__":
  100. print('Setting up a Jet database for server to use for remote testing...')
  101. temp = maketemp()
  102. makemdb(temp, 'server_test.mdb')