blob: 5f32d25abc8bf5668b791443493751c7e17af3e6 [file] [log] [blame]
Martin v. Löwis4ca196d2002-02-24 16:51:45 +00001import sys, os
Guido van Rossumea32cbb2001-10-12 15:34:29 +00002
Martin v. Löwis4ca196d2002-02-24 16:51:45 +00003# 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öwisacbf4612009-01-24 16:19:45 +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):
22 if isinstance(s, bytes):
23 s = s.decode("mbcs")
24 hdir = ctypes.windll.kernel32.\
25 CreateFileW(s, 0x80, # FILE_READ_ATTRIBUTES
26 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("", 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
41 s = buf[:res]
42 # Ignore leading \\?\
43 if s.startswith("\\\\?\\"):
44 s = s[4:]
Martin v. Löwis2d5157e2010-06-04 19:50:26 +000045 if s.startswith("UNC"):
46 s = "\\" + s[3:]
Martin v. Löwisacbf4612009-01-24 16:19:45 +000047 return s
48
Vinay Sajip7ded1f02012-05-26 03:45:29 +010049prefix = os.path.join(sys.base_prefix,"tcl")
Christian Heimes99170a52007-12-19 02:07:34 +000050if not os.path.exists(prefix):
51 # devdir/../tcltk/lib
Vinay Sajip7ded1f02012-05-26 03:45:29 +010052 prefix = os.path.join(sys.base_prefix, os.path.pardir, "tcltk", "lib")
Christian Heimes99170a52007-12-19 02:07:34 +000053 prefix = os.path.abspath(prefix)
Martin v. Löwis4ca196d2002-02-24 16:51:45 +000054# if this does not exist, no further search is needed
55if os.path.exists(prefix):
Martin v. Löwisacbf4612009-01-24 16:19:45 +000056 prefix = convert_path(prefix)
Guido van Rossume014a132006-08-19 16:53:45 +000057 if "TCL_LIBRARY" not in os.environ:
Martin v. Löwis4ca196d2002-02-24 16:51:45 +000058 for name in os.listdir(prefix):
59 if name.startswith("tcl"):
60 tcldir = os.path.join(prefix,name)
61 if os.path.isdir(tcldir):
62 os.environ["TCL_LIBRARY"] = tcldir
Martin v. Löwis838a3592002-11-09 19:01:44 +000063 # Compute TK_LIBRARY, knowing that it has the same version
64 # as Tcl
Martin v. Löwis4ca196d2002-02-24 16:51:45 +000065 import _tkinter
66 ver = str(_tkinter.TCL_VERSION)
Guido van Rossume014a132006-08-19 16:53:45 +000067 if "TK_LIBRARY" not in os.environ:
Martin v. Löwis838a3592002-11-09 19:01:44 +000068 v = os.path.join(prefix, 'tk'+ver)
69 if os.path.exists(os.path.join(v, "tclIndex")):
70 os.environ['TK_LIBRARY'] = v
71 # We don't know the Tix version, so we must search the entire
72 # directory
Guido van Rossume014a132006-08-19 16:53:45 +000073 if "TIX_LIBRARY" not in os.environ:
Martin v. Löwis838a3592002-11-09 19:01:44 +000074 for name in os.listdir(prefix):
75 if name.startswith("tix"):
76 tixdir = os.path.join(prefix,name)
77 if os.path.isdir(tixdir):
78 os.environ["TIX_LIBRARY"] = tixdir