From 20261d110e656d7b45f4db04c8d29aaf8a8919e3 Mon Sep 17 00:00:00 2001 From: Neocky Date: Mon, 15 Mar 2021 01:20:18 +0100 Subject: [PATCH 1/5] Added catch for HTTPError and added papermc ground work Changes: - Added HTTPError catch when searching for a plugin - Created first ground work for papermc server jar support --- src/plugin/plugin_downloader.py | 10 ++++++++-- src/serverjar/serverjar_paper.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/serverjar/serverjar_paper.py diff --git a/src/plugin/plugin_downloader.py b/src/plugin/plugin_downloader.py index 423c1ee..98bb0c7 100644 --- a/src/plugin/plugin_downloader.py +++ b/src/plugin/plugin_downloader.py @@ -78,9 +78,15 @@ def searchPackage(ressourceName): ressourceSelected = ressourceSelected - 1 ressourceId = packageName[ressourceSelected]["id"] if not checkConfig().localPluginFolder: - getSpecificPackage(ressourceId, checkConfig().sftp_folderPath) + try: + getSpecificPackage(ressourceId, checkConfig().sftp_folderPath) + except HTTPError as err: + print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) else: - getSpecificPackage(ressourceId, checkConfig().pathToPluginFolder) + try: + getSpecificPackage(ressourceId, checkConfig().pathToPluginFolder) + except HTTPError as err: + print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'): diff --git a/src/serverjar/serverjar_paper.py b/src/serverjar/serverjar_paper.py new file mode 100644 index 0000000..dc56407 --- /dev/null +++ b/src/serverjar/serverjar_paper.py @@ -0,0 +1,32 @@ +import sys + +from utils.consoleoutput import oColors +from utils.web_request import doAPIRequest +from handlers.handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection + +def getInstalledPaperMinecraftVersion(localPaperName): + print("test") + +def getInstalledPaperVersion(localPaperName): + print("test") + +# https://papermc.io/api/docs/swagger-ui/index.html?configUrl=/api/openapi/swagger-config#/ +def papermc_downloader(paperBuild, paperVersionGroup='1.16'): + url = f"https://papermc.io/api/v2/projects/paper/{serverVersion}/{inputPackageVersion}" # v1 is deprecated + #https://papermc.io/api/v2/projects/paper/version_group/1.16/builds get all builds for all 1.16 versions + #https://papermc.io/api/v2/projects/paper/versions/1.16.5/builds/450 gets file name + # input serverversion = 1.16.5 inputpackageversion = 450 + # regex 1.16 % build url + # search for 450 in version group 1.16 + # if found get file name from builds/450 + # build url & download package + # https://papermc.io/api/v2/projects/paper/versions/1.16.5/builds/450/downloads/paper-1.16.5-450.jar + + + papermcdetails = doAPIRequest(url) + errorExists = papermcdetails["error"] + if errorExists in papermcdetails: + print(f"PaperMc version: {inputPackageVersion} couldn't be found") + print("Aborting the download of PaperMc.") + input("Press any key + enter to exit...") + sys.exit() From 5e296e858fd0cf1f97c3ba217ff5fdf36a5026cb Mon Sep 17 00:00:00 2001 From: Neocky Date: Mon, 15 Mar 2021 16:26:00 +0100 Subject: [PATCH 2/5] Added papermc support & fixing some issues Changes: - adding papermc support - paper can be checked for an update - can be updated to latest or specific version - download specific paper version for different minecraft versions --- src/handlers/handle_input.py | 19 +++- src/handlers/handle_sftp.py | 27 +++++- src/serverjar/serverjar_checker.py | 85 ++++++++++++++++ src/serverjar/serverjar_paper.py | 150 +++++++++++++++++++++++++---- src/utils/utilities.py | 2 +- 5 files changed, 258 insertions(+), 25 deletions(-) create mode 100644 src/serverjar/serverjar_checker.py diff --git a/src/handlers/handle_input.py b/src/handlers/handle_input.py index 1fb3cda..8cd6983 100644 --- a/src/handlers/handle_input.py +++ b/src/handlers/handle_input.py @@ -6,6 +6,9 @@ from handlers.handle_config import checkConfig from plugin.plugin_downloader import searchPackage, getSpecificPackage from plugin.plugin_updatechecker import updateInstalledPackage, checkInstalledPackage from plugin.plugin_remover import removePlugin +from serverjar.serverjar_checker import checkInstalledServerjar, updateServerjar +from serverjar.serverjar_paper import papermc_downloader + def createInputLists(): global COMMANDLIST @@ -16,7 +19,8 @@ def createInputLists(): 'search', 'exit', 'help', - 'remove' + 'remove', + 'get-paper' ] global INPUTSELECTEDOBJECT INPUTSELECTEDOBJECT = [ @@ -47,10 +51,16 @@ def handleInput(inputCommand, inputSelectedObject, inputParams): searchPackage(inputSelectedObject) break if inputCommand == 'update': - updateInstalledPackage(inputSelectedObject) + if inputSelectedObject == 'serverjar': + updateServerjar(inputParams) + else: + updateInstalledPackage(inputSelectedObject) break if inputCommand == 'check': - checkInstalledPackage(inputSelectedObject) + if inputSelectedObject == 'serverjar': + checkInstalledServerjar() + else: + checkInstalledPackage(inputSelectedObject) break if inputCommand == 'search': searchPackage(inputSelectedObject) @@ -63,6 +73,9 @@ def handleInput(inputCommand, inputSelectedObject, inputParams): if inputCommand == 'remove': removePlugin(inputSelectedObject) break + if inputCommand == 'get-paper': + papermc_downloader(inputSelectedObject, inputParams) + break else: print(oColors.brightRed + "Command not found. Please try again." + oColors.standardWhite) getInput() diff --git a/src/handlers/handle_sftp.py b/src/handlers/handle_sftp.py index 8049959..aeebe07 100644 --- a/src/handlers/handle_sftp.py +++ b/src/handlers/handle_sftp.py @@ -45,6 +45,17 @@ def sftp_upload_file(sftp, itemPath): sftp.close() +def sftp_upload_server_jar(sftp, itemPath): + try: + sftp.chdir('.') + sftp.put(itemPath) + + except FileNotFoundError: + print(oColors.brightRed + "The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) + print(oColors.brightRed + "Aborting installation." + oColors.standardWhite) + sftp.close() + + def sftp_listAll(sftp): try: sftp.chdir('plugins') @@ -56,4 +67,18 @@ def sftp_listAll(sftp): try: return installedPlugins except UnboundLocalError: - print(oColors.brightRed + "No plugins were found." + oColors.standardWhite) \ No newline at end of file + print(oColors.brightRed + "No plugins were found." + oColors.standardWhite) + + +def sftp_listFilesInServerRoot(sftp): + try: + #sftp.chdir('plugins') + filesInServerRoot = sftp.listdir() + + except FileNotFoundError: + print(oColors.brightRed + "The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite) + + try: + return filesInServerRoot + except UnboundLocalError: + print(oColors.brightRed + "No Serverjar was found." + oColors.standardWhite) \ No newline at end of file diff --git a/src/serverjar/serverjar_checker.py b/src/serverjar/serverjar_checker.py new file mode 100644 index 0000000..cd153f1 --- /dev/null +++ b/src/serverjar/serverjar_checker.py @@ -0,0 +1,85 @@ +import os +import sys +from urllib.error import HTTPError + +from handlers.handle_sftp import createSFTPConnection, sftp_listFilesInServerRoot +from handlers.handle_config import checkConfig +from utils.consoleoutput import oColors +from serverjar.serverjar_paper import paperCheckForUpdate, papermc_downloader + + +def checkInstalledServerjar(): + if not checkConfig().localPluginFolder: + sftp = createSFTPConnection() + serverRootList = sftp_listFilesInServerRoot(sftp) + else: + serverRootList = os.path.dirname(checkConfig().pathToPluginFolder) + os.chdir('..') + serverRootList = os.listdir(serverRootList) + installedServerjarFullName = None + try: + for files in serverRootList: + try: + if '.jar' in files: + installedServerjarFullName = files + break + except TypeError: + continue + except TypeError: + print(oColors.brightRed + "Serverjar couldn't be found." + oColors.standardWhite) + print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) + + if installedServerjarFullName == None: + print(oColors.brightRed + "Serverjar couldn't be found." + oColors.standardWhite) + print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) + input("Press any key + enter to exit...") + sys.exit() + + if 'paper' in installedServerjarFullName: + paperCheckForUpdate(installedServerjarFullName) + + else: + print(oColors.brightRed + f"{installedServerjarFullName} isn't supported.") + print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) + + +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: + for files in serverRootList: + try: + if '.jar' in files: + installedServerjarFullName = files + break + except TypeError: + continue + except TypeError: + print(oColors.brightRed + "Serverjar couldn't be found." + oColors.standardWhite) + print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) + + if installedServerjarFullName == None: + print(oColors.brightRed + "Serverjar couldn't be found." + oColors.standardWhite) + print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) + input("Press any key + enter to exit...") + sys.exit() + + if 'paper' in installedServerjarFullName: + print(serverJarBuild) + try: + papermc_downloader(serverJarBuild, installedServerjarFullName) + except HTTPError as err: + print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) + + else: + print(oColors.brightRed + f"{installedServerjarFullName} isn't supported.") + print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) + \ No newline at end of file diff --git a/src/serverjar/serverjar_paper.py b/src/serverjar/serverjar_paper.py index dc56407..d170958 100644 --- a/src/serverjar/serverjar_paper.py +++ b/src/serverjar/serverjar_paper.py @@ -1,32 +1,142 @@ +import os import sys +import re +import urllib.request from utils.consoleoutput import oColors from utils.web_request import doAPIRequest -from handlers.handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection +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 + + +# = 1.16.5 def getInstalledPaperMinecraftVersion(localPaperName): - print("test") + mcVersionFull = re.search(r'(\d*\.*\d)+', localPaperName) + try: + mcVersion = mcVersionFull.group() + except AttributeError: + mcVersion = mcVersionFull + return mcVersion + +# = 550 def getInstalledPaperVersion(localPaperName): - print("test") + paperBuildFull = re.search(r'([\d]*.jar)', localPaperName) + try: + paperBuild = paperBuildFull.group() + except AttributeError: + paperBuild = paperBuildFull + paperBuild = paperBuild.replace('.jar', '') + return paperBuild + + +def findVersionGroup(mcVersion): + versionGroups = ['1.16', '1.15'] + versionGroupFound = False + for versionGroup in versionGroups: + if versionGroupFound == True: + break + url = f"https://papermc.io/api/v2/projects/paper/version_group/{versionGroup}/builds" + papermcdetails = doAPIRequest(url) + papermcVersionForMc = papermcdetails["versions"] + for versions in papermcVersionForMc: + if versions == mcVersion: + versionGroupFound = True + paperVersionGroup = versionGroup + break + if versionGroup == mcVersion: + versionGroupFound = True + paperVersionGroup = versionGroup + print(versionGroup) + break + + return paperVersionGroup + + +def findLatestBuild(paperVersionGroup): + url = f"https://papermc.io/api/v2/projects/paper/version_group/{paperVersionGroup}/builds" + papermcbuilds = doAPIRequest(url) + latestPaperBuild = papermcbuilds["builds"][-1]["build"] + return latestPaperBuild + + +def versionBehind(installedPaperBuild, latestPaperBuild): + installedPaperBuildint = int(installedPaperBuild) + latestPaperBuildint = int(latestPaperBuild) + versionsBehind = latestPaperBuildint - installedPaperBuildint + return versionsBehind + + + +def getDownloadFileName(paperMcVersion, paperBuild): + url = f"https://papermc.io/api/v2/projects/paper/versions/{paperMcVersion}/builds/{paperBuild}" + buildDetails = doAPIRequest(url) + downloadName = buildDetails["downloads"]["application"]["name"] + return downloadName + + +def paperCheckForUpdate(installedServerjarFullName): + mcVersion = getInstalledPaperMinecraftVersion(installedServerjarFullName) + paperInstalledBuild = getInstalledPaperVersion(installedServerjarFullName) + versionGroup = findVersionGroup(mcVersion) + paperLatestBuild = findLatestBuild(versionGroup) + paperVersionBehind = versionBehind(paperInstalledBuild, paperLatestBuild) + + print(f"Paper for {mcVersion}") + print("Index | Name | Old V. | New V. | Versions behind ") + print(f" [1]".ljust(8), end='') + print(f"paper".ljust(24), end='') + print(f"{paperInstalledBuild}".ljust(8), end='') + print(" ", end='') + print(f"{paperLatestBuild}".ljust(8), end='') + print(" ", end='') + print(f"{paperVersionBehind}".ljust(8)) + # https://papermc.io/api/docs/swagger-ui/index.html?configUrl=/api/openapi/swagger-config#/ -def papermc_downloader(paperBuild, paperVersionGroup='1.16'): - url = f"https://papermc.io/api/v2/projects/paper/{serverVersion}/{inputPackageVersion}" # v1 is deprecated - #https://papermc.io/api/v2/projects/paper/version_group/1.16/builds get all builds for all 1.16 versions - #https://papermc.io/api/v2/projects/paper/versions/1.16.5/builds/450 gets file name - # input serverversion = 1.16.5 inputpackageversion = 450 - # regex 1.16 % build url - # search for 450 in version group 1.16 - # if found get file name from builds/450 - # build url & download package - # https://papermc.io/api/v2/projects/paper/versions/1.16.5/builds/450/downloads/paper-1.16.5-450.jar +def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersion=None): + if checkConfig().localPluginFolder == False: + downloadPath = createTempPluginFolder() + else: + downloadPath = checkConfig().pathToPluginFolder + downloadPath = downloadPath.replace(r'\plugins', '') + if mcVersion == None: + mcVersion = '1.16.5' + + if installedServerjarName != None: + mcVersion = getInstalledPaperMinecraftVersion(installedServerjarName) - papermcdetails = doAPIRequest(url) - errorExists = papermcdetails["error"] - if errorExists in papermcdetails: - print(f"PaperMc version: {inputPackageVersion} couldn't be found") - print("Aborting the download of PaperMc.") - input("Press any key + enter to exit...") - sys.exit() + if paperBuild == 'latest': + versionGroup = findVersionGroup(mcVersion) + paperBuild = findLatestBuild(versionGroup) + 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) + downloadFileName = getDownloadFileName(mcVersion, paperBuild) + + downloadPackagePath = f"{downloadPath}\\{downloadFileName}" + + if checkConfig().localPluginFolder == False: + downloadPath = createTempPluginFolder() + + 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}...") + urllib.request.urlretrieve(url, downloadPackagePath) + filesizeData = calculateFileSize(filesize) + + print(f"Downloadsize: {filesizeData} KB") + print(f"File downloaded here: {downloadPackagePath}") + if not checkConfig().localPluginFolder: + sftpSession = createSFTPConnection() + sftp_upload_server_jar(sftpSession, downloadPackagePath) + deleteTempPluginFolder(downloadPath) diff --git a/src/utils/utilities.py b/src/utils/utilities.py index d6b503a..5190bb2 100644 --- a/src/utils/utilities.py +++ b/src/utils/utilities.py @@ -59,7 +59,7 @@ def check_requirements(): def createTempPluginFolder(): - tempPluginFolder = ".\\plugins" + tempPluginFolder = ".\\TempSFTPUploadFolder" if not os.path.isdir(tempPluginFolder): try: os.mkdir(tempPluginFolder) From 430dbcd8d2bfa5a6ee73a038d1986c9c3701bfc2 Mon Sep 17 00:00:00 2001 From: Neocky Date: Tue, 16 Mar 2021 01:04:13 +0100 Subject: [PATCH 3/5] 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 From 76e1d7f5fdeaaeea6d9fa6e97be8029e57c02b90 Mon Sep 17 00:00:00 2001 From: Neocky Date: Tue, 16 Mar 2021 21:10:09 +0100 Subject: [PATCH 4/5] Fixed array was not created Changes: - fixed array would not be created for a plugin when plugin would throw a type- or valueerror - deleted testing prints - minor code cleanup --- src/handlers/handle_input.py | 2 +- src/plugin/plugin_updatechecker.py | 22 ++++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/handlers/handle_input.py b/src/handlers/handle_input.py index 8cd6983..310d1b3 100644 --- a/src/handlers/handle_input.py +++ b/src/handlers/handle_input.py @@ -7,7 +7,7 @@ from plugin.plugin_downloader import searchPackage, getSpecificPackage from plugin.plugin_updatechecker import updateInstalledPackage, checkInstalledPackage from plugin.plugin_remover import removePlugin from serverjar.serverjar_checker import checkInstalledServerjar, updateServerjar -from serverjar.serverjar_paper import papermc_downloader +from serverjar.serverjar_paper import papermc_downloader def createInputLists(): diff --git a/src/plugin/plugin_updatechecker.py b/src/plugin/plugin_updatechecker.py index bb98ef6..b13af0c 100644 --- a/src/plugin/plugin_updatechecker.py +++ b/src/plugin/plugin_updatechecker.py @@ -74,6 +74,7 @@ def checkInstalledPackage(inputSelectedObject="all"): fileVersion = getFileVersion(plugin) pluginId = getInstalledPlugin(fileName, fileVersion) except TypeError: + i += 1 continue pluginIdStr = str(pluginId) @@ -84,7 +85,7 @@ def checkInstalledPackage(inputSelectedObject="all"): pluginLatestVersion = INSTALLEDPLUGINLIST[i][2] except IndexError: pluginLatestVersion = 'N/A' - + if pluginLatestVersion == None: pluginLatestVersion = 'N/A' @@ -114,7 +115,7 @@ def checkInstalledPackage(inputSelectedObject="all"): print(f"{pluginLatestVersion}".ljust(12), end='') print(f" {pluginIsOutdated}".ljust(5)) - i = i + 1 + i += 1 except TypeError: print(oColors.brightRed + "Aborted checking for plugins." + oColors.standardWhite) print(oColors.brightYellow + f"Old packages: [{oldPackages}/{i}]" + oColors.standardWhite) @@ -139,18 +140,18 @@ def updateInstalledPackage(inputSelectedObject='all'): fileVersion = getFileVersion(plugin) pluginId = getInstalledPlugin(fileName, fileVersion) latestVersion = getLatestPluginVersion(pluginId) - print(fileName) - print(fileVersion) except TypeError: + i += 1 continue except ValueError: + i += 1 continue pluginIdStr = str(pluginId) 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='') @@ -257,12 +258,10 @@ 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 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" @@ -276,19 +275,14 @@ 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) - print("in if") - print(plugin_match_found) - return pluginID # just testing - #break + return pluginID - else: # TODO fix duplicate entrys when update all + else: 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 From 01b9a52b403c7055382fcc6b49d392476edd28c8 Mon Sep 17 00:00:00 2001 From: Neocky Date: Tue, 16 Mar 2021 21:45:37 +0100 Subject: [PATCH 5/5] Added server software parts and new usage parts --- README.md | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 995be3f..33ebb01 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@

