This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/cloudbot

155 lines
5.2 KiB
Plaintext
Raw Normal View History

2012-02-29 10:12:17 +01:00
#!/usr/bin/env python
# Bot Wrapper by neersighted
import sys
import os
import subprocess
2012-02-29 18:44:05 +01:00
import re
usage = "usage: ./cloudbot {start|stop|restart|status}"
2012-02-29 18:59:18 +01:00
quit = "Goodbye! Thanks for using CloudBot!"
2012-02-29 10:12:17 +01:00
pwd = os.getcwd()
clearlog = ": >./bot.log && "
command = ":"
2012-02-29 19:30:31 +01:00
nocol = "\033[1;m"
red = "\033[1;31m"
green = "\033[1;32m"
2012-02-29 18:44:05 +01:00
daemoncheck = subprocess.check_output("locate /usr/bin/daemon", shell=True)
daemon = re.match(r'^/usr/bin/daemon$', daemoncheck)
screencheck = subprocess.check_output("locate /usr/bin/screen", shell=True)
2012-02-29 19:30:31 +01:00
screen = re.match(r'^/usr/bin/screen$', screencheck)
try:
runningcheck = subprocess.check_output("ps ax|grep cloudbot|"\
"grep -v grep|grep -v ./cloudbot", shell=True)
running = re.match(r'^[1-9]+', runningcheck)
except (subprocess.CalledProcessError):
running = False
2012-02-29 18:44:05 +01:00
2012-02-29 19:30:31 +01:00
error1 = red + "Neither screen nor daemon is installed! "\
"This script cannot run! {ERROR 1}" + nocol
error2 = red + "Could not find bot.py! Are you in the wrong folder? "\
"(" + pwd + ") {ERROR 2}" + nocol
error3 = red + "Invalid choice, exiting! {ERROR 3}" + nocol
error4 = red + "Program killed by user! {ERROR 4}" + nocol
error5 = red + "Author error! We be derpin'! {ERROR 5}" + nocol
2012-02-29 18:44:05 +01:00
start = "echo " + error1
stop = "echo " + error1
restart = "echo " + error1
if daemon:
start = "daemon -n cloudbot -O " + pwd + \
"/bot.log -r python " + pwd + "/bot.py"
stop = clearlog + "daemon -n cloudbot --stop"
restart = stop + " && " + start
2012-02-29 19:30:31 +01:00
pid = subprocess.check_output("pidof /usr/bin/daemon", shell=True)
2012-02-29 18:44:05 +01:00
elif screen:
start = "screen -S cloudbot -dm python" + pwd +\
"/bot.py >>" + pwd + "/bot.log 2>&1"
stop = clearlog + "kill `pidof /usr/bin/screen`"
restart = stop + " && " + start
2012-02-29 19:30:31 +01:00
pid = subprocess.check_output("pidof /usr/bin/screen", shell=True)
2012-02-29 18:44:05 +01:00
else:
print error1
print " ______ __ ______ __ __ "\
" _______ .______ ______ .___________."
print " / || | / __ \ | | | | "\
"| \ | _ \ / __ \ | |"
print "| ,----'| | | | | | | | | | "\
"| .--. || |_) | | | | | `---| |----`"
print "| | | | | | | | | | | | "\
"| | | || _ < | | | | | | "
print "| `----.| `----.| `--' | | `--' | "\
"| '--' || |_) | | `--' | | | "
print " \______||_______| \______/ \______/ "\
"|_______/ |______/ \______/ |__| "
print "http://git.io/cloudbot "\
" by lukeroge"
2012-02-29 18:59:18 +01:00
try:
2012-02-29 19:30:31 +01:00
if running:
if (len(sys.argv) > 1):
if (sys.argv[1] == 'start'):
print "Bot is alread running, cannot start!"
elif (sys.argv[1] == 'stop'):
command = stop
print "Stopping with: " + command
elif (sys.argv[1] == 'restart'):
command = restart
print "Restarting with: " + command
elif (sys.argv[1] == 'status'):
print green + "Bot is running! " + pid + nocol
else:
print usage
exit
2012-02-29 18:59:18 +01:00
else:
2012-02-29 19:30:31 +01:00
print "{1|start} {2|stop} {3|restart} {4|status} {5|exit}"
read = int(raw_input('Please choose a option: '))
if read == 1:
print "Bot is alread running, cannot start!"
elif read == 2:
command = stop
print "Stopping with: " + command
elif read == 3:
command = restart
print "Restarting with: " + command
elif read == 4:
print green + "Bot is running! " + pid + nocol
elif read == 5:
print quit
else:
print error3
2012-02-29 18:44:05 +01:00
else:
2012-02-29 19:30:31 +01:00
if (len(sys.argv) > 1):
if (sys.argv[1] == 'start'):
command = start
print "Starting with: " + command
elif (sys.argv[1] == 'stop'):
print "Bot is not running, cannot stop!"
elif (sys.argv[1] == 'restart'):
print "Bot is not running, cannot restart!"
elif (sys.argv[1] == 'status'):
print red + "Bot is not running!" +nocol
else:
print usage
exit
2012-02-29 18:59:18 +01:00
else:
2012-02-29 19:30:31 +01:00
print "{1|start} {2|stop} {3|restart} {4|status} {5|exit}"
read = int(raw_input('Please choose a option: '))
if read == 1:
command = start
print "Starting with: " + command
elif read == 2:
print "Bot is not running, cannot stop!"
elif read == 3:
command = restart
elif read == 4:
print red + "Bot is not running, cannot restart!" + nocol
elif read == 5:
print quit
else:
print error3
exit
2012-02-29 18:59:18 +01:00
except (TypeError, ValueError):
print error3
exit
except (KeyboardInterrupt):
print error4
exit
except (NameError, SyntaxError):
print error5
exit
2012-02-29 10:16:21 +01:00
if os.path.isfile("./bot.py"):
command = command
2012-02-29 10:12:17 +01:00
else:
2012-02-29 18:44:05 +01:00
print error2
exit
2012-02-29 18:59:18 +01:00
2012-02-29 10:12:17 +01:00
subprocess.call(command, shell=True)
2012-02-29 18:59:18 +01:00
print quit
2012-02-29 18:44:05 +01:00
exit