spaceningui/subfiles/comunic.py

150 lines
4.7 KiB
Python

#communication stuff here
import requests
import json
from hashlib import sha256
from . import helpers, savetools
from websockets.sync.client import connect
from subprocess import Popen, PIPE
from sys import platform
from os import listdir
from os.path import isfile, join, exists
from pathlib import Path
import subprocess
def downloadLatestDll(self, filepath):
x = requests.get('https://openwf.io/supplementals/client%20drop-in/meta')
meta = json.loads(x.text)
url = f"https://openwf.io/supplementals/client%20drop-in/{meta['version']}/dwmapi.dll"#
content = requests.get(url, stream=True).content
hashofdll = sha256()
hashofdll.update(content)#
if hashofdll.hexdigest() == meta['sha256']:
with open(filepath + "dwmapi.dll", "wb") as out_file:
out_file.write(content)
helpers.messageBox(self=self,title="Bootstrapper Downloaded", message="Bootstrapper Downloaded!", type="info")
else:
helpers.messageBox(self=self,title="Unknown Error.", message="SpaceninGUI could not verify the hash of the Bootstrapper. Sorry :(", type="error")#
def getLatestVersions(self, cacheFile, value):
ver = {
"server" : (
"no lol" #not finished update check for server, check back later :3
),
"client" : ""
}
#client code here
x = requests.get('https://openwf.io/supplementals/client%20drop-in/meta')
meta = json.loads(x.text)
ver["client"] = meta['version']
try:
oldVer = savetools.loadFile(self, cacheFile)["client"]
except:
with open('tempFile.json', 'w') as f:
json.dump({
"client":'notassigned',
'server':'notassigned'
},f,indent=2)
oldVer = savetools.loadFile(self, cacheFile)["client"]
if oldVer == ver["client"]:
client="Up To Date!"
else:
client="New Version " + ver["client"]
server = "server Check Not Implemented"
value[0].set("Latest Server Version: " + server)
value[1].set("Latest Client Version: " + client)
def serverBackground(self, cd, logArea): # To my knowledge, running the server in the main thread would stall the gui, fuck that lol
logmax = helpers.getLogMax()
logarr=[]
def mongo():
sysctl = Popen(['systemctl', 'start', 'mongodb'])
sysctl.wait()#
def execute(cd):
popen = Popen(['npm', 'run', 'dev'], stdin=PIPE, stdout=PIPE, universal_newlines=True, cwd=cd)
for stdout_line in iter(popen.stdout.readline, ""):
yield stdout_line
popen.stdout.close()
return_code = popen.wait()
if return_code:
raise subprocess.CalledProcessError(return_code)#
if platform == "linux":
helpers.messageBox(self, title="MongoDB", message="The mongodb server is required to start SNS\nYou will need to give sudo permission for this.", type="info")
mongo()
for path in execute(cd):
if len(logarr) < logmax:
logarr.append(path)
else:
logarr.pop(0)
logarr.append(path)
if logArea:
logArea.configure(state="normal")
logArea.delete(1.0, "end")
for element in logarr:
pass
logArea.configure(state="disabled")
def getClientResponse():
try:
with connect("ws://localhost:61558") as websocket:
message = json.loads(websocket.recv())
except:
message=False
return message
def checkForClient(clientpath):
onlyfiles = [f for f in listdir(clientpath) if isfile(join(clientpath, f))]
if "dwmapi.dll" in onlyfiles:
return True
else:
return False
def runPluto(name, plutolist, conffile):
openwfpath = helpers.getclientpath(conffile) + "/OpenWF/scripts/"
onlyfiles = [f for f in listdir(openwfpath) if isfile(join(openwfpath, f))]
if helpers.getclientresponse():
running = []
print(helpers.getclientresponse()["running_scripts"])
if name in running:
requests.get(url=("http://localhost:61558/stop_script?OpenWF/scripts/" + name))
else:
requests.get(url=("http://localhost:61558/start_script?OpenWF/scripts/" + name))
for script in helpers.getclientresponse()["running_scripts"]:
running.append(script.removeprefix("OpenWF/scripts/"))#
for file in onlyfiles:
if file in running:
if plutolist[file]: plutolist[file].set("Running")
else:
if plutolist[file]: plutolist[file].set("Not Running")
def getSnPath(file):
if Path(file).is_file():
with open(file) as f:
d = json.load(f)
return d["spaceninjapath"]
else:
return False