-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstart.py
executable file
·94 lines (83 loc) · 3.18 KB
/
start.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
#!/usr/bin/env python3
# Copyright 2011-2014 orabot Developers
#
# This file is part of orabot, which is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import time
import gzip
import shutil
import orabot
try:
os.mkdir("var") # Create directory for temp files and logs
os.mkdir("db") # Create directory where database is stored
os.chmod("db", 0o700)
except OSError as e:
if e.args[0] == 17: # Directory already exists
pass # Ignore
else:
raise e # Raise exception again
# a class which works like the shell command "tee"
class Tee:
def __init__(self, fd, logfile):
self.fd = fd
self.logfile = logfile
def __del__(self):
self.logfile.close()
def write(self, text):
if (text.strip() != ''):
text = time.strftime('[%Y-%m-%d %H:%M:%S] ') + text
self.fd.write(text)
self.logfile.write(text)
self.log_rotate(text)
def flush(self):
self.fd.flush()
self.logfile.flush()
def log_rotate(self, text):
log_file = open('var/console_log.txt')
log_file_lines = log_file.readlines()
if len(log_file_lines) > 50000:
_files = []
_dir = os.listdir('var/')
for filename in _dir:
if filename[0:11] == 'console_log':
_files.append(filename)
_nums = []
for file in _files:
if file == "console_log.txt":
_nums.append(0)
else:
_nums.append(int(file.split('console_log_')[1].split('.')[0]))
_nums.sort()
_nums.reverse()
for number in _nums:
if number != 0: # rename every existing log file
source = 'var/console_log_'+str(number)+'.txt.gz'
dest = 'var/console_log_'+str(number+1)+'.txt.gz'
shutil.move(source, dest)
else: # gzip last one
shutil.copy('var/console_log.txt', 'var/console_log_1.txt')
file_to_gz = open('var/console_log_1.txt', 'rb')
result_gz_file = gzip.open('var/console_log_1.txt.gz', 'wb')
result_gz_file.writelines(file_to_gz)
result_gz_file.close()
file_to_gz.close()
os.remove("var/console_log_1.txt")
open('var/console_log.txt', 'w').close()
logfile = "var/console_log.txt"
log = open(logfile, "a")
sys.stdout = Tee(sys.stdout, log)
sys.stderr = Tee(sys.stderr, log)
print("Starting bot. Press CTRL+C to exit.")
orabot.main()