From fddc0c252c8572a6e5dccad7f442eea48a769222 Mon Sep 17 00:00:00 2001 From: Neocky Date: Sat, 27 Mar 2021 12:07:19 +0100 Subject: [PATCH] Better config handling & better output for serverjar Changes: - better config handling - better output for serverjar checking --- src/__main__.py | 6 +- src/handlers/handle_config.py | 95 +++++++++--------------------- src/handlers/handle_input.py | 17 +++--- src/plugin/plugin_downloader.py | 16 ++--- src/plugin/plugin_remover.py | 13 ++-- src/plugin/plugin_updatechecker.py | 19 +++--- src/serverjar/serverjar_checker.py | 20 ++++--- src/serverjar/serverjar_paper.py | 27 +++++---- src/utils/utilities.py | 14 +++-- 9 files changed, 100 insertions(+), 127 deletions(-) diff --git a/src/__main__.py b/src/__main__.py index 8abf093..77a4580 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -1,14 +1,14 @@ from utils.consoleoutput import consoleTitle, clearConsole, printMainMenu from utils.utilities import check_requirements from handlers.handle_input import createInputLists, getInput -from handlers.handle_config import checkConfig, configurationValues +from handlers.handle_config import checkConfig def mainFunction(): consoleTitle() clearConsole() - #checkConfig() - #check_requirements() + checkConfig() + check_requirements() createInputLists() printMainMenu() getInput() diff --git a/src/handlers/handle_config.py b/src/handlers/handle_config.py index f048683..16ed848 100644 --- a/src/handlers/handle_config.py +++ b/src/handlers/handle_config.py @@ -7,43 +7,41 @@ from utils.consoleoutput import oColors class configurationValues: - def __init__(self): - config = configparser.ConfigParser() - config.sections() - config.read("config.ini") - localPluginFolder = config['General']['LocalPluginFolder'] - self.pathToPluginFolder = Path(config['General']['PathToPluginFolder']) - seperateDownloadPath = config['General']['SeperateDownloadPath'] - self.pathToSeperateDownloadPath = Path(config['General']['PathToSeperateDownloadPath']) + config = configparser.ConfigParser() + config.sections() + config.read("config.ini") + localPluginFolder = config['General']['LocalPluginFolder'] + pathToPluginFolder = Path(config['General']['PathToPluginFolder']) + seperateDownloadPath = config['General']['SeperateDownloadPath'] + pathToSeperateDownloadPath = Path(config['General']['PathToSeperateDownloadPath']) - self.sftp_server = config['SFTP - Remote Server']['Server'] - self.sftp_user = config['SFTP - Remote Server']['Username'] - self.sftp_password = config['SFTP - Remote Server']['Password'] - sftp_port = config['SFTP - Remote Server']['Port'] - self.sftp_folderPath = config['SFTP - Remote Server']['PluginFolderOnServer'] - sftp_seperateDownloadPath = config['SFTP - Remote Server']['SeperateDownloadPath'] - self.sftp_pathToSeperateDownloadPath = config['SFTP - Remote Server']['PathToSeperateDownloadPath'] + sftp_server = config['SFTP - Remote Server']['Server'] + sftp_user = config['SFTP - Remote Server']['Username'] + sftp_password = config['SFTP - Remote Server']['Password'] + sftp_port = config['SFTP - Remote Server']['Port'] + sftp_folderPath = config['SFTP - Remote Server']['PluginFolderOnServer'] + sftp_seperateDownloadPath = config['SFTP - Remote Server']['SeperateDownloadPath'] + sftp_pathToSeperateDownloadPath = config['SFTP - Remote Server']['PathToSeperateDownloadPath'] - self.sftp_port = int(sftp_port) - if localPluginFolder == 'True': - self.localPluginFolder = True - else: - self.localPluginFolder = False + sftp_port = int(sftp_port) + if localPluginFolder == 'True': + localPluginFolder = True + else: + localPluginFolder = False - if seperateDownloadPath == 'True': - self.seperateDownloadPath = True - else: - self.seperateDownloadPath = False - - if sftp_seperateDownloadPath == 'True': - self.sftp_seperateDownloadPath = True - else: - self.sftp_seperateDownloadPath = False + if seperateDownloadPath == 'True': + seperateDownloadPath = True + else: + seperateDownloadPath = False + if sftp_seperateDownloadPath == 'True': + sftp_seperateDownloadPath = True + else: + sftp_seperateDownloadPath = False def checkConfig(): - currentFolder = os.getcwd() + #currentFolder = os.getcwd() configAvailable = os.path.isfile("config.ini") if not configAvailable: @@ -51,42 +49,7 @@ def checkConfig(): print(oColors.brightRed + "Config created. Edit config before executing again!" + oColors.standardWhite) input("Press any key + enter to exit...") sys.exit() - - #class configValues: - # config = configparser.ConfigParser() - # config.sections() - # config.read("config.ini") - # localPluginFolder = config['General']['LocalPluginFolder'] - # pathToPluginFolder = Path(config['General']['PathToPluginFolder']) - # seperateDownloadPath = config['General']['SeperateDownloadPath'] - # pathToSeperateDownloadPath = Path(config['General']['PathToSeperateDownloadPath']) - - # sftp_server = config['SFTP - Remote Server']['Server'] - # sftp_user = config['SFTP - Remote Server']['Username'] - # sftp_password = config['SFTP - Remote Server']['Password'] - # sftp_port = config['SFTP - Remote Server']['Port'] - # sftp_folderPath = config['SFTP - Remote Server']['PluginFolderOnServer'] - # sftp_seperateDownloadPath = config['SFTP - Remote Server']['SeperateDownloadPath'] - # sftp_pathToSeperateDownloadPath = config['SFTP - Remote Server']['PathToSeperateDownloadPath'] - - # sftp_port = int(sftp_port) - # if localPluginFolder == 'True': - # localPluginFolder = True - # else: - # localPluginFolder = False - - # if seperateDownloadPath == 'True': - # seperateDownloadPath = True - # else: - # seperateDownloadPath = False - - # if sftp_seperateDownloadPath == 'True': - # sftp_seperateDownloadPath = True - # else: - # sftp_seperateDownloadPath = False - - os.chdir(currentFolder) - #return configValues + #os.chdir(currentFolder) def createConfig(): diff --git a/src/handlers/handle_input.py b/src/handlers/handle_input.py index 8a418de..4108b1e 100644 --- a/src/handlers/handle_input.py +++ b/src/handlers/handle_input.py @@ -2,7 +2,7 @@ import sys from utils.consoleoutput import oColors from utils.utilities import getHelp, getCommandHelp -from handlers.handle_config import checkConfig +from handlers.handle_config import configurationValues from plugin.plugin_downloader import searchPackage, getSpecificPackage from plugin.plugin_updatechecker import updateInstalledPackage, checkInstalledPackage from plugin.plugin_remover import removePlugin @@ -30,21 +30,22 @@ def createInputLists(): def handleInput(inputCommand, inputSelectedObject, inputParams): + configValues = configurationValues() while True: if inputCommand == 'get': if inputSelectedObject.isdigit(): - if not checkConfig().localPluginFolder: - if checkConfig().sftp_seperateDownloadPath is True: - pluginPath = checkConfig().sftp_pathToSeperateDownloadPath + if not configValues.localPluginFolder: + if configValues.sftp_seperateDownloadPath is True: + pluginPath = configValues.sftp_pathToSeperateDownloadPath else: - pluginPath = checkConfig().sftp_folderPath + pluginPath = configValues.sftp_folderPath getSpecificPackage(inputSelectedObject, pluginPath, inputParams) break else: - if checkConfig().seperateDownloadPath is True: - pluginPath = checkConfig().pathToSeperateDownloadPath + if configValues.seperateDownloadPath is True: + pluginPath = configValues.pathToSeperateDownloadPath else: - pluginPath = checkConfig().pathToPluginFolder + pluginPath = configValues.pathToPluginFolder getSpecificPackage(inputSelectedObject, pluginPath, inputParams) break else: diff --git a/src/plugin/plugin_downloader.py b/src/plugin/plugin_downloader.py index a0497c9..e28a4eb 100644 --- a/src/plugin/plugin_downloader.py +++ b/src/plugin/plugin_downloader.py @@ -6,7 +6,7 @@ from pathlib import Path from utils.consoleoutput import oColors from utils.web_request import doAPIRequest from utils.utilities import createTempPluginFolder, deleteTempPluginFolder, calculateFileSizeKb, calculateFileSizeMb -from handlers.handle_config import checkConfig, configurationValues +from handlers.handle_config import configurationValues from handlers.handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection @@ -51,6 +51,7 @@ def getVersionName(packageId, versionId): def searchPackage(ressourceName): + configValues = configurationValues() url = f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name&sort=-downloads" packageName = doAPIRequest(url) i = 1 @@ -71,14 +72,14 @@ def searchPackage(ressourceName): if ressourceSelected != 0: ressourceSelected = ressourceSelected - 1 ressourceId = packageName[ressourceSelected]["id"] - if not checkConfig().localPluginFolder: + if not configValues.localPluginFolder: try: - getSpecificPackage(ressourceId, checkConfig().sftp_folderPath) + getSpecificPackage(ressourceId, configValues.sftp_folderPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) else: try: - getSpecificPackage(ressourceId, checkConfig().pathToPluginFolder) + getSpecificPackage(ressourceId, configValues.pathToPluginFolder) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) @@ -109,7 +110,8 @@ def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'): def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'): - if checkConfig().localPluginFolder == False: + configValues = configurationValues() + if configValues.localPluginFolder == False: downloadPath = createTempPluginFolder() url = f"https://api.spiget.org/v2/resources/{ressourceId}" packageDetails = doAPIRequest(url) @@ -119,13 +121,13 @@ def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'): packageVersion = getVersionName(ressourceId, versionId) packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" downloadPackagePath = Path(f"{downloadPath}/{packageDownloadName}") - if checkConfig().localPluginFolder: + if configValues.localPluginFolder: if inputPackageVersion is None or inputPackageVersion == 'latest': downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) else: downloadSpecificVersion(ressourceId, downloadPackagePath, versionId) - if not checkConfig().localPluginFolder: + if not configValues.localPluginFolder: if inputPackageVersion is None or inputPackageVersion == 'latest': downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) deleteTempPluginFolder(downloadPath) diff --git a/src/plugin/plugin_remover.py b/src/plugin/plugin_remover.py index b9019f8..1367822 100644 --- a/src/plugin/plugin_remover.py +++ b/src/plugin/plugin_remover.py @@ -3,18 +3,19 @@ import re from pathlib import Path from utils.consoleoutput import oColors -from handlers.handle_config import checkConfig +from handlers.handle_config import configurationValues from handlers.handle_sftp import createSFTPConnection, sftp_listAll from plugin.plugin_updatechecker import getFileName, getFileVersion, getInstalledPlugin, createPluginList def removePlugin(pluginToRemove): + configValues = configurationValues() createPluginList() - if not checkConfig().localPluginFolder: + if not configValues.localPluginFolder: sftp = createSFTPConnection() pluginList = sftp_listAll(sftp) else: - pluginList = os.listdir(checkConfig().pathToPluginFolder) + pluginList = os.listdir(configValues.pathToPluginFolder) i = 0 try: for plugin in pluginList: @@ -28,8 +29,8 @@ def removePlugin(pluginToRemove): if pluginToRemove == pluginIdStr or re.search(pluginToRemove, fileName, re.IGNORECASE): print(f"Removing: {fileName}") - if not checkConfig().localPluginFolder: - pluginPath = checkConfig().sftp_folderPath + if not configValues.localPluginFolder: + pluginPath = configValues.sftp_folderPath pluginPath = f"{pluginPath}/{plugin}" sftp = createSFTPConnection() sftp.remove(pluginPath) @@ -37,7 +38,7 @@ def removePlugin(pluginToRemove): i += 1 break else: - pluginPath = checkConfig().pathToPluginFolder + pluginPath = configValues.pathToPluginFolder pluginPath = Path(f"{pluginPath}/{plugin}") os.remove(pluginPath) print(f"Removed: {fileName}") diff --git a/src/plugin/plugin_updatechecker.py b/src/plugin/plugin_updatechecker.py index 240e6f0..a02e949 100644 --- a/src/plugin/plugin_updatechecker.py +++ b/src/plugin/plugin_updatechecker.py @@ -8,7 +8,7 @@ from rich.progress import track from utils.consoleoutput import oColors from utils.web_request import doAPIRequest -from handlers.handle_config import checkConfig, configurationValues +from handlers.handle_config import configurationValues from handlers.handle_sftp import createSFTPConnection, sftp_listAll from plugin.plugin_downloader import getSpecificPackage @@ -64,10 +64,11 @@ def compareVersions(plugin_latest_version, pluginVersion): def eggCrackingJar(localJarFileName): - if not checkConfig().localPluginFolder: - pluginPath = checkConfig().sftp_folderPath + configValues = configurationValues() + if not configValues.localPluginFolder: + pluginPath = configValues.sftp_folderPath else: - pluginPath = checkConfig().pathToPluginFolder + pluginPath = configValues.pathToPluginFolder pathToPluginJar = Path(f"{pluginPath}/{localJarFileName}") pluginVersion = '' with ZipFile(pathToPluginJar, 'r') as pluginJar: @@ -139,8 +140,8 @@ def checkInstalledPackage(inputSelectedObject="all"): print(" ", end='') print(f"{fileName}".ljust(33), end='') print(f"{fileVersion}".ljust(15), end='') - print(f"{pluginLatestVersion}".ljust(14), end='') - print(f" {pluginIsOutdated}".ljust(5) + oColors.standardWhite) + print(f"{pluginLatestVersion}".ljust(15), end='') + print(f"{pluginIsOutdated}".ljust(5) + oColors.standardWhite) break else: if pluginLatestVersion == 'N/A': @@ -150,8 +151,8 @@ def checkInstalledPackage(inputSelectedObject="all"): print(" ", end='') print(f"{fileName}".ljust(33), end='') print(f"{fileVersion}".ljust(15), end='') - print(f"{pluginLatestVersion}".ljust(14), end='') - print(f" {pluginIsOutdated}".ljust(5) + oColors.standardWhite) + print(f"{pluginLatestVersion}".ljust(15), end='') + print(f"{pluginIsOutdated}".ljust(5) + oColors.standardWhite) i += 1 except TypeError: @@ -290,7 +291,7 @@ def updateInstalledPackage(inputSelectedObject='all'): i = i + 1 except TypeError: print(oColors.brightRed + "Error occured: Aborted updating for plugins." + oColors.standardWhite) - print(oColors.brightYellow + f"[{pluginsUpdated}/{i}] Plugins updated" + oColors.standardWhite) + print(oColors.brightYellow + f"Plugins updated: [{pluginsUpdated}/{i}]" + oColors.standardWhite) if inputSelectedObject =='all' and pluginsUpdated == 0: print(oColors.brightGreen + "All found plugins are on the latest version!" + oColors.standardWhite) diff --git a/src/serverjar/serverjar_checker.py b/src/serverjar/serverjar_checker.py index d018600..0f6ab86 100644 --- a/src/serverjar/serverjar_checker.py +++ b/src/serverjar/serverjar_checker.py @@ -4,17 +4,18 @@ from urllib.error import HTTPError from pathlib import Path from handlers.handle_sftp import createSFTPConnection, sftp_listFilesInServerRoot -from handlers.handle_config import checkConfig +from handlers.handle_config import configurationValues from utils.consoleoutput import oColors from serverjar.serverjar_paper import paperCheckForUpdate, papermc_downloader def checkInstalledServerjar(): - if not checkConfig().localPluginFolder: + configValues = configurationValues() + if not configValues.localPluginFolder: sftp = createSFTPConnection() serverRootList = sftp_listFilesInServerRoot(sftp) else: - serverRootList = os.path.dirname(checkConfig().pathToPluginFolder) + serverRootList = os.path.dirname(configValues.pathToPluginFolder) serverRootList = os.listdir(serverRootList) installedServerjarFullName = None try: @@ -34,7 +35,7 @@ def checkInstalledServerjar(): print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) input("Press any key + enter to exit...") sys.exit() - + print(oColors.brightBlack + f"Checking: {installedServerjarFullName}" + oColors.standardWhite) if 'paper' in installedServerjarFullName: paperCheckForUpdate(installedServerjarFullName) @@ -44,19 +45,20 @@ def checkInstalledServerjar(): def updateServerjar(serverJarBuild='latest'): + configValues = configurationValues() try: if serverJarBuild == None: serverJarBuild = 'latest' - if not checkConfig().localPluginFolder: + if not configValues.localPluginFolder: sftp = createSFTPConnection() - serverRootPath = checkConfig().sftp_folderPath + serverRootPath = configValues.sftp_folderPath serverRootPath = Path(str(serverRootPath).replace(r'/plugins', '')) serverRootList = sftp_listFilesInServerRoot(sftp) else: - serverRoot = os.path.dirname(checkConfig().pathToPluginFolder) + serverRoot = os.path.dirname(configValues.pathToPluginFolder) serverRootList = os.listdir(serverRoot) - serverRootPath = checkConfig().pathToPluginFolder + serverRootPath = configValues.pathToPluginFolder helpPath = Path('/plugins') helpPathstr = str(helpPath) serverRootPath = Path(str(serverRootPath).replace(helpPathstr, '')) @@ -91,7 +93,7 @@ def updateServerjar(serverJarBuild='latest'): if 'paper' in installedServerjarFullName: print(f"Updating Paper to build: {serverJarBuild}") - if not checkConfig().localPluginFolder: + if not configValues.localPluginFolder: try: papermc_downloader(serverJarBuild, installedServerjarFullName) sftp.remove(serverJarPath) diff --git a/src/serverjar/serverjar_paper.py b/src/serverjar/serverjar_paper.py index 2b43734..67dbde3 100644 --- a/src/serverjar/serverjar_paper.py +++ b/src/serverjar/serverjar_paper.py @@ -7,7 +7,7 @@ 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 handlers.handle_config import configurationValues from utils.utilities import createTempPluginFolder, deleteTempPluginFolder, calculateFileSizeMb @@ -107,23 +107,24 @@ def paperCheckForUpdate(installedServerjarFullName): paperLatestBuild = findLatestBuild(versionGroup) paperVersionBehind = versionBehind(paperInstalledBuild, paperLatestBuild) - print(f"Paper for {mcVersion}") - print("Index | Name | Installed V. | Latest V. | Versions behind ") - print(f" [1]".ljust(8), end='') - print(f"paper".ljust(21), end='') - print(f"{paperInstalledBuild}".ljust(8), end='') - print(" ", end='') - print(f"{paperLatestBuild}".ljust(8), end='') - print(" ", end='') + print("┌─────┬────────────────────────────────┬──────────────┬──────────────┬───────────────────┐") + print("│ No. │ Name │ Installed V. │ Latest V. │ Versions behind │") + print("└─────┴────────────────────────────────┴──────────────┴──────────────┴───────────────────┘") + print(" [1]".rjust(6), end='') + print(" ", end='') + print("paper".ljust(33), end='') + print(f"{paperInstalledBuild}".ljust(15), end='') + print(f"{paperLatestBuild}".ljust(15), end='') print(f"{paperVersionBehind}".ljust(8)) # https://papermc.io/api/docs/swagger-ui/index.html?configUrl=/api/openapi/swagger-config#/ def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersion=None): - if checkConfig().localPluginFolder == False: + configValues = configurationValues() + if configValues.localPluginFolder == False: downloadPath = createTempPluginFolder() else: - downloadPath = checkConfig().pathToPluginFolder + downloadPath = configValues.pathToPluginFolder helpPath = Path('/plugins') helpPathstr = str(helpPath) downloadPath = Path(str(downloadPath).replace(helpPathstr, '')) @@ -149,7 +150,7 @@ def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersi downloadPackagePath = Path(f"{downloadPath}/{downloadFileName}") - if checkConfig().localPluginFolder == False: + if configValues.localPluginFolder == False: downloadPath = createTempPluginFolder() url = f"https://papermc.io/api/v2/projects/paper/versions/{mcVersion}/builds/{paperBuild}/downloads/{downloadFileName}" @@ -161,7 +162,7 @@ def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersi print(f"Downloadsize: {filesizeData} MB") print(f"File downloaded here: {downloadPackagePath}") - if not checkConfig().localPluginFolder: + if not configValues.localPluginFolder: sftpSession = createSFTPConnection() sftp_upload_server_jar(sftpSession, downloadPackagePath) deleteTempPluginFolder(downloadPath) diff --git a/src/utils/utilities.py b/src/utils/utilities.py index a65f91c..4dcf20b 100644 --- a/src/utils/utilities.py +++ b/src/utils/utilities.py @@ -6,7 +6,7 @@ import requests from pathlib import Path from utils.consoleoutput import oColors -from handlers.handle_config import checkConfig, configurationValues +from handlers.handle_config import configurationValues from handlers.handle_sftp import createSFTPConnection @@ -95,11 +95,12 @@ def getCommandHelp(optionalParams): def check_local_plugin_folder(): - if checkConfig().localPluginFolder: - if checkConfig().seperateDownloadPath: - pluginFolderPath = checkConfig().pathToSeperateDownloadPath + configValues = configurationValues() + if configValues.localPluginFolder: + if configValues.seperateDownloadPath: + pluginFolderPath = configValues.pathToSeperateDownloadPath else: - pluginFolderPath = checkConfig().pathToPluginFolder + pluginFolderPath = configValues.pathToPluginFolder if not os.path.isdir(pluginFolderPath): print(oColors.brightRed + "Plugin folder coulnd*t be found. Creating one..." + oColors.standardWhite) @@ -129,9 +130,10 @@ def apiTest(): def check_requirements(): + configValues = configurationValues() apiTest() check_local_plugin_folder() - if not checkConfig().localPluginFolder: + if not configValues.localPluginFolder: createSFTPConnection()