From 430dbcd8d2bfa5a6ee73a038d1986c9c3701bfc2 Mon Sep 17 00:00:00 2001 From: Neocky Date: Tue, 16 Mar 2021 01:04:13 +0100 Subject: [PATCH] Finished paper support; Created independent paths Changes: - Created OS independent paths - finished paper support - created many catches for paper handling errors --- src/handlers/handle_config.py | 8 ++-- src/plugin/plugin_downloader.py | 3 +- src/plugin/plugin_remover.py | 5 ++- src/plugin/plugin_updatechecker.py | 39 +++++++++-------- src/serverjar/__init__.py | 0 src/serverjar/serverjar_checker.py | 64 ++++++++++++++++++++-------- src/serverjar/serverjar_paper.py | 68 +++++++++++++++++++++--------- src/utils/utilities.py | 10 ++++- 8 files changed, 132 insertions(+), 65 deletions(-) create mode 100644 src/serverjar/__init__.py diff --git a/src/handlers/handle_config.py b/src/handlers/handle_config.py index 5c90a7d..3a12f26 100644 --- a/src/handlers/handle_config.py +++ b/src/handlers/handle_config.py @@ -1,13 +1,13 @@ import os import sys import configparser +from pathlib import Path from utils.consoleoutput import oColors def checkConfig(): currentFolder = os.getcwd() - os.chdir('..') configAvailable = os.path.isfile("config.ini") if not configAvailable: @@ -21,9 +21,9 @@ def checkConfig(): config.sections() config.read("config.ini") localPluginFolder = config['General']['LocalPluginFolder'] - pathToPluginFolder = config['General']['PathToPluginFolder'] + pathToPluginFolder = Path(config['General']['PathToPluginFolder']) seperateDownloadPath = config['General']['SeperateDownloadPath'] - pathToSeperateDownloadPath = config['General']['PathToSeperateDownloadPath'] + pathToSeperateDownloadPath = Path(config['General']['PathToSeperateDownloadPath']) sftp_server = config['SFTP - Remote Server']['Server'] sftp_user = config['SFTP - Remote Server']['Username'] @@ -75,5 +75,5 @@ def createConfig(): config['SFTP - Remote Server']['PathToSeperateDownloadPath'] = '.\\plugins' - with open('./config.ini', 'w') as configfile: + with open('config.ini', 'w') as configfile: config.write(configfile) diff --git a/src/plugin/plugin_downloader.py b/src/plugin/plugin_downloader.py index 98bb0c7..203b8e9 100644 --- a/src/plugin/plugin_downloader.py +++ b/src/plugin/plugin_downloader.py @@ -1,6 +1,7 @@ import re import urllib.request from urllib.error import HTTPError +from pathlib import Path from utils.consoleoutput import oColors from utils.web_request import doAPIRequest @@ -119,7 +120,7 @@ def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'): versionId = getVersionID(ressourceId, inputPackageVersion) packageVersion = getVersionName(ressourceId, versionId) packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" - downloadPackagePath = f"{downloadPath}\\{packageDownloadName}" + downloadPackagePath = Path(f"{downloadPath}/{packageDownloadName}") if checkConfig().localPluginFolder: if inputPackageVersion is None or inputPackageVersion == 'latest': downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) diff --git a/src/plugin/plugin_remover.py b/src/plugin/plugin_remover.py index bd569ac..b9019f8 100644 --- a/src/plugin/plugin_remover.py +++ b/src/plugin/plugin_remover.py @@ -1,5 +1,6 @@ import os import re +from pathlib import Path from utils.consoleoutput import oColors from handlers.handle_config import checkConfig @@ -29,7 +30,7 @@ def removePlugin(pluginToRemove): print(f"Removing: {fileName}") if not checkConfig().localPluginFolder: pluginPath = checkConfig().sftp_folderPath - pluginPath = f"{pluginPath}\\{plugin}" + pluginPath = f"{pluginPath}/{plugin}" sftp = createSFTPConnection() sftp.remove(pluginPath) print(f"Removed: {fileName}") @@ -37,7 +38,7 @@ def removePlugin(pluginToRemove): break else: pluginPath = checkConfig().pathToPluginFolder - pluginPath = f"{pluginPath}\\{plugin}" + pluginPath = Path(f"{pluginPath}/{plugin}") os.remove(pluginPath) print(f"Removed: {fileName}") i += 1 diff --git a/src/plugin/plugin_updatechecker.py b/src/plugin/plugin_updatechecker.py index 8237c73..bb98ef6 100644 --- a/src/plugin/plugin_updatechecker.py +++ b/src/plugin/plugin_updatechecker.py @@ -1,6 +1,7 @@ import os import re from urllib.error import HTTPError +from pathlib import Path from utils.consoleoutput import oColors from utils.web_request import doAPIRequest @@ -97,7 +98,7 @@ def checkInstalledPackage(inputSelectedObject="all"): if pluginIsOutdated == True: oldPackages = oldPackages + 1 - + if inputSelectedObject != "*" and inputSelectedObject != "all": if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): print(f" [{1}]".ljust(8), end='') @@ -138,6 +139,8 @@ def updateInstalledPackage(inputSelectedObject='all'): fileVersion = getFileVersion(plugin) pluginId = getInstalledPlugin(fileName, fileVersion) latestVersion = getLatestPluginVersion(pluginId) + print(fileName) + print(fileVersion) except TypeError: continue except ValueError: @@ -147,7 +150,7 @@ def updateInstalledPackage(inputSelectedObject='all'): if pluginId == None: print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite) continue - + print(INSTALLEDPLUGINLIST[i]) if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): if INSTALLEDPLUGINLIST[i][3] == True: print(f" [{indexNumberUpdated+1}]".ljust(8), end='') @@ -161,7 +164,7 @@ def updateInstalledPackage(inputSelectedObject='all'): pluginPath = checkConfig().sftp_pathToSeperateDownloadPath else: pluginPath = checkConfig().sftp_folderPath - pluginPath = f"{pluginPath}\\{plugin}" + pluginPath = Path(f"{pluginPath}/{plugin}") sftp = createSFTPConnection() indexNumberUpdated += 1 pluginsUpdated += 1 @@ -173,13 +176,13 @@ def updateInstalledPackage(inputSelectedObject='all'): print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) pluginsUpdated -= 1 except FileNotFoundError: - print(oColors.brightRed + f"Error: Old plugin file coulnd't be deleted" + oColors.standardWhite) + print(oColors.brightRed + "Error: Old plugin file coulnd't be deleted" + oColors.standardWhite) else: if checkConfig().seperateDownloadPath is True: pluginPath = checkConfig().pathToSeperateDownloadPath else: pluginPath = checkConfig().pathToPluginFolder - pluginPath = f"{pluginPath}\\{plugin}" + pluginPath = Path(f"{pluginPath}/{plugin}") indexNumberUpdated += 1 pluginsUpdated += 1 try: @@ -211,7 +214,7 @@ def updateInstalledPackage(inputSelectedObject='all'): else: pluginPath = checkConfig().sftp_folderPath pluginPath = checkConfig().sftp_folderPath - pluginPath = f"{pluginPath}\\{plugin}" + pluginPath = f"{pluginPath}/{plugin}" sftp = createSFTPConnection() indexNumberUpdated += 1 pluginsUpdated += 1 @@ -230,7 +233,7 @@ def updateInstalledPackage(inputSelectedObject='all'): pluginPath = checkConfig().pathToSeperateDownloadPath else: pluginPath = checkConfig().pathToPluginFolder - pluginPath = f"{pluginPath}\\{plugin}" + pluginPath = Path(f"{pluginPath}/{plugin}") indexNumberUpdated += 1 pluginsUpdated += 1 try: @@ -254,13 +257,13 @@ def updateInstalledPackage(inputSelectedObject='all'): def getInstalledPlugin(localFileName, localFileVersion): url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name&sort=-downloads" packageName = doAPIRequest(url) - i = 1 + #i = 1 plugin_match_found = False pluginID = None for ressource in packageName: if plugin_match_found == True: + plugin_match_found = False break - pID = ressource["id"] url2 = f"https://api.spiget.org/v2/resources/{pID}/versions?size=100&sort=-name" packageVersions = doAPIRequest(url2) @@ -273,23 +276,19 @@ def getInstalledPlugin(localFileName, localFileVersion): plugin_latest_version = getLatestPluginVersion(pID) plugin_is_outdated = compareVersions(plugin_latest_version, updateVersion) addToPluginList(pID, updateId, plugin_latest_version , plugin_is_outdated) - break + print("in if") + print(plugin_match_found) + return pluginID # just testing + #break - i = i + 1 - else: + else: # TODO fix duplicate entrys when update all if plugin_match_found != True: pID = None updateId = None plugin_latest_version = None plugin_is_outdated = None addToPluginList(pID, updateId, plugin_latest_version , plugin_is_outdated) + print("in else") + print(plugin_match_found) return pluginID - - - # start query - # get id - # search with id for all version upates - # get version that matches installed version - # if match then download latest update - # else get second query diff --git a/src/serverjar/__init__.py b/src/serverjar/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/serverjar/serverjar_checker.py b/src/serverjar/serverjar_checker.py index cd153f1..d018600 100644 --- a/src/serverjar/serverjar_checker.py +++ b/src/serverjar/serverjar_checker.py @@ -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.") diff --git a/src/serverjar/serverjar_paper.py b/src/serverjar/serverjar_paper.py index d170958..2b43734 100644 --- a/src/serverjar/serverjar_paper.py +++ b/src/serverjar/serverjar_paper.py @@ -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) diff --git a/src/utils/utilities.py b/src/utils/utilities.py index 5190bb2..c3296a2 100644 --- a/src/utils/utilities.py +++ b/src/utils/utilities.py @@ -3,6 +3,7 @@ import os import sys import shutil import requests +from pathlib import Path from utils.consoleoutput import oColors from handlers.handle_config import checkConfig @@ -59,7 +60,7 @@ def check_requirements(): def createTempPluginFolder(): - tempPluginFolder = ".\\TempSFTPUploadFolder" + tempPluginFolder = Path("./TempSFTPUploadFolder") if not os.path.isdir(tempPluginFolder): try: os.mkdir(tempPluginFolder) @@ -76,3 +77,10 @@ def deleteTempPluginFolder(tempPluginFolder): shutil.rmtree(tempPluginFolder) except OSError as e: print ("Error: %s - %s." % (e.filename, e.strerror)) + + +def calculateFileSizeMb(downloadFileSize): + fileSizeDownload = int(downloadFileSize) + fileSizeMb = fileSizeDownload / 1024 / 1024 + roundedFileSize = round(fileSizeMb, 2) + return roundedFileSize