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