blob: 9286889f9ee8219869e3acb32eebe32cc64c1fe4 (
plain)
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
|
#!/usr/bin/python
import os
import shutil
import sys
import time
from retrace import *
if __name__ == "__main__":
now = int(time.time())
logfile = "%s/cleanup.log" % CONFIG["LogDir"]
try:
log = open(logfile, "a")
log.write(time.strftime("[%Y-%m-%d %H:%M:%S] Running cleanup\n"))
files = os.listdir(CONFIG["WorkDir"])
except IOError, ex:
print "Error opening log file: %s" % ex
sys.exit(1)
except OSError, ex:
log.write("Unable to list work directory: %s" % ex)
sys.exit(2)
for filename in files:
filepath = "%s/%s" % (CONFIG["WorkDir"], filename)
if os.path.isdir(filepath):
try:
if (now - os.path.getctime(filepath)) / 3600 >= CONFIG["DeleteTaskAfter"]:
log.write("Deleting directory '%s'\n" % filepath)
shutil.rmtree(filepath)
except OSError, ex:
log.write("Error deleting directory: %s\n" % (filepath, ex))
except IOError, ex:
print "Unable to write to log file: %s" % ex
sys.exit(3)
try:
log.close()
except IOError, ex:
print "Error closing log file: %s" % ex
sys.exit(4)
|