From ad08d9991c2ec4517dfa99e403dfa88177ef59f5 Mon Sep 17 00:00:00 2001 From: Robrecht Cannoodt Date: Tue, 13 Dec 2022 14:54:43 +0100 Subject: [PATCH] first commit for adding `extract_if_need_be` functionality --- viashpy/utils.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/viashpy/utils.py b/viashpy/utils.py index b6c5b97..a2a08df 100644 --- a/viashpy/utils.py +++ b/viashpy/utils.py @@ -64,3 +64,33 @@ def extract_tar(pathname: Path | str, output_dir: Path | str): members_to_move = [mem for mem in members if mem.path != Path(".")] open_tar.extractall(unpacked_path, members=members_to_move) return unpacked_path + +# helper function for cheching whether something is a gzip +def is_gz_file(path: Path) -> bool: + with open(path, "rb") as file: + return file.read(2) == b"\x1f\x8b" + +# if {par_value} is a Path, extract it to a temp_dir_path and return the resulting path +def extract_if_need_be(pathname: Path | str, output_dir: Path | str) -> Path: + pathname, output_dir = Path(pathname), Path(output_dir) + + if pathname.is_file() and tarfile.is_tarfile(pathname): + logger.info("Tar detected; extracting %s", pathname) + return extract_tar(pathname, output_dir) + + elif pathname.is_file() and is_gz_file(pathname): + # Remove extension (if it exists) + extaction_file_name = Path(pathname.stem) + unpacked_path = output_dir / extaction_file_name + logger.info("Gzip detected; extracting %s", pathname) + + import gzip + import shutil + + with gzip.open(pathname, "rb") as f_in: + with open(unpacked_path, "wb") as f_out: + shutil.copyfileobj(f_in, f_out) + return unpacked_path + + else: + return pathname \ No newline at end of file