/Lib/lib-tk/FixTk.py

http://unladen-swallow.googlecode.com/ · Python · 76 lines · 54 code · 4 blank · 18 comment · 19 complexity · 475f035510e0d5f1d03112b3c4b6415e MD5 · raw file

  1. import sys, os
  2. # Delay import _tkinter until we have set TCL_LIBRARY,
  3. # so that Tcl_FindExecutable has a chance to locate its
  4. # encoding directory.
  5. # Unfortunately, we cannot know the TCL_LIBRARY directory
  6. # if we don't know the tcl version, which we cannot find out
  7. # without import Tcl. Fortunately, Tcl will itself look in
  8. # <TCL_LIBRARY>\..\tcl<TCL_VERSION>, so anything close to
  9. # the real Tcl library will do.
  10. # Expand symbolic links on Vista
  11. try:
  12. import ctypes
  13. ctypes.windll.kernel32.GetFinalPathNameByHandleW
  14. except (ImportError, AttributeError):
  15. def convert_path(s):
  16. return s
  17. else:
  18. def convert_path(s):
  19. if isinstance(s, str):
  20. s = s.decode("mbcs")
  21. hdir = ctypes.windll.kernel32.\
  22. CreateFileW(s, 0x80, # FILE_READ_ATTRIBUTES
  23. 1, # FILE_SHARE_READ
  24. None, 3, # OPEN_EXISTING
  25. 0x02000000, # FILE_FLAG_BACKUP_SEMANTICS
  26. None)
  27. if hdir == -1:
  28. # Cannot open directory, give up
  29. return s
  30. buf = ctypes.create_unicode_buffer(u"", 32768)
  31. res = ctypes.windll.kernel32.\
  32. GetFinalPathNameByHandleW(hdir, buf, len(buf),
  33. 0) # VOLUME_NAME_DOS
  34. ctypes.windll.kernel32.CloseHandle(hdir)
  35. if res == 0:
  36. # Conversion failed (e.g. network location)
  37. return s
  38. s = buf[:res]
  39. # Ignore leading \\?\
  40. if s.startswith(u"\\\\?\\"):
  41. s = s[4:]
  42. return s
  43. prefix = os.path.join(sys.prefix,"tcl")
  44. if not os.path.exists(prefix):
  45. # devdir/../tcltk/lib
  46. prefix = os.path.join(sys.prefix, os.path.pardir, "tcltk", "lib")
  47. prefix = os.path.abspath(prefix)
  48. # if this does not exist, no further search is needed
  49. if os.path.exists(prefix):
  50. prefix = convert_path(prefix)
  51. if not os.environ.has_key("TCL_LIBRARY"):
  52. for name in os.listdir(prefix):
  53. if name.startswith("tcl"):
  54. tcldir = os.path.join(prefix,name)
  55. if os.path.isdir(tcldir):
  56. os.environ["TCL_LIBRARY"] = tcldir
  57. # Compute TK_LIBRARY, knowing that it has the same version
  58. # as Tcl
  59. import _tkinter
  60. ver = str(_tkinter.TCL_VERSION)
  61. if not os.environ.has_key("TK_LIBRARY"):
  62. v = os.path.join(prefix, 'tk'+ver)
  63. if os.path.exists(os.path.join(v, "tclIndex")):
  64. os.environ['TK_LIBRARY'] = v
  65. # We don't know the Tix version, so we must search the entire
  66. # directory
  67. if not os.environ.has_key("TIX_LIBRARY"):
  68. for name in os.listdir(prefix):
  69. if name.startswith("tix"):
  70. tixdir = os.path.join(prefix,name)
  71. if os.path.isdir(tixdir):
  72. os.environ["TIX_LIBRARY"] = tixdir