mvn_flight/bin/recursive_unpack_targz.py

38 lines
1.3 KiB
Python

import os
import tarfile
def extract_tar_gz(filepath, extract_dir):
""" Unpack archive in specified directory """
try:
with tarfile.open(filepath, "r:gz") as tar:
tar.extractall(path=extract_dir)
print(f"[+] Extracted: {filepath}")
except Exception as e:
print(f"[!] Error extracting {filepath}: {e}")
def should_extract(archive_path):
""" check exist directory's name without .tag.gs """
dirname = os.path.splitext(os.path.splitext(archive_path)[0])[0]
list_ignore = ['brd.tar.gz', 'aux_data.tar.gz', 'uvi.tar.gz', 'aux.tar.gz']
if all(elem not in archive_path for elem in list_ignore):
return not os.path.isdir(dirname)
def walk_and_extract(start_dir):
""" recursive directory traversal with unpacking """
for root, _, files in os.walk(start_dir):
for file in files:
if file.endswith(".tar.gz"):
archive_path = os.path.join(root, file)
target_dir = os.path.splitext(os.path.splitext(archive_path)[0])[0]
if should_extract(archive_path):
extract_tar_gz(archive_path, target_dir)
if __name__ == "__main__":
import sys
if len(sys.argv) != 2:
print("Usage: python recursive_unpack_targz.py /path/to/start/dir")
else:
walk_and_extract(sys.argv[1])