-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
79 lines (63 loc) · 2.34 KB
/
utils.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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import io
import tarfile
import logging
import shutil
def tar_paths(paths):
"""
Compresses directories and files to a single tar file.
Returns the tar file as a data stream, None if error.
"""
# Check single path vs. multiple
if isinstance(paths, str):
paths = (paths,)
# Filter out non-existent paths
paths = [x for x in paths if os.path.exists(x)]
# Make sure the tar file will actually contain something
if not paths:
logging.warning("No files/folders to add, not creating tar file")
return None
logging.debug("Creating tar file")
tar_stream = io.BytesIO()
try:
zfile = tarfile.open(fileobj=tar_stream, mode='w|gz')
except EnvironmentError as e:
logging.warning("Couldn't create tar file")
return None
for path in paths:
if os.path.isdir(path):
root_len = len(os.path.abspath(path))
# If compressing multiple things, preserve the top-level folder names
if len(paths) > 1:
root_len -= len(os.path.basename(path)) + len(os.sep)
# Walk through the directory, adding all files
for root, dirs, files in os.walk(path):
archive_root = os.path.abspath(root)[root_len:]
for f in files:
fullpath = os.path.join(root, f)
archive_name = os.path.join(archive_root, f)
try:
zfile.add(fullpath, archive_name, recursive=False)
except EnvironmentError as e:
logging.warning("Couldn't add file: %s", (str(e),))
else:
# Exists and not a directory, assume a file
try:
zfile.add(path, os.path.basename(path), recursive=False)
except EnvironmentError as e:
logging.warning("Couldn't add file: %s", (str(e),))
zfile.close()
tar_stream.seek(0)
return tar_stream
def untar_to_path(zfile, target):
with tarfile.open(fileobj=zfile, mode='r') as zfile:
zfile.extractall(path=target)
def untar_stream_to_path(stream, target):
zfile = io.BytesIO(stream)
untar_to_path(zfile, target)
def rmrf(path):
"""Just delete it, dude"""
if os.path.isdir(path):
shutil.rmtree(path)
elif os.path.exists(path):
os.remove(path)