-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
206 lines (157 loc) · 5.22 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import subprocess
import ctypes
import os
import shutil
import tarfile
from pathlib import Path
def onerror(func, path, exc_info):
"""
Error handler for shutil.rmtree.
If the error is due to an access error (read-only file),
it attempts to add write permission and then retries.
If the error is for another reason, it re-raises the error.
Usage: shutil.rmtree(path, onerror=onerror)
"""
import stat
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
def re_launch_elevated():
commands = f"/k pushd {starting_dir} && python run.py"
ctypes.windll.shell32.ShellExecuteW(None, "runas", "cmd.exe", commands, None, 1)
def install_venv():
venv_path = Path(".venv")
if venv_path.exists():
shutil.rmtree(".venv", ignore_errors=False, onerror=onerror)
subprocess.run(["python", "-m", "venv", ".venv"], check=True)
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def check_chocolatey():
try:
subprocess.check_output(["choco", "--version"])
print("Chocolatey is installed.")
return True
except FileNotFoundError:
print("Chocolatey is not installed.")
return False
except subprocess.CalledProcessError:
print("Error checking Chocolatey.")
return False
def check_mingw(echo_output: bool):
try:
subprocess.check_output(["gcc", "--version"])
if echo_output:
print("MinGW-w64 is installed.")
return True
except FileNotFoundError:
print("MinGW-w64 is not installed.")
return False
except subprocess.CalledProcessError:
print("Error checking MinGW-w64.")
return False
def check_git():
try:
subprocess.check_output(["git", "--version"])
print("Git is installed.")
return True
except FileNotFoundError:
print("Git is not installed.")
return False
except subprocess.CalledProcessError:
print("Error checking for Git.")
return False
def clone_pyinstaller():
pyinstaller_path = Path.cwd() / "pyinstaller"
if pyinstaller_path.exists():
shutil.rmtree(str(pyinstaller_path), ignore_errors=False, onerror=onerror)
subprocess.run(
[
"git",
"clone",
"https://github.com/pyinstaller/pyinstaller",
str(pyinstaller_path),
],
check=True,
)
pyinstaller_path = Path.cwd() / "pyinstaller"
if pyinstaller_path.is_dir() and pyinstaller_path.exists():
return True
def check_for_dependencies():
if not check_mingw(True):
choco_installed = check_chocolatey()
if not choco_installed:
print("You must install Chocolatey")
return False
else:
check_admin = is_admin()
if not check_admin:
print("You must run as administrator")
return False
else:
subprocess.run(["choco", "install", "mingw"], check=True)
if check_mingw(False):
if not check_git():
print("You must install git")
return False
get_pyinstaller = clone_pyinstaller()
if not get_pyinstaller:
return False
return True
def extract_pyinstaller():
dist_path = starting_dir / "pyinstaller" / "dist"
os.chdir(dist_path)
# Find the tar.gz file
tar_files = list(Path.cwd().glob("*.tar.gz"))
if not tar_files:
print("No .tar.gz files found in the directory.")
return
# Assuming there is only one .tar.gz file
tar_file = tar_files[0]
# Extract the content
with tarfile.open(tar_file, "r:gz") as tar:
# Extract to a temporary folder
temp_folder = Path.cwd() / "temp_extraction"
tar.extractall(temp_folder)
# Find the extracted folder
extracted_folder = next(temp_folder.iterdir())
# Rename the folder to 'custom_pyinstaller'
pyinstaller_path = extracted_folder.rename(starting_dir / "custom_pyinstaller")
# Optionally, remove the temporary folder
temp_folder.rmdir()
return pyinstaller_path
def clean_dir(staring_dir):
shutil.rmtree(str(staring_dir / ".venv"), ignore_errors=False, onerror=onerror)
shutil.rmtree(
str(staring_dir / "pyinstaller"), ignore_errors=False, onerror=onerror
)
def main():
check_admin = is_admin()
if not check_admin:
re_launch_elevated()
return
install_venv()
dependencies = check_for_dependencies()
if not dependencies:
print("Missing a dependency")
return False
pyinstaller_path = Path.cwd() / "pyinstaller"
os.chdir(pyinstaller_path / "bootloader")
subprocess.run(
["python", "waf", "distclean", "all", "--target-arch=64bit", "--gcc"],
check=True,
)
os.chdir(starting_dir)
subprocess.run(["cmd", "/c", "build_pyinstaller.bat"], check=True)
custom_pyinstaller = extract_pyinstaller()
if custom_pyinstaller.exists():
subprocess.run(["explorer", str(custom_pyinstaller.parent)])
os.chdir(starting_dir)
clean_dir(starting_dir)
if __name__ == "__main__":
starting_dir = Path.cwd()
main()