extension_native_helpers.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ### Tools
  2. __all__ = ["Dtool_ObjectToDict", "Dtool_funcToMethod", "Dtool_PreloadDLL"]
  3. import imp,sys,os
  4. # The following code exists to work around a problem that exists
  5. # with Python 2.5 or greater.
  6. # Specifically, Python 2.5 is designed to import files named *.pyd
  7. # only; it will not import files named *.dll (or *.so). We work
  8. # around this problem by explicitly preloading all of the dll's we
  9. # expect to need.
  10. dll_suffix = ''
  11. if sys.platform == "win32":
  12. # On Windows, dynamic libraries end in ".dll".
  13. dll_ext = '.dll'
  14. # We allow the caller to preload dll_suffix into the sys module.
  15. dll_suffix = getattr(sys, 'dll_suffix', None)
  16. if dll_suffix is None:
  17. # Otherwise, we try to determine it from the executable name:
  18. # python_d.exe implies _d across the board.
  19. dll_suffix = ''
  20. if sys.executable.endswith('_d.exe'):
  21. dll_suffix = '_d'
  22. elif sys.platform == "darwin":
  23. # On OSX, the dynamic libraries usually end in .dylib, but
  24. # sometimes we need .so.
  25. try:
  26. from direct.extensions_native.extensions_darwin import dll_ext
  27. except ImportError:
  28. dll_ext = '.dylib'
  29. else:
  30. # On most other UNIX systems (including linux), .so is used.
  31. dll_ext = '.so'
  32. if sys.platform == "win32":
  33. # On Windows, we must furthermore ensure that the PATH is modified
  34. # to locate all of the DLL files.
  35. # First, search for the directory that contains all of our compiled
  36. # modules.
  37. target = None
  38. filename = "libpandaexpress%s%s" % (dll_suffix, dll_ext)
  39. for dir in sys.path + [sys.prefix]:
  40. lib = os.path.join(dir, filename)
  41. if (os.path.exists(lib)):
  42. target = dir
  43. if target == None:
  44. message = "Cannot find %s" % (filename)
  45. raise ImportError, message
  46. # And add that directory to the system path.
  47. path = os.environ["PATH"]
  48. if not path.startswith(target + ";"):
  49. os.environ["PATH"] = target + ";" + path
  50. def Dtool_PreloadDLL(module):
  51. if (sys.modules.has_key(module)):
  52. return
  53. # Search for the appropriate directory.
  54. target = None
  55. filename = module + dll_suffix + dll_ext
  56. for dir in sys.path + [sys.prefix]:
  57. lib = os.path.join(dir, filename)
  58. if (os.path.exists(lib)):
  59. target = dir
  60. break
  61. if target == None:
  62. message = "DLL loader cannot find %s." % (module)
  63. raise ImportError, message
  64. # Now import the file explicitly.
  65. pathname = os.path.join(target, filename)
  66. imp.load_dynamic(module, pathname)
  67. Dtool_PreloadDLL("libpandaexpress")
  68. from libpandaexpress import *
  69. def Dtool_ObjectToDict(clas, name, obj):
  70. clas.DtoolClassDict[name] = obj;
  71. def Dtool_funcToMethod(func, clas, method_name=None):
  72. """Adds func to class so it is an accessible method; use method_name to specify the name to be used for calling the method.
  73. The new method is accessible to any instance immediately."""
  74. func.im_class=clas
  75. func.im_func=func
  76. func.im_self=None
  77. if not method_name:
  78. method_name = func.__name__
  79. clas.DtoolClassDict[method_name] = func;