From 927767b0aaa2b67f9ecdf67b67fe5ed5d5c0ed56 Mon Sep 17 00:00:00 2001 From: Neocky Date: Tue, 30 Mar 2021 23:55:04 +0200 Subject: [PATCH] Added FTP support Changes: - added ftp support - added ftp to every function which had already sftp interaction - edited config - added handle_ftp.py fixed old configcheck() bugs - added sftp/ftp to eggcrackingjar mechanism - added new line input with enter --- src/__main__.py | 1 - src/handlers/handle_config.py | 43 ++++++++----- src/handlers/handle_ftp.py | 100 +++++++++++++++++++++++++++++ src/handlers/handle_input.py | 12 +++- src/handlers/handle_sftp.py | 32 +++++---- src/plugin/plugin_downloader.py | 30 +++++---- src/plugin/plugin_remover.py | 17 +++-- src/plugin/plugin_updatechecker.py | 96 ++++++++++++++++++--------- src/serverjar/serverjar_checker.py | 9 ++- src/serverjar/serverjar_paper.py | 12 +++- src/utils/utilities.py | 13 ++-- 11 files changed, 278 insertions(+), 87 deletions(-) create mode 100644 src/handlers/handle_ftp.py diff --git a/src/__main__.py b/src/__main__.py index 77a4580..e6b9f08 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -12,5 +12,4 @@ def mainFunction(): createInputLists() printMainMenu() getInput() - mainFunction() diff --git a/src/handlers/handle_config.py b/src/handlers/handle_config.py index 54baf55..d84b462 100644 --- a/src/handlers/handle_config.py +++ b/src/handlers/handle_config.py @@ -11,16 +11,17 @@ class configurationValues: 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']) + localPluginFolder = config['General']['UseLocalPluginFolder'] + self.pathToPluginFolder = Path(config['Local - This Machine']['PathToPluginFolder']) + seperateDownloadPath = config['Local - This Machine']['SeperateDownloadPath'] + self.pathToSeperateDownloadPath = Path(config['Local - This Machine']['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'] + sftp_port = config['SFTP - Remote Server']['SFTPPort'] self.sftp_folderPath = config['SFTP - Remote Server']['PluginFolderOnServer'] + sftp_useSftp = config['SFTP - Remote Server']['USE_SFTP'] sftp_seperateDownloadPath = config['SFTP - Remote Server']['SeperateDownloadPath'] self.sftp_pathToSeperateDownloadPath = config['SFTP - Remote Server']['PathToSeperateDownloadPath'] @@ -40,6 +41,11 @@ class configurationValues: else: self.sftp_seperateDownloadPath = False + if sftp_useSftp == 'True': + self.sftp_useSftp = True + else: + self.sftp_useSftp = False + def checkConfig(): configAvailable = os.path.isfile("config.ini") @@ -53,23 +59,28 @@ def checkConfig(): def createConfig(): config = configparser.ConfigParser(allow_no_value=True) config['General'] = {} - config['General'][';'] = 'If a local plugin folder exists (True/False): (If False SFTP will be used)' - config['General']['LocalPluginFolder'] = 'True' - config['General']['PathToPluginFolder'] = 'C:/Users/USER/Desktop/plugins' - config['General'][';_'] = 'If you want a different folder to store the updated plugins change to (True/False) and the path below' - config['General']['SeperateDownloadPath'] = 'False' - config['General']['PathToSeperateDownloadPath'] = 'C:/Users/USER/Desktop/plugins' + config['General'][';'] = 'If a local plugin folder exists (True/False) (If False SFTP/FTP will be used):' + config['General']['UseLocalPluginFolder'] = 'True' + + config['Local - This Machine'] = {} + config['Local - This Machine']['PathToPluginFolder'] = 'C:/Users/USER/Desktop/plugins' + config['Local - This Machine'][';'] = 'For a different folder to store the updated plugins change to (True/False) and the path below' + config['Local - This Machine']['SeperateDownloadPath'] = 'False' + config['Local - This Machine']['PathToSeperateDownloadPath'] = 'C:/Users/USER/Desktop/plugins' config['SFTP - Remote Server'] = {} config['SFTP - Remote Server']['Server'] = '0.0.0.0' config['SFTP - Remote Server']['Username'] = 'user' config['SFTP - Remote Server']['Password'] = 'password' - config['SFTP - Remote Server'][';'] = 'Normally you won*t need to change anything below this line' - config['SFTP - Remote Server']['Port'] = '22' - config['SFTP - Remote Server']['PluginFolderOnServer'] = '.\\plugins' - config['SFTP - Remote Server'][';_'] = 'If you want a different folder to store the updated plugins change to (True/False) and the path below' + config['SFTP - Remote Server'][';'] = 'If a different Port for SFTP needs to be used (Works only for SFTP)' + config['SFTP - Remote Server']['SFTPPort'] = '22' + config['SFTP - Remote Server'][';_'] = 'Change the path below if the plugin folder path is different on the SFTP/FTP server (Change only if you know what you are doing)' + config['SFTP - Remote Server']['PluginFolderOnServer'] = './plugins' + config['SFTP - Remote Server'][';__'] = 'If you want to use FTP instead of SFTP change to (False) else use (True)' + config['SFTP - Remote Server']['USE_SFTP'] = 'True' + config['SFTP - Remote Server'][';___'] = 'For a different folder to store the updated plugins (Only with the update command!) change to (True/False) and the path below' config['SFTP - Remote Server']['SeperateDownloadPath'] = 'False' - config['SFTP - Remote Server']['PathToSeperateDownloadPath'] = '.\\plugins' + config['SFTP - Remote Server']['PathToSeperateDownloadPath'] = './plugins' with open('config.ini', 'w') as configfile: diff --git a/src/handlers/handle_ftp.py b/src/handlers/handle_ftp.py new file mode 100644 index 0000000..4d56f69 --- /dev/null +++ b/src/handlers/handle_ftp.py @@ -0,0 +1,100 @@ +import os +import sys +import ftplib + +from utils.consoleoutput import oColors +from handlers.handle_config import configurationValues + + +def createFTPConnection(): + configValues = configurationValues() + ftp = ftplib.FTP(configValues.sftp_server, user=configValues.sftp_user, \ + passwd=configValues.sftp_password) + try: + return ftp + except UnboundLocalError: + print(oColors.brightRed + "[FTP]: Check your config.ini!" + oColors.standardWhite) + print(oColors.brightRed + "Exiting program..." + oColors.standardWhite) + sys.exit() + + +def ftp_showPlugins(ftp): + configValues = configurationValues() + ftp.cwd(configValues.sftp_folderPath) + for attr in ftp.dir(): + print(attr.filename, attr) + + +def ftp_cdPluginDir(ftp): + configValues = configurationValues() + ftp.cwd(configValues.sftp_folderPath) + + +def ftp_upload_file(ftp, itemPath): + configValues = configurationValues() + try: + ftp.cwd(configValues.sftp_folderPath) + itemPath = os.path.relpath(itemPath, 'TempSFTPFolder/') + itemPath = str(itemPath) + currentDirectory = os.getcwd() + os.chdir('TempSFTPFolder') + with open (itemPath, 'rb') as plugin_file: + ftp.storbinary('STOR '+ str(itemPath), plugin_file) + except FileNotFoundError: + print(oColors.brightRed + "The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) + print(oColors.brightRed + "Aborting uploading." + oColors.standardWhite) + os.chdir(currentDirectory) + ftp.close() + + +def ftp_upload_server_jar(ftp, itemPath): + try: + print(f"itempath: {itemPath}") + ftp.cwd('.') + itemPath = os.path.relpath(itemPath, 'TempSFTPFolder/') + itemPath = str(itemPath) + currentDirectory = os.getcwd() + os.chdir('TempSFTPFolder') + with open (itemPath, 'rb') as server_jar: + ftp.storbinary('STOR '+ str(itemPath), server_jar) + except FileNotFoundError: + print(oColors.brightRed + "The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite) + print(oColors.brightRed + "Aborting uploading." + oColors.standardWhite) + os.chdir(currentDirectory) + ftp.close() + + +def ftp_listAll(ftp): + configValues = configurationValues() + try: + ftp.cwd(configValues.sftp_folderPath) + installedPlugins = ftp.nlst() + except FileNotFoundError: + print(oColors.brightRed + "The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) + + try: + return installedPlugins + except UnboundLocalError: + print(oColors.brightRed + "No plugins were found." + oColors.standardWhite) + + +def ftp_listFilesInServerRoot(ftp): + try: + ftp.cwd('.') + filesInServerRoot = ftp.nlst() + 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) + + +def ftp_downloadFile(ftp, downloadPath, fileToDownload): + configValues = configurationValues() + ftp.cwd(configValues.sftp_folderPath) + filedata = open(downloadPath,'wb') + ftp.retrbinary('RETR '+fileToDownload, filedata.write) + filedata.close() + ftp.quit() diff --git a/src/handlers/handle_input.py b/src/handlers/handle_input.py index 4108b1e..b192111 100644 --- a/src/handlers/handle_input.py +++ b/src/handlers/handle_input.py @@ -82,18 +82,24 @@ def handleInput(inputCommand, inputSelectedObject, inputParams): break else: print(oColors.brightRed + "Error: Command not found. Please try again. :(" + oColors.standardWhite) + print(oColors.brightRed + "Use: '" + oColors.standardWhite +"help command" + oColors.brightRed +"' to get all available commands" + oColors.standardWhite) getInput() getInput() def getInput(): + inputCommand = None while True: try: inputCommand, inputSelectedObject, *inputParams = input("pluGET >> ").split() break except ValueError: - print(oColors.brightRed + "Wrong input! Use: > 'command' 'selectedObject' [optionalParams]" + oColors.standardWhite) - print(oColors.brightRed + "Use: '" + oColors.standardWhite +"help command" + oColors.brightRed +"' to get all available commands" + oColors.standardWhite) - + if inputCommand == None: + continue + else: + print(oColors.brightRed + "Wrong input! Use: > 'command' 'selectedObject' [optionalParams]" + oColors.standardWhite) + print(oColors.brightRed + "Use: '" + oColors.standardWhite +"help command" + oColors.brightRed +"' to get all available commands" + oColors.standardWhite) + except KeyboardInterrupt: + sys.exit() inputParams = inputParams[0] if inputParams else None handleInput(inputCommand, inputSelectedObject, inputParams) diff --git a/src/handlers/handle_sftp.py b/src/handlers/handle_sftp.py index aeebe07..3be570b 100644 --- a/src/handlers/handle_sftp.py +++ b/src/handlers/handle_sftp.py @@ -1,17 +1,20 @@ import sys +import os import pysftp import paramiko +import ftplib from utils.consoleoutput import oColors -from handlers.handle_config import checkConfig +from handlers.handle_config import configurationValues def createSFTPConnection(): + configValues = configurationValues() cnopts = pysftp.CnOpts() cnopts.hostkeys = None # TODO fix this try: - sftp = pysftp.Connection(checkConfig().sftp_server, username=checkConfig().sftp_user, \ - password=checkConfig().sftp_password, port=checkConfig().sftp_port, cnopts=cnopts) + sftp = pysftp.Connection(configValues.sftp_server, username=configValues.sftp_user, \ + password=configValues.sftp_password, port=configValues.sftp_port, cnopts=cnopts) except paramiko.ssh_exception.AuthenticationException: print(oColors.brightRed + "[SFTP]: Wrong Username/Password" + oColors.standardWhite) except paramiko.ssh_exception.SSHException: @@ -41,7 +44,7 @@ def sftp_upload_file(sftp, 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) + print(oColors.brightRed + "Aborting uploading." + oColors.standardWhite) sftp.close() @@ -49,10 +52,9 @@ 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) + print(oColors.brightRed + "The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite) + print(oColors.brightRed + "Aborting uploading." + oColors.standardWhite) sftp.close() @@ -60,7 +62,6 @@ def sftp_listAll(sftp): try: sftp.chdir('plugins') installedPlugins = sftp.listdir() - except FileNotFoundError: print(oColors.brightRed + "The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) @@ -72,13 +73,22 @@ def sftp_listAll(sftp): 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 + print(oColors.brightRed + "No Serverjar was found." + oColors.standardWhite) + + +def sftp_downloadFile(sftp, downloadPath, fileToDownload): + configValues = configurationValues() + sftp.cwd(configValues.sftp_folderPath) + currentDirectory = os.getcwd() + os.chdir('TempSFTPFolder') + sftp.get(fileToDownload) + sftp.close() + os.chdir(currentDirectory) + \ No newline at end of file diff --git a/src/plugin/plugin_downloader.py b/src/plugin/plugin_downloader.py index e92a056..1131ed8 100644 --- a/src/plugin/plugin_downloader.py +++ b/src/plugin/plugin_downloader.py @@ -7,7 +7,8 @@ from utils.consoleoutput import oColors from utils.web_request import doAPIRequest 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 +from handlers.handle_sftp import sftp_upload_file, createSFTPConnection +from handlers.handle_ftp import ftp_upload_file, ftp_cdPluginDir, createFTPConnection def handleRegexPackageName(packageNameFull): @@ -110,8 +111,12 @@ def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'): filesizeData = calculateFileSizeKb(filesize) print("Downloaded " + (str(filesizeData)).rjust(9) + f" KB here {downloadPath}") if not configValues.localPluginFolder: - sftpSession = createSFTPConnection() - sftp_upload_file(sftpSession, downloadPath) + if configValues.sftp_useSftp: + sftpSession = createSFTPConnection() + sftp_upload_file(sftpSession, downloadPath) + else: + ftpSession = createFTPConnection() + ftp_upload_file(ftpSession, downloadPath) def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'): @@ -125,17 +130,14 @@ def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'): versionId = getVersionID(ressourceId, inputPackageVersion) packageVersion = getVersionName(ressourceId, versionId) packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" + if not configValues.localPluginFolder: + downloadPackagePath = f"{downloadPath}/{packageDownloadName}" + else: downloadPackagePath = Path(f"{downloadPath}/{packageDownloadName}") - if configValues.localPluginFolder: - if inputPackageVersion is None or inputPackageVersion == 'latest': - downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) - else: - downloadSpecificVersion(ressourceId, downloadPackagePath, versionId) + if inputPackageVersion is None or inputPackageVersion == 'latest': + downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) + else: + downloadSpecificVersion(ressourceId, downloadPackagePath, versionId) if not configValues.localPluginFolder: - if inputPackageVersion is None or inputPackageVersion == 'latest': - downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) - deleteTempPluginFolder(downloadPath) - else: - downloadSpecificVersion(ressourceId, downloadPackagePath, versionId) - deleteTempPluginFolder(downloadPath) + deleteTempPluginFolder(downloadPath) diff --git a/src/plugin/plugin_remover.py b/src/plugin/plugin_remover.py index 1367822..261cac5 100644 --- a/src/plugin/plugin_remover.py +++ b/src/plugin/plugin_remover.py @@ -5,6 +5,7 @@ from pathlib import Path from utils.consoleoutput import oColors from handlers.handle_config import configurationValues from handlers.handle_sftp import createSFTPConnection, sftp_listAll +from handlers.handle_ftp import createFTPConnection, ftp_listAll from plugin.plugin_updatechecker import getFileName, getFileVersion, getInstalledPlugin, createPluginList @@ -12,8 +13,12 @@ def removePlugin(pluginToRemove): configValues = configurationValues() createPluginList() if not configValues.localPluginFolder: - sftp = createSFTPConnection() - pluginList = sftp_listAll(sftp) + if not configValues.sftp_useSftp: + ftp = createFTPConnection() + pluginList = ftp_listAll(ftp) + else: + sftp = createSFTPConnection() + pluginList = sftp_listAll(sftp) else: pluginList = os.listdir(configValues.pathToPluginFolder) i = 0 @@ -32,8 +37,12 @@ def removePlugin(pluginToRemove): if not configValues.localPluginFolder: pluginPath = configValues.sftp_folderPath pluginPath = f"{pluginPath}/{plugin}" - sftp = createSFTPConnection() - sftp.remove(pluginPath) + if not configValues.sftp_useSftp: + ftp = createFTPConnection() + ftp.delete(pluginPath) + else: + sftp = createSFTPConnection() + sftp.remove(pluginPath) print(f"Removed: {fileName}") i += 1 break diff --git a/src/plugin/plugin_updatechecker.py b/src/plugin/plugin_updatechecker.py index df2eb6e..db2a945 100644 --- a/src/plugin/plugin_updatechecker.py +++ b/src/plugin/plugin_updatechecker.py @@ -9,8 +9,10 @@ from rich.progress import track from utils.consoleoutput import oColors from utils.web_request import doAPIRequest from handlers.handle_config import configurationValues -from handlers.handle_sftp import createSFTPConnection, sftp_listAll +from handlers.handle_sftp import createSFTPConnection, sftp_listAll, sftp_downloadFile +from handlers.handle_ftp import createFTPConnection, ftp_listAll, ftp_downloadFile from plugin.plugin_downloader import getSpecificPackage +from utils.utilities import createTempPluginFolder, deleteTempPluginFolder def createPluginList(): @@ -66,10 +68,19 @@ def compareVersions(plugin_latest_version, pluginVersion): def eggCrackingJar(localJarFileName): configValues = configurationValues() if not configValues.localPluginFolder: - pluginPath = configValues.sftp_folderPath + if configValues.sftp_useSftp: + tempPluginFolderPath = createTempPluginFolder() + sftp = createSFTPConnection() + pathToPluginJar = Path(f"{tempPluginFolderPath}/{localJarFileName}") + sftp_downloadFile(sftp, pathToPluginJar, localJarFileName) + else: + tempPluginFolderPath = createTempPluginFolder() + ftp = createFTPConnection() + pathToPluginJar = Path(f"{tempPluginFolderPath}/{localJarFileName}") + ftp_downloadFile(ftp, pathToPluginJar, localJarFileName) else: pluginPath = configValues.pathToPluginFolder - pathToPluginJar = Path(f"{pluginPath}/{localJarFileName}") + pathToPluginJar = Path(f"{pluginPath}/{localJarFileName}") pluginVersion = '' with ZipFile(pathToPluginJar, 'r') as pluginJar: try: @@ -83,6 +94,8 @@ def eggCrackingJar(localJarFileName): except FileNotFoundError: pluginVersion = '' + if not configValues.localPluginFolder: + deleteTempPluginFolder(tempPluginFolderPath) return pluginVersion @@ -90,8 +103,12 @@ def checkInstalledPackage(inputSelectedObject="all"): configValues = configurationValues() createPluginList() if not configValues.localPluginFolder: - sftp = createSFTPConnection() - pluginList = sftp_listAll(sftp) + if configValues.sftp_useSftp: + connection = createSFTPConnection() + pluginList = sftp_listAll(connection) + else: + connection = createFTPConnection() + pluginList = ftp_listAll(connection) else: pluginList = os.listdir(configValues.pathToPluginFolder) i = 0 @@ -164,8 +181,12 @@ def updateInstalledPackage(inputSelectedObject='all'): configValues = configurationValues() createPluginList() if not configValues.localPluginFolder: - sftp = createSFTPConnection() - pluginList = sftp_listAll(sftp) + if configValues.sftp_useSftp: + connection = createSFTPConnection() + pluginList = sftp_listAll(connection) + else: + connection = createFTPConnection() + pluginList = ftp_listAll(connection) else: pluginList = os.listdir(configValues.pathToPluginFolder) i = 0 @@ -198,7 +219,6 @@ def updateInstalledPackage(inputSelectedObject='all'): print(" ", end='') print(f"{fileName}".ljust(33), end='') print(f"{fileVersion}".ljust(13), end='') - #print(" ", end='') print(f"{latestVersion}".ljust(13)) if not configValues.localPluginFolder: @@ -248,27 +268,45 @@ def updateInstalledPackage(inputSelectedObject='all'): print(" ", end='') print(f"{fileName}".ljust(33), end='') print(f"{fileVersion}".ljust(13), end='') - #print(" ", end='') print(f"{latestVersion}".ljust(13)) if not configValues.localPluginFolder: - if configValues.sftp_seperateDownloadPath is True: - pluginPath = configValues.sftp_pathToSeperateDownloadPath + if configValues.sftp_useSftp: + if configValues.sftp_seperateDownloadPath is True: + pluginPath = configValues.sftp_pathToSeperateDownloadPath + else: + pluginPath = configValues.sftp_folderPath + pluginPath = f"{pluginPath}/{plugin}" + sftp = createSFTPConnection() + indexNumberUpdated += 1 + pluginsUpdated += 1 + try: + getSpecificPackage(pluginId, configValues.sftp_folderPath) + if configValues.sftp_seperateDownloadPath is False: + sftp.remove(pluginPath) + except HTTPError as err: + print(oColors.brightRed + f"HTTPError: {err.code} - {err.reason}" + oColors.standardWhite) + pluginsUpdated -= 1 + except FileNotFoundError: + print(oColors.brightRed + f"FileNotFoundError: Old plugin file coulnd't be deleted" + oColors.standardWhite) else: - pluginPath = configValues.sftp_folderPath - pluginPath = f"{pluginPath}/{plugin}" - sftp = createSFTPConnection() - indexNumberUpdated += 1 - pluginsUpdated += 1 - try: - 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) - pluginsUpdated -= 1 - except FileNotFoundError: - print(oColors.brightRed + f"Error: Old plugin file coulnd't be deleted" + oColors.standardWhite) + if configValues.sftp_seperateDownloadPath is True: + pluginPath = configValues.sftp_pathToSeperateDownloadPath + else: + pluginPath = configValues.sftp_folderPath + pluginPath = f"{pluginPath}/{plugin}" + ftp = createFTPConnection() + indexNumberUpdated += 1 + pluginsUpdated += 1 + try: + getSpecificPackage(pluginId, configValues.sftp_folderPath) + if configValues.sftp_seperateDownloadPath is False: + ftp.delete(pluginPath) + except HTTPError as err: + print(oColors.brightRed + f"HTTPError: {err.code} - {err.reason}" + oColors.standardWhite) + pluginsUpdated -= 1 + except FileNotFoundError: + print(oColors.brightRed + f"FileNotFoundError: Old plugin file coulnd't be deleted" + oColors.standardWhite) else: if configValues.seperateDownloadPath is True: @@ -282,11 +320,11 @@ def updateInstalledPackage(inputSelectedObject='all'): 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) - pluginsUpdated -= 1 + #except HTTPError as err: + # print(oColors.brightRed + f"HTTPError: {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 + f"FileNotFoundError: Old plugin file coulnd't be deleted" + oColors.standardWhite) i = i + 1 except TypeError: diff --git a/src/serverjar/serverjar_checker.py b/src/serverjar/serverjar_checker.py index 7e702b0..54117cc 100644 --- a/src/serverjar/serverjar_checker.py +++ b/src/serverjar/serverjar_checker.py @@ -4,6 +4,7 @@ from urllib.error import HTTPError from pathlib import Path from handlers.handle_sftp import createSFTPConnection, sftp_listFilesInServerRoot +from handlers.handle_ftp import createFTPConnection, ftp_listFilesInServerRoot from handlers.handle_config import configurationValues from utils.consoleoutput import oColors from serverjar.serverjar_paper import paperCheckForUpdate, papermc_downloader @@ -12,8 +13,12 @@ from serverjar.serverjar_paper import paperCheckForUpdate, papermc_downloader def checkInstalledServerjar(): configValues = configurationValues() if not configValues.localPluginFolder: - sftp = createSFTPConnection() - serverRootList = sftp_listFilesInServerRoot(sftp) + if not configValues.sftp_useSftp: + ftp = createFTPConnection() + serverRootList = ftp_listFilesInServerRoot(ftp) + else: + sftp = createSFTPConnection() + serverRootList = sftp_listFilesInServerRoot(sftp) else: serverRootList = os.path.dirname(configValues.pathToPluginFolder) serverRootList = os.listdir(serverRootList) diff --git a/src/serverjar/serverjar_paper.py b/src/serverjar/serverjar_paper.py index f46f56a..daa90f9 100644 --- a/src/serverjar/serverjar_paper.py +++ b/src/serverjar/serverjar_paper.py @@ -7,7 +7,8 @@ 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_sftp import createSFTPConnection, sftp_upload_server_jar +from handlers.handle_ftp import createFTPConnection, ftp_upload_server_jar from handlers.handle_config import configurationValues from utils.utilities import createTempPluginFolder, deleteTempPluginFolder, calculateFileSizeMb @@ -165,8 +166,13 @@ def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersi filesizeData = calculateFileSizeMb(filesize) print("Downloaded " + (str(filesizeData)).rjust(9) + f" MB here {downloadPackagePath}") if not configValues.localPluginFolder: - sftpSession = createSFTPConnection() - sftp_upload_server_jar(sftpSession, downloadPackagePath) + if not configValues.sftp_useSftp: + ftpSession = createFTPConnection() + ftp_upload_server_jar(ftpSession, downloadPackagePath) + else: + sftpSession = createSFTPConnection() + sftp_upload_server_jar(sftpSession, downloadPackagePath) + deleteTempPluginFolder(downloadPath) print(oColors.brightGreen + "Downloaded successfully " + oColors.standardWhite + f"Paper {paperBuild}" + \ diff --git a/src/utils/utilities.py b/src/utils/utilities.py index 4dcf20b..38f5772 100644 --- a/src/utils/utilities.py +++ b/src/utils/utilities.py @@ -8,6 +8,7 @@ from pathlib import Path from utils.consoleoutput import oColors from handlers.handle_config import configurationValues from handlers.handle_sftp import createSFTPConnection +from handlers.handle_ftp import createFTPConnection def getHelp(): @@ -134,16 +135,20 @@ def check_requirements(): apiTest() check_local_plugin_folder() if not configValues.localPluginFolder: - createSFTPConnection() + if configValues.sftp_useSftp: + createSFTPConnection() + else: + createFTPConnection() def createTempPluginFolder(): - tempPluginFolder = Path("./TempSFTPUploadFolder") + configValues = configurationValues() + tempPluginFolder = Path("./TempSFTPFolder") if not os.path.isdir(tempPluginFolder): try: os.mkdir(tempPluginFolder) except OSError: - print(oColors.brightRed + "Creation of directory %s failed" % checkConfig().pathToPluginFolder) + print(oColors.brightRed + "Creation of directory %s failed" % configValues.pathToPluginFolder) print(oColors.brightRed + "Please check the config file!" + oColors.standardWhite) input("Press any key + enter to exit...") sys.exit() @@ -167,4 +172,4 @@ def calculateFileSizeKb(downloadFileSize): fileSizeDownload = int(downloadFileSize) fileSizeKb = fileSizeDownload / 1024 roundedFileSize = round(fileSizeKb, 2) - return roundedFileSize \ No newline at end of file + return roundedFileSize