-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentArranger.py
386 lines (308 loc) · 12.2 KB
/
contentArranger.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import magic
import re
import pickle
from multiprocessing import Process
import sys
import os
import shutil
import datetime
UserDefinedTypes, DefaultTypes = os.path.expanduser('~/Documents/ContentArranger/UserDefinedTypes'), os.path.expanduser('~/Documents/ContentArranger/DefaultTypes')
class fileOpenError(IOError):
def __init__(self, fileName):
super(fileOpenError, self).__init__("Error while opening " + fileName)
class movingFileError(Exception):
def __init__(self):
super(movingFileError, self).__init__('Error while moving files')
class doesntExistError(Exception):
def __init__(self, path):
super(doesntExistError, self).__init__(path, "doesn't exists .")
def scanFiles(folderName, defaultT=True, commandLine=False):
'''function scans files in folder and return list of processes to be done...'''
try:
f = open(UserDefinedTypes, 'rb')
except IOError:
if commandLine:
print('could not open UserDefinedTypes .')
sys.exit(1)
raise fileOpenError('UserDefinedTypes')
userList = pickle.load(f)
f.close()
try:
f = open(DefaultTypes, 'rb')
except IOError:
if commandLine:
print('could not open DefaultTypes .')
sys.exit(1)
raise fileOpenError('DefaultTypes')
defaultList = pickle.load(f)
f.close()
processList = []
for file in os.listdir(folderName):
if file[0] == '.':
continue
path = os.path.join(folderName, file)
if not os.path.isfile(path) or os.path.isdir(path): continue
try:
type = magic.from_file(path)
except:
print('There was a problem reading', file, file=sys.stderr)
continue
usrDef = False
for tup in userList:
if tup and type in tup[1]:
usrDef = True
if tup[0] != '-1':
processList.append((path, os.path.join(folderName, tup[0])))
break
if not usrDef and defaultT:
found = False
for tup in defaultList:
for exp in tup[1]:
if re.match(exp, type):
found = True
processList.append((path, os.path.join(folderName, tup[0])))
break
if found: break
if not found:
processList.append((path, os.path.join(folderName, 'Others')))
return processList
def arrange(folderName, defaultT=True, flag=False, processNum=10, commandLine=False):
'''function manages the arrage...'''
if not os.path.exists(folderName):
if commandLine:
print(folderName, 'does not exists .')
sys.exit(1)
raise doesntExistError(folderName)
if not os.path.isdir(folderName):
if commandLine:
print(folderName, 'is not a directory .')
sys.exit(1)
raise Exception(folderName, 'is not a directory .')
processList = scanFiles(folderName, defaultT, commandLine)
if flag:
try:
print(datetime.date.today())
logFile = open(os.path.join(folderName, str(datetime.date.today()) + '.txt'), 'w')
print(os.path.abspath(folderName), file=logFile)
except IOError:
if commandLine:
print('could not create logfile .')
sys.exit(1)
raise Exception('could not create logfile .')
for pr in processList:
if not os.path.exists(pr[1]):
os.makedirs(pr[1])
if flag: print('making', os.path.basename(pr[1]), file=logFile)
doing = []
for i in processList[:processNum]:
if not os.path.exists(os.path.join(i[1], os.path.basename(i[0]))):
doing.append(Process(target=shutil.move, args=(i)))
doing[-1].start()
if flag: print('moving', os.path.basename(i[0]), 'to', os.path.basename(i[1]),file=logFile)
else:
print('problem moving', os.path.basename(i[0]),'. file with this name already exists in', os.path.basename(i[1]), '.', file=sys.stderr)
i = processNum
try:
while doing:
doing[0].join()
del doing[0]
if len(processList) > i:
if not os.path.exists(os.path.join(processList[i][1], os.path.basename(processList[i][0]))):
doing.append(Process(target=shutil.move, args=processList[i]))
doing[-1].start()
if flag: print('moving', os.path.basename(processList[i][0]), 'to', os.path.basename(processList[i][1]), file=logFile)
else:
print('problem moving', os.path.basename(processList[i][0]),'. file with this name already exists in', os.path.basename(processList[i][1]), '.', file=sys.stderr)
i += 1
except:
raise movingFileError()
return True
def removeType(sampleFile, folderName, commandLine=False):
'''function that removes user defined types...'''
if not os.path.exists(sampleFile):
if commandLine:
print('problem reading', sampleFile, '.')
sys.exit(1)
raise doesntExistError(sampleFile)
type = type = magic.from_file(sampleFile)
try:
f = open(UserDefinedTypes, 'rb')
except IOError:
if commandLine:
print('could not open UserDefinedTypes .')
sys.exit(1)
else:
raise fileOpenError('UserDefinedTypes')
list = pickle.load(f)
f.close()
found = False
for i in range(len(list)):
if list[i] and list[i][0] == folderName:
found = True
if not type in list[i][1]:
if commandLine:
print('There is no such type in', folderName, ".")
sys.exit(1)
raise Exception("Type doesn't exits .")
list[i][1].remove(type)
break
if not found:
if commandLine:
print(folderName, 'does not exist.')
sys.exit(1)
raise Exception(folderName, "doesn't exists .")
try:
f = open(UserDefinedTypes, 'wb')
except IOError:
raise fileOpenError('UserDefinedTypes')
pickle._Pickler(f, 2).dump(list)
f.close()
if commandLine:
print(type, 'removed from', folderName, '.')
sys.exit(0)
else: return True
def addType(sampleFile, folderName, commandLine=False):
'''function that let the user define specific types...'''
if not os.path.exists(sampleFile):
if commandLine:
print('problem reading', sampleFile, '.')
sys.exit(1)
raise doesntExistError(sampleFile)
type = magic.from_file(sampleFile)
try:
f = open(UserDefinedTypes, 'rb')
except IOError:
if commandLine:
print('could not open', 'UserDefinedTypes', '.')
sys.exit(1)
raise fileOpenError('UserDefinedTypes')
list = pickle.load(f)
f.close()
folderInd = -1
for i in range(len(list)):
if list[i] and type in list[i][1]:
if commandLine:
if folderName != list[i][0]:
print('This type already exists in', list[i][0],
'folder, first try to remove it with "... --remove SampleFile FolderName" in order to add it to', folderName, file=sys.stderr)
else: print('This type already exists in this folder.', file=sys.stderr)
sys.exit(1)
else:
raise Exception('Type already exists .')
if list[i] and list[i][0] == folderName: folderInd = i
if folderInd != -1: list[i][1].append(type)
else: list.append((folderName, [type,]))
try:
f = open(UserDefinedTypes, 'wb')
except IOError:
raise fileOpenError('UserDefinedTypes')
pickle._Pickler(f, 2).dump(list)
f.close()
if commandLine:
print(type, 'added to', folderName)
sys.exit(0)
return True
def reset(commandLine=False):
'''function that resets the UserDefined file to hold nothing...'''
if not os.path.exists(UserDefinedTypes):
if commandLine:
print('could not open', 'UserDefinedTypes .')
sys.exit(1)
raise doesntExistError('UserDefinedTypes')
try:
f = open(UserDefinedTypes, 'wb')
except IOError:
raise fileOpenError('UserDefinedTypes')
l = [()]
pickle._Pickler(f, 2).dump(l)
f.close()
if commandLine:
print('No user defined types anymore .')
sys.exit(0)
return True
def recover(logFile, processNum=10, commandLine=False):
'''function that recovers a folder base on the logfile ...'''
try:
f = open(logFile, 'r')
except:
if commandLine:
print('problem reading logfile .')
sys.exit(1)
raise fileOpenError(logFile)
foldersToDelete, filesToMove, list = [], [], [i for i in f.read().split('\n')]
f.close()
path = list[0]
if not os.path.exists(path):
if commandLine:
print(path, 'does not exists .')
sys.exit(1)
else:
doesntExistError(path)
del list[0]
if not os.path.exists(path):
if commandLine:
print('path specified in log file does not exists')
sys.exit(1)
raise doesntExistError(path)
for log in list:
if not log: continue
tmpL = log.split()
if tmpL[0] == 'making':
if os.path.exists(os.path.join(path, " ".join(tmpL[1:]))):
foldersToDelete.append(" ".join(tmpL[1:]))
else:
if os.path.exists((os.path.join(os.path.join(path, " ".join(tmpL[3:])), tmpL[1]))):
filesToMove.append((os.path.join(os.path.join(path, " ".join(tmpL[3:])), tmpL[1]), path))
procs = [Process(target=shutil.move, args=i) for i in filesToMove[:processNum] if (not os.path.exists(os.path.join(path, os.path.basename(i[0]))))]
i = processNum
for process in procs: process.start()
while procs or i < len(filesToMove):
if procs:
procs[0].join()
del [procs[0]]
if i < len(filesToMove):
k = filesToMove[i]
if not os.path.exists(os.path.join(path, os.path.basename(k[0]))) and os.path.exists(k[0]):
procs.append(Process(target=shutil.move, args=k))
procs[-1].start()
i += 1
for folder in foldersToDelete:
if len(os.listdir(os.path.join(path, folder))) == 0: os.removedirs(os.path.join(path, folder))
try: os.remove(logFile)
except:
if commandLine:
print('problem removing logfile .')
sys.exit(1)
return False
if commandLine:
print(path, 'recovered successfully .')
sys.exit(0)
return True
def help(commandLine=False):
try:
f = open(os.path.expanduser('~/Documents/ContentArranger/help'), 'r')
if(commandLine): print(f.read())
else: return f.read()
except:
print('problem opening help file .')
def main():
del sys.argv[0]
if len(sys.argv) < 1 or (len(sys.argv) > 3 and (sys.argv[0] != '--add' and sys.argv[0] != '--remove')) or (
len(sys.argv) == 2 and (sys.argv[0] != '--log' and sys.argv[0] != '--recover' and sys.argv[0] != '--off')) or (
len(sys.argv) == 3 and ((sys.argv[0] != '--add' and sys.argv[0] != '--remove') and (sys.argv[0] != '--off' or sys.argv[1] != '--log'))):
print("Wrong Usage, Type 'command --help' to see available options .")
sys.exit(0)
#in case of adding new type
if sys.argv[0] == '--add': addType(sys.argv[1], " ".join(sys.argv[2:]), True)
elif sys.argv[0] == '--remove': removeType(sys.argv[1], " ".join(sys.argv[2:]), True)
elif len(sys.argv) == 1:
if(sys.argv[0] == '--reset'): reset(True)
elif(sys.argv[0] == '--help'): help(True)
else: arrange(sys.argv[0], True, False, 10, True)
elif len(sys.argv) == 2:
if sys.argv[0] == '--log': arrange(sys.argv[1], True, True, 10, True)
elif sys.argv[0] == '--off': arrange(sys.argv[1], False, False, 10, True)
elif sys.argv[0] == '--recover': recover(sys.argv[1], 10, True)
elif len(sys.argv) == 3:
arrange(sys.argv[2], False, True, 10, True)
if __name__ == '__main__': main()