generated from KOLANICH/python_project_boilerplate.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tar.py
32 lines (27 loc) · 1.11 KB
/
tar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import typing
import tarfile
from pathlib import Path
from fsutilz import isNestedIn
from ...styles import styles
from ..compression import CompressionT
from . import ArchiveFormat
class Tar(ArchiveFormat):
@staticmethod
def unpack(archPath: Path, extrDir: Path, config: "FetchConfig", compression: typing.Optional[CompressionT] = None) -> None:
extrDir = extrDir.resolve()
packedSize = archPath.stat().st_size
if compression:
unpackedSize = compression.extractOriginalSize(archPath, packedSize)
else:
unpackedSize = packedSize
with tarfile.open(archPath, "r" + (":" + compression.id if compression else "")) as arch:
# todo: unit="B", unit_divisor=1024, unit_scale=True
with config.progressReporter(unpackedSize, str(styles.operationName("unpacking") + " " + styles.entity("archive"))) as pb:
for f in arch:
fp = (extrDir / f.name).absolute()
if isNestedIn(extrDir, fp):
if fp.is_file() or fp.is_symlink():
fp.unlink()
fp.parent.mkdir(parents=True, exist_ok=True)
arch.extract(f, extrDir, set_attrs=True)
pb.report(str(fp.relative_to(extrDir)), incr=f.size)