Finished paper support; Created independent paths

Changes:
- Created OS independent paths
- finished paper support
- created many catches for paper handling errors
This commit is contained in:
Neocky
2021-03-16 01:04:13 +01:00
parent 5e296e858f
commit 430dbcd8d2
8 changed files with 132 additions and 65 deletions

View File

View File

@@ -1,6 +1,7 @@
import os
import sys
from urllib.error import HTTPError
from pathlib import Path
from handlers.handle_sftp import createSFTPConnection, sftp_listFilesInServerRoot
from handlers.handle_config import checkConfig
@@ -14,7 +15,6 @@ def checkInstalledServerjar():
serverRootList = sftp_listFilesInServerRoot(sftp)
else:
serverRootList = os.path.dirname(checkConfig().pathToPluginFolder)
os.chdir('..')
serverRootList = os.listdir(serverRootList)
installedServerjarFullName = None
try:
@@ -34,7 +34,7 @@ def checkInstalledServerjar():
print(oColors.brightRed + "Aborting the process." + oColors.standardWhite)
input("Press any key + enter to exit...")
sys.exit()
if 'paper' in installedServerjarFullName:
paperCheckForUpdate(installedServerjarFullName)
@@ -44,16 +44,31 @@ def checkInstalledServerjar():
def updateServerjar(serverJarBuild='latest'):
if serverJarBuild == None:
serverJarBuild = 'latest'
if not checkConfig().localPluginFolder:
sftp = createSFTPConnection()
serverRootList = sftp_listFilesInServerRoot(sftp)
else:
serverRoot = os.path.dirname(checkConfig().pathToPluginFolder)
os.chdir('..')
serverRootList = os.listdir(serverRoot)
installedServerjarFullName = None
try:
if serverJarBuild == None:
serverJarBuild = 'latest'
if not checkConfig().localPluginFolder:
sftp = createSFTPConnection()
serverRootPath = checkConfig().sftp_folderPath
serverRootPath = Path(str(serverRootPath).replace(r'/plugins', ''))
serverRootList = sftp_listFilesInServerRoot(sftp)
else:
serverRoot = os.path.dirname(checkConfig().pathToPluginFolder)
serverRootList = os.listdir(serverRoot)
serverRootPath = checkConfig().pathToPluginFolder
helpPath = Path('/plugins')
helpPathstr = str(helpPath)
serverRootPath = Path(str(serverRootPath).replace(helpPathstr, ''))
installedServerjarFullName = None
except FileNotFoundError:
print(oColors.brightRed + "Path couldn't be found!" + oColors.standardWhite)
print(oColors.brightRed + "Check your config!" + oColors.standardWhite)
print(oColors.brightRed + "Aborting the process." + oColors.standardWhite)
input("Press any key + enter to exit...")
sys.exit()
try:
for files in serverRootList:
try:
@@ -71,13 +86,28 @@ def updateServerjar(serverJarBuild='latest'):
print(oColors.brightRed + "Aborting the process." + oColors.standardWhite)
input("Press any key + enter to exit...")
sys.exit()
serverJarPath = Path(f"{serverRootPath}/{installedServerjarFullName}")
if 'paper' in installedServerjarFullName:
print(serverJarBuild)
try:
papermc_downloader(serverJarBuild, installedServerjarFullName)
except HTTPError as err:
print(f"Updating Paper to build: {serverJarBuild}")
if not checkConfig().localPluginFolder:
try:
papermc_downloader(serverJarBuild, installedServerjarFullName)
sftp.remove(serverJarPath)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
except FileNotFoundError:
print(oColors.brightRed + "Error: Old serverjar file coulnd't be deleted" + oColors.standardWhite)
else:
try:
papermc_downloader(serverJarBuild, installedServerjarFullName)
os.remove(serverJarPath)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
except FileNotFoundError:
print(oColors.brightRed + "Error: Old serverjar file coulnd't be deleted" + oColors.standardWhite)
else:
print(oColors.brightRed + f"{installedServerjarFullName} isn't supported.")

View File

@@ -2,14 +2,13 @@ import os
import sys
import re
import urllib.request
from pathlib import Path
from utils.consoleoutput import oColors
from utils.web_request import doAPIRequest
from handlers.handle_sftp import sftp_upload_server_jar, sftp_cdPluginDir, createSFTPConnection
from handlers.handle_config import checkConfig
from utils.utilities import createTempPluginFolder, deleteTempPluginFolder
from plugin.plugin_downloader import calculateFileSize
from utils.utilities import createTempPluginFolder, deleteTempPluginFolder, calculateFileSizeMb
# = 1.16.5
@@ -50,12 +49,29 @@ def findVersionGroup(mcVersion):
if versionGroup == mcVersion:
versionGroupFound = True
paperVersionGroup = versionGroup
print(versionGroup)
break
return paperVersionGroup
def findBuildVersion(wantedPaperBuild):
versionGroups = ['1.16', '1.15']
paperBuildFound = False
for versionGroup in versionGroups:
if paperBuildFound is True:
break
url = f"https://papermc.io/api/v2/projects/paper/version_group/{versionGroup}/builds"
papermcdetails = doAPIRequest(url)
paperMcBuilds = papermcdetails["builds"]
for build in paperMcBuilds:
paperBuild = str(build["build"])
if paperBuild == wantedPaperBuild:
paperBuildFound = True
paperVersionGroup = build["version"]
break
return paperVersionGroup
def findLatestBuild(paperVersionGroup):
url = f"https://papermc.io/api/v2/projects/paper/version_group/{paperVersionGroup}/builds"
papermcbuilds = doAPIRequest(url)
@@ -63,6 +79,13 @@ def findLatestBuild(paperVersionGroup):
return latestPaperBuild
def findLatestBuildForVersion(mcVersion):
url = f"https://papermc.io/api/v2/projects/paper/versions/{mcVersion}"
papermcbuilds = doAPIRequest(url)
latestPaperBuild = papermcbuilds["builds"][-1]
return latestPaperBuild
def versionBehind(installedPaperBuild, latestPaperBuild):
installedPaperBuildint = int(installedPaperBuild)
latestPaperBuildint = int(latestPaperBuild)
@@ -70,7 +93,6 @@ def versionBehind(installedPaperBuild, latestPaperBuild):
return versionsBehind
def getDownloadFileName(paperMcVersion, paperBuild):
url = f"https://papermc.io/api/v2/projects/paper/versions/{paperMcVersion}/builds/{paperBuild}"
buildDetails = doAPIRequest(url)
@@ -86,13 +108,13 @@ def paperCheckForUpdate(installedServerjarFullName):
paperVersionBehind = versionBehind(paperInstalledBuild, paperLatestBuild)
print(f"Paper for {mcVersion}")
print("Index | Name | Old V. | New V. | Versions behind ")
print("Index | Name | Installed V. | Latest V. | Versions behind ")
print(f" [1]".ljust(8), end='')
print(f"paper".ljust(24), end='')
print(f"paper".ljust(21), end='')
print(f"{paperInstalledBuild}".ljust(8), end='')
print(" ", end='')
print(" ", end='')
print(f"{paperLatestBuild}".ljust(8), end='')
print(" ", end='')
print(" ", end='')
print(f"{paperVersionBehind}".ljust(8))
@@ -102,27 +124,30 @@ def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersi
downloadPath = createTempPluginFolder()
else:
downloadPath = checkConfig().pathToPluginFolder
downloadPath = downloadPath.replace(r'\plugins', '')
helpPath = Path('/plugins')
helpPathstr = str(helpPath)
downloadPath = Path(str(downloadPath).replace(helpPathstr, ''))
if mcVersion == None:
mcVersion = '1.16.5'
if paperBuild == 'latest':
mcVersion = '1.16.5'
else:
mcVersion = findBuildVersion(paperBuild)
if installedServerjarName != None:
mcVersion = getInstalledPaperMinecraftVersion(installedServerjarName)
if paperBuild == 'latest':
versionGroup = findVersionGroup(mcVersion)
paperBuild = findLatestBuild(versionGroup)
paperBuild = findLatestBuildForVersion(mcVersion)
try:
downloadFileName = getDownloadFileName(mcVersion, paperBuild)
except KeyError:
print(oColors.brightRed + f"This version wasn't found for {mcVersion}" + oColors.standardWhite)
print(oColors.brightRed + f"Reverting to latest version for {mcVersion}" + oColors.standardWhite)
versionGroup = findVersionGroup(mcVersion)
paperBuild = findLatestBuild(versionGroup)
paperBuild = findLatestBuildForVersion(mcVersion)
downloadFileName = getDownloadFileName(mcVersion, paperBuild)
downloadPackagePath = f"{downloadPath}\\{downloadFileName}"
downloadPackagePath = Path(f"{downloadPath}/{downloadFileName}")
if checkConfig().localPluginFolder == False:
downloadPath = createTempPluginFolder()
@@ -130,13 +155,16 @@ def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersi
url = f"https://papermc.io/api/v2/projects/paper/versions/{mcVersion}/builds/{paperBuild}/downloads/{downloadFileName}"
remotefile = urllib.request.urlopen(url)
filesize = remotefile.info()['Content-Length']
print(f"Starting paper-{paperBuild} download for {mcVersion}...")
print(f"Starting Paper-{paperBuild} download for {mcVersion}...")
urllib.request.urlretrieve(url, downloadPackagePath)
filesizeData = calculateFileSize(filesize)
filesizeData = calculateFileSizeMb(filesize)
print(f"Downloadsize: {filesizeData} KB")
print(f"Downloadsize: {filesizeData} MB")
print(f"File downloaded here: {downloadPackagePath}")
if not checkConfig().localPluginFolder:
sftpSession = createSFTPConnection()
sftp_upload_server_jar(sftpSession, downloadPackagePath)
deleteTempPluginFolder(downloadPath)
print(oColors.brightGreen + "Downloaded successfully " + oColors.standardWhite + f"Paper-{paperBuild}" + \
oColors.brightGreen + " for " + oColors.standardWhite + f"{mcVersion}" + oColors.standardWhite)