-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtarballer.py
More file actions
executable file
·60 lines (44 loc) · 1.48 KB
/
tarballer.py
File metadata and controls
executable file
·60 lines (44 loc) · 1.48 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
"""
@file tarballer.py
@brief Compresses a directory into a tarball.
@author Evan Elias Young
@date 2017-07-12
@date 2022-02-04
@copyright Copyright 2022 Evan Elias Young. All rights reserved.
"""
import tarfile
import sys
import os
from lzma import PRESET_EXTREME
def compress_folder(directory: str, name: str) -> None:
"""Compresses a folder to a .tar.xz.
Args:
directory (string): The directory of the folder to compress.
name (string): The name of the folder to compress.
"""
os.chdir(directory)
with tarfile.open(f"{name}.tar.xz", "w:xz", preset=PRESET_EXTREME) as tar: # type: ignore
tar.add(name)
def decompress_tar(directory: str, name: str) -> None:
"""Decompresses a .tar.xz to a folder.
Args:
directory (string): The directory of the .tar.xz to expand.
name (string): The name of the .tar.xz to expand.
"""
name = name.replace(".tar.xz", "")
os.chdir(directory)
with tarfile.open(f"{name}.tar.xz", "r:xz") as tar:
tar.extractall(".")
if __name__ == "__main__":
args: list[str] = sys.argv[1:]
if len(args) != 0:
path: str = args[0]
else:
raise Exception("NOENT")
base_dir: str = os.path.dirname(path)
base_name: str = os.path.basename(path)
if os.path.isdir(path):
compress_folder(base_dir, base_name)
if os.path.isfile(path) and path.endswith(".tar.xz"):
decompress_tar(base_dir, base_name)