diff --git a/src/handlers/handle_config.py b/src/handlers/handle_config.py index 94e318a..54baf55 100644 --- a/src/handlers/handle_config.py +++ b/src/handlers/handle_config.py @@ -6,52 +6,49 @@ from pathlib import Path 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']) + + 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'] + + self.sftp_port = int(sftp_port) + if localPluginFolder == 'True': + self.localPluginFolder = True + else: + self.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 + + def checkConfig(): - currentFolder = os.getcwd() configAvailable = os.path.isfile("config.ini") - if not configAvailable: createConfig() 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 - def createConfig(): config = configparser.ConfigParser(allow_no_value=True) 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 382438f..15c6f6c 100644 --- a/src/plugin/plugin_downloader.py +++ b/src/plugin/plugin_downloader.py @@ -5,18 +5,11 @@ from pathlib import Path from utils.consoleoutput import oColors from utils.web_request import doAPIRequest -from utils.utilities import createTempPluginFolder, deleteTempPluginFolder -from handlers.handle_config import checkConfig +from utils.utilities import createTempPluginFolder, deleteTempPluginFolder, calculateFileSizeKb, calculateFileSizeMb +from handlers.handle_config import configurationValues from handlers.handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection -def calculateFileSize(downloadFileSize): - fileSizeDownload = int(downloadFileSize) - fileSizeKb = fileSizeDownload / 1024 - roundedFileSize = round(fileSizeKb, 2) - return roundedFileSize - - def handleRegexPackageName(packageNameFull): packageNameFull2 = packageNameFull # trims the part of the package that has for example "[1.1 Off]" in it @@ -58,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 @@ -78,19 +72,20 @@ 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) def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'): + configValues = configurationValues() if versionID != 'latest': #url = f"https://spigotmc.org/resources/{ressourceId}/download?version={versionID}" print(oColors.brightRed + "Sorry but specific version downloads aren't supported because of cloudflare protection. :(" + oColors.standardWhite) @@ -102,16 +97,22 @@ def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'): remotefile = urllib.request.urlopen(url) filesize = remotefile.info()['Content-Length'] urllib.request.urlretrieve(url, downloadPath) - filesizeData = calculateFileSize(filesize) - print(f"Downloadsize: {filesizeData} KB") - print(f"File downloaded here: {downloadPath}") - if not checkConfig().localPluginFolder: + filesize = int(filesize) + print(" ", end='') + if filesize >= 1000000: + filesizeData = calculateFileSizeMb(filesize) + print("Downloaded " + (str(filesizeData)).rjust(9) + f" MB here {downloadPath}") + else: + filesizeData = calculateFileSizeKb(filesize) + print("Downloaded " + (str(filesizeData)).rjust(9) + f" KB here {downloadPath}") + if not configValues.localPluginFolder: sftpSession = createSFTPConnection() sftp_upload_file(sftpSession, downloadPath) 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) @@ -121,13 +122,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 c775dc1..df2eb6e 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 +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: @@ -86,20 +87,21 @@ def eggCrackingJar(localJarFileName): def checkInstalledPackage(inputSelectedObject="all"): + 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 - oldPackages = 0 + oldPlugins = 0 print(oColors.brightBlack + f"Checking: {inputSelectedObject}" + oColors.standardWhite) - print("┌─────┬────────────────────────────────┬──────────────┬───────────┬───────────────────┐") - print("│ No. │ Name │ Installed V. │ Latest V. │ Update available │") - print("└─────┴────────────────────────────────┴──────────────┴───────────┴───────────────────┘") + print("┌─────┬────────────────────────────────┬──────────────┬──────────────┬───────────────────┐") + print("│ No. │ Name │ Installed V. │ Latest V. │ Update available │") + print("└─────┴────────────────────────────────┴──────────────┴──────────────┴───────────────────┘") try: - for plugin in track(pluginList, description="Checking for updates" ,transient=True, complete_style="cyan"): + for plugin in track(pluginList, description="Checking for updates" ,transient=True, complete_style="bright_yellow"): try: fileName = getFileName(plugin) fileVersion = getFileVersion(plugin) @@ -127,42 +129,45 @@ def checkInstalledPackage(inputSelectedObject="all"): pluginIsOutdated = 'N/A' if pluginIsOutdated == True: - oldPackages = oldPackages + 1 + oldPlugins = oldPlugins + 1 if inputSelectedObject != "*" and inputSelectedObject != "all": if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): if pluginLatestVersion == 'N/A': - print(oColors.brightBlack + f" [{1}]".ljust(8), end='') + print(oColors.brightBlack + f" [{1}]".rjust(6), end='') else: - print(f" [{1}]".ljust(8), end='') + print(f" [{1}]".rjust(6), end='') + print(" ", end='') print(f"{fileName}".ljust(33), end='') print(f"{fileVersion}".ljust(15), end='') - print(f"{pluginLatestVersion}".ljust(12), 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': - print(oColors.brightBlack + f" [{i+1}]".ljust(8), end='') + print(oColors.brightBlack + f" [{i+1}]".rjust(6), end='') else: - print(f" [{i+1}]".ljust(8), end='') + print(f" [{i+1}]".rjust(6), end='') + print(" ", end='') print(f"{fileName}".ljust(33), end='') print(f"{fileVersion}".ljust(15), end='') - print(f"{pluginLatestVersion}".ljust(12), 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: print(oColors.brightRed + "Error occured: Aborted checking for updates." + oColors.standardWhite) - print(oColors.brightYellow + f"Old packages: [{oldPackages}/{i}]" + oColors.standardWhite) + print(oColors.brightYellow + f"Outdated plugins: [{oldPlugins}/{i}]" + oColors.standardWhite) def updateInstalledPackage(inputSelectedObject='all'): + 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 pluginsUpdated = 0 indexNumberUpdated = 0 @@ -171,7 +176,7 @@ def updateInstalledPackage(inputSelectedObject='all'): print("│ No. │ Name │ Old V. │ New V. │") print("└─────┴────────────────────────────────┴────────────┴──────────┘") try: - for plugin in track(pluginList, description="Updating" ,transient=True, complete_style="red"): + for plugin in track(pluginList, description="Updating" ,transient=True, complete_style="bright_magenta"): try: fileName = getFileName(plugin) fileVersion = getFileVersion(plugin) @@ -189,24 +194,25 @@ def updateInstalledPackage(inputSelectedObject='all'): continue if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): if INSTALLEDPLUGINLIST[i][3] == True: - print(f" [{indexNumberUpdated+1}]".ljust(8), end='') + print(f" [{indexNumberUpdated+1}]".rjust(6), end='') + print(" ", end='') print(f"{fileName}".ljust(33), end='') - print(f"{fileVersion}".ljust(8), end='') - print(" ", end='') - print(f"{latestVersion}".ljust(8)) + print(f"{fileVersion}".ljust(13), end='') + #print(" ", end='') + print(f"{latestVersion}".ljust(13)) - 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 pluginPath = Path(f"{pluginPath}/{plugin}") sftp = createSFTPConnection() indexNumberUpdated += 1 pluginsUpdated += 1 try: - getSpecificPackage(pluginId, checkConfig().sftp_folderPath) - if checkConfig().sftp_seperateDownloadPath is False: + getSpecificPackage(pluginId, configValues.sftp_folderPath) + if configValues.sftp_seperateDownloadPath is False: sftp.remove(pluginPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) @@ -214,16 +220,16 @@ def updateInstalledPackage(inputSelectedObject='all'): except FileNotFoundError: print(oColors.brightRed + "Error: Old plugin file coulnd't be deleted" + oColors.standardWhite) else: - if checkConfig().seperateDownloadPath is True: - pluginPath = checkConfig().pathToSeperateDownloadPath + if configValues.seperateDownloadPath is True: + pluginPath = configValues.pathToSeperateDownloadPath else: - pluginPath = checkConfig().pathToPluginFolder + pluginPath = configValues.pathToPluginFolder pluginPath = Path(f"{pluginPath}/{plugin}") indexNumberUpdated += 1 pluginsUpdated += 1 try: - getSpecificPackage(pluginId, checkConfig().pathToPluginFolder) - if checkConfig().seperateDownloadPath is False: + getSpecificPackage(pluginId, configValues.pathToPluginFolder) + if configValues.seperateDownloadPath is False: os.remove(pluginPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) @@ -238,24 +244,25 @@ def updateInstalledPackage(inputSelectedObject='all'): if inputSelectedObject == 'all': if INSTALLEDPLUGINLIST[i][3] == True: - print(f" [{indexNumberUpdated+1}]".ljust(8), end='') + print(f" [{indexNumberUpdated+1}]".rjust(6), end='') + print(" ", end='') print(f"{fileName}".ljust(33), end='') - print(f"{fileVersion}".ljust(8), end='') - print(" ", end='') - print(f"{latestVersion}".ljust(8)) + print(f"{fileVersion}".ljust(13), end='') + #print(" ", end='') + print(f"{latestVersion}".ljust(13)) - 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 pluginPath = f"{pluginPath}/{plugin}" sftp = createSFTPConnection() indexNumberUpdated += 1 pluginsUpdated += 1 try: - getSpecificPackage(pluginId, checkConfig().sftp_folderPath) - if checkConfig().sftp_seperateDownloadPath is False: + getSpecificPackage(pluginId, configValues.sftp_folderPath) + if configValues.sftp_seperateDownloadPath is False: sftp.remove(pluginPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) @@ -264,16 +271,16 @@ def updateInstalledPackage(inputSelectedObject='all'): print(oColors.brightRed + f"Error: Old plugin file coulnd't be deleted" + oColors.standardWhite) else: - if checkConfig().seperateDownloadPath is True: - pluginPath = checkConfig().pathToSeperateDownloadPath + if configValues.seperateDownloadPath is True: + pluginPath = configValues.pathToSeperateDownloadPath else: - pluginPath = checkConfig().pathToPluginFolder + pluginPath = configValues.pathToPluginFolder pluginPath = Path(f"{pluginPath}/{plugin}") indexNumberUpdated += 1 pluginsUpdated += 1 try: - getSpecificPackage(pluginId, checkConfig().pathToPluginFolder) - if checkConfig().seperateDownloadPath is False: + getSpecificPackage(pluginId, configValues.pathToPluginFolder) + if configValues.seperateDownloadPath is False: os.remove(pluginPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) @@ -284,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..7e702b0 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, '')) @@ -90,8 +92,8 @@ def updateServerjar(serverJarBuild='latest'): serverJarPath = Path(f"{serverRootPath}/{installedServerjarFullName}") if 'paper' in installedServerjarFullName: - print(f"Updating Paper to build: {serverJarBuild}") - if not checkConfig().localPluginFolder: + print(oColors.brightBlack + f"Updating Paper to build: {serverJarBuild}" + oColors.standardWhite) + 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..f46f56a 100644 --- a/src/serverjar/serverjar_paper.py +++ b/src/serverjar/serverjar_paper.py @@ -3,11 +3,12 @@ import sys import re import urllib.request from pathlib import Path +from rich.console import Console 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 +108,25 @@ 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)) + print(oColors.brightYellow + f"Versions behind: [{paperVersionBehind}]" + oColors.standardWhite) # 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,22 +152,22 @@ 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}" remotefile = urllib.request.urlopen(url) filesize = remotefile.info()['Content-Length'] - print(f"Starting Paper-{paperBuild} download for {mcVersion}...") - urllib.request.urlretrieve(url, downloadPackagePath) + print(f"Getting Paper {paperBuild} for {mcVersion}") + console = Console() + with console.status("Downloading...", spinner='line', spinner_style='bright_magenta') as status: + urllib.request.urlretrieve(url, downloadPackagePath) filesizeData = calculateFileSizeMb(filesize) - - print(f"Downloadsize: {filesizeData} MB") - print(f"File downloaded here: {downloadPackagePath}") - if not checkConfig().localPluginFolder: + print("Downloaded " + (str(filesizeData)).rjust(9) + f" MB here {downloadPackagePath}") + if not configValues.localPluginFolder: sftpSession = createSFTPConnection() sftp_upload_server_jar(sftpSession, downloadPackagePath) deleteTempPluginFolder(downloadPath) - print(oColors.brightGreen + "Downloaded successfully " + oColors.standardWhite + f"Paper-{paperBuild}" + \ + 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 a1a4ce8..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 +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() @@ -160,3 +162,9 @@ def calculateFileSizeMb(downloadFileSize): fileSizeMb = fileSizeDownload / 1024 / 1024 roundedFileSize = round(fileSizeMb, 2) return roundedFileSize + +def calculateFileSizeKb(downloadFileSize): + fileSizeDownload = int(downloadFileSize) + fileSizeKb = fileSizeDownload / 1024 + roundedFileSize = round(fileSizeKb, 2) + return roundedFileSize \ No newline at end of file