Martin v. Löwis | 4ca196d | 2002-02-24 16:51:45 +0000 | [diff] [blame] | 1 | import sys, os |
Guido van Rossum | ea32cbb | 2001-10-12 15:34:29 +0000 | [diff] [blame] | 2 | |
Martin v. Löwis | 4ca196d | 2002-02-24 16:51:45 +0000 | [diff] [blame] | 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öwis | acbf461 | 2009-01-24 16:19:45 +0000 | [diff] [blame] | 13 | # Expand symbolic links on Vista |
| 14 | try: |
| 15 | import ctypes |
| 16 | ctypes.windll.kernel32.GetFinalPathNameByHandleW |
| 17 | except (ImportError, AttributeError): |
| 18 | def convert_path(s): |
| 19 | return s |
| 20 | else: |
| 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:] |
| 45 | return s |
| 46 | |
Martin v. Löwis | 4ca196d | 2002-02-24 16:51:45 +0000 | [diff] [blame] | 47 | prefix = os.path.join(sys.prefix,"tcl") |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 48 | if not os.path.exists(prefix): |
| 49 | # devdir/../tcltk/lib |
| 50 | prefix = os.path.join(sys.prefix, os.path.pardir, "tcltk", "lib") |
| 51 | prefix = os.path.abspath(prefix) |
Martin v. Löwis | 4ca196d | 2002-02-24 16:51:45 +0000 | [diff] [blame] | 52 | # if this does not exist, no further search is needed |
| 53 | if os.path.exists(prefix): |
Martin v. Löwis | acbf461 | 2009-01-24 16:19:45 +0000 | [diff] [blame] | 54 | prefix = convert_path(prefix) |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 55 | if "TCL_LIBRARY" not in os.environ: |
Martin v. Löwis | 4ca196d | 2002-02-24 16:51:45 +0000 | [diff] [blame] | 56 | for name in os.listdir(prefix): |
| 57 | if name.startswith("tcl"): |
| 58 | tcldir = os.path.join(prefix,name) |
| 59 | if os.path.isdir(tcldir): |
| 60 | os.environ["TCL_LIBRARY"] = tcldir |
Martin v. Löwis | 838a359 | 2002-11-09 19:01:44 +0000 | [diff] [blame] | 61 | # Compute TK_LIBRARY, knowing that it has the same version |
| 62 | # as Tcl |
Martin v. Löwis | 4ca196d | 2002-02-24 16:51:45 +0000 | [diff] [blame] | 63 | import _tkinter |
| 64 | ver = str(_tkinter.TCL_VERSION) |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 65 | if "TK_LIBRARY" not in os.environ: |
Martin v. Löwis | 838a359 | 2002-11-09 19:01:44 +0000 | [diff] [blame] | 66 | v = os.path.join(prefix, 'tk'+ver) |
| 67 | if os.path.exists(os.path.join(v, "tclIndex")): |
| 68 | os.environ['TK_LIBRARY'] = v |
| 69 | # We don't know the Tix version, so we must search the entire |
| 70 | # directory |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 71 | if "TIX_LIBRARY" not in os.environ: |
Martin v. Löwis | 838a359 | 2002-11-09 19:01:44 +0000 | [diff] [blame] | 72 | for name in os.listdir(prefix): |
| 73 | if name.startswith("tix"): |
| 74 | tixdir = os.path.join(prefix,name) |
| 75 | if os.path.isdir(tixdir): |
| 76 | os.environ["TIX_LIBRARY"] = tixdir |