Apache-2.0 - downloads + downloads Hits @@ -19,7 +19,7 @@ # pluGET -A powerfull package manager which handles [Plugins](https://www.spigotmc.org/resources/) for minecraft servers. +A powerfull package manager which handles [Plugins](https://www.spigotmc.org/resources/) and Server Software for minecraft servers. ## Issues? Found a bug? @@ -31,6 +31,7 @@ This is a package manager for minecraft [Spigot](https://www.spigotmc.org/) serv This is a standalone program written in python. The program works with a locally installed server or with a remote host through SFTP, when configured in the config. It uses the [Spiget](https://spiget.org/) API to download and compare plugin versions and can download the latest version of plugins from the [Spigot](https://www.spigotmc.org/) site. +It can also compare and download the latest update of specifif server software (e.g. [PaperMC](https://papermc.io/)). Plugin management was the hard part of mangaging a minecraft server. The time it took to check the [Spigot ressource](https://www.spigotmc.org/resources/) page for updates for the installed plugins and updating all plugins manually which have available updates was too long and shocking. So I built pluGET to automate and ease the plugin handling of a minecraft server and to turn the most time consuming part of managing a minecraft server to an easy one. @@ -45,10 +46,16 @@ If you still have questions [here](https://github.com/Neocky/pluGET#need-help) i So what can it do exactly? pluGET can: - work locally or through sftp -- download the latest version of a plugin -- update every installed/one specific plugin -- check for an update of every installed/one specific plugin -- remove a plugin from the plugin folder +- manage plugins: + - download the latest version of a plugin + - update every installed/one specific plugin + - check for an update of every installed/one specific plugin + - remove a plugin from the plugin folder +- manager server software: + - download a specific server software version + - check instlalled server software for update + - update installed server software to a specific version + - supported server software: [PaperMc](https://papermc.io/) There are more features in the work. Check [Projects](https://github.com/Neocky/pluGET/projects) for a complete list. @@ -85,10 +92,13 @@ Edit the config to your needs and relaunch pluGET. ## Usage +> As always, if you update plugins, shut down your server! + Execute the `launcher.bat` in the `\pluGET` folder. This will launch pluGET correctly. Another way is to launch the `src\__main__.py` file. The following are examples of input for the general usage: -(Hint: 'all' can always be exchanged through the plugin name or the plugin id and reverse) +(Hint: [thingsInBrackets] are optional & 'all' can always be exchanged through the plugin name or the plugin id and reverse) +### Manage Plugins #### Download the latest update of a specific package: `get [pluginID/pluginName]` ``` @@ -140,6 +150,26 @@ exit . ``` help . ``` +### Manage Server Software +#### Check installed server software for updates: +`check serverjar` +``` +check serverjar +``` +### Update installed server software to latest/specific version: +`update serverjar [Version]` +``` +update serverjar 'PaperMCVersion' +``` +### Download specific paper version: +`get-paper [paperBuild] [minecraftVersion]` +``` +get-paper 550 1.16.5 +``` +or: +``` +get-paper 321 +``` ## Known problems ### Can't get installed version