blob: 8af27b582684339232e89bef0e5f51258a1a7740 [file] [log] [blame]
Georg Brandl33cece02008-05-20 06:58:21 +00001import sys, os
2
3# Delay import _tkinter until we have set TCL_LIBRARY,
4# so that Tcl_FindExecutable has a chance to locate its
5# encoding directory.
6
7# Unfortunately, we cannot know the TCL_LIBRARY directory
8# if we don't know the tcl version, which we cannot find out
9# without import Tcl. Fortunately, Tcl will itself look in
10# <TCL_LIBRARY>\..\tcl<TCL_VERSION>, so anything close to
11# the real Tcl library will do.
12
Martin v. Löwisf081e1c2009-01-24 15:47:27 +000013# Expand symbolic links on Vista
14try:
15 import ctypes
16 ctypes.windll.kernel32.GetFinalPathNameByHandleW
17except (ImportError, AttributeError):
18 def convert_path(s):
19 return s
20else:
21 def convert_path(s):
Florent Xicluna0b9a18a2010-03-06 11:01:08 +000022 assert isinstance(s, str) # sys.prefix contains only bytes
23 udir = s.decode("mbcs")
Martin v. Löwisf081e1c2009-01-24 15:47:27 +000024 hdir = ctypes.windll.kernel32.\
Florent Xicluna0b9a18a2010-03-06 11:01:08 +000025 CreateFileW(udir, 0x80, # FILE_READ_ATTRIBUTES
Martin v. Löwisf081e1c2009-01-24 15:47:27 +000026 1, # FILE_SHARE_READ
27 None, 3, # OPEN_EXISTING
28 0x02000000, # FILE_FLAG_BACKUP_SEMANTICS
29 None)
30 if hdir == -1:
31 # Cannot open directory, give up
32 return s
33 buf = ctypes.create_unicode_buffer(u"", 32768)
34 res = ctypes.windll.kernel32.\
35 GetFinalPathNameByHandleW(hdir, buf, len(buf),
36 0) # VOLUME_NAME_DOS
37 ctypes.windll.kernel32.CloseHandle(hdir)
38 if res == 0:
39 # Conversion failed (e.g. network location)
40 return s
Florent Xicluna0b9a18a2010-03-06 11:01:08 +000041 s = buf[:res].encode("mbcs")
Martin v. Löwisf081e1c2009-01-24 15:47:27 +000042 # Ignore leading \\?\
Florent Xicluna0b9a18a2010-03-06 11:01:08 +000043 if s.startswith("\\\\?\\"):
Martin v. Löwisf081e1c2009-01-24 15:47:27 +000044 s = s[4:]
Martin v. Löwiseba67c02010-06-04 19:39:07 +000045 if s.startswith("UNC"):
46 s = "\\" + s[3:]
Martin v. Löwisf081e1c2009-01-24 15:47:27 +000047 return s
48
Georg Brandl33cece02008-05-20 06:58:21 +000049prefix = os.path.join(sys.prefix,"tcl")
50if not os.path.exists(prefix):
Zachary Ware21a23502014-11-01 22:34:09 -050051 # devdir/externals/tcltk/lib
Zachary Ware47343722015-07-16 00:24:48 -050052 tcltk = 'tcltk'
53 if sys.maxsize > 2**31 - 1:
54 tcltk = 'tcltk64'
55 prefix = os.path.join(sys.prefix, "externals", tcltk, "lib")
Georg Brandl33cece02008-05-20 06:58:21 +000056 prefix = os.path.abspath(prefix)
57# if this does not exist, no further search is needed
58if os.path.exists(prefix):
Martin v. Löwisf081e1c2009-01-24 15:47:27 +000059 prefix = convert_path(prefix)
Benjamin Peterson6e3dbbd2009-10-09 22:15:50 +000060 if "TCL_LIBRARY" not in os.environ:
Georg Brandl33cece02008-05-20 06:58:21 +000061 for name in os.listdir(prefix):
62 if name.startswith("tcl"):
63 tcldir = os.path.join(prefix,name)
64 if os.path.isdir(tcldir):
65 os.environ["TCL_LIBRARY"] = tcldir
66 # Compute TK_LIBRARY, knowing that it has the same version
67 # as Tcl
68 import _tkinter
69 ver = str(_tkinter.TCL_VERSION)
Benjamin Peterson6e3dbbd2009-10-09 22:15:50 +000070 if "TK_LIBRARY" not in os.environ:
Georg Brandl33cece02008-05-20 06:58:21 +000071 v = os.path.join(prefix, 'tk'+ver)
72 if os.path.exists(os.path.join(v, "tclIndex")):
73 os.environ['TK_LIBRARY'] = v
74 # We don't know the Tix version, so we must search the entire
75 # directory
Benjamin Peterson6e3dbbd2009-10-09 22:15:50 +000076 if "TIX_LIBRARY" not in os.environ:
Georg Brandl33cece02008-05-20 06:58:21 +000077 for name in os.listdir(prefix):
78 if name.startswith("tix"):
79 tixdir = os.path.join(prefix,name)
80 if os.path.isdir(tixdir):
81 os.environ["TIX_LIBRARY"] = tixdir