setuptestframework.py 4.1 KB

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