refactor(clew): add folder creation helper

This commit is contained in:
2026-09-07 14:46:44 -04:00
parent d6cdf482e4
commit cf337b6c4e

View File

@@ -69,6 +69,11 @@ def prepare_output(output_root: Path, overwrite: bool) -> Path:
return output_root / "extracted"
def get_folder(path: Path) -> Path:
path.mkdir(parents=True, exist_ok=True)
return path
def relative_to(root: Path, path: Path) -> Path:
return path.resolve().relative_to(root.resolve())
@@ -91,7 +96,7 @@ def normalize_suffixes(suffixes: list[str] | None) -> list[str] | None:
def copy_loose_file(file: Path, game_root: Path, extracted_dir: Path) -> None:
relative = relative_to(game_root, file)
destination = extracted_dir / relative
destination.parent.mkdir(parents=True, exist_ok=True)
get_folder(destination.parent)
shutil.copy2(file, destination)
LOGGER.info("copied loose file: %s", relative)
@@ -103,8 +108,7 @@ def extract_archive(
suffixes: list[str] | None,
) -> None:
archive_relative = relative_to(game_root, archive)
output_dir = extracted_dir / archive_relative.with_suffix("")
output_dir.mkdir(parents=True, exist_ok=True)
output_dir = get_folder(extracted_dir / archive_relative.with_suffix(""))
LOGGER.info("extracting archive: %s", archive_relative)
with archive.open("rb") as handle:
@@ -115,8 +119,7 @@ def extract_archive(
def configure_logging(output_root: Path) -> None:
log_level = logging.INFO
timestamp = datetime.now(UTC).strftime("%Y%m%dT%H%M%SZ")
logs_dir = output_root / "logs"
logs_dir.mkdir(parents=True, exist_ok=True)
logs_dir = get_folder(output_root / "logs")
log_file = logs_dir / f"{logging.getLevelName(log_level)}-clew-{timestamp}.log"
formatter = logging.Formatter("%(message)s")