|
36 | 36 | import types
|
37 | 37 | import os
|
38 | 38 | import subprocess
|
39 |
| -import sys |
40 |
| - |
41 |
| - |
42 |
| -def _setup_tkinter(): |
43 |
| - |
44 |
| - print("GraalPy requires additional setup to enable Tkinter support on macOS.\n") |
45 |
| - |
46 |
| - print("Ensure pip and required packages are installed:") |
47 |
| - print(f" {sys.executable} -m ensurepip") |
48 |
| - print(f" {sys.executable} -m pip install cffi setuptools\n") |
| 39 | +import importlib.util |
| 40 | +import shutil |
| 41 | +import platform |
49 | 42 |
|
50 |
| - print("Install system dependencies:") |
51 |
| - print(" brew install tcl-tk@8") |
| 43 | +def ensure_installed(name, *extra): |
| 44 | + try: |
| 45 | + return importlib.import_module(name) |
| 46 | + except ImportError: |
| 47 | + subprocess.check_call([sys.executable, "-m", "pip", "install", name, *extra]) |
| 48 | + return importlib.import_module(name) |
| 49 | + |
| 50 | +def install_system_dependencies(): |
| 51 | + if sys.platform == "darwin": |
| 52 | + if not shutil.which("brew"): |
| 53 | + print("Homebrew not found.") |
| 54 | + install_brew = input("Install Homebrew now? [Y/n]: ").strip().low() |
| 55 | + if install_brew in ("", "y", "yes"): |
| 56 | + subprocess.check_call([ |
| 57 | + "/bin/bash", "-c", |
| 58 | + "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" |
| 59 | + ]) |
| 60 | + else: |
| 61 | + print("Cannot continue without Homebrew. Please install it and rerun.") |
| 62 | + sys.exit(1) |
52 | 63 |
|
53 |
| - if sys.stdin.isatty(): |
54 |
| - resp = input("Would you like to run pip setup and build now? [Y/n]: ").strip().lower() |
55 |
| - if resp in ("", "y", "yes"): |
56 |
| - try: |
57 |
| - subprocess.check_call([sys.executable, "-m", "ensurepip"]) |
58 |
| - subprocess.check_call([sys.executable, "-m", "pip", "install", "cffi", "setuptools"]) |
59 |
| - subprocess.check_call(["brew", "install", "tcl-tk@8"]) |
60 |
| - current_dir = os.path.dirname(__file__) |
61 |
| - tklib_build_path = os.path.abspath(os.path.join( |
62 |
| - current_dir, |
63 |
| - "../../../../../darwin-aarch64/GRAALPY_JVM_STANDALONE/lib/python3.12/_tkinter/tklib_build.py" |
64 |
| - )) |
65 |
| - |
66 |
| - subprocess.check_call([sys.executable, tklib_build_path]) |
67 |
| - except Exception as build_err: |
68 |
| - raise build_err |
| 64 | + subprocess.check_call(["brew", "install", "tcl-tk@8"]) |
69 | 65 | else:
|
70 |
| - print("\n Run in an interactive terminal to auto-run pip setup and build.") |
71 |
| - |
72 |
| - |
| 66 | + subprocess.check_call(["sudo", "apt", "install", "-y", "tcl8.6-dev", "tk8.6-dev"]) |
| 67 | + |
| 68 | +def run_tkinter_build_script(): |
| 69 | + current_dir = os.path.dirname(__file__) |
| 70 | + system = platform.system().lower() |
| 71 | + machine = platform.machine().lower() |
| 72 | + if machine == 'x86_64': |
| 73 | + machine = 'x64' |
| 74 | + elif machine == 'arm64' or machine == 'aarch64': |
| 75 | + machine = 'aarch64' |
| 76 | + |
| 77 | + platform_id = f"{system}-{machine}" |
| 78 | + |
| 79 | + py_version = f"{sys.version_info.major}.{sys.version_info.minor}" |
| 80 | + base_dir = os.path.abspath(os.path.join(current_dir, "..", "..", "..", "..", "..")) |
| 81 | + script_path = os.path.join( |
| 82 | + base_dir, |
| 83 | + platform_id, |
| 84 | + "GRAALPY_JVM_STANDALONE", |
| 85 | + "lib", |
| 86 | + f"python{py_version}", |
| 87 | + "_tkinter", |
| 88 | + "tklib_build.py" |
| 89 | + ) |
| 90 | + if not os.path.exists(script_path): |
| 91 | + raise FileNotFoundError(f"Build script not found: {script_path}") |
| 92 | + subprocess.check_call([sys.executable, script_path]) |
| 93 | + |
| 94 | +def setup_tkinter(): |
| 95 | + |
| 96 | + print("Checking required Python packages...") |
| 97 | + for pkg in ( "cffi", "setuptools" ): |
| 98 | + ensure_installed(pkg) |
| 99 | + |
| 100 | + install_system_dependencies() |
| 101 | + run_tkinter_build_script() |
73 | 102 |
|
74 | 103 | try:
|
75 | 104 | import _tkinter
|
76 | 105 | except Exception:
|
77 |
| - _setup_tkinter() |
| 106 | + setup_tkinter() |
78 | 107 | import _tkinter
|
79 | 108 | TclError = _tkinter.TclError
|
80 | 109 |
|
|
0 commit comments