diff --git a/scripts/supybot-botchk b/scripts/supybot-botchk new file mode 100644 index 000000000..1f522b93b --- /dev/null +++ b/scripts/supybot-botchk @@ -0,0 +1,121 @@ +#!/usr/bin/env python + +### +# Copyright (c) 2005, Jeremiah Fincher +# 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): + fd = file(filename) + 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 + + 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 ' + 'given bot directory.') + 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) + os.system('touch %s' % options.pidfile) + + pid = None + try: + pid = readPid(options.pidfile) + debug('Found pidFile with proper pid contents of %s' % pid) + except ValueError, e: + 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: + debug('Bot not found, starting.') + cmdline = '%s --daemon %s' % (options.supybot, options.conffile) + ret = os.system(cmdline) + debug('Bot started, command line %r returned %s.' % (cmdline, ret)) + sys.exit(ret) + else: + sys.exit(0) + +# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: diff --git a/setup.py b/setup.py index 4805cfbbb..5916906c9 100644 --- a/setup.py +++ b/setup.py @@ -185,6 +185,7 @@ setup( scripts=['scripts/supybot', 'scripts/supybot-test', + 'scripts/supybot-botchk', 'scripts/supybot-wizard', 'scripts/supybot-adduser', 'scripts/supybot-newplugin']