labyrinth-cs-proj/starter.py

77 lines
2.2 KiB
Python
Raw Normal View History

import os
import pickle
import subprocess
import sys
2022-11-02 20:02:13 +00:00
from maze.modules import bruh
from maze.modules.PlayerBase_func import databaseinit
2022-11-22 10:35:23 +00:00
user = password = None
with open("credentials.pickle", "rb") as f:
try:
while True:
d = pickle.load(f)
if d["credtype"] == "mysql":
user = d["user"]
password = d["pass"]
break
except EOFError:
user = password = None
def getcreds():
2022-11-22 10:35:23 +00:00
if user:
return user, password
else:
print(
"""MySQL username or password not initialised. Defaulting to 'root' and ''.
Run 'python starter.py initsql' to initialise credentials of your choice. """
)
with open("credentials.pickle", "rb+") as f:
try:
while True:
pos = f.tell()
d = pickle.load(f)
if d["credtype"] == "mysql":
d["user"] = "root"
d["pass"] = ""
f.seek(pos)
pickle.dump(d, f)
except EOFError:
pass
return None, None
2022-11-02 20:02:13 +00:00
if len(sys.argv) == 1:
getcreds()
bruh()
sys.exit()
else:
if sys.argv[1] == "dumpsample":
getcreds()
databaseinit()
subprocess.call(
2022-11-22 10:35:23 +00:00
f"mysql -u {user} --password={password} -D labyrinth < {os.path.abspath('dbdump.sql')}",
shell=True#,
#stdout=subprocess.DEVNULL,
#stderr=subprocess.DEVNULL,
)
2022-11-22 10:35:23 +00:00
print("Successfully dumped sample data")
elif sys.argv[1] == "initsql":
user = input("Enter MySQL username: ")
password = input("Enter MySQL password: ")
with open("credentials.pickle", "rb+") as f:
try:
while True:
pos = f.tell()
d = pickle.load(f)
if d["credtype"] == "mysql":
d["user"] = user
d["pass"] = password
f.seek(pos)
pickle.dump(d, f)
except EOFError:
pass
print("Successfully set.")