Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cursed-hr): fallback to source dir if tmp dir not working #544

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions src/cursed_hr/cursed_hr.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pathlib import Path
from typing import Any, BinaryIO

import platformdirs
import zstandard as zstd
from gallia.log import PenlogPriority, PenlogRecord
from gallia.services.uds.core.service import NegativeResponse, UDSRequest, UDSResponse
Expand Down Expand Up @@ -261,14 +262,9 @@ def uncompressed_file(self) -> BinaryIO:
self.window.addstr(f"Loading contents from {self.in_file}")
self.window.refresh()

try:
if self.in_file.suffix in [".zst", ".gz"]:
self.window.erase()
self.window.addstr(f"Loading contents from {self.in_file}: Decompressing file ...")
self.window.refresh()

file = tempfile.TemporaryFile()
if self.in_file.suffix in [".zst", ".gz"]:

def copy_to_file(tmp_file: Any) -> None:
match self.in_file.suffix:
case ".zst":
with self.in_file.open("rb") as in_file:
Expand All @@ -277,14 +273,37 @@ def uncompressed_file(self) -> BinaryIO:
case ".gz":
with gzip.open(self.in_file, "rb") as in_file:
shutil.copyfileobj(in_file, file)
else:
file = self.in_file.open("rb") # type: ignore

file.flush()
tmp_file.flush()

return file
except Exception as e:
raise ValueError("Unsupported file format") from e
self.window.erase()
self.window.addstr(f"Loading contents from {self.in_file}: Decompressing file ...")
self.window.refresh()

file = tempfile.TemporaryFile()

try:
try:
copy_to_file(file)
except OSError:
file.close()

self.window.erase()
self.window.addstr(
f"Could not decompress to {tempfile.gettempdir()}. Trying to decompress to source directory ..."
)
self.window.refresh()

file = tempfile.TemporaryFile(dir=platformdirs.user_cache_dir())

copy_to_file(file)
except:
file.close()
raise
else:
file = self.in_file.open("rb") # type: ignore

return file

def parse_structure(self, file: BinaryIO | mmap.mmap) -> None:
"""
Expand Down