Skip to content

Commit 15162fd

Browse files
RachidaTanassathorakivo
authored andcommitted
Refactor Tkinter setup script: dynamic path resolution, package installation, and system dependency handling
1 parent 7b80f6b commit 15162fd

File tree

1 file changed

+61
-32
lines changed

1 file changed

+61
-32
lines changed

graalpython/lib-python/3/tkinter/__init__.py

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,45 +36,74 @@
3636
import types
3737
import os
3838
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
4942

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)
5263

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"])
6965
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()
73102

74103
try:
75104
import _tkinter
76105
except Exception:
77-
_setup_tkinter()
106+
setup_tkinter()
78107
import _tkinter
79108
TclError = _tkinter.TclError
80109

0 commit comments

Comments
 (0)