ulwgl_dl_util: add post cleanup routine

- When we're downloading the latest Proton, it's possible the user can interrupt download or extraction process which can lead to corrupted or incomplete files. As a result, in the next run of the launcher, a FileNotFoundError can be falsely raised for the $PROTONPATH/proton file. In the case of an interrupt, be sure to remove the relevant files before we exit.
This commit is contained in:
R1kaB3rN 2024-02-15 19:39:24 -08:00
parent 621b6c1dd5
commit 2512cdc23a
No known key found for this signature in database

View File

@ -5,6 +5,7 @@ from tarfile import open as tar_open
from requests import Response from requests import Response
from typing import Dict, List, Tuple, Any, Union from typing import Dict, List, Tuple, Any, Union
from hashlib import sha512 from hashlib import sha512
from shutil import rmtree
def get_ulwgl_proton(env: Dict[str, str]) -> Union[Dict[str, str], None]: def get_ulwgl_proton(env: Dict[str, str]) -> Union[Dict[str, str], None]:
@ -43,16 +44,15 @@ def get_ulwgl_proton(env: Dict[str, str]) -> Union[Dict[str, str], None]:
if ( if (
files and Path(Path().home().as_posix() + "/.cache/ULWGL").joinpath(files[1][0]) files and Path(Path().home().as_posix() + "/.cache/ULWGL").joinpath(files[1][0])
).is_file(): ).is_file():
proton: str = files[1][0] print(files[1][0] + " found in: " + cache.as_posix())
print(f"{proton} found in: {cache.as_posix()}")
_extract_dir( _extract_dir(
Path(Path().home().as_posix() + "/.cache/ULWGL").joinpath(proton), Path(Path().home().as_posix() + "/.cache/ULWGL").joinpath(files[1][0]),
steam_compat, steam_compat,
) )
# Set PROTONPATH to .local/share/Steam/GE-Proton*
environ["PROTONPATH"] = steam_compat.joinpath( environ["PROTONPATH"] = steam_compat.joinpath(
proton[: proton.find(".")] files[1][0][: files[1][0].find(".")]
).as_posix() ).as_posix()
env["PROTONPATH"] = environ["PROTONPATH"] env["PROTONPATH"] = environ["PROTONPATH"]
@ -68,7 +68,22 @@ def get_ulwgl_proton(env: Dict[str, str]) -> Union[Dict[str, str], None]:
return env return env
except ValueError as err: except ValueError as err:
# Digest mismatched, which should be rare
# In this case, just leave it up to the user to handle it and refer to the cache again
print(err) print(err)
except KeyboardInterrupt:
tarball: str = files[1][0]
proton: str = tarball[: tarball.find(".")]
# The files may have been left in an incomplete state
# Remove the tarball and proton we had extracted
print("Keyboard Interrupt received.\nCleaning ...")
if cache.joinpath(tarball).is_file():
print(f"Purging {tarball} in {cache} ...")
cache.joinpath(tarball).unlink()
if steam_compat.joinpath(proton).is_dir():
print(f"Purging {proton} in {steam_compat} ...")
rmtree(steam_compat.joinpath(proton).as_posix())
# Cache # Cache
for proton in cache.glob("GE-Proton*.tar.gz"): for proton in cache.glob("GE-Proton*.tar.gz"):