forked from jawsper/modularirc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__main__.py
executable file
·71 lines (64 loc) · 2.07 KB
/
__main__.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
#!/usr/bin/env python3
import os
import sys
import logging
import select
import time
from imp import reload
import Bot
pid_file = os.path.join(os.path.dirname(__file__), 'ircbot.pid')
logging_file = os.path.join(os.path.dirname(__file__), 'ircbot.log')
logging_level = logging.INFO
logging_format = '[%(asctime)s] %(levelname)s: %(message)s'
def main(argv):
if os.path.exists(pid_file):
print('PID file exists! If the bot is not running, please delete this file before trying to start again!')
sys.exit(1)
fork = False
if len(argv) > 0:
if argv[0] == '-fork':
fork = True
if fork:
logging.basicConfig(filename=logging_file, level=logging_level, format=logging_format)
else:
logging.basicConfig(level=logging_level, format=logging_format)
logging.info('Welcome to botje')
if fork:
try:
pid = os.fork()
if pid > 0:
logging.info('Forked into PID {0}'.format(pid))
with open(pid_file, 'w') as f:
f.write(str(pid))
print(pid)
return 0
except OSError as error:
logging.error('Unable to fork. Error: {0} ({1})'.format(error.errno, error.strerror))
return 1
botje = Bot.Bot()
while True:
try:
logging.info('Starting bot...')
botje.start()
except Bot.BotRestartException:
continue
except Bot.BotReloadException:
logging.info('Force reloading Bot class')
botje = None
reload(Bot)
botje = Bot.Bot()
botje.modules.reload_modules()
continue
except select.error as e:
logging.exception('select.error')
continue
except (KeyboardInterrupt, Bot.BotExitException):
botje.die()
break
logging.warning('Botje died, restarting in 5...')
time.sleep(5)
logging.info('Exiting bot...')
if fork:
os.remove(pid_file)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))