Skip to content

Commit c915d93

Browse files
RachidaTanassathorakivo
authored andcommitted
refactor: ask user permission before each package installation
1 parent 15162fd commit c915d93

File tree

1 file changed

+76
-13
lines changed

1 file changed

+76
-13
lines changed

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

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,61 @@
3636
import types
3737
import os
3838
import subprocess
39-
import importlib.util
39+
import importlib
4040
import shutil
4141
import platform
4242

43-
def ensure_installed(name, *extra):
43+
GREEN = "\033[92m"
44+
RED = "\033[91m"
45+
RESET = "\033[0m"
46+
47+
def check_package(name: str) -> bool:
4448
try:
45-
return importlib.import_module(name)
49+
importlib.import_module(name)
50+
return True
4651
except ImportError:
47-
subprocess.check_call([sys.executable, "-m", "pip", "install", name, *extra])
48-
return importlib.import_module(name)
52+
return False
53+
54+
def is_tcl_tk_installed( name: str ) -> bool:
55+
56+
if sys.platform == "darwin":
57+
try:
58+
subprocess.check_output(["brew", "list", "--versions", name])
59+
return True
60+
except subprocess.CalledProcessError:
61+
return False
62+
else:
63+
try:
64+
subprocess.check_output(
65+
["tclsh", "-c", "puts [info patchlevel]"],
66+
stderr=subprocess.DEVNULL
67+
)
68+
return True
69+
except subprocess.CalledProcessError:
70+
return False
71+
72+
def prompt_user_install(package_name: str) -> bool:
73+
74+
response = input(f"Would you like to install {package_name}? [Y/n]: ").strip().lower()
75+
return response in ("", "y", "yes")
76+
77+
def install_tcl_tk(name:str) -> bool:
78+
if sys.platform == "darwin":
79+
try:
80+
subprocess.check_call(["brew", "install", name])
81+
print(f"{GREEN}{name} installed successfully.{RESET}")
82+
except subprocess.CalledProcessError:
83+
print(f"{RED}Failed to install {name}.{RESET}")
84+
sys.exit(1)
85+
86+
else:
87+
subprocess.check_call(["sudo", "apt", "install", "-y", "tcl8.6-dev", "tk8.6-dev"])
4988

5089
def install_system_dependencies():
90+
5191
if sys.platform == "darwin":
5292
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"):
93+
if prompt_user_install("Homebrew"):
5694
subprocess.check_call([
5795
"/bin/bash", "-c",
5896
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
@@ -61,11 +99,22 @@ def install_system_dependencies():
6199
print("Cannot continue without Homebrew. Please install it and rerun.")
62100
sys.exit(1)
63101

64-
subprocess.check_call(["brew", "install", "tcl-tk@8"])
102+
tcl_tk = "tcl-tk@8"
103+
sys.stdout.write(f"Checking {tcl_tk}... ")
104+
sys.stdout.flush()
105+
if is_tcl_tk_installed(tcl_tk):
106+
sys.stdout.write(f"{GREEN}Installed{RESET}\n")
65107
else:
66-
subprocess.check_call(["sudo", "apt", "install", "-y", "tcl8.6-dev", "tk8.6-dev"])
108+
sys.stdout.write(f"{RED}Not installed{RESET}\n")
109+
if prompt_user_install(tcl_tk):
110+
install_tcl_tk(tcl_tk)
111+
else:
112+
print(f"{RED}Cannot continue without {tcl_tk}. Please install it and try again.{RESET}")
113+
sys.exit(1)
114+
67115

68116
def run_tkinter_build_script():
117+
69118
current_dir = os.path.dirname(__file__)
70119
system = platform.system().lower()
71120
machine = platform.machine().lower()
@@ -93,9 +142,23 @@ def run_tkinter_build_script():
93142

94143
def setup_tkinter():
95144

96-
print("Checking required Python packages...")
97-
for pkg in ( "cffi", "setuptools" ):
98-
ensure_installed(pkg)
145+
packages = ["cffi", "setuptools"]
146+
147+
for pkg in packages:
148+
sys.stdout.write(f"Checking {pkg}...")
149+
sys.stdout.flush()
150+
151+
if check_package(pkg):
152+
sys.stdout.write(f"{GREEN}Installed{RESET}\n")
153+
else:
154+
sys.stdout.write(f"{RED}Not installed{RESET}\n")
155+
if prompt_user_install(pkg):
156+
subprocess.check_call([sys.executable, "-m", "pip", "install", pkg])
157+
print(f"{GREEN}{pkg} installed successfully.{RESET}")
158+
159+
else:
160+
print(f"Cannot continue without {pkg}. Please install using: \"pip install {pkg}\"")
161+
sys.exit(1)
99162

100163
install_system_dependencies()
101164
run_tkinter_build_script()

0 commit comments

Comments
 (0)