-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py2
More file actions
executable file
·270 lines (218 loc) · 6.59 KB
/
run.py2
File metadata and controls
executable file
·270 lines (218 loc) · 6.59 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
#!/usr/bin/env python
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, 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, 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, e:
print(e.__str__())
try:
strptime(dir_name, old_date_format)
result = True
except Exception, 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.6
# 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
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, 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, 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]
junk_files = [path for path in childs_path if not is_archive_dir(path)]
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")
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("")
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']))
else:
# confirm if wpd is not user's desktop
confirmed = True
desktop_dir = expanduser("~") + '/Desktop'
if (os.environ['PWD'] != desktop_dir):
response = ''
while (True):
response = raw_input("You are perform cleaning a directory out side of Desktop, 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"