forked from Lengsir/cos4_rm_recursive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrm_recursive.py
446 lines (418 loc) · 14.5 KB
/
rm_recursive.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import os
import signal
import json
import string
import urllib
import datetime
import random
import threading
import time
import logging
import datetime
from time import strftime, localtime
from datetime import date
from datetime import timedelta
from lib import threadpool
from lib import CosClient
from lib import CosConfig
from lib import DelFileRequest
from lib import DelFolderRequest
from lib import ListFolderRequest
from time import sleep
from logging.handlers import RotatingFileHandler
MAX_RETRY_TIMES = 3
LOG_SAVE_EVERY_NUM = 1024
ONE_TASK_DEL_FILE_NUMS = 50
CONFIG_FILE_PATH = "./conf/config.json"
BUCKET_PATH_LIST_PATH = "./conf/bucketlist.txt"
HAS_FORK = hasattr(os, 'fork')
def loginit():
global config
if (config.log_file_name == ""):
return
log_level = logging.ERROR
if config.log_level == 0:
log_level = logging.DEBUG
if config.log_level == 1:
log_level = logging.INFO
if config.log_level == 2:
log_level = logging.WARNING
#定义一个RotatingFileHandler,最多备份5个日志文件,每个日志文件最大20M
logger = logging.getLogger("")
Rthandler = RotatingFileHandler(config.log_file_name, maxBytes= 20*1024*1024,backupCount=5)
Rthandler.setLevel(log_level)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
Rthandler.setFormatter(formatter)
logger.addHandler(Rthandler)
#输出日志到屏幕
console = logging.StreamHandler()
console.setFormatter(formatter)
if (config.log_out_to_screen == 1):
logger.addHandler(console)
logger.setLevel(log_level)
return logger
#日期相关操作
class Dateop():
@staticmethod
def isValidDate(str):
try:
time.strptime(str, "%Y""%m""%d")
return True
except:
return False
@staticmethod
def getdaystr(n=0):
dt = date.today()-timedelta(days=n)
tt = dt.timetuple()
daystr = strftime("%Y""%m""%d",tt)
return daystr
@staticmethod
def cmpDateAgo(t1,t2):
if (Dateop.isValidDate(t1)==False or Dateop.isValidDate(t2)==False):
return False
if (int(t1) <= int (t2)):
return True
return False
@staticmethod
def isNeedDeleteDir(dirname, n=0):
if (len(dirname) != 8):
return False
if Dateop.isValidDate(dirname) == False:
return False
d2 = Dateop.getdaystr(n);
if Dateop.cmpDateAgo(dirname, d2):
return True
return False
#删除文件统计
class FileStat():
global cos_log
def __init__(self):
self.delfilesuccnum = 0
self.deldirsuccnum = 0
self.delfilefailnum = 0
self.deldirfailnum = 0
self.lock = threading.Lock()
def addDelFileFailNum(self,num=1):
self.lock.acquire(1)
self.delfilefailnum += num
self.lock.release()
def addDelDirFailNum(self,num=1):
self.lock.acquire(1)
self.deldirfailnum += num
self.lock.release()
def addDelDirSuccNum(self, num=1):
self.lock.acquire(1)
self.deldirsuccnum += num
self.lock.release()
def addDelFileSuccNum(self, num=1):
self.lock.acquire(1)
self.delfilesuccnum += num
self.lock.release()
def printStat(self):
msg ="".join(["delfilesuccnum=",str(self.delfilesuccnum),
",delfilefailnum=",str(self.delfilefailnum),
",deldirsuccnum=",str(self.deldirsuccnum),
",deldirfailnum=",str(self.deldirfailnum)])
print(msg)
def logStat(self):
curtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
log = ''.join(["delfilenum=",str(self.delfilesuccnum),
",deldirnum=",str(self.deldirsuccnum),",delfilefailnum=",
str(self.delfilefailnum),",deldirfailnum=",str(self.deldirfailnum)])
cos_log.info(log)
#执行时间统计
class TimeStat(object):
global cos_log
def __init__(self):
self.start()
def start(self):
self.start = datetime.datetime.now()
self.t1 = time.time()
msg = "delete task started ..........."
cos_log.info(msg)
def end(self):
self.end = datetime.datetime.now()
self.t2 = time.time()
msg = "delete task ended\n\nrm task finished,\ntimecost:"+str(self.t2-self.t1) + " (s)"
cos_log.info(msg)
#删除文件列表中的文件
def delfiles(cos_client, bucket, filelist):
for f in filelist:
delfile(cos_client, bucket, f)
def delfolders(cos_client, bucket, folderlist):
for f in folderlist:
delfolder(cos_client, bucket, f)
#文件夹删除
def delfolder(cos_client, bucket, folder):
global stat
global cos_log
if not folder:
return 0
delfolderreq = DelFolderRequest(bucket, folder)
retry = 0
while (retry < MAX_RETRY_TIMES):
ret = cos_client.del_folder(delfolderreq)
#msg = "delfolder fail, bucket="+bucket+",folder="+folder+ret['message']
msg = "delfolder fail, bucket="+bucket+",folder="+folder+str(ret.get('message'))
if (ret['code'] == 0):
break
elif (ret['code'] == -166):
cos_log.warning(msg)
break
#操作太频繁,频控
elif (ret['code'] == -71):
sleep(random.randint(1,5))
cos_log.warning(msg)
retry += 1
continue
#文件夹非空
elif (ret['code'] == -173):
break
else:
cos_log.warning(msg)
retry += 1
if (ret['code'] != 0 and ret['code'] != -166):
stat.addDelDirFailNum()
#cos_log.error("delfolder fail, bucket="+bucket+",folder="+folder+ret['message'])
cos_log.error("delfolder fail, bucket="+bucket+",folder="+folder+str(ret.get('message')))
return ret['code']
if (ret['code'] == 0):
stat.addDelDirSuccNum()
msg = "delfolder success, bucket="+bucket+",folder="+folder
cos_log.info(msg)
return 0
#文件删除
def delfile(cos_client, bucket, filepath):
global stat
global cos_log
delfilereq = DelFileRequest(bucket, filepath)
retry = 0
while (retry < MAX_RETRY_TIMES):
ret = cos_client.del_file(delfilereq)
#msg = "delfile fail bucket="+bucket+",file="+filepath+ret['message']
msg = "delfile fail bucket="+bucket+",file="+filepath+str(ret.get('message'))
if (ret['code'] == 0):
break
#文件不存在
elif (ret['code'] == -166):
cos_log.warning(msg)
break
#单目录写操作过快
elif (ret['code'] == -143):
sleep(random.randint(1,5))
cos_log.warning(msg)
retry += 1
continue
#操作太频繁,频控
elif (ret['code'] == -71):
sleep(random.randint(1,5))
cos_log.warning(msg)
retry += 1
continue
else:
cos_log.warning(msg)
retry += 1
continue
if (ret['code'] != 0 and ret['code'] != -166):
stat.addDelFileFailNum()
#cos_log.error("delfile fail, bucket="+bucket+",file="+filepath+ret['message'])
cos_log.error("delfile fail, bucket="+bucket+",file="+filepath+str(ret.get('message')))
return ret['code']
if (ret['code'] == 0):
stat.addDelFileSuccNum()
msg = "delfile success, bucket="+bucket+",file="+filepath
cos_log.info(msg)
return 0
#递归文件夹进行文件删除
def delete_r(cos_client, bucket, path, thread_pool_file):
global stat
global config
global cos_log
cos_log.debug("delete_r bucket:"+bucket+",path:"+path)
context = u""
#递归文件夹
while True:
listfolderreq = ListFolderRequest(bucket, path, 1000, u'', context)
retry = 0
while (retry < MAX_RETRY_TIMES):
listret = cos_client.list_folder(listfolderreq)
if listret['code'] != 0 :
retry += 1
sleep(random.randint(1,3))
continue
else:
break
if (listret['code'] != 0):
#cos_log.error("delete_r: list folder fail:"+path +",return msg:"+ listret['message'])
cos_log.error("delete_r: list folder fail:"+path +",return msg:"+ str(listret.get('message')))
return listret['code']
if (len(listret['data']['infos']) == 0):
break;
filelist = []
dirlist = []
for info in listret['data']['infos']:
fullname = path + info['name']
#list出来的文件列表中文件夹和文件本身是混杂一起的
if info.has_key('filesize'):
filelist.append(fullname)
if (len(filelist) >= config.one_task_del_file_num):
args = [cos_client, bucket, filelist]
args_tuple = (args,None)
args_list = [args_tuple]
requests = threadpool.makeRequests(delfiles, args_list)
for req in requests:
thread_pool_file.putRequest(req)
filelist = []
continue
else:
pass
else:
dirlist.append(fullname)
if (len(dirlist) >= config.one_task_del_file_num):
args = [cos_client, bucket, dirlist]
args_tuple = (args,None)
args_list = [args_tuple]
requests = threadpool.makeRequests(delfolders, args_list)
for req in requests:
thread_pool_file.putRequest(req)
dirlist = []
continue
else:
pass
pass
if (len(filelist) > 0):
args = [cos_client, bucket, filelist]
args_tuple = (args,None)
args_list = [args_tuple]
requests = threadpool.makeRequests(delfiles, args_list)
for req in requests:
thread_pool_file.putRequest(req)
filelist = []
else:
pass
if (len(dirlist) > 0):
args = [cos_client, bucket, dirlist]
args_tuple = (args,None)
args_list = [args_tuple]
requests = threadpool.makeRequests(delfolders, args_list)
for req in requests:
thread_pool_file.putRequest(req)
filelist = []
else:
pass
cos_log.debug("delete_r thread pool file waiting\n")
thread_pool_file.wait()
cos_log.debug("delete_r thread pool file waiting end\n")
if (listret['data']['listover'] == False):
context = listret['data']['context']
continue
else:
break
stat.logStat()
return 0
#解析配置文件
class Config:
def __init__(self, configfile):
fp = open(configfile)
try:
config = json.loads(fp.read())
finally:
fp.close()
self.appid = config['appid']
self.secret_id = config['secret_id'].decode("utf8")
self.secret_key = config['secret_key'].decode("utf8")
self.region = config['region'].decode("utf8")
self.log_level = config['log_level']
self.log_file_name = config['log_file_name']
self.dir_thread_num = config['dir_thread_num']
self.file_thread_num = config['file_thread_num']
self.one_task_del_file_num = ONE_TASK_DEL_FILE_NUMS
self.log_out_to_screen = config['log_out_to_screen']
#解析待删除的bucket和文件夹
class BucketDirList:
def __init__(self, fname):
fp = open(fname)
try:
self.bucketDirList = fp.readlines()
finally:
fp.close()
def getBucketName(self,bp):
bucket_dir = bp.strip().split(',')
if (len(bucket_dir) != 2):
return ""
bucket = bucket_dir[0].strip().decode("utf8")
return bucket
def getPath(self,bp):
bucket_dir = bp.strip().split(',')
if (len(bucket_dir) != 2):
return ""
path = bucket_dir[1].strip().decode("utf8")
return path
#支持Ctrl+C终止程序
class Watcher():
def __init__(self):
self.child = os.fork()
if self.child == 0:
return
else:
self.watch()
def watch(self):
global cos_log
try:
os.wait()
except KeyboardInterrupt:
cos_log.ERROR("ctrl+c terminated rm_recursive.py, exiting...")
self.kill()
sys.exit()
def kill(self):
try:
os.kill(self.child, signal.SIGKILL)
except OSError:
pass
def main():
global thread_pool
global config
global cos_log
bucketdirList = BucketDirList(BUCKET_PATH_LIST_PATH)
cosconfig = CosConfig(300,300,False,region=config.region)
cos_client = CosClient(config.appid, config.secret_id, config.secret_key, config.region)
cos_client.set_config(cosconfig)
thread_pool_dir = threadpool.ThreadPool(config.dir_thread_num)
for var in bucketdirList.bucketDirList:
cos_log.debug(var)
bucket = bucketdirList.getBucketName(var)
path = bucketdirList.getPath(var)
if (bucket == "" or path == ""):
cos_log.error("config is invalid at line %s, please check it and try again!" % var)
continue
thread_pool_file = threadpool.ThreadPool(config.file_thread_num)
cos_log.debug("bucket:"+bucket +",path:"+path)
cos_log.debug('please commit the target to be deleted. bucket: %s, path: %s (yes/no)' % (bucket, path))
answer = sys.stdin.readline()
if answer.lower().strip() != "yes":
cos_log.debug('answer is not yes. quit')
return;
args = [cos_client,bucket, path, thread_pool_file]
args_tuple = (args, None)
args_list = [args_tuple]
requests = threadpool.makeRequests(delete_r, args_list)
for req in requests:
thread_pool_dir.putRequest(req)
cos_log.debug("thread_pool_dir waiting.....\n")
thread_pool_dir.wait()
thread_pool_dir.dismissWorkers(config.dir_thread_num, True)
cos_log.debug("thread_pool_dir wait end.....\n")
if __name__ == '__main__':
global cos_log
config = Config(CONFIG_FILE_PATH)
cos_log = loginit()
stat = FileStat()
timestat = TimeStat()
if HAS_FORK:
Watcher()
main()
timestat.end()
stat.logStat()