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/disabled_stuff/wrapper.old

197 lines
5.5 KiB
Plaintext
Raw Permalink Normal View History

2012-02-29 10:12:17 +01:00
#!/usr/bin/env python
# Bot Wrapper by neersighted
2012-03-01 10:30:46 +01:00
# Import required modules
2012-02-29 10:12:17 +01:00
import os
2012-03-01 11:08:16 +01:00
import sys
2012-02-29 10:12:17 +01:00
import subprocess
2012-02-29 20:01:31 +01:00
import json
2012-03-01 11:08:16 +01:00
import re
2012-02-29 18:44:05 +01:00
2012-03-01 11:08:16 +01:00
# Files
configfile = os.path.isfile("./config")
2012-03-01 18:31:44 +01:00
botfile = os.path.isfile("./bot.py")
2012-03-01 06:01:53 +01:00
2012-03-01 10:30:46 +01:00
# Colors
2012-02-29 19:38:50 +01:00
nocol = "\033[1;m"
red = "\033[1;31m"
green = "\033[1;32m"
2012-03-01 11:08:16 +01:00
# Messages
firstrun = "Welclome to your first run of: "
usage = "usage: ./cloudbot {start|stop|restart|status}"
iusage = "{1|start} {2|stop} {3|restart} {4|status} {5|exit}"
2012-03-01 19:02:21 +01:00
quit = "Thanks for using CloudBot!"
2012-03-01 11:08:16 +01:00
error1 = red + "Neither screen nor daemon is installed! "\
"This program cannot run! {ERROR 1}" + nocol
error2 = red + "Could not find bot.py! Are you in the wrong folder? "\
"{ERROR 2}" + nocol
error3 = red + "Invalid choice, exiting! {ERROR 3}" + nocol
error4 = red + "Program killed by user! {ERROR 4}" + nocol
2012-03-01 19:02:21 +01:00
error5 = red + "Invalid backend in config! (Or, backend not installed)"\
" {ERROR 5}" + nocol
2012-03-01 11:08:16 +01:00
error6 = red + "Author error! We be derpin'! {ERROR 6}" + nocol
2012-03-01 10:30:46 +01:00
# Commands
2012-02-29 10:12:17 +01:00
pwd = os.getcwd()
2012-03-01 10:18:02 +01:00
clearlog = ": > ./bot.log"
2012-03-01 11:08:16 +01:00
2012-03-01 10:30:46 +01:00
start = "echo " + "'" + error1 + "'"
stop = "echo " + "'" + error1 + "'"
restart = "echo " + "'" + error1 + "'"
pid = "echo 'Cannot get pid'"
2012-02-29 19:30:31 +01:00
2012-03-01 11:08:16 +01:00
daemonstart = "daemon -r -n cloudbot -O " + pwd + \
"/bot.log python " + pwd + "/bot.py"
daemonstop = "daemon -n cloudbot --stop"
daemonrestart = "./cloudbot stop > /dev/null 2>&1 && ./cloudbot start > /dev/null 2>&1"
2012-03-01 11:08:16 +01:00
daemonpid = "pidof /usr/bin/daemon"
2012-03-01 19:02:21 +01:00
screenstart = "screen -d -m -S cloudbot -t cloudbot python " + pwd +\
"/bot.py > " + pwd + "/bot.log 2>&1"
2012-03-01 11:08:16 +01:00
screenstop = "kill `pidof /usr/bin/screen`"
screenrestart = "./cloudbot stop > /dev/null 2>&1 && ./cloudbot start > /dev/null 2>&1"
2012-03-01 11:08:16 +01:00
screenpid = "pidof /usr/bin/screen"
2012-03-01 10:30:46 +01:00
# Checks
2012-03-01 11:08:16 +01:00
if configfile:
try:
config = json.load(open('config'))
command = ":"
except ValueError, e:
print 'error: malformed config', e
else:
config = False
command = "python bot.py"
2012-03-01 06:11:42 +01:00
daemoncheck = subprocess.check_output("locate /usr/bin/daemon", shell=True)
2012-02-29 18:44:05 +01:00
daemon = re.match(r'^/usr/bin/daemon$', daemoncheck)
2012-03-01 11:08:16 +01:00
2012-03-01 06:11:42 +01:00
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)
2012-03-01 11:08:16 +01:00
if configfile:
2012-03-01 09:46:21 +01:00
backend = config.get("wrapper", {}).get("backend", "daemon")
2012-03-01 23:17:12 +01:00
daemonloc = config.get("wrapper", {}).get("daemonloc", "/usr/bin/daemon")
screenloc = config.get("wrapper", {}).get("screenloc", "/usr/bin/screen")
2012-03-01 09:46:21 +01:00
else:
backend = False
2012-03-01 23:17:12 +01:00
daemonloc = "/usr/bin/daemon"
screenloc = "/usr/bin/screen"
2012-02-29 19:30:31 +01:00
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-03-01 10:30:46 +01:00
# Set commands
2012-03-01 19:02:21 +01:00
if (backend == "daemon"):
if daemon:
2012-03-01 11:08:16 +01:00
start = daemonstart
stop = daemonstop
restart = daemonrestart
pid = daemonpid
2012-02-29 20:01:31 +01:00
else:
print error5
exit
2012-03-01 19:02:21 +01:00
elif (backend == "screen"):
if screen:
2012-03-01 11:08:16 +01:00
start = screenstart
stop = screenstop
restart = screenrestart
pid = screenpid
2012-02-29 20:01:31 +01:00
else:
print error5
exit
2012-03-01 19:02:21 +01:00
elif (backend == False):
print firstrun
2012-02-29 18:44:05 +01:00
else:
2012-03-01 19:02:21 +01:00
print error5
exit
2012-03-01 10:30:46 +01:00
# Fancy banner
2012-02-29 18:44:05 +01:00
print " ______ __ ______ __ __ "\
" _______ .______ ______ .___________."
print " / || | / __ \ | | | | "\
"| \ | _ \ / __ \ | |"
print "| ,----'| | | | | | | | | | "\
"| .--. || |_) | | | | | `---| |----`"
print "| | | | | | | | | | | | "\
"| | | || _ < | | | | | | "
print "| `----.| `----.| `--' | | `--' | "\
"| '--' || |_) | | `--' | | | "
print " \______||_______| \______/ \______/ "\
"|_______/ |______/ \______/ |__| "
print "http://git.io/cloudbot "\
" by lukeroge"
2012-03-01 11:08:16 +01:00
2012-03-01 10:30:46 +01:00
# Read arguments/turn interactive
2012-02-29 18:59:18 +01:00
try:
2012-03-01 18:31:44 +01:00
if (len(sys.argv) > 1):
read = 0
2012-02-29 18:44:05 +01:00
else:
2012-03-01 18:31:44 +01:00
sys.argv = "interactive"
print iusage
read = int(raw_input("Please choose a option: "))
if (sys.argv[1] == "start") or (read == 1):
if running:
print "Bot is already running, cannot start!"
else:
command = start
2012-03-01 19:02:21 +01:00
print "Starting... (" + backend + ")"
2012-03-01 18:31:44 +01:00
elif (sys.argv[1] == "stop") or (read == 2):
if running:
command = stop
print "Stopping... (" + backend + ")"
2012-02-29 18:59:18 +01:00
else:
2012-03-01 18:31:44 +01:00
print "Bot is not running, cannot stop!"
elif (sys.argv[1] == "restart") or (read == 3):
if running:
command = restart
print "Restarting... (" + backend + ")"
else:
print "Bot is not running, cannot restart!"
elif (sys.argv[1] == "status") or (read == 4):
if running:
command = pid
print green + "Bot is running! " + nocol
else:
print red + "Bot is not running! " + nocol
elif (sys.argv[1] == "clear"):
command = clearlog
2012-03-01 19:02:21 +01:00
elif (sys.argv[1] == "exit") or (read == 5):
2012-03-01 18:31:44 +01:00
exit
elif (sys.argv[1] == "interactive"):
pass
else:
print usage
exit
2012-03-01 19:02:21 +01:00
2012-03-01 10:30:46 +01:00
# Pretify errors
2012-02-29 20:01:31 +01:00
except (TypeError, ValueError), e:
2012-02-29 18:59:18 +01:00
print error3
exit
2012-02-29 20:01:31 +01:00
except (KeyboardInterrupt), e:
2012-02-29 18:59:18 +01:00
print error4
exit
2012-02-29 20:01:31 +01:00
except (NameError, SyntaxError), e:
print error6
2012-02-29 18:59:18 +01:00
exit
2012-03-01 10:30:46 +01:00
# Check for bot files
2012-03-01 11:08:16 +01:00
if botfile:
2012-03-01 19:02:21 +01:00
pass
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-03-01 10:30:46 +01:00
# Call command
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