-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmainutils.py
executable file
·671 lines (577 loc) · 22.1 KB
/
mainutils.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-only
import os
import sys
import pwd
import shutil
from typing import (
Callable,
Optional,
List,
Union,
Any,
)
from corenodep import (
parse_version,
)
from coreutils import (
exit_with_message,
save_conf_setting,
load_conf_setting,
cache,
log,
)
if getattr(sys, "frozen", False):
SCRIPT_IMP_FILE = os.path.realpath(sys.executable)
else:
SCRIPT_IMP_FILE = os.path.realpath(__file__)
SCRIPT_PATH = os.path.dirname(SCRIPT_IMP_FILE)
# Get the GitHub releases from "USERNAME/REPO"
def get_github_releases(repo_name: str) -> List[Any]:
try:
import requests
except Exception as e:
log(f"Failed to fetch releases:\n{e}")
return []
url = f"https://api.github.com/repos/{repo_name}/releases"
response = requests.get(url)
if response.status_code == 200:
releases = response.json()
return releases
else:
log(
f"Failed to grab the GitHub releases from '{url}'.\nTo fix this, try deleting the config file"
)
return []
# Find the version w.x in the releases best compatible with version y.z
def find_closest_compatible_release(
releases: list,
current_version_parts: List[Union[int, None]] = [None, None],
) -> List[Union[Optional[List[int]], Optional[str]]]:
closest_release = None
closest_version = None
closest_release_url = None
priority = 6
for release in releases:
tag_name = release.get("tag_name")
if tag_name and tag_name.startswith("PfxVer"):
release_version_parts = parse_version(tag_name)
if release_version_parts and current_version_parts:
if (
release_version_parts[0] == current_version_parts[0]
and release_version_parts[1] == current_version_parts[1]
):
# Exact match
closest_release = release
closest_version = release_version_parts
closest_release_url = release["assets"][0][
"browser_download_url"
]
break
elif (
release_version_parts[0] == current_version_parts[0]
and release_version_parts[1] < current_version_parts[1]
):
# Same major, lower minor version
if priority > 2 or (
priority == 2
and (
not closest_release
or release_version_parts[1] > closest_version[1]
)
):
priority = 2
closest_release = release
closest_version = release_version_parts
closest_release_url = release["assets"][0][
"browser_download_url"
]
elif (
release_version_parts[0] == current_version_parts[0]
and release_version_parts[1] > current_version_parts[1]
):
# Same major, higher minor version
if priority > 3 or (
priority == 3
and (
not closest_release
or release_version_parts[1] < closest_version[1]
)
):
priority = 3
closest_release = release
closest_version = release_version_parts
closest_release_url = release["assets"][0][
"browser_download_url"
]
elif release_version_parts[0] < current_version_parts[0]:
# Lower major version
if priority > 4 or (
priority == 4
and (
not closest_release
or release_version_parts[0] > closest_version[0]
or (
release_version_parts[0] == closest_version[0]
and release_version_parts[1]
> closest_version[1]
)
)
):
priority = 4
closest_release = release
closest_version = release_version_parts
closest_release_url = release["assets"][0][
"browser_download_url"
]
elif release_version_parts[0] > current_version_parts[0]:
# Higher major version
if priority > 5 or (
priority == 5
and (
not closest_release
or release_version_parts[0] < closest_version[0]
or (
release_version_parts[0] == closest_version[0]
and release_version_parts[1]
< closest_version[1]
)
)
):
priority = 5
closest_release = release
closest_version = release_version_parts
closest_release_url = release["assets"][0][
"browser_download_url"
]
return closest_version, closest_release_url
# Function to execute a command and display output in a popup
def popup_execute(
title: str, command: str, onwrite: Optional[Callable[[str], None]] = None
) -> int:
import FreeSimpleGUI as sg
import subprocess as sp
sg.theme("systemdefault")
text_str = [""]
text = sg.Multiline("", disabled=True, autoscroll=True, size=(80, 30))
layout = [[text]]
window = sg.Window(title, layout, finalize=True)
exitcode = [-1]
def process_func() -> None:
process = sp.Popen(command, stdout=sp.PIPE, shell=True)
for line in iter(process.stdout.readline, ""):
if line is None or line == b"":
break
s_line = line.decode("utf8")
log(s_line)
text_str[0] = text_str[0] + s_line + "\n"
if onwrite is not None:
onwrite(s_line)
exitcode[0] = process.wait()
window.perform_long_operation(process_func, "-PROCESS COMPLETE-")
while True:
event, values = window.read(timeout=1000)
if event == "-PROCESS COMPLETE-":
break
elif event is None:
exit_with_message(
"Window Closed",
"The window was closed. Exiting...",
timeout=5,
)
else:
if len(text_str[0]) < 1:
continue
text.update(text_str[0])
window.close()
return exitcode[0]
# Function to handle download progress
def download_progress(
link: str, file_name: str, set_progress: Callable[[int, int], None]
) -> None:
import requests
with open(file_name, "wb") as f:
response = requests.get(link, stream=True)
total_length = response.headers.get("content-length")
if total_length is None: # no content length header
f.write(response.content)
else:
dl = 0
total_length = int(total_length)
for data in response.iter_content(chunk_size=4096):
dl += len(data)
f.write(data)
if set_progress is not None:
set_progress(dl, total_length)
# Function to download a file with progress display
def popup_download(title: str, link: str, file_name: str) -> str:
import FreeSimpleGUI as sg
sg.theme("systemdefault")
status = [0, 0]
cache = os.path.join(SCRIPT_PATH, ".cache")
if not os.path.isdir(cache):
os.makedirs(cache)
progress = sg.ProgressBar(100, orientation="h", s=(50, 10))
text = sg.Text("0%")
layout = [[progress], [text]]
window = sg.Window(title, layout, finalize=True)
def update_log(status: list[int], dl: int, total: int) -> None:
status.clear()
status.append(dl)
status.append(total)
file_path_end = os.path.join(cache, file_name)
file_path = os.path.join(cache, "." + file_name)
if os.path.isfile(file_path):
os.remove(file_path)
download_func = lambda: download_progress(
link, file_path, lambda dl, total: update_log(status, dl, total)
)
window.perform_long_operation(download_func, "-DL COMPLETE-")
while True:
event, values = window.read(timeout=1000)
if event == "-DL COMPLETE-":
os.rename(file_path, file_path_end)
break
elif event is None:
exit_with_message(
"Window Closed",
"The window was closed. Exiting...",
timeout=5,
)
else:
if len(status) < 2:
continue
dl, total = status
perc = int(100 * (dl / total)) if total > 0 else 0
text.update(f"{perc}% ({dl}/{total})")
progress.update(perc)
window.close()
return file_path_end
# Function to get or download .NET Framework 4.8
def get_dotnet48() -> str:
# Newer if you like to test: "https://download.visualstudio.microsoft.com/download/pr/2d6bb6b2-226a-4baa-bdec-798822606ff1/8494001c276a4b96804cde7829c04d7f/ndp48-x86-x64-allos-enu.exe"
LINK = "https://download.visualstudio.microsoft.com/download/pr/7afca223-55d2-470a-8edc-6a1739ae3252/abd170b4b0ec15ad0222a809b761a036/ndp48-x86-x64-allos-enu."
cache_func = lambda FILE: popup_download(
"Downloading dotnet48", LINK, FILE
)
dotnet48 = cache("ndp48-x86-x64-allos-enu.exe", cache_func)
return dotnet48
# Function to turn syslinks into files
def deref(path: str) -> None:
import FreeSimpleGUI as sg
def dereference_links() -> None:
links = find_symlinks(path)
total_links = len(links)
extra.update("De-referencing links, please be patient...")
for i, link in enumerate(links):
target, src = link
try:
if os.path.exists(src):
with open(src, "rb") as src_file:
data = src_file.read()
os.remove(target)
with open(target, "wb") as target_file:
target_file.write(data)
else:
os.remove(target) # Remove the broken link
except Exception as e:
log(f"Failed to de-reference {target}: {e}")
update_progress(int((i + 1) / total_links * 100))
def find_symlinks(path: str) -> List[List[str]]:
import pathlib
links = []
directory = pathlib.Path(path)
for item in directory.rglob("*"):
if item.is_symlink():
target = str(item)
src = os.readlink(target)
links.append([target, src])
return links
def update_progress(percentage: int) -> None:
progress.update(percentage)
text.update(f"{percentage}%")
window.refresh()
sg.theme("systemdefault")
progress = sg.ProgressBar(100, orientation="h", size=(50, 10))
text = sg.Text("0%")
extra = sg.Text("Reading directory, please wait...")
layout = [[extra], [progress], [text]]
window = sg.Window("De-referencing Links", layout, finalize=True)
window.refresh()
window.perform_long_operation(dereference_links, "-DEREF DONE-")
while True:
event, values = window.read(timeout=1000)
if event == "-DEREF DONE-":
break
elif event is None:
exit_with_message(
"Window Closed",
"The window was closed. Exiting...",
timeout=5,
)
window.close()
# Function to copy a folder from a to b, with an ignore and allow (has priority) list
def copy_folder_with_progress(
source: str,
dest: str,
zipup: bool = False,
ignore: Optional[List[Union[None, str]]] = None,
include_override: Optional[List[Union[None, str]]] = None,
) -> None:
import zipfile
import FreeSimpleGUI as sg
if not ignore:
ignore = []
if not include_override:
include_override = []
if None in ignore:
ignore.remove(None)
ignore = list(ignore) + [
"pfx/drive_c/users",
"pfx/dosdevices",
"pfx/drive_c/Program Files (x86)",
"pfx/drive_c/Program Files",
"pfx/drive_c/ProgramData",
"drive_c/openxr",
"pfx/drive_c/vrclient",
"version",
"config_info",
]
if None in include_override:
include_override.remove(None)
include_override = list(include_override) + [
"pfx/drive_c/ProgramData/Microsoft",
"pfx/drive_c/Program Files (x86)/Microsoft.NET",
"pfx/drive_c/Program Files (x86)/Windows NT",
"pfx/drive_c/Program Files (x86)/Common Files",
"pfx/drive_c/Program Files/Common Files",
"pfx/drive_c/Program Files/Common Files",
"pfx/drive_c/Program Files/Windows NT",
]
log(f"ignoring: {ignore}\nincluding anyway: {include_override}")
def traverse_folders(path: str) -> List[str]:
import pathlib
allf = []
directory = pathlib.Path(path)
for item in directory.rglob("*"):
if item.is_file():
allf.append(item)
return allf
def update_progress(copied: int, total: int) -> None:
"""Update the GUI with the current progress."""
percentage = int(100 * (copied / total)) if total > 0 else 0
text.update(f"{percentage}% ({copied}/{total})")
progress.update(percentage)
window.refresh()
def copy_files() -> None:
files = traverse_folders(source)
copy = []
for f in files:
rfile = os.path.relpath(
f, source
) # get file path relative to source
use = True # by default, use the file
# Check if the file is in one of the dirs to ignore
for i in ignore:
if os.path.commonprefix([rfile, i]) == i:
use = False # don't use the file if it's in an ignore directory
break # break out of the ignore loop
# If the file is not in any ignored directory, check if it's in one of the dirs to include
if not use:
for i in include_override:
if os.path.commonprefix([rfile, i]) == i:
use = True # use the file if it's in an include_override directory
break # break out of the include_override loop
if use:
copy.append(rfile)
total_files = len(copy)
if zipup:
extra.update("Zipping file, please be patient...")
else:
extra.update("Copying prefix, please be patient...")
window.refresh()
if zipup:
with zipfile.ZipFile(dest, "w", zipfile.ZIP_DEFLATED) as zipf:
for i, f in enumerate(copy):
arcname = f
zipf.write(os.path.join(source, f), arcname)
update_progress(i + 1, total_files)
else:
for i, f in enumerate(copy):
src_path = os.path.join(source, f)
dest_path = os.path.join(dest, f)
try:
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
shutil.copy2(src_path, dest_path, follow_symlinks=False)
update_progress(i + 1, total_files)
except Exception as e:
log(f"Failed to copy {src_path} to {dest_path}: {e}")
sg.theme("systemdefault")
progress = sg.ProgressBar(100, orientation="h", s=(50, 10))
text = sg.Text("0% (0/?)")
extra = sg.Text("Reading prefix directory, please wait...")
layout = [[extra], [progress], [text]]
if zipup:
window = sg.Window("Copying Prefix", layout, finalize=True)
else:
window = sg.Window("Zipping File", layout, finalize=True)
window.refresh()
window.perform_long_operation(copy_files, "-COPY DONE-")
while True:
event, values = window.read(timeout=1000)
if event == "-COPY DONE-":
break
elif event is None:
exit_with_message(
"Window Closed",
"The window was closed. Exiting...",
timeout=5,
)
window.close()
def unpack_zip_with_progress(zip_path: str, dest_path: str) -> None:
import zipfile
import subprocess
import FreeSimpleGUI as sg
def update_progress(unzipped: int, total: int) -> None:
"""Update the GUI with the current progress."""
percentage = int(100 * (unzipped / total)) if total > 0 else 0
text.update(f"{percentage}% ({unzipped}/{total})")
progress.update(percentage)
window.refresh()
def unpack_files() -> None:
with zipfile.ZipFile(zip_path, "r") as zip_ref:
files = zip_ref.namelist()
total_files = len(files)
extra.update("Unpacking prefix, please be patient...")
window.refresh()
for i, file in enumerate(files):
full_file = os.path.join(dest_path, file)
try: # try to create folder if missing
if len(
os.path.dirname(full_file)
) > 0 and not os.path.isdir(os.path.dirname(full_file)):
os.makedirs(os.path.dirname(full_file), exist_ok=True)
except Exception as e:
log(
"failed to make dir '"
+ os.path.dirname(full_file)
+ "':\n\t"
+ e
)
try: # try to delete old file
if os.path.isfile(full_file) or os.path.islink(full_file):
os.remove(full_file)
except Exception as e:
log(f"failed to remove file '{full_file}':\n\t{e}")
try:
zip_ref.extract(file, dest_path)
except Exception as e:
log(
f"Failed to extract '{file}' to '{dest_path}':\n\t{e}"
)
update_progress(i + 1, total_files)
sg.theme("systemdefault")
progress = sg.ProgressBar(100, orientation="h", s=(50, 10))
text = sg.Text("0% (0/?)")
extra = sg.Text("Reading ZIP file, please wait...")
layout = [[extra], [progress], [text]]
window = sg.Window("Unpacking Prefix", layout, finalize=True)
window.refresh()
myuser = None
try:
myuser = os.getlogin()
except Exception as e:
pass
if not myuser:
try:
myuser = pwd.getpwuid(os.getuid()).pw_name
except Exception as e:
pass
if not myuser:
myuser = "steamuser"
try: # try own dest folder
subprocess.run(
["chown", "-R", myuser, dest_path],
capture_output=True,
text=True,
timeout=10,
)
except Exception as e:
log(
"failed to own folder as '"
+ myuser
+ "' for '"
+ dest_path
+ "':\n\t"
+ str(e)
)
try: # try to allow read and write on dest folder
subprocess.run(
["chmod", "-R", "ug+rw", dest_path],
capture_output=True,
text=True,
timeout=10,
)
except Exception as e:
log("failed to allow rw on '" + dest_path + "':\n\t" + str(e))
window.perform_long_operation(unpack_files, "-UNPACK DONE-")
while True:
event, values = window.read(timeout=1000)
if event == "-UNPACK DONE-":
break
elif event is None:
exit_with_message(
"Window Closed",
"The window was closed. Exiting...",
timeout=5,
)
window.close()
def is_flatpak() -> bool:
return "FLATPAK_ID" in os.environ or os.path.exists("/.flatpak-info")
def flatpakrunner():
import subprocess
import time
cachedir = os.path.join(SCRIPT_PATH, ".cache")
os.makedirs(cachedir, exist_ok=True)
flatpakrunfile = os.path.join(cachedir, "insideflatpak.tmp")
errorfile = os.path.join(cachedir, "flatpakerror.tmp")
warnfile = os.path.join(cachedir, "flatpakwarn.tmp")
log(f"Looking for run-file '{flatpakrunfile}'")
save_conf_setting("FlatpakRunning", "new")
time.sleep(2)
if load_conf_setting("FlatpakRunning") != "true" and os.path.isfile(
flatpakrunfile
):
os.remove(flatpakrunfile)
while not os.path.isfile(flatpakrunfile):
time.sleep(1)
print("Looking")
time.sleep(0.5)
flcmd = []
with open(flatpakrunfile, "r") as frf:
for line in frf:
flcmd.append(line.rstrip("\n"))
log(f"Running inside flatpak:\n\t{flcmd}")
try:
process = subprocess.run(flcmd, capture_output=True, text=True)
except Exception as e:
with open(errorfile, "w") as fef:
fef.write(str(e))
else:
print(str(process.stdout))
print(str(process.stderr))
try:
if os.getenv("SteamCompatDataPath") == None:
wserver = subprocess.run(
["flatpak-spawn", "--host", "wineserver", "--wait"],
bufsize=1,
capture_output=True,
text=True,
)
print(str(wserver.stdout))
print(str(wserver.stderr))
except Exception as e:
with open(warnfile, "w") as fwf:
fwf.write(str(e))
if os.path.isfile(flatpakrunfile):
os.remove(flatpakrunfile)