-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbuild.py
217 lines (172 loc) · 5.52 KB
/
build.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
207
208
209
210
211
212
213
214
215
216
217
import os
import subprocess
import sys
import os
import subprocess
import sys
import shutil
import urllib.request
linux_and_mac_py_ver = "python3"
def checkIfExeExists(exe):
path = shutil.which(exe)
return path is not None
def getPlatform():
return sys.platform
def python_path():
return (
"venv\\Scripts\\python.exe" if getPlatform() == "win32" else "venv/bin/python3"
)
python_version = (
linux_and_mac_py_ver
if getPlatform() != "win32" and checkIfExeExists(linux_and_mac_py_ver)
else "python3"
)
def get_site_packages():
command = [
python_path(),
"-c",
'import site; print("\\n".join(site.getsitepackages()))',
]
result = subprocess.run(command, stdout=subprocess.PIPE, text=True)
site_packages = result.stdout.strip().split('\n')[0]
if os.path.exists(site_packages):
return site_packages
site_packages = site_packages.replace('dist','site')
if os.path.exists(site_packages):
return site_packages
print(site_packages)
raise FileNotFoundError("Unable to locate site packages for python venv!")
def download_file(url, destination):
print(f"Downloading file from {url}")
urllib.request.urlretrieve(url, destination)
print("File downloaded successfully")
def zero_mainwindow_size():
import xml.etree.ElementTree as ET
def set_mainwindow_size_zero(path="testRVEInterface.ui"):
tree = ET.parse(path)
root = tree.getroot()
geometry = root.find('.//property[@name="geometry"]/rect')
if geometry is not None:
width = geometry.find("width")
height = geometry.find("height")
if width is not None:
width.text = "0"
if height is not None:
height.text = "0"
tree.write(path)
set_mainwindow_size_zero()
def build_gui():
print("Building GUI")
zero_mainwindow_size()
if getPlatform() == "darwin" or getPlatform() == "linux":
os.system(
f"{get_site_packages()}/PySide6/Qt/libexec/uic -g python testRVEInterface.ui > mainwindow.py"
)
if getPlatform() == "win32":
os.system(
r".\venv\Lib\site-packages\PySide6\uic.exe -g python testRVEInterface.ui > mainwindow.py"
)
def install_pip():
download_file("https://bootstrap.pypa.io/get-pip.py", "get-pip.py")
command = ["python3", "get-pip.py"]
subprocess.run(command)
def install_pip_in_venv():
command = [
"venv\\Scripts\\python.exe" if getPlatform() == "win32" else "venv/bin/python3",
"get-pip.py",
]
subprocess.run(command)
def build_resources():
print("Building resources.rc")
if getPlatform() == "darwin" or getPlatform() == "linux":
os.system(
f"{get_site_packages()}/PySide6/Qt/libexec/rcc -g python resources.qrc > resources_rc.py"
)
if getPlatform() == "win32":
os.system(
r".\venv\Lib\site-packages\PySide6\rcc.exe -g python resources.qrc > resources_rc.py"
)
def create_venv():
print("Creating virtual environment")
command = [python_version, "-m", "venv", "venv"]
subprocess.run(command)
def install_requirements_in_venv():
print("Installing requirements in virtual environment")
command = [
python_path(),
"-m",
"pip",
"install",
"-r",
"requirements.txt",
]
subprocess.run(command)
def build_executable(dist_dir=None):
print("Building executable")
if getPlatform() == "win32" or getPlatform() == "darwin":
if dist_dir is None:
dist_dir = "dist"
command = [
python_path(),
"-m",
"PyInstaller",
"REAL-Video-Enhancer.py",
"--collect-all",
"PySide6",
"--icon=icons/logo-v2.ico",
"--noconfirm",
"--noupx",
"--distpath",
dist_dir,
# "--noconsole", this caused issues, maybe I can fix it later
]
else:
if dist_dir is None:
dist_dir = "bin"
command = [
python_path(),
"-m",
"cx_Freeze",
"REAL-Video-Enhancer.py",
"--target-dir",
dist_dir,
]
subprocess.run(command)
def copy_backend(build_dir=None):
print("Copying backend")
if getPlatform() == "win32":
if build_dir is None:
build_dir = "dist"
try:
os.system(f"cp -r backend {build_dir}/REAL-Video-Enhancer/backend")
except Exception:
pass
if not os.path.exists(rf"{build_dir}\\REAL-Video-Enhancer\\backend"):
os.system(
f'xcopy "./backend" "./{build_dir}/REAL-Video-Enhancer/backend" /E /I'
)
if getPlatform() == "linux":
if build_dir is None:
build_dir = "bin"
os.system(f"cp -r backend {build_dir}/")
def clean():
print("Cleaning up")
os.remove("get-pip.py")
def build_venv():
create_venv()
install_pip_in_venv()
install_requirements_in_venv()
if len(sys.argv) > 1:
if sys.argv[1] == "--create_venv" or sys.argv[1] == "--build_exe":
build_venv()
if not os.path.exists("venv"):
build_venv()
build_gui()
build_resources()
if "--build_dir_override" in sys.argv:
build_dir = sys.argv[sys.argv.index("--build_dir_override") + 1]
build_executable(build_dir)
copy_backend(build_dir=build_dir)
if "--build_exe" in sys.argv and "--build_dir_override" not in sys.argv:
build_executable()
copy_backend()