Repository navigation
Expand file tree
/
Copy path_common.py
More file actions
256 lines (207 loc) · 6.74 KB
/
Copy path_common.py
File metadata and controls
256 lines (207 loc) · 6.74 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import os
import shutil
import zipfile
from concurrent.futures import ThreadPoolExecutor
from fnmatch import fnmatch
from mtlogger import logger
from mtprompt import Prompt
from threading import Lock
from tqdm import tqdm
from _constants import FAILED, INCOMPLETE, SUCCEEDED, ZIP_TYPES
prompt_lock = Lock()
def compress_child_folders(
parent_folder_path: str,
output_type: str,
remove_original=False,
exclusion_patterns: list | None = None,
min_depth=1,
max_depth=1,
):
if max_depth < 1:
logger.error('Depth must be 1 or greater.')
return FAILED
folders = []
for root, dirs, _ in os.walk(parent_folder_path, topdown=False):
for dir_name in dirs:
folder_path = os.path.join(root, dir_name)
rel_folder_path = os.path.relpath(folder_path, parent_folder_path)
current_depth = rel_folder_path.count(os.sep) + 1
if min_depth <= current_depth <= max_depth:
folders.append(folder_path)
if folders:
with ThreadPoolExecutor() as executor:
results = list(
tqdm(
executor.map(
lambda folder: compress_folder(
folder,
output_type,
remove_original,
exclusion_patterns,
show_progress=False,
),
folders,
),
total=len(folders),
desc=f'Processing "{parent_folder_path}"',
)
)
status = get_status_from_results(results)
if status == FAILED:
logger.error(
f'Failed to compress one or more folders in "{parent_folder_path}".'
)
elif status == INCOMPLETE:
logger.warn(
f'One or more folders were not compressed in "{parent_folder_path}".'
)
return status
else:
logger.warn(f'No folders found in "{parent_folder_path}".')
return INCOMPLETE
def compress_folder(
folder_path: str,
output_type: str = ZIP_TYPES[0],
remove_original=False,
exclusion_patterns: list | None = None,
show_progress=True,
):
folder_name = os.path.basename(folder_path)
parent_dir = os.path.dirname(folder_path)
zip_filename = f'{folder_name}.{output_type.lower()}'
tmp_zip_path = os.path.join(parent_dir, f'.{zip_filename}.tmp')
final_zip_path = os.path.join(parent_dir, zip_filename)
already_exists = os.path.exists(final_zip_path)
try:
if already_exists:
with prompt_lock:
overwrite = Prompt.bool(
f'\nArchive "{final_zip_path}" already exists. Overwrite?',
default=False,
)
if not overwrite:
logger.warn(
f'Skipping "{folder_path}". A compressed file with the same name already exists.'
)
return INCOMPLETE
files_to_compress = []
for root, dir_names, file_names in os.walk(folder_path):
if exclusion_patterns:
for dir_name in dir_names[:]:
if any(
fnmatch(dir_name, pattern.rstrip('/'))
for pattern in exclusion_patterns
):
dir_names.remove(dir_name)
for file_name in file_names:
if exclusion_patterns and any(
fnmatch(file_name, pattern) for pattern in exclusion_patterns
):
continue
files_to_compress.append(os.path.join(root, file_name))
if files_to_compress:
with zipfile.ZipFile(
tmp_zip_path, 'w', zipfile.ZIP_DEFLATED
) as compressed_file:
for file_path in (
tqdm(files_to_compress, unit='file')
if show_progress
else files_to_compress
):
compressed_file.write(
file_path, os.path.relpath(file_path, folder_path)
)
os.replace(tmp_zip_path, final_zip_path)
if remove_original:
shutil.rmtree(folder_path)
return SUCCEEDED
logger.warn(f'No files found in "{folder_path}".')
return INCOMPLETE
except Exception as ex:
logger.error(f'An error occurred while processing "{folder_name}":\n{ex}')
return FAILED
finally:
try:
if os.path.exists(tmp_zip_path):
os.remove(tmp_zip_path)
except Exception as cleanup_ex:
logger.error(
f'Unable to remove temporary archive "{tmp_zip_path}":\n{cleanup_ex}'
)
return FAILED
def extract_child_archives(
parent_folder_path: str,
remove_archives=True,
):
logger.log(f'Extracting archives in "{parent_folder_path}"...')
archives = []
for root, _, files in os.walk(parent_folder_path, topdown=False):
for file_name in files:
if any(file_name.upper().endswith(f'.{ext}') for ext in ZIP_TYPES):
archives.append(os.path.join(root, file_name))
if archives:
with ThreadPoolExecutor() as executor:
results = list(
tqdm(
executor.map(
lambda archive_path: extract_archive(archive_path, remove_archives),
archives,
),
total=len(archives),
desc=f'Processing "{parent_folder_path}"',
)
)
status = get_status_from_results(results)
if status == FAILED:
logger.error(
f'Failed to extract one or more archives in "{parent_folder_path}".'
)
elif status == INCOMPLETE:
logger.warn(
f'One or more archives were not extracted in "{parent_folder_path}".'
)
return status
else:
logger.warn(f'No archives found in "{parent_folder_path}".')
return INCOMPLETE
def extract_archive(
archive_path: str,
remove_archive=True,
):
folder_name = os.path.splitext(os.path.basename(archive_path))[0]
target_dir = os.path.join(os.path.dirname(archive_path), folder_name)
already_exists = os.path.exists(target_dir)
try:
if already_exists:
with prompt_lock:
overwrite = Prompt.bool(
f'\nFolder "{target_dir}" already exists. Overwrite?', default=False
)
if not overwrite:
logger.trace(f'Skipping "{archive_path}". Folder already exists.')
return INCOMPLETE
with zipfile.ZipFile(archive_path, 'r') as compressed_file:
target_dir_abs = os.path.abspath(target_dir)
for member in compressed_file.infolist():
member_path = os.path.abspath(
os.path.join(target_dir_abs, member.filename)
)
if os.path.commonpath((target_dir_abs, member_path)) != target_dir_abs:
raise ValueError(
f'Archive member escapes target directory: "{member.filename}"'
)
compressed_file.extractall(target_dir)
if remove_archive and (not already_exists or overwrite):
os.remove(archive_path)
return SUCCEEDED
except Exception as ex:
logger.error(f'An error occurred while processing "{folder_name}":\n{ex}')
return FAILED
def get_status_from_results(results):
status = SUCCEEDED
for result in results:
if result == FAILED:
return FAILED
if result == INCOMPLETE:
status = INCOMPLETE
return status