36
36
import types
37
37
import os
38
38
import subprocess
39
- import importlib . util
39
+ import importlib
40
40
import shutil
41
41
import platform
42
42
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 :
44
48
try :
45
- return importlib .import_module (name )
49
+ importlib .import_module (name )
50
+ return True
46
51
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" ])
49
88
50
89
def install_system_dependencies ():
90
+
51
91
if sys .platform == "darwin" :
52
92
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" ):
56
94
subprocess .check_call ([
57
95
"/bin/bash" , "-c" ,
58
96
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
@@ -61,11 +99,22 @@ def install_system_dependencies():
61
99
print ("Cannot continue without Homebrew. Please install it and rerun." )
62
100
sys .exit (1 )
63
101
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 " )
65
107
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
+
67
115
68
116
def run_tkinter_build_script ():
117
+
69
118
current_dir = os .path .dirname (__file__ )
70
119
system = platform .system ().lower ()
71
120
machine = platform .machine ().lower ()
@@ -93,9 +142,23 @@ def run_tkinter_build_script():
93
142
94
143
def setup_tkinter ():
95
144
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 )
99
162
100
163
install_system_dependencies ()
101
164
run_tkinter_build_script ()
0 commit comments