forked from beyond-all-reason/spads_config_bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clear_logs_and_replays.py
54 lines (45 loc) · 1.45 KB
/
clear_logs_and_replays.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
#!/bin/python
import os
import sys
import argparse
import codecs
if __file__:
scriptdir = os.path.dirname(os.path.realpath(__file__))
if scriptdir != os.getcwd():
print("Changing working directory to the scripts path:", os.path.dirname(os.path.realpath(__file__)))
os.chdir(scriptdir)
os.chdir('../var/ClusterManager')
logstotruncate = [
'log/spring-dedicated.log',
'log/spads.log',
'log/spads.log',
'log/chat/game.log',
'log/chat/battle.log',
'log/chat/pv_AutohostMonitor.log',
]
def truncate_logfile(fname, keepsize = 100000):
full_log = ""
with codecs.open(fname, 'r', encoding='utf-8',
errors='ignore') as fdata:
full_log = fdata.read()
fdata.close()
trunc = full_log[-keepsize:]
print(fname, len(full_log)/1000000.0,'MB', full_log[-50:])
fp = open(fname,'w')
fp.write(trunc)
fp.close()
return
for hostdir in os.listdir(os.getcwd()):
if os.path.isdir(hostdir):
print (hostdir)
for logtotruncate in logstotruncate:
if os.path.isfile(os.path.join(hostdir, logtotruncate)):
#print (logtotruncate)
try:
truncate_logfile(os.path.join(hostdir, logtotruncate))
except:
print ("Failed to clean log file", hostdir, logtotruncate)
if os.path.isdir(os.path.join(hostdir,'demos-server')):
cmd_delete_older_than_month = 'find %s/demos-server -type f -mtime +30 -delete | wc -l' %(hostdir)
print ("Deleting Replays older than a month", cmd_delete_older_than_month)
os.system(cmd_delete_older_than_month)