2020-01-05 20:29:41 +01:00
|
|
|
#!/usr/bin/env python3
|
2005-02-03 15:55:31 +01:00
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2005, Jeremiah Fincher
|
2012-09-01 16:16:48 +02:00
|
|
|
# Copyright (c) 2009, James McCoy
|
2005-02-03 15:55:31 +01:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
VERBOSE = False
|
|
|
|
|
|
|
|
def readPid(filename):
|
2012-08-04 13:13:16 +02:00
|
|
|
fd = open(filename)
|
2005-02-03 15:55:31 +01:00
|
|
|
try:
|
|
|
|
return int(fd.read().strip())
|
|
|
|
finally:
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
def isAlive(pid):
|
|
|
|
try:
|
|
|
|
os.kill(pid, 0)
|
|
|
|
return True
|
|
|
|
except OSError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def debug(s):
|
|
|
|
if VERBOSE:
|
|
|
|
if not s.endswith(os.linesep):
|
|
|
|
s += os.linesep
|
|
|
|
sys.stdout.write(s)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# XXX I wanted this for conf.version, but this will create directories. We
|
|
|
|
# really need to refactor conf so it either doesn't create directories, or
|
|
|
|
# so that static information (like the version) can be imported from
|
|
|
|
# somewhere else.
|
|
|
|
# import supybot.conf as conf
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import optparse
|
2009-11-22 20:31:58 +01:00
|
|
|
import subprocess
|
2005-02-03 15:55:31 +01:00
|
|
|
|
|
|
|
parser = optparse.OptionParser(usage='Usage: %prog [options]')
|
|
|
|
parser.add_option('', '--verbose', action='store_true',
|
|
|
|
help='Makes output verbose.')
|
|
|
|
parser.add_option('', '--botdir',
|
|
|
|
help='Determines what directory the bot resides in and '
|
|
|
|
'should be started from.')
|
|
|
|
parser.add_option('', '--pidfile',
|
|
|
|
help='Determines what file to look in for the pid of '
|
|
|
|
'the running bot. This should be relative to the '
|
2010-04-26 05:24:42 +02:00
|
|
|
'given bot directory. Note that for this to actually '
|
|
|
|
'work, you have to make a matching entry in the '
|
|
|
|
'supybot.pidFile config in the supybot registry.')
|
2005-02-03 15:55:31 +01:00
|
|
|
parser.add_option('', '--supybot', default='supybot',
|
|
|
|
help='Determines where the supybot executable is '
|
|
|
|
'located. If not given, assumes that supybot is '
|
|
|
|
'in $PATH.')
|
|
|
|
parser.add_option('', '--conffile',
|
|
|
|
help='Determines what configuration file should be '
|
|
|
|
'given to the supybot executable when (re)starting the '
|
|
|
|
'bot.')
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
VERBOSE = options.verbose
|
|
|
|
|
|
|
|
if args:
|
|
|
|
parser.error('Extra arguments given.')
|
|
|
|
if not options.botdir:
|
|
|
|
parser.error('No botdir given.')
|
|
|
|
if not options.pidfile:
|
|
|
|
parser.error('No pidfile given.')
|
|
|
|
if not options.conffile:
|
|
|
|
parser.error('No conffile given.')
|
|
|
|
|
|
|
|
os.chdir(options.botdir)
|
2013-02-07 15:37:20 +01:00
|
|
|
open(options.pidfile, 'a').close()
|
2005-02-03 15:55:31 +01:00
|
|
|
|
|
|
|
pid = None
|
|
|
|
try:
|
|
|
|
pid = readPid(options.pidfile)
|
|
|
|
debug('Found pidFile with proper pid contents of %s' % pid)
|
2013-04-27 16:16:08 +02:00
|
|
|
except ValueError as e:
|
2005-02-03 15:55:31 +01:00
|
|
|
foundBot = False
|
|
|
|
|
|
|
|
if pid is not None:
|
|
|
|
foundBot = isAlive(pid)
|
|
|
|
if foundBot:
|
|
|
|
debug('Pid %s is alive and belongs to us.' % pid)
|
|
|
|
else:
|
|
|
|
debug('Pid %s is not the bot.' % pid)
|
|
|
|
|
|
|
|
if not foundBot:
|
2009-04-27 17:39:22 +02:00
|
|
|
# First, we check if the pidfile is writable. If not, supybot will just exit,
|
|
|
|
# so we go ahead and refuse to start it.
|
|
|
|
try:
|
2013-02-02 19:46:59 +01:00
|
|
|
open(options.pidfile, 'r+')
|
2013-04-27 16:16:08 +02:00
|
|
|
except EnvironmentError as e:
|
2009-04-27 18:10:56 +02:00
|
|
|
debug('pidfile (%s) is not writable: %s' % (options.pidfile, e))
|
2009-04-27 17:39:22 +02:00
|
|
|
sys.exit(-1)
|
2005-02-03 15:55:31 +01:00
|
|
|
debug('Bot not found, starting.')
|
2013-08-24 03:35:35 +02:00
|
|
|
cmdline = [options.supybot, '--daemon', options.conffile]
|
|
|
|
inst = subprocess.Popen(cmdline, close_fds=True,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
stdin=None, stdout=subprocess.PIPE)
|
|
|
|
debug('Output from supybot: %r' % inst.stdout.read())
|
2005-02-17 10:27:01 +01:00
|
|
|
ret = inst.wait()
|
2013-08-24 03:35:35 +02:00
|
|
|
debug('Bot started, command line %r returned %s.' % (' '.join(cmdline),
|
|
|
|
ret))
|
2005-02-03 15:55:31 +01:00
|
|
|
sys.exit(ret)
|
|
|
|
else:
|
|
|
|
sys.exit(0)
|
2009-03-12 00:26:49 +01:00
|
|
|
|
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|