-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path003-cleaner.py
executable file
·72 lines (54 loc) · 1.7 KB
/
003-cleaner.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
#!/usr/bin/python
DATA_MAX = 5
MIRROR_MAX = 1
"""
Clean the site/data and site/mirror regularly to release space.
==============================================================
"""
import os
from subprocess import call
import sys
import time
sitePath = os.path.join(os.path.realpath(os.path.dirname(sys.argv[0])), 'site')
dataPath = os.path.join(sitePath, 'data')
mirrorPath = os.path.join(sitePath, 'mirror')
removeList = []
def howOld(timeobj):
nowtime = time.mktime(time.gmtime())
compare = time.mktime(timeobj)
return nowtime - compare
def filterOldFile(path, days):
dataList = os.listdir(path)
outList = []
for filename in dataList:
fileFullname = os.path.join(path, filename)
filetime = False
try:
filetime = time.strptime(filename[0:12], '%Y%m%d%H%M')
except:
pass
if filetime == False:
try:
filetime = time.gmtime(os.path.getmtime(fileFullname))
except:
pass
if filetime:
if howOld(filetime) > 86400 * days:
outList.append(fileFullname)
return outList
if os.path.isdir(dataPath):
dataOldList = filterOldFile(dataPath, DATA_MAX)
removeList += dataOldList
if os.path.isdir(mirrorPath):
mirrorOldList = filterOldFile(mirrorPath, MIRROR_MAX)
removeList += mirrorOldList
doRemove = False
if len(sys.argv) >= 2:
doRemove = (sys.argv[1] == 'REMOVE')
if not doRemove:
print "This is only a test. Use `./003-cleaner.py REMOVE` to fire actual removing actions."
removeCommands = [['rm', '-rf', i] for i in removeList]
for command in removeCommands:
print ' '.join(command)
if doRemove:
call(command)