moved FTP operations to separate file
This commit is contained in:
parent
8c8b40e731
commit
1f4c4529f7
141
Backend.py
141
Backend.py
@ -1,4 +1,3 @@
|
|||||||
from ftplib import FTP_TLS as FTP
|
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import tempfile
|
import tempfile
|
||||||
@ -7,12 +6,10 @@ import time
|
|||||||
import os
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
import zipfile
|
import zipfile
|
||||||
from customtkinter import *
|
|
||||||
import time
|
|
||||||
import math
|
|
||||||
import tarfile
|
import tarfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import shutil
|
import shutil
|
||||||
|
from FTP import *
|
||||||
|
|
||||||
def install_prism():
|
def install_prism():
|
||||||
global data_dir
|
global data_dir
|
||||||
@ -22,11 +19,8 @@ def install_prism():
|
|||||||
if os.name == "posix":
|
if os.name == "posix":
|
||||||
suff = "lin.tar.gz"
|
suff = "lin.tar.gz"
|
||||||
prism_file = "prism/prism-"+suff
|
prism_file = "prism/prism-"+suff
|
||||||
ftp = FTP("gitea.piwalker.net")
|
|
||||||
ftp.login()
|
|
||||||
ftp.prot_p()
|
|
||||||
with io.BytesIO() as buff:
|
with io.BytesIO() as buff:
|
||||||
ftp.retrbinary("RETR "+prism_file, buff.write)
|
ftpDownload(prism_file, buff)
|
||||||
buff.seek(0)
|
buff.seek(0)
|
||||||
if os.name == "nt":
|
if os.name == "nt":
|
||||||
with zipfile.ZipFile(buff) as zip:
|
with zipfile.ZipFile(buff) as zip:
|
||||||
@ -35,11 +29,10 @@ def install_prism():
|
|||||||
with tarfile.open(fileobj=buff) as tar:
|
with tarfile.open(fileobj=buff) as tar:
|
||||||
tar.extractall(os.path.join(data_dir, "prism"))
|
tar.extractall(os.path.join(data_dir, "prism"))
|
||||||
with io.BytesIO() as buff:
|
with io.BytesIO() as buff:
|
||||||
ftp.retrbinary("RETR prism/prismlauncher.cfg", buff.write)
|
ftpDownload("prism/prismlauncher.cfg", buff)
|
||||||
buff.seek(0)
|
buff.seek(0)
|
||||||
with open(os.path.join(data_dir, "prism", "prismlauncher.cfg"), 'wb') as file:
|
with open(os.path.join(data_dir, "prism", "prismlauncher.cfg"), 'wb') as file:
|
||||||
file.write(buff.getbuffer())
|
file.write(buff.getbuffer())
|
||||||
ftp.close()
|
|
||||||
suff = "win"
|
suff = "win"
|
||||||
cmd = "java.exe"
|
cmd = "java.exe"
|
||||||
if os.name == "posix":
|
if os.name == "posix":
|
||||||
@ -58,10 +51,6 @@ def install_prism():
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_tar_members_stripped(tar, n_folders_stripped = 1):
|
def get_tar_members_stripped(tar, n_folders_stripped = 1):
|
||||||
members = []
|
members = []
|
||||||
for member in tar.getmembers():
|
for member in tar.getmembers():
|
||||||
@ -111,11 +100,8 @@ def install_java(java):
|
|||||||
java_path = "java/java-"+str(java)+"-"+suff+ext
|
java_path = "java/java-"+str(java)+"-"+suff+ext
|
||||||
if not os.path.exists(data_dir+"/java/java-"+str(java)+"-"+suff):
|
if not os.path.exists(data_dir+"/java/java-"+str(java)+"-"+suff):
|
||||||
print("Downloading Java "+str(java))
|
print("Downloading Java "+str(java))
|
||||||
ftp = FTP("gitea.piwalker.net")
|
|
||||||
ftp.login()
|
|
||||||
ftp.prot_p()
|
|
||||||
with io.BytesIO() as buff:
|
with io.BytesIO() as buff:
|
||||||
ftp.retrbinary("RETR "+java_path,buff.write)
|
ftpDownload(java_path, buff)
|
||||||
if os.name == "posix":
|
if os.name == "posix":
|
||||||
buff.seek(0)
|
buff.seek(0)
|
||||||
with tarfile.open(fileobj=buff) as tar:
|
with tarfile.open(fileobj=buff) as tar:
|
||||||
@ -154,14 +140,10 @@ def perform_installation(instance_name, prism_command, prism_instance_path, pack
|
|||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ftp = FTP("gitea.piwalker.net")
|
|
||||||
ftp.login()
|
|
||||||
ftp.prot_p()
|
|
||||||
ftp.cwd(pack)
|
|
||||||
|
|
||||||
# Fetching versions.json from FTP
|
# Fetching versions.json from FTP
|
||||||
bio = io.BytesIO()
|
bio = io.BytesIO()
|
||||||
ftp.retrbinary("RETR versions.json", bio.write)
|
ftpDownload(pack+"/versions.json", bio)
|
||||||
bio.seek(0)
|
bio.seek(0)
|
||||||
versions = json.load(bio)
|
versions = json.load(bio)
|
||||||
bio.close()
|
bio.close()
|
||||||
@ -182,7 +164,7 @@ def perform_installation(instance_name, prism_command, prism_instance_path, pack
|
|||||||
with tempfile.TemporaryDirectory() as temp_dir:
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
modpack_file_path = os.path.join(temp_dir, instance_name + ".mrpack")
|
modpack_file_path = os.path.join(temp_dir, instance_name + ".mrpack")
|
||||||
with open(modpack_file_path, 'wb') as modpack:
|
with open(modpack_file_path, 'wb') as modpack:
|
||||||
ftpDownload(ftp, versions[-1]["File"], modpack)
|
ftpDownload(pack+"/"+versions[-1]["File"], modpack)
|
||||||
|
|
||||||
# Running PrismLauncher with modpack
|
# Running PrismLauncher with modpack
|
||||||
subprocess.Popen([prism_command, '-I', modpack_file_path])
|
subprocess.Popen([prism_command, '-I', modpack_file_path])
|
||||||
@ -239,16 +221,13 @@ def perform_installation(instance_name, prism_command, prism_instance_path, pack
|
|||||||
|
|
||||||
|
|
||||||
def upload_pack(username, password, version_tag, fileName, pack):
|
def upload_pack(username, password, version_tag, fileName, pack):
|
||||||
ftp = FTP("gitea.piwalker.net", username, password)
|
|
||||||
ftp.prot_p()
|
|
||||||
ftp.cwd(pack)
|
|
||||||
bio = io.BytesIO()
|
bio = io.BytesIO()
|
||||||
ftp.retrbinary("RETR versions.json", bio.write)
|
ftpDownload(pack+"/versions.json", bio)
|
||||||
bio.seek(0)
|
bio.seek(0)
|
||||||
versions = json.load(bio)
|
versions = json.load(bio)
|
||||||
bio.close()
|
bio.close()
|
||||||
time = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
|
time = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
|
||||||
versions.append({"Version":version_tag, "Date":time, "File":"Versions/"+pack+time+".mrpack"})
|
versions.append({"Version":version_tag, "Date":time, "File":"Versions/"+pack+"-"+time+".mrpack"})
|
||||||
with zipfile.ZipFile(fileName, 'r') as zin:
|
with zipfile.ZipFile(fileName, 'r') as zin:
|
||||||
zipbytes = io.BytesIO()
|
zipbytes = io.BytesIO()
|
||||||
zout = zipfile.ZipFile(zipbytes, 'w')
|
zout = zipfile.ZipFile(zipbytes, 'w')
|
||||||
@ -258,128 +237,52 @@ def upload_pack(username, password, version_tag, fileName, pack):
|
|||||||
zout.writestr(item, buffer)
|
zout.writestr(item, buffer)
|
||||||
zout.writestr("overrides/version.txt", version_tag)
|
zout.writestr("overrides/version.txt", version_tag)
|
||||||
zout.close()
|
zout.close()
|
||||||
zipbytes.seek(0)
|
ftpUpload(pack+"/"+versions[-1]["File"], zipbytes, username, password)
|
||||||
ftp.storbinary("STOR "+versions[len(versions)-1]["File"], zipbytes)
|
|
||||||
bio = io.BytesIO()
|
bio = io.BytesIO()
|
||||||
bio.write(json.dumps(versions).encode())
|
bio.write(json.dumps(versions).encode())
|
||||||
bio.seek(0)
|
ftpUpload(pack+"/versions.json", bio, username, password)
|
||||||
ftp.storbinary("STOR versions.json", bio)
|
|
||||||
bio.close()
|
bio.close()
|
||||||
modpackUpdate(pack, ftp)
|
modpackUpdate(pack, username, password)
|
||||||
ftp.close()
|
|
||||||
|
|
||||||
def getModpacks():
|
def getModpacks():
|
||||||
ftp = FTP("gitea.piwalker.net")
|
|
||||||
ftp.login()
|
|
||||||
ftp.prot_p()
|
|
||||||
bio = io.BytesIO()
|
bio = io.BytesIO()
|
||||||
ftp.retrbinary("RETR modpacks.json", bio.write)
|
ftpDownload("modpacks.json", bio)
|
||||||
bio.seek(0)
|
bio.seek(0)
|
||||||
ftp.close()
|
|
||||||
return json.load(bio)
|
return json.load(bio)
|
||||||
|
|
||||||
def uploadModpacks(modpacks, ftp):
|
def uploadModpacks(modpacks, username, password):
|
||||||
ftp.cwd("/ftp");
|
|
||||||
bio = io.BytesIO()
|
bio = io.BytesIO()
|
||||||
bio.write(json.dumps(modpacks).encode())
|
bio.write(json.dumps(modpacks).encode())
|
||||||
bio.seek(0)
|
ftpUpload("modpacks.json", bio, username, password)
|
||||||
ftp.storbinary("STOR modpacks.json", bio)
|
|
||||||
bio.close()
|
bio.close()
|
||||||
|
|
||||||
def modpackUpdate(id, ftp):
|
def modpackUpdate(id, username, password):
|
||||||
modpacks = getModpacks()
|
modpacks = getModpacks()
|
||||||
time = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
|
time = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
|
||||||
for modpack in modpacks:
|
for modpack in modpacks:
|
||||||
if modpack["id"] == id:
|
if modpack["id"] == id:
|
||||||
modpack["last-updated"] = time
|
modpack["last-updated"] = time
|
||||||
uploadModpacks(modpacks, ftp);
|
uploadModpacks(modpacks, username, password);
|
||||||
|
|
||||||
def createModpack(id, name, username, password):
|
def createModpack(id, name, username, password):
|
||||||
modpacks = getModpacks()
|
modpacks = getModpacks()
|
||||||
time = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
|
time = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
|
||||||
modpacks.append({"id":id, "name":name, "last-updated":time})
|
modpacks.append({"id":id, "name":name, "last-updated":time})
|
||||||
ftp = FTP("gitea.piwalker.net", username, password)
|
uploadModpacks(modpacks, username, password)
|
||||||
ftp.prot_p()
|
ftpMakeDirectory(id, username, password)
|
||||||
uploadModpacks(modpacks,ftp)
|
|
||||||
ftp.mkd(id)
|
|
||||||
ftp.cwd(id)
|
|
||||||
versions = []
|
versions = []
|
||||||
bio = io.BytesIO()
|
bio = io.BytesIO()
|
||||||
bio.write(json.dumps(versions).encode())
|
bio.write(json.dumps(versions).encode())
|
||||||
bio.seek(0)
|
ftpUpload(id+"/versions.json", bio, username, password)
|
||||||
ftp.storbinary("STOR versions.json", bio)
|
ftpMakeDirectory(id+"/Versions", username, password)
|
||||||
ftp.mkd("Versions")
|
|
||||||
ftp.close()
|
|
||||||
|
|
||||||
def deleteModpack(username, password, id):
|
def deleteModpack(username, password, id):
|
||||||
ftp = FTP("gitea.piwalker.net", username, password)
|
|
||||||
ftp.prot_p()
|
|
||||||
modpacks = getModpacks()
|
modpacks = getModpacks()
|
||||||
for pack in modpacks:
|
for pack in modpacks:
|
||||||
if pack["id"] == id:
|
if pack["id"] == id:
|
||||||
modpacks.remove(pack)
|
modpacks.remove(pack)
|
||||||
break
|
break
|
||||||
uploadModpacks(modpacks, ftp)
|
uploadModpacks(modpacks, username, password)
|
||||||
deleteFolder(ftp, id)
|
ftpDeleteDirectory(id, username, password)
|
||||||
|
|
||||||
def deleteFolder(ftp, path):
|
|
||||||
print("Deleting folder: "+path)
|
|
||||||
ftp.cwd(path)
|
|
||||||
for item in ftp.nlst():
|
|
||||||
try:
|
|
||||||
print("deleting file: "+path)
|
|
||||||
ftp.delete(item)
|
|
||||||
except:
|
|
||||||
deleteFolder(ftp, item)
|
|
||||||
ftp.cwd("..")
|
|
||||||
ftp.rmd(path)
|
|
||||||
|
|
||||||
def ftpDownload(ftp, file, stream):
|
|
||||||
#create tkinter window
|
|
||||||
print("downloading file: "+file)
|
|
||||||
dialog = CTk()
|
|
||||||
if os.name == 'posix':
|
|
||||||
dialog.attributes('-type', 'dialog')
|
|
||||||
dialog.title("Downloading Modpack")
|
|
||||||
set_appearance_mode("dark")
|
|
||||||
set_default_color_theme("blue")
|
|
||||||
|
|
||||||
global pbar
|
|
||||||
pbar = CTkProgressBar(master=dialog)
|
|
||||||
pbar.pack(padx=20, pady=20)
|
|
||||||
global progress
|
|
||||||
global ETA
|
|
||||||
progress = StringVar()
|
|
||||||
ETA = StringVar()
|
|
||||||
progress_label = CTkLabel(master=dialog, textvariable=progress)
|
|
||||||
progress_label.pack()
|
|
||||||
eta_label = CTkLabel(master=dialog, textvariable=ETA)
|
|
||||||
eta_label.pack()
|
|
||||||
size = ftp.size(file)
|
|
||||||
global total
|
|
||||||
total = 0
|
|
||||||
global start
|
|
||||||
global timer
|
|
||||||
timer = 0
|
|
||||||
start = time.time()
|
|
||||||
dialog.update()
|
|
||||||
|
|
||||||
def downloadCallback(data):
|
|
||||||
global total
|
|
||||||
global pbar
|
|
||||||
global start
|
|
||||||
global progress
|
|
||||||
global ETA
|
|
||||||
global timer
|
|
||||||
stream.write(data)
|
|
||||||
total += len(data)
|
|
||||||
if time.time() - timer >= 1:
|
|
||||||
progress.set(str(round(total/1048576, 1))+" MB / "+str(round(size/1048576, 1))+" MB @ " + str(round((total/1048576)/(time.time()-start), 3))+" MB/s")
|
|
||||||
time_left = (size-total)/(total/(time.time()-start))
|
|
||||||
ETA.set("ETA: " + str(datetime.timedelta(seconds=math.ceil(time_left))))
|
|
||||||
pbar.set(total/size)
|
|
||||||
timer = time.time()
|
|
||||||
dialog.update()
|
|
||||||
ftp.retrbinary("RETR " + file, downloadCallback)
|
|
||||||
dialog.destroy()
|
|
||||||
|
|
||||||
|
18
Upload.py
18
Upload.py
@ -7,15 +7,12 @@ global version
|
|||||||
global modpack
|
global modpack
|
||||||
global modpack_name
|
global modpack_name
|
||||||
global modpack_id
|
global modpack_id
|
||||||
global username
|
|
||||||
global password
|
|
||||||
global modpacks
|
global modpacks
|
||||||
|
|
||||||
def get_credentials():
|
def get_credentials():
|
||||||
global username
|
|
||||||
global password
|
|
||||||
username = simpledialog.askstring("username", "Username: ")
|
username = simpledialog.askstring("username", "Username: ")
|
||||||
password = simpledialog.askstring("password", "Password: ", show="*")
|
password = simpledialog.askstring("password", "Password: ", show="*")
|
||||||
|
return username, password
|
||||||
|
|
||||||
def select_file():
|
def select_file():
|
||||||
global fileName
|
global fileName
|
||||||
@ -28,11 +25,9 @@ def select_file():
|
|||||||
def update_pack():
|
def update_pack():
|
||||||
global modpacks
|
global modpacks
|
||||||
global fileName
|
global fileName
|
||||||
global username
|
|
||||||
global password
|
|
||||||
global modpack
|
global modpack
|
||||||
global version
|
global version
|
||||||
get_credentials()
|
username, password = get_credentials()
|
||||||
pack_id = None
|
pack_id = None
|
||||||
for pack in modpacks:
|
for pack in modpacks:
|
||||||
if pack["name"] == modpack.get():
|
if pack["name"] == modpack.get():
|
||||||
@ -41,8 +36,6 @@ def update_pack():
|
|||||||
messagebox.Message(title="complete", message="Modpack updated successfuly").show()
|
messagebox.Message(title="complete", message="Modpack updated successfuly").show()
|
||||||
|
|
||||||
def create_pack():
|
def create_pack():
|
||||||
global username
|
|
||||||
global password
|
|
||||||
global modpacks
|
global modpacks
|
||||||
global modpack_id
|
global modpack_id
|
||||||
global modpack_name
|
global modpack_name
|
||||||
@ -55,17 +48,15 @@ def create_pack():
|
|||||||
if pack["name"] == modpack_name.get():
|
if pack["name"] == modpack_name.get():
|
||||||
messagebox.Message(title="Error", message="Pack name already exists").show()
|
messagebox.Message(title="Error", message="Pack name already exists").show()
|
||||||
return
|
return
|
||||||
get_credentials()
|
username, password = get_credentials()
|
||||||
Backend.createModpack(modpack_id.get(), modpack_name.get(), username, password)
|
Backend.createModpack(modpack_id.get(), modpack_name.get(), username, password)
|
||||||
Backend.upload_pack(username, password, version.get(), fileName, modpack_id.get())
|
Backend.upload_pack(username, password, version.get(), fileName, modpack_id.get())
|
||||||
messagebox.Message(title="complete", message="Modpack created successfully").show()
|
messagebox.Message(title="complete", message="Modpack created successfully").show()
|
||||||
|
|
||||||
def delete_pack():
|
def delete_pack():
|
||||||
global username
|
|
||||||
global password
|
|
||||||
global modpacks
|
global modpacks
|
||||||
global modpack_delete
|
global modpack_delete
|
||||||
get_credentials()
|
username, password = get_credentials()
|
||||||
pack_id = None
|
pack_id = None
|
||||||
for pack in modpacks:
|
for pack in modpacks:
|
||||||
if pack["name"] == modpack_delete.get():
|
if pack["name"] == modpack_delete.get():
|
||||||
@ -82,7 +73,6 @@ def main():
|
|||||||
global modpack_name
|
global modpack_name
|
||||||
global modpack_delete
|
global modpack_delete
|
||||||
fileName = "modpack.mrpack"
|
fileName = "modpack.mrpack"
|
||||||
|
|
||||||
app = CTk()
|
app = CTk()
|
||||||
if os.name == 'posix':
|
if os.name == 'posix':
|
||||||
app.attributes('-type', 'dialog')
|
app.attributes('-type', 'dialog')
|
||||||
|
Loading…
Reference in New Issue
Block a user