This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutler.py
331 lines (288 loc) · 9.82 KB
/
butler.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import argparse
import os
import shutil
import sys
from datetime import datetime
from os.path import basename
from typing import Set
from zipfile import ZipFile
# Archive extensions to exclude when archiving
archives_extension = [".zip", ".7z", ".gz", ".bz", ".gzip", ".bzip", ".iso", ".rar"]
def get_args():
"""
Get arguments from CLI
:return:
"""
root_parser = argparse.ArgumentParser(
prog="butler",
description="""The Butler helps keep the castle clean and tidy""",
epilog="""(c) CoolCoderCarl""",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
serving_parser = root_parser.add_subparsers(dest="serving")
clean_parser = serving_parser.add_parser(
"clean",
help="Clean target directory. Example /tmp/, both slash required",
)
clean_parser.add_argument(
"-s",
"--source",
dest="source",
help="Source dir name. Example /tmp/, both slash required",
type=str,
)
clean_parser.add_argument("-f", "--force", action=argparse.BooleanOptionalAction)
# Include only files by mask
# Exclude files by mask
group_up_parser = serving_parser.add_parser(
"group",
help="Group up files in target dir",
)
group_up_parser.add_argument(
"-s",
"--source",
dest="source",
help="Source dir name. Example /tmp/, both slash required",
type=str,
)
group_up_parser.add_argument(
"-t",
"--target",
dest="target",
help="Target dir name. Example ALL",
type=str,
)
archive_parser = serving_parser.add_parser(
"archive",
help="Create archive from target directory. Example /tmp/, both slash required",
)
archive_parser.add_argument(
"-s",
"--source",
dest="source",
help="Source dir name. Example /tmp/, both slash required",
type=str,
)
# Target dir where to save archive
# Exclude files according extensions
combine_parser = serving_parser.add_parser(
"combine",
help="Combine files according extensions",
)
combine_parser.add_argument(
"-s",
"--source",
dest="source",
help="Source dir name. Example /tmp/, both slash required",
type=str,
)
combine_parser.add_argument(
"-t",
"--target",
dest="target",
help="Target dir name. Example ALL",
type=str,
)
combine_parser.add_argument(
"-e",
"--ext",
dest="ext",
help="Target extensions name. Example DOCX",
type=str,
)
return root_parser
# Shortening
namespace = get_args().parse_args(sys.argv[1:])
def get_butler_name() -> str:
"""
Get Butler name from sys.argv to escape it in logic
One option for Windows OS family
Second for Nix based OS
:return:
"""
if "win" in sys.platform:
return sys.argv[0].split("\\")[-1]
else:
return sys.argv[0].split("/")[-1]
def create_target_dir(extensions: set, new_dir_name: str, file_path: str):
"""
Create target directory according to extensions specifications
:param extensions: set of extensions
:param new_dir_name: name of the dir where files will be moved
:param file_path: path of the files which will be moved
:return:
"""
for ext in extensions:
if namespace.source == ".":
new_dir_path = new_dir_name.upper() + ext.upper()
else:
new_dir_path = namespace.source + new_dir_name.upper() + ext.upper()
try:
os.mkdir(new_dir_path)
except OSError:
pass
try:
moving_files(file_path, new_dir_path)
except OSError:
pass
def get_files_extension(path_to_dir: str, special_ext="") -> Set:
"""
Return list of files in target directory. Exit if there is no files
:param path_to_dir: got list of files from dat dir
:param special_ext: extensions which used for combining
:return:
"""
list_dir = os.listdir(path_to_dir)
if len(list_dir) == 0:
exit(0)
files = [file for file in list_dir if os.path.isfile(path_to_dir + file)]
result = []
if len(special_ext) == 0:
for file in files:
result.append("." + file.split(".")[-1])
else:
for file in files:
if special_ext in file.split(".")[-1]:
result.append("." + file.split(".")[-1])
result = set(result)
return result
def get_files_to_combine(path_to_dir: str, special_files_extensions: str) -> Set:
"""
Get files in target directory according their extensions
:param path_to_dir: got list of files from dat dir
:param special_files_extensions: special extensions for files which need to combine
:return:
"""
list_dir = os.listdir(path_to_dir)
if len(list_dir) == 0:
exit(0)
files = [file for file in list_dir if os.path.isfile(path_to_dir + file)]
result = []
for file in files:
ext = file.split(".")[-1]
if ext.lower() == special_files_extensions.lower():
result.append(file)
result = set(result)
return result
# Move & rename if files already exist & notify
def moving_files(move_from: str, move_to: str):
"""
Moved files from source to target
Check is files ext and target directory match
:param move_from: got from file_path in group_up_files func
:param move_to: got from new_dir_path in group_up_files func
:return:
"""
file_ext = move_from.split(".")[-1]
dir_ext = move_to.split(".")[-1]
if file_ext.lower() == dir_ext.lower():
shutil.move(move_from, move_to)
def delete_empty_dir(force: bool, path_to_clean: str):
"""
Remove target directory
:param force: Bool key, if set remove target directory
:param path_to_clean: path to directory which will be cleared
:return:
"""
if force:
try:
os.rmdir(path_to_clean)
except OSError:
pass
def clean_the_dir(path_to_clean: str):
"""
Clean the target directory, but not delete directory itself
:param path_to_clean: path to directory which will be cleared
:return:
"""
if namespace.source == "/":
exit(1)
else:
if len(os.listdir(path_to_clean)) == 0:
delete_empty_dir(namespace.force, namespace.source)
exit(0)
else:
for filename in os.listdir(path_to_clean):
path = os.path.join(path_to_clean, filename)
if get_butler_name().lower() in (path.split("/")[-1]).lower():
pass
else:
try:
shutil.rmtree(path)
except OSError:
os.remove(path)
delete_empty_dir(namespace.force, namespace.source)
def group_up_files(new_dir_name: str):
"""
Group up files in target directory
Create directory for files in target directory with ALL.EXT template according the files extensions
Move all files to relevant directory
:param new_dir_name: name of the dir where files will be moved
:return:
"""
if namespace.source == "/":
exit(1)
else:
extensions = get_files_extension(namespace.source)
for filename in os.listdir(namespace.source):
if get_butler_name().lower() in filename.lower():
pass
elif os.path.isdir(namespace.source + filename):
pass
else:
file_path = os.path.join(namespace.source, filename)
create_target_dir(extensions, new_dir_name, file_path)
def create_archive(dir_to_archive: str):
"""
Archive all files in target directory & add archive near the butler.exe
Ignore files with archive extensions
:param dir_to_archive: path to directory where files will be archived
:return:
"""
now = datetime.now()
date_time = now.strftime("%m.%d.%Y_%H.%M.%S")
if namespace.source == "/":
exit(1)
else:
if len(os.listdir(dir_to_archive)) == 0:
exit(0)
else:
with ZipFile(str(date_time) + ".zip", "w") as zip_obj:
for folder_name, sub_folders, filenames in os.walk(dir_to_archive):
for filename in filenames:
if get_butler_name().lower() in filename.lower():
pass
else:
for a_ext in archives_extension:
if filename.endswith(a_ext):
pass
zip_path = os.path.join(folder_name, filename)
zip_obj.write(zip_path, basename(zip_path))
def combine_the_files(new_dir_name: str):
"""
Combine files in target directory according to their extensions
:param new_dir_name: name of the dir where files will be moved
:return:
"""
if namespace.source == "/":
exit(1)
else:
files_to_combine = get_files_to_combine(namespace.source, namespace.ext)
for filename in files_to_combine:
if get_butler_name().lower() in filename.lower():
pass
elif os.path.isdir(namespace.source + filename):
pass
else:
file_path = os.path.join(namespace.source, filename)
extensions = get_files_extension(namespace.source, namespace.ext)
create_target_dir(extensions, new_dir_name, file_path)
if __name__ == "__main__":
if namespace.serving == "clean":
clean_the_dir(namespace.source)
elif namespace.serving == "group":
group_up_files(namespace.target)
elif namespace.serving == "archive":
create_archive(namespace.source)
elif namespace.serving == "combine":
combine_the_files(namespace.target)