2019-02-16 22:53:35 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import signal
|
|
|
|
import os
|
2019-02-16 23:04:13 +03:00
|
|
|
import stat
|
2019-02-16 22:53:35 +03:00
|
|
|
|
2018-12-06 15:36:42 +03:00
|
|
|
|
2018-12-08 02:01:44 +03:00
|
|
|
neededmodules = [
|
|
|
|
"bottle",
|
2019-02-16 23:21:29 +03:00
|
|
|
"waitress",
|
|
|
|
"setproctitle"
|
2018-12-08 02:01:44 +03:00
|
|
|
]
|
2018-12-06 15:36:42 +03:00
|
|
|
|
2019-02-18 02:34:25 +03:00
|
|
|
recommendedmodules = [
|
|
|
|
"wand"
|
|
|
|
]
|
|
|
|
|
2019-02-16 22:53:35 +03:00
|
|
|
SOURCE_URL = "https://github.com/krateng/maloja/archive/master.zip"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def install():
|
|
|
|
toinstall = []
|
2019-02-18 02:34:25 +03:00
|
|
|
toinstallr = []
|
2019-02-16 22:53:35 +03:00
|
|
|
for m in neededmodules:
|
|
|
|
try:
|
|
|
|
exec("import " + m) #I'm sorry
|
|
|
|
except:
|
|
|
|
toinstall.append(m)
|
|
|
|
|
2019-02-18 02:34:25 +03:00
|
|
|
for m in recommendedmodules:
|
|
|
|
try:
|
|
|
|
exec("import " + m)
|
|
|
|
except:
|
|
|
|
toinstallr.append(m)
|
|
|
|
|
2019-02-16 22:53:35 +03:00
|
|
|
if toinstall != []:
|
|
|
|
print("The following python modules need to be installed:")
|
|
|
|
for m in toinstall:
|
|
|
|
print("\t" + m)
|
2019-02-18 02:34:25 +03:00
|
|
|
if toinstallr != []:
|
|
|
|
print("The following python modules are highly recommended, some features will not work without them:")
|
|
|
|
for m in toinstallr:
|
|
|
|
print("\t" + m)
|
2019-02-16 22:53:35 +03:00
|
|
|
|
2019-02-18 02:34:25 +03:00
|
|
|
if toinstall != [] or toinstallr != []:
|
|
|
|
if os.geteuid() != 0:
|
|
|
|
print("Installing python modules should be fairly straight-forward, but Maloja can try to install \
|
|
|
|
them automatically. For this, you need to run this script as a root user.")
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
print("Installing python modules should be fairly straight-forward, but Maloja can try to install \
|
|
|
|
them automatically, This might or might not work / bloat your system / cause a nuclear war.")
|
|
|
|
fail = False
|
|
|
|
if toinstall != []:
|
|
|
|
print("Attempt to install required modules? [Y/n]")
|
|
|
|
answer = input()
|
|
|
|
|
|
|
|
if answer.lower() in ["y","yes","yea","1","positive","true"]:
|
|
|
|
for m in neededmodules:
|
|
|
|
try:
|
|
|
|
print("Installing " + m + " with pip...")
|
|
|
|
import pip
|
|
|
|
#os.system("pip3 install " + m)
|
|
|
|
pip.main(["install",m])
|
|
|
|
print("Success!")
|
|
|
|
except:
|
|
|
|
print("Failure!")
|
|
|
|
fail = True
|
|
|
|
|
|
|
|
elif answer.lower() in ["n","no","nay","0","negative","false"]:
|
|
|
|
return False #if you dont want to auto install required, you probably dont want to install recommended
|
|
|
|
else:
|
|
|
|
print("What?")
|
|
|
|
return False
|
|
|
|
if toinstallr != []:
|
|
|
|
print("Attempt to install recommended modules? [Y/n]")
|
|
|
|
answer = input()
|
|
|
|
|
|
|
|
if answer.lower() in ["y","yes","yea","1","positive","true",""]:
|
|
|
|
for m in recommendedmodules:
|
|
|
|
try:
|
|
|
|
print("Installing " + m + " with pip...")
|
|
|
|
import pip
|
|
|
|
#os.system("pip3 install " + m)
|
|
|
|
pip.main(["install",m])
|
|
|
|
print("Success!")
|
|
|
|
except:
|
|
|
|
print("Failure!")
|
|
|
|
fail = True
|
|
|
|
|
|
|
|
elif answer.lower() in ["n","no","nay","0","negative","false"]:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
print("What?")
|
|
|
|
return False
|
|
|
|
|
|
|
|
if fail: return False
|
|
|
|
print("All modules successfully installed!")
|
|
|
|
return True
|
|
|
|
|
|
|
|
else:
|
|
|
|
print("All necessary modules seem to be installed.")
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-02-16 22:53:35 +03:00
|
|
|
def getInstance():
|
|
|
|
try:
|
|
|
|
output = subprocess.check_output(["pidof","Maloja"])
|
|
|
|
pid = int(output)
|
|
|
|
return pid
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start():
|
2019-02-18 02:34:25 +03:00
|
|
|
if install():
|
2019-02-16 22:53:35 +03:00
|
|
|
|
2019-02-18 02:34:25 +03:00
|
|
|
try:
|
|
|
|
p = subprocess.Popen(["python3","server.py"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
|
|
|
|
print("Maloja started! PID: " + str(p.pid))
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
print("Error while starting Maloja. Are you sure it's not already running?")
|
|
|
|
return False
|
|
|
|
|
2018-12-08 02:01:44 +03:00
|
|
|
|
2019-02-16 22:53:35 +03:00
|
|
|
|
|
|
|
def restart():
|
|
|
|
#pid = getInstance()
|
|
|
|
#if pid == None:
|
|
|
|
# print("Server is not running.")
|
|
|
|
#else:
|
|
|
|
# stop()
|
|
|
|
#start()
|
|
|
|
|
|
|
|
wasrunning = stop()
|
|
|
|
start()
|
|
|
|
return wasrunning
|
|
|
|
|
|
|
|
def stop():
|
|
|
|
pid = getInstance()
|
|
|
|
if pid == None:
|
|
|
|
print("Server is not running")
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
os.kill(pid,signal.SIGTERM)
|
2019-02-18 02:34:25 +03:00
|
|
|
print("Maloja stopped! PID: " + str(pid))
|
2019-02-16 22:53:35 +03:00
|
|
|
return True
|
|
|
|
|
|
|
|
def update():
|
|
|
|
|
|
|
|
import urllib.request
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
import zipfile
|
|
|
|
import distutils.dir_util
|
|
|
|
|
|
|
|
print("Updating Maloja...")
|
|
|
|
with urllib.request.urlopen(SOURCE_URL) as response:
|
|
|
|
with tempfile.NamedTemporaryFile(delete=True) as tmpfile:
|
|
|
|
shutil.copyfileobj(response,tmpfile)
|
|
|
|
|
|
|
|
with zipfile.ZipFile(tmpfile.name,"r") as z:
|
|
|
|
|
|
|
|
for f in z.namelist():
|
2019-02-18 02:34:25 +03:00
|
|
|
#print("extracting " + f)
|
2019-02-16 22:53:35 +03:00
|
|
|
z.extract(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
distutils.dir_util.copy_tree("./maloja-master/","./",verbose=2)
|
|
|
|
shutil.rmtree("./maloja-master")
|
|
|
|
print("Done!")
|
|
|
|
|
2019-02-16 23:04:13 +03:00
|
|
|
os.chmod("./maloja",os.stat("./maloja").st_mode | stat.S_IXUSR)
|
2019-02-16 22:53:35 +03:00
|
|
|
|
|
|
|
if stop(): start() #stop returns whether it was running before, in which case we restart it
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if sys.argv[1] == "start": start()
|
|
|
|
elif sys.argv[1] == "restart": restart()
|
|
|
|
elif sys.argv[1] == "stop": stop()
|
|
|
|
elif sys.argv[1] == "update": update()
|
2019-02-18 02:34:25 +03:00
|
|
|
else: print("Valid commands: start restart stop update")
|
2019-02-16 22:53:35 +03:00
|
|
|
|