| |
| import argparse |
| import gzip |
| import subprocess |
| from concurrent.futures import FIRST_COMPLETED, ProcessPoolExecutor, wait |
| from pathlib import Path |
|
|
|
|
| DEFAULT_WORKERS = 32 |
| DEFAULT_CHUNK_MB = 32 |
|
|
|
|
| def gzip_chunk(data: bytes, compresslevel: int) -> bytes: |
| return gzip.compress(data, compresslevel=compresslevel, mtime=0) |
|
|
|
|
| def stream_tar_gz_parallel( |
| input_dir: Path, |
| output_file: Path, |
| workers: int, |
| chunk_size: int, |
| compresslevel: int, |
| ) -> None: |
| tar_cmd = ["tar", "-cf", "-", "-C", str(input_dir.parent), input_dir.name] |
| tar_proc = subprocess.Popen(tar_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| if tar_proc.stdout is None or tar_proc.stderr is None: |
| raise RuntimeError("Failed to start tar process") |
|
|
| pending = {} |
| completed = {} |
| next_index = 0 |
| next_to_write = 0 |
| max_in_flight = max(workers * 2, 1) |
|
|
| with output_file.open("wb") as out_f, ProcessPoolExecutor(max_workers=workers) as executor: |
| try: |
| while True: |
| while len(pending) < max_in_flight: |
| chunk = tar_proc.stdout.read(chunk_size) |
| if not chunk: |
| break |
| future = executor.submit(gzip_chunk, chunk, compresslevel) |
| pending[future] = next_index |
| next_index += 1 |
|
|
| if not pending: |
| break |
|
|
| done, _ = wait(pending, return_when=FIRST_COMPLETED) |
| for future in done: |
| completed[pending.pop(future)] = future.result() |
|
|
| while next_to_write in completed: |
| out_f.write(completed.pop(next_to_write)) |
| next_to_write += 1 |
| finally: |
| tar_proc.stdout.close() |
|
|
| stderr = tar_proc.stderr.read().decode("utf-8", errors="replace").strip() |
| tar_proc.stderr.close() |
| return_code = tar_proc.wait() |
| if return_code != 0: |
| raise RuntimeError(f"tar failed with exit code {return_code}: {stderr}") |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser(description="Compress a directory into tar.gz with parallel gzip workers.") |
| parser.add_argument("--input-dir", required=True, help="Directory to compress.") |
| parser.add_argument("--output", required=True, help="Output tar.gz file path.") |
| parser.add_argument("--workers", type=int, default=DEFAULT_WORKERS, help="Number of gzip worker processes.") |
| parser.add_argument("--chunk-mb", type=int, default=DEFAULT_CHUNK_MB, help="Chunk size in MB.") |
| parser.add_argument("--compresslevel", type=int, default=6, help="Gzip level from 0 to 9.") |
| args = parser.parse_args() |
|
|
| input_dir = Path(args.input_dir).resolve() |
| output_file = Path(args.output).resolve() |
|
|
| if not input_dir.exists(): |
| raise FileNotFoundError(f"Input directory not found: {input_dir}") |
| if not input_dir.is_dir(): |
| raise NotADirectoryError(f"Input path is not a directory: {input_dir}") |
|
|
| output_file.parent.mkdir(parents=True, exist_ok=True) |
| stream_tar_gz_parallel( |
| input_dir=input_dir, |
| output_file=output_file, |
| workers=args.workers, |
| chunk_size=args.chunk_mb * 1024 * 1024, |
| compresslevel=args.compresslevel, |
| ) |
| print(f"Created archive: {output_file}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|