Skip to content

Commit

Permalink
ulwgl_dl_util: fix bug for print statements
Browse files Browse the repository at this point in the history
Co-authored-by: Stelios Tsampas <[email protected]>

- Do not print to stdout when finding or downloading Proton as it is not intended to be processed directly as input for other applications.

- Related to #31 (comment)
  • Loading branch information
R1kaB3rN committed Feb 19, 2024
1 parent 9a3c07d commit 5a28836
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions ulwgl_dl_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ssl import create_default_context
from json import loads as loads_json
from urllib.request import urlretrieve
from sys import stderr


def get_ulwgl_proton(env: Dict[str, str]) -> Union[Dict[str, str]]:
Expand All @@ -21,7 +22,7 @@ def get_ulwgl_proton(env: Dict[str, str]) -> Union[Dict[str, str]]:
try:
files = _fetch_releases()
except HTTPException:
print("Offline.\nContinuing ...")
print("Offline.\nContinuing ...", file=stderr)

cache: Path = Path.home().joinpath(".cache/ULWGL")
steam_compat: Path = Path.home().joinpath(".local/share/Steam/compatibilitytools.d")
Expand Down Expand Up @@ -114,12 +115,12 @@ def _fetch_proton(
proton_dir: str = proton[: proton.find(".tar.gz")] # Proton dir

# TODO: Parallelize this
print(f"Downloading {hash} ...")
print(f"Downloading {hash} ...", file=stderr)
urlretrieve(hash_url, cache.joinpath(hash).as_posix())
print(f"Downloading {proton} ...")
print(f"Downloading {proton} ...", file=stderr)
urlretrieve(proton_url, cache.joinpath(proton).as_posix())

print("Completed.")
print("Completed.", file=stderr)

with cache.joinpath(proton).open(mode="rb") as file:
if (
Expand All @@ -128,7 +129,7 @@ def _fetch_proton(
):
err: str = "Digests mismatched.\nFalling back to cache ..."
raise ValueError(err)
print(f"{proton}: SHA512 is OK")
print(f"{proton}: SHA512 is OK", file=stderr)

_extract_dir(cache.joinpath(proton), steam_compat)
environ["PROTONPATH"] = steam_compat.joinpath(proton_dir).as_posix()
Expand All @@ -140,23 +141,23 @@ def _fetch_proton(
def _extract_dir(proton: Path, steam_compat: Path) -> None:
"""Extract from the cache to another location."""
with tar_open(proton.as_posix(), "r:gz") as tar:
print(f"Extracting {proton} -> {steam_compat.as_posix()} ...")
print(f"Extracting {proton} -> {steam_compat.as_posix()} ...", file=stderr)
tar.extractall(path=steam_compat.as_posix())
print("Completed.")
print("Completed.", file=stderr)


def _cleanup(tarball: str, proton: str, cache: Path, steam_compat: Path) -> None:
"""Remove files that may have been left in an incomplete state to avoid corruption.
We want to do this when a download for a new release is interrupted
"""
print("Keyboard Interrupt.\nCleaning ...")
print("Keyboard Interrupt.\nCleaning ...", file=stderr)

if cache.joinpath(tarball).is_file():
print(f"Purging {tarball} in {cache} ...")
print(f"Purging {tarball} in {cache} ...", file=stderr)
cache.joinpath(tarball).unlink()
if steam_compat.joinpath(proton).is_dir():
print(f"Purging {proton} in {steam_compat} ...")
print(f"Purging {proton} in {steam_compat} ...", file=stderr)
rmtree(steam_compat.joinpath(proton).as_posix())


Expand All @@ -170,15 +171,16 @@ def _get_from_steamcompat(
proton_dir: str = files[1][0][: files[1][0].find(".tar.gz")]

for proton in steam_compat.glob("ULWGL-Proton*"):
print(f"{proton.name} found in: {steam_compat.as_posix()}")
print(f"{proton.name} found in: {steam_compat.as_posix()}", file=stderr)
environ["PROTONPATH"] = proton.as_posix()
env["PROTONPATH"] = environ["PROTONPATH"]

# Notify the user that they're not using the latest
if proton_dir and proton.name != proton_dir:
print(
"ULWGL-Proton is outdated.\nFor latest release, please download "
+ files[1][1]
+ files[1][1],
file=stderr,
)

return env
Expand Down Expand Up @@ -214,7 +216,7 @@ def _get_from_cache(
if path:
proton_dir: str = name[: name.find(".tar.gz")] # Proton dir

print(f"{name} found in: {path}")
print(f"{name} found in: {path}", file=stderr)
try:
_extract_dir(path, steam_compat)
environ["PROTONPATH"] = steam_compat.joinpath(proton_dir).as_posix()
Expand All @@ -223,7 +225,7 @@ def _get_from_cache(
return env
except KeyboardInterrupt:
if steam_compat.joinpath(proton_dir).is_dir():
print(f"Purging {proton_dir} in {steam_compat} ...")
print(f"Purging {proton_dir} in {steam_compat} ...", file=stderr)
rmtree(steam_compat.joinpath(proton_dir).as_posix())
raise

Expand All @@ -238,7 +240,7 @@ def _get_latest(
When the digests mismatched or when interrupted, refer to cache for an old version
"""
if files:
print("Fetching latest release ...")
print("Fetching latest release ...", file=stderr)
try:
_fetch_proton(env, steam_compat, cache, files)
env["PROTONPATH"] = environ["PROTONPATH"]
Expand Down

0 comments on commit 5a28836

Please sign in to comment.