-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·349 lines (285 loc) · 9.37 KB
/
run.py
File metadata and controls
executable file
·349 lines (285 loc) · 9.37 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python3
from time import strptime
import datetime
import shutil
import sys
__author__ = 'jaredchu'
import os
from os.path import expanduser
import random
import help
old_date_format = '%d-%m-%Y'
date_format = '%Y-%m-%d'
def random_file(dir_path):
if (os.path.isdir(dir_path)):
for i in range(0, 100):
new_file_path = dir_path + random.randint(0, 9999).__str__() + '.txt'
if not os.path.isfile(new_file_path):
new_file = open(new_file_path, 'w')
new_file.write('')
new_file.close()
def random_dir(dir_path):
if (os.path.isdir(dir_path)):
for i in range(0, 10):
new_dir_path = dir_path + random.randint(0, 9999).__str__()
if not os.path.isdir(new_dir_path):
os.mkdir(new_dir_path)
def is_archive_dir(path):
result = False
dir_name = path
try:
dir_name = path.split('/')[-1]
except Exception as e:
print((e.__str__()))
# remove conflict with old version
result = is_old_archive_dir(path)
try:
strptime(dir_name, date_format)
result = True
except Exception as e:
# print('This dir is not archive dir' + e.__str__())
pass
return result
def is_old_archive_dir(path):
result = False
dir_name = path
try:
dir_name = path.split('/')[-1]
except Exception as e:
print((e.__str__()))
try:
strptime(dir_name, old_date_format)
result = True
except Exception as e:
# print('This dir is not archive dir' + e.__str__())
pass
return result
def get_name(path):
return path.split('/')[-1]
def with_slash(string):
if (string[-1] == '/'):
return string
else:
return string + '/'
# mem_var
dir_count = 0
file_count = 0
# program var
version = 1.7
# ARGS
file_only = False
folder_only = False
all_type = True
if (sys.argv.__len__() > 1):
args = sys.argv[1]
if ('-' in args):
if ('f' in args):
file_only = True
folder_only = False
all_type = False
elif ('d' in args):
file_only = False
folder_only = True
all_type = False
show_version = False
for arg in sys.argv:
if (arg == '--version'):
show_version = True
is_test = False
for arg in sys.argv:
if (arg == '--test'):
is_test = True
is_help = False
for arg in sys.argv:
if (arg == '--help'):
is_help = True
is_archive = False
for arg in sys.argv:
if (arg == '--archive'):
is_archive = True
set_target = False
target_dir = ''
for index, arg in enumerate(sys.argv):
if (arg == '--target'):
set_target = True
try:
target_dir = sys.argv[index + 1]
except Exception as e:
pass
filter_by_text = False
filter_text = ""
for index, arg in enumerate(sys.argv):
if (arg == '--contain'):
filter_by_text = True
try:
filter_text = sys.argv[index + 1]
except Exception as e:
pass
is_upgrade_folder_name_format = False
for arg in sys.argv:
if (arg == '--upgrade'):
is_upgrade_folder_name_format = True
# Test
def test(working_dir_path):
random_dir(working_dir_path)
random_file(working_dir_path)
# Upgrade folder name format
# todo: this function is not completed
def upgrade(working_dir_path):
return
# childs = os.listdir(working_dir_path)
# childs_path = [working_dir_path + x for x in childs]
# for child_path in childs_path:
# if is_old_archive_dir(child_path):
# new_child_path = working_dir_path + datetime.datetime.now().strftime(date_format)
# print("Rename " + child_path + " to " + with_slash(new_child_path))
# Main progarm
def main(working_dir_path):
print("")
print("-- Cleanup started --")
print("")
childs = os.listdir(working_dir_path)
childs_path = [working_dir_path + x for x in childs]
# Filter out Archive folder and non-date folders
junk_files = [path for path in childs_path if not is_archive_dir(path) and get_name(path) != "Archive"]
new_archive_dir_name = datetime.datetime.now().strftime(date_format)
new_archive_dir_path = working_dir_path + new_archive_dir_name
if not junk_files:
print(("Working directory is empty " + working_dir_path))
return
else:
if not os.path.isdir(new_archive_dir_path):
print(("Create new directory " + new_archive_dir_path))
os.mkdir(new_archive_dir_path)
else:
print(("Directory " + new_archive_dir_path + " is exits"))
# Add timestamp to make unique directory name
timestamp = datetime.datetime.now().strftime("%H%M%S")
new_archive_dir_name = f"{new_archive_dir_name}_{timestamp}"
new_archive_dir_path = working_dir_path + new_archive_dir_name
print(f"Creating new directory {new_archive_dir_path}")
os.mkdir(new_archive_dir_path)
def move_junk(src):
global dir_count
global file_count
do_move = False
# Move command
if (filter_by_text):
if (len(filter_text) > 0 and filter_text in get_name(src)):
do_move = True
else:
do_move = True
if (do_move):
shutil.move(src, new_archive_dir_path)
print(("Move " + src + " to " + with_slash(new_archive_dir_path)))
if (os.path.isdir(src)):
dir_count += 1
if (os.path.isfile(src)):
file_count += 1
for junk in junk_files:
if (file_only == True):
if (os.path.isfile(junk)):
move_junk(junk)
elif (folder_only == True):
if (os.path.isdir(junk)):
move_junk(junk)
elif (all_type == True):
move_junk(junk)
# print("")
# print("Complete, moved " + dir_count.__str__() + " folder and " + file_count.__str__() + " file")
print("")
print("-- All done! --")
print("")
def archive_date_folders(working_dir_path):
print("")
print("-- Archiving date folders started --")
print("")
# Create Archive folder if it doesn't exist
archive_dir = working_dir_path + "Archive"
if not os.path.isdir(archive_dir):
print(f"Creating Archive directory at {archive_dir}")
os.mkdir(archive_dir)
# Get all items in the working directory
childs = os.listdir(working_dir_path)
childs_path = [working_dir_path + x for x in childs]
# Filter only date-formatted folders
date_folders = [path for path in childs_path if is_archive_dir(path) and os.path.isdir(path)]
if not date_folders:
print("No date-formatted folders found to archive")
return
# Move each date folder to Archive
for folder in date_folders:
folder_name = get_name(folder)
target_path = archive_dir + "/" + folder_name
print(f"Moving {folder} to {target_path}")
shutil.move(folder, target_path)
print("")
print("-- Date folders archived successfully! --")
print("")
if (show_version):
print(("Current version is " + version.__str__()))
elif (is_test):
test(with_slash(os.environ['PWD']))
elif (is_help):
help.get_help()
elif (is_upgrade_folder_name_format):
upgrade(with_slash(os.environ['PWD']))
elif (is_archive):
# confirm if wpd is not user's desktop or temp
confirmed = True
desktop_dir = expanduser("~") + '/Desktop'
temp_dir = expanduser("~") + '/Temp'
if (os.environ['PWD'] != desktop_dir and os.environ['PWD'] != temp_dir):
response = ''
while (True):
response = input("You are performing archiving in a directory outside of Desktop or Temp, are you sure [y/n]: ").lower()
if (response == 'y' or response == 'n'):
break
else:
print("Please enter y or n")
if (response == 'y'):
confirmed = True
else:
confirmed = False
if (confirmed):
if (set_target == True):
wdp = ''
if ('/' == target_dir[0] and os.path.isdir(target_dir)):
wpd = target_dir
else:
wpd = with_slash(os.environ['PWD']) + target_dir
archive_date_folders(wpd)
else:
archive_date_folders(with_slash(os.environ['PWD']))
else:
print("Canceled")
else:
# confirm if wpd is not user's desktop or temp
confirmed = True
desktop_dir = expanduser("~") + '/Desktop'
temp_dir = expanduser("~") + '/Temp'
if (os.environ['PWD'] != desktop_dir and os.environ['PWD'] != temp_dir):
response = ''
while (True):
response = input("You are perform cleaning a directory outside of Desktop or Temp, are you sure [y/n]: ").lower()
if (response == 'y' or response == 'n'):
break
else:
print("Please enter y or n")
if (response == 'y'):
confirmed = True
else:
confirmed = False
if (confirmed):
if (set_target == True):
wdp = ''
if ('/' == target_dir[0] and os.path.isdir(target_dir)):
wpd = target_dir
else:
wpd = with_slash(os.environ['PWD']) + target_dir
main(wpd)
else:
main(with_slash(os.environ['PWD']))
pass
else:
print("Canceled")