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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313 | class InstallQtBinding(PreLaunchHook):
"""Install Qt binding to unreal's python packages."""
app_groups = ("unreal")
launch_types = (LaunchTypes.local)
def execute(self) -> None:
"""Entry point for the hook."""
try:
self._execute()
except Exception: # noqa: BLE001
self.log.warning(
"Processing of %s crashed.",
self.__class__.__name__, exc_info=True
)
@staticmethod
def _find_python_executable(
path: Path) -> tuple[Union[Path, None], Union[int, None]]:
"""Find python executable in unreal's directory.
Args:
path (Path): Python Executable path.
Returns:
valid_path (Path): Path to python executable.
"""
result = subprocess.check_output([path.as_posix(), "--version"])
version_str = result.decode().strip().split()[1]
version_parts = version_str.split('.')
version_int = int(version_parts[1])
for version in python_versions:
if int(version) == version_int:
return version
return None, None
def _execute(self) -> None: # noqa: PLR0912, C901
"""Execute the hook.
Todo:
* This method is too complex (PLR0912). It should be refactored
to smaller methods.
"""
platform = system().lower()
executable = self.launch_context.executable.executable_path
expected_executable = "UnrealEditor"
if platform == "windows":
expected_executable += ".exe"
if os.path.basename(executable) != expected_executable:
self.log.info((
f"Executable does not lead to {expected_executable} file."
"Can't determine Unreal's python to check/install"
" Qt binding."
))
return
versions_dir = self.find_parent_directory(executable)
unreal_python_dir = None
if platform == "windows":
unreal_python_dir = os.path.join(
versions_dir, "ThirdParty", "Python3", "Win64")
elif platform == "darwin":
unreal_python_dir = os.path.join(
versions_dir, "ThirdParty", "Python3", "Mac", "bin", "python3")
else:
unreal_python_dir = os.path.join(
versions_dir, "ThirdParty", "Python3", "Linux", "bin")
if not os.path.exists(unreal_python_dir):
self.log.warning(
"Couldn't find the directory for python executable "
"for unreal in %s", unreal_python_dir)
return
if platform == "windows":
python_executable = Path(unreal_python_dir) / "python.exe"
else:
python_executable = Path(unreal_python_dir) / "python"
if not python_executable.exists():
self.log.warning(
"Couldn't find python executable "
"for unreal %s", python_executable.as_posix())
return
py_version = self._find_python_executable(python_executable)
unreal_settings = self.data["project_settings"]["unreal"]
prelaunch_settings = unreal_settings["prelaunch_settings"]
pyside_name = "PySide6"
if py_version <= MAX_PYSIDE2_PYTHON_VERSION:
pyside_name = "PySide2"
# Check if PySide2 is installed and skip if yes
if self.is_pyside_installed(python_executable, pyside_name):
self.log.debug(
"unreal has already installed %s.", pyside_name)
return
# Install PySide2/PySide6 in unreal's python
if platform == "windows":
result = self.install_pyside_windows(
python_executable, pyside_name, prelaunch_settings)
else:
result = self.install_pyside(
python_executable, pyside_name, prelaunch_settings)
if result:
self.log.info(
"Successfully installed %s module to unreal.", pyside_name)
else:
self.log.warning(
"Failed to install %s module to unreal.", pyside_name)
def install_pyside_windows(
self, python_executable: Path, pyside_name: str, settings: dict) -> Union[None, int]:
"""Install PySide2 python module to unreal's python.
Installation requires administration rights that's why it is required
to use "pywin32" module which can execute command's and ask for
administration rights.
Note:
This is asking for administrative right always, no matter if
it is actually needed or not. Unfortunately getting
correct permissions for directory on Windows isn't that trivial.
You can either use `win32security` module or run `icacls` command
in subprocess and parse its output.
"""
if settings["arbitrary_site_package_location"]:
qt_binding_dir = self.use_arbitrary_location_for_qt(python_executable)
else:
qt_binding_dir = os.path.join(os.path.dirname(python_executable), "Lib")
args = [
python_executable.as_posix(),
"-m",
"pip",
"install",
"--ignore-installed",
pyside_name,
"--target",
f"{qt_binding_dir}"
]
args = self.use_dependency_path(args, settings)
return_code = self.pip_install(args)
return return_code
def install_pyside(
self, python_executable: Path, pyside_name: str, settings: dict) -> int:
"""Install PySide2 python module to unreal's python."""
args = [
python_executable.as_posix(),
"-m",
"pip",
"install",
"--ignore-installed",
pyside_name
]
if settings["arbitrary_site_package_location"]:
qt_binding_dir = self.use_arbitrary_location_for_qt(python_executable)
args += ["--target", f"{qt_binding_dir}"]
args = self.use_dependency_path(args, settings)
return_code = self.pip_install(args)
return return_code
@staticmethod
def is_pyside_installed(python_executable: Path, pyside_name: str) -> bool:
"""Check if PySide2/6 module is in unreal python env.
Args:
python_executable (Path): Path to python executable.
pyside_name (str): Name of pyside (to distinguish between PySide2
and PySide6).
Returns:
bool: True if PySide2 is installed, False otherwise.
"""
# Get pip list from unreal's python executable
args = [python_executable.as_posix(), "-m", "pip", "list"]
process = subprocess.Popen(args, stdout=subprocess.PIPE)
stdout, _ = process.communicate()
lines = stdout.decode().split(os.linesep)
# Second line contain dashes that define maximum length of module name.
# Second column of dashes define maximum length of module version.
package_dashes, *_ = lines[1].split(" ")
package_len = len(package_dashes)
# Got through printed lines starting at line 3
for idx in range(2, len(lines)):
line = lines[idx]
if not line:
continue
package_name = line[:package_len].strip()
if package_name.lower() == pyside_name.lower():
return True
return False
def find_parent_directory(self, file_path: str, target_dir="Binaries") -> str:
# Split the path into components
path_components = file_path.split(os.sep)
# Traverse the path components to find the target directory
for i in range(len(path_components) - 1, -1, -1):
if path_components[i] == target_dir:
# Join the components to form the target directory path
return os.sep.join(path_components[:i + 1])
return None
def use_dependency_path(self, commands: list, settings: dict) -> list:
if not settings.get("use_dependency"):
self.log.warning(
"Skipping to install Pyside with dependency path.")
return commands
if dependency_path := settings.get("dependency_path"):
commands.extend(
[
"--no-index",
f"--find-links={dependency_path}"
]
)
else:
self.log.warning("No dependency path filled in the setting.")
return commands
self.log.info(f"Using dependency path: {dependency_path}")
return commands
def pip_install(self, args: list):
try:
# Parameters
# - use "-m pip" as module pip to install PySide2/6 and argument
# "--ignore-installed" is to force install module to unreal
# site-packages and make sure it is binary compatible
process = subprocess.Popen(
args, stdout=subprocess.PIPE, universal_newlines=True
)
process.communicate()
except PermissionError:
self.log.warning(
'Permission denied with command: "%s".', " ".join(args),
exc_info=True)
except OSError as error:
self.log.warning(
'OS error has occurred: "%s".', error, exc_info=True)
except subprocess.SubprocessError:
pass
else:
return process.returncode == 0
def use_arbitrary_location_for_qt(self, python_executable):
"""Use custom location for storing PySide-related site-packages
and add the package paths as part of the unreal python environment
Args:
python_executable (Path): Python Executable
Returns:
str: Qt-binding directory
"""
ue_python_hash = hashlib.sha512(
python_executable.as_posix().encode("utf-8")).hexdigest()[:8]
qt_binding_dir = (
Path(get_launcher_local_dir()) / "unreal_dependencies" / ue_python_hash
)
# add arbitrary_site_packages as part of the unreal python path
ue_pythonpath = self.launch_context.env.get("UE_PYTHONPATH")
if ue_pythonpath:
ue_pythonpath = os.pathsep.join(
[ue_pythonpath, qt_binding_dir.as_posix()])
else:
ue_pythonpath = qt_binding_dir
self.launch_context.env["UE_PYTHONPATH"] = ue_pythonpath
return qt_binding_dir.as_posix()
|