-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
312 lines (266 loc) · 10.6 KB
/
main.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
import zipfile
import logging
import sys
import argparse
import shutil
from subprocess import Popen
from pathlib import Path
from tempfile import TemporaryDirectory
import httpx
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.logging import RichHandler
from rich.prompt import Confirm, Prompt
BUILD_NAMES = []
BUILD_TYPES = {
"1": {"type": "release", "description": "(recommended) The default Remix version that prioritizes speed"},
"2": {"type": "debugoptimized", "description": "For debugging issues. This build is still fast."},
"3": {"type": "debug", "description": "For debugging only. Unplayable in games"},
}
def get_build_type():
print("Please choose a build type:")
for choice, build_info in BUILD_TYPES.items():
print(f"{choice}: \033[38;5;208m{build_info['type']}\033[0m - {build_info['description']}")
while True:
chosen_build_type = input("Your choice: ")
if chosen_build_type in BUILD_TYPES:
return BUILD_TYPES[chosen_build_type]['type']
else:
print("Invalid choice. Please try again.")
parser = argparse.ArgumentParser(
prog="RTXRemix Downloader",
description="Downloads the latest RTXRemix builds.",
)
parser.add_argument(
'-d',
"--debug",
action="store_true",
help="Enables debug logging.",
)
parser.add_argument(
'-b',
"--build-type",
default="release",
choices=["release", "debug", "debugoptimized"],
help="Specifies the build type to download.",
)
args = argparse.Namespace()
args.debug = False # or True if you want to enable debug by default
args.build_type = get_build_type()
print(f"Downloading {args.build_type} builds")
REPOSITORIES = {
"NVIDIAGameWorks/rtx-remix": {
"repo_type": "release",
"move_to": None,
"temp_dir": None,
"main_directory": True,
},
"NVIDIAGameWorks/dxvk-remix": {
"repo_type": "artifact",
"move_to": ".trex", # Move this to a special subdirectory
"temp_dir": None,
"main_directory": False,
"artifact_branch": "main",
},
"NVIDIAGameWorks/bridge-remix": {
"repo_type": "artifact",
"move_to": None, # Move this to the root of the temp directory
"temp_dir": None,
"main_directory": False,
"artifact_branch": "main",
},
}
HTTP = httpx.Client(
headers={
"User-Agent": f"Python/httpx v{httpx.__version__} - RTXRemix Downloader Script"
}
)
LOGGER = logging.getLogger("rtxremix")
FORMAT = "%(message)s"
logging.basicConfig(
level="INFO", format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
)
logging.getLogger("httpx").setLevel(logging.WARNING)
CONSOLE = Console()
PROGRESS = Progress(
SpinnerColumn(),
TextColumn("[bold blue] {task.completed} of {task.total} steps completed"),
console=CONSOLE,
)
STEP_COUNTER = PROGRESS.add_task("Steps", total=len(REPOSITORIES) * 2 + 4)
class HiddenPrompt(Prompt):
prompt_suffix = ""
def replace_recursively(root_path: Path, move_to: Path) -> None:
"""Recursively replaces a directory with its contents while preserving the directory structure"""
for child in root_path.iterdir():
if child.is_file():
LOGGER.debug(f"Replacing {child} with {move_to.joinpath(child.name)}")
shutil.move(str(child), str(move_to.joinpath(child.name)))
elif child.is_dir():
LOGGER.debug(
f"Replacing {child} with {move_to.joinpath(child.name) if move_to else None}"
)
move_to.joinpath(child.name).mkdir(exist_ok=True)
replace_recursively(child, move_to.joinpath(child.name))
child.rmdir()
def fetch_release(repo: str, temp_dir: TemporaryDirectory) -> TemporaryDirectory:
"""Fetches the latest release from a repository"""
path = Path(temp_dir.name)
PROGRESS.print(
f"Fetching the latest release info from [bold blue]{repo}[/bold blue]"
)
resp = HTTP.get(f"https://api.github.com/repos/{repo}/releases/latest")
json = resp.json()
for asset in json["assets"]:
if "symbols" not in asset["name"]:
download_url = asset["browser_download_url"]
size = asset["size"]
PROGRESS.print(f"Downloading latest release from [bold blue]{repo}[/bold blue]")
PROGRESS.advance(STEP_COUNTER)
with open(path.joinpath(f"{json['name']}.zip"), "wb") as f:
with HTTP.stream(
"GET", download_url, timeout=30, follow_redirects=True
) as resp:
for data in resp.iter_bytes():
f.write(data)
PROGRESS.print(f"Extracting latest release from [bold blue]{repo}[/bold blue]")
PROGRESS.advance(STEP_COUNTER)
zipfile.ZipFile(path.joinpath(f"{json['name']}.zip")).extractall(path)
path.joinpath(f"{json['name']}.zip").unlink()
# Move the contents of the zip to the root of the temp directory.
child_path = next(path.iterdir())
replace_recursively(child_path, path)
child_path.rmdir()
return temp_dir
def fetch_artifact(repo: str, temp_dir: TemporaryDirectory) -> TemporaryDirectory:
"""Fetches the latest artifact from a repository"""
path = Path(temp_dir.name)
PROGRESS.print(
f"Fetching the latest artifact info from [bold blue]{repo}[/bold blue]"
)
resp = HTTP.get(f"https://api.github.com/repos/{repo}/actions/runs")
json = resp.json()
# Grab the first succeeded run on the specified artifact branch.
# Runs are sorted from newest to oldest by GitHub. So no need to date check.
for run in json["workflow_runs"]:
if (
run["head_branch"] == REPOSITORIES[repo]["artifact_branch"]
and run["conclusion"] == "success"
):
resp = HTTP.get(run["artifacts_url"])
json = resp.json()
break
for artifact in json["artifacts"]:
if args.build_type in artifact["name"]:
artifact_name = artifact["name"]
id = artifact["id"]
size = artifact["size_in_bytes"]
BUILD_NAMES.append(artifact_name)
break
PROGRESS.print(f"Downloading latest artifact from [bold blue]{repo}[/bold blue]")
PROGRESS.advance(STEP_COUNTER)
with open(path.joinpath(f"{artifact_name}.zip"), "wb") as f:
with HTTP.stream(
"GET",
f"https://nightly.link/{repo}/actions/artifacts/{id}.zip",
timeout=30,
follow_redirects=True,
) as resp:
for data in resp.iter_bytes():
f.write(data)
PROGRESS.print(f"Extracting latest artifact from [bold blue]{repo}[/bold blue]")
PROGRESS.advance(STEP_COUNTER)
zipfile.ZipFile(path.joinpath(f"{artifact_name}.zip")).extractall(path)
path.joinpath(f"{artifact_name}.zip").unlink()
return temp_dir
def main() -> None:
"""Main loop"""
HiddenPrompt.ask(
"[b]RTX Remix Download Script[/b]\n"
"This script requests the latest artifact builds from the official Github repositories.\n"
"This downloads the file in the same location as the script, unzips and cleans up after itself.\n"
"Find us on Discord: [blue]https://discord.gg/rtxremix[/blue]\n"
"[i]This script is not affiliated with NVIDIA or the RTXRemix project.[/i]\n"
"\n"
"Press Enter to continue...",
password=True,
console=CONSOLE,
)
with PROGRESS:
for repo, data in REPOSITORIES.items():
if data["repo_type"] == "release":
data["temp_dir"] = fetch_release(
repo, TemporaryDirectory(prefix="RTXREMIX-")
)
elif data["repo_type"] == "artifact":
data["temp_dir"] = fetch_artifact(
repo, TemporaryDirectory(prefix="RTXREMIX-")
)
main_directory = None
for repo, data in REPOSITORIES.items():
if data["main_directory"]:
main_directory = Path(
data["temp_dir"].name
) # Returns a TemporaryDirectory object
break
# Move all the files to their appropiate locations
PROGRESS.print("Moving artifacts to their appropiate locations")
PROGRESS.advance(STEP_COUNTER)
for repo, data in REPOSITORIES.items():
if data["main_directory"] is False:
if data["move_to"] is None:
replace_recursively(Path(data["temp_dir"].name), main_directory)
else:
replace_recursively(
Path(data["temp_dir"].name),
main_directory.joinpath(data["move_to"]),
)
# Delete debugging symbols
# Yes this looks ugly. Too bad!
PROGRESS.print("Cleaning up debugging symbols")
PROGRESS.advance(STEP_COUNTER)
for child in main_directory.rglob("*.pdb"):
child.unlink()
for child in main_directory.rglob("CRC.txt"):
child.unlink()
for child in main_directory.rglob("artifacts_readme.txt"):
child.unlink()
# Move main_directory to working dir
PROGRESS.print('Moving files to the "remix" directory')
PROGRESS.advance(STEP_COUNTER)
final_path = Path(sys.argv[0]).parent.joinpath("remix")
final_path.mkdir(exist_ok=True)
replace_recursively(main_directory, final_path)
# Print the names of the downloaded packages
print("Downloaded the following packages:")
for name in BUILD_NAMES:
print(name)
# Write build names to a text file
with open(final_path.joinpath('build_names.txt'), 'w') as f:
for name in BUILD_NAMES:
f.write(f'{name}\n')
# Cleanup the temp dirs
PROGRESS.print("Cleaning up temporary directories")
PROGRESS.advance(STEP_COUNTER)
for repo, data in REPOSITORIES.items():
data["temp_dir"].cleanup()
PROGRESS.print("[green]Success![/green]")
if Confirm.ask(
"Would you like to open the [bold green]Remix[/bold green] directory now?",
default=True,
console=CONSOLE,
):
Popen(f'explorer "{final_path}"')
HiddenPrompt.ask(
"You can find the latest RTX Remix install in:\n"
f"[bold blue]{final_path.resolve()}[/bold blue]\n"
"RTX Remix install guide:\n"
"[blue]https://github.com/NVIDIAGameWorks/rtx-remix/wiki/runtime-user-guide[/blue]\n"
"\n"
"Press [bold]Enter[/bold] to close this window..."
)
if __name__ == "__main__":
if args.debug is True:
LOGGER.setLevel(logging.DEBUG)
logging.getLogger("httpx").setLevel(logging.DEBUG)
main()