diff --git a/launcher.bat b/launcher.bat index ee1454a..af366e3 100644 --- a/launcher.bat +++ b/launcher.bat @@ -1,2 +1,2 @@ @ECHO OFF -py "%~dp0\src\handle_input.py" \ No newline at end of file +py "%~dp0\src\__main__.py" \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/__main__.py b/src/__main__.py new file mode 100644 index 0000000..c69e4bb --- /dev/null +++ b/src/__main__.py @@ -0,0 +1,20 @@ +from utils.consoleoutput import consoleTitle, clearConsole, printMainMenu, oColors +from utils.utilities import getHelp, check_requirements +from handlers.handle_input import createInputLists, getInput, outputTest +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 + + +def mainFunction(): + consoleTitle() + clearConsole() + checkConfig() + check_requirements() + createInputLists() + printMainMenu() + getInput() + #outputTest() + +mainFunction() diff --git a/src/handle_sftp.py b/src/handle_sftp.py deleted file mode 100644 index e2fa4b9..0000000 --- a/src/handle_sftp.py +++ /dev/null @@ -1,41 +0,0 @@ -import sys -import pysftp -from handle_config import checkConfig -from consoleoutput import oColors - -def createSFTPConnection(): - cnopts = pysftp.CnOpts() - cnopts.hostkeys = None # TODO fix this - sftp = pysftp.Connection(checkConfig().sftp_server, username=checkConfig().sftp_user, password=checkConfig().sftp_password, port=checkConfig().sftp_port, cnopts=cnopts) - return sftp - -def sftp_showPlugins(sftp): - sftp.cd('plugins') - for attr in sftp.listdir_attr(): - print(attr.filename, attr) - -def sftp_cdPluginDir(sftp): - sftp.cd('plugins') - - -def sftp_upload_file(sftp, itemPath): - #sftp = createSFTPConnection() - try: - sftp.chdir('plugins') - 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') - installedPlugins = sftp.listdir() - - except FileNotFoundError: - print(oColors.brightRed + "The *plugins* folder couldn*t be found on the remote host!" + oColors.standardWhite) - - return installedPlugins \ No newline at end of file diff --git a/src/handlers/__init__.py b/src/handlers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/handle_config.py b/src/handlers/handle_config.py similarity index 58% rename from src/handle_config.py rename to src/handlers/handle_config.py index 1326b71..b7b8eda 100644 --- a/src/handle_config.py +++ b/src/handlers/handle_config.py @@ -1,9 +1,8 @@ -# handles the config and everything around it +import os.path import sys import configparser -import os.path -from consoleoutput import oColors +from utils.consoleoutput import oColors def checkConfig(): @@ -21,11 +20,11 @@ def checkConfig(): localPluginFolder = config['General']['LocalPluginFolder'] pathToPluginFolder = config['General']['PathToPluginFolder'] - sftp_server = config['Remote Server']['Server'] - sftp_user = config['Remote Server']['Username'] - sftp_password = config['Remote Server']['Password'] - sftp_port = config['Remote Server']['Port'] - sftp_folderPath = config['Remote Server']['PluginFolder'] + 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']['PluginFolderForUpload'] sftp_port = int(sftp_port) if localPluginFolder == 'True': @@ -39,15 +38,16 @@ 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 use sftp)' + config['General'][';'] = 'If a local plugin folder exists (True/False): (If False use SFTP)' config['General']['LocalPluginFolder'] = 'True' config['General']['PathToPluginFolder'] = 'C:\\Users\\USER\\Desktop\\plugins' - config['Remote Server'] = {} - config['Remote Server']['Server'] = '0.0.0.0' - config['Remote Server']['Username'] = 'user' - config['Remote Server']['Password'] = 'longpassword' - config['Remote Server']['Port'] = '22' - config['Remote Server']['PluginFolder'] = '.\\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'] = 'longpassword' + 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']['PluginFolderForUpload'] = '.\\plugins' with open('./config.ini', 'w') as configfile: config.write(configfile) diff --git a/src/handle_input.py b/src/handlers/handle_input.py similarity index 80% rename from src/handle_input.py rename to src/handlers/handle_input.py index 2cae347..c446393 100644 --- a/src/handle_input.py +++ b/src/handlers/handle_input.py @@ -1,12 +1,12 @@ -import time import sys -from consoleoutput import consoleTitle, clearConsole, printMainMenu, oColors -from plugin_downloader import searchPackage, getSpecificPackage -from plugin_updatechecker import updateInstalledPackage, checkInstalledPackage -from handle_config import checkConfig -from utilities import getHelp, check_requirements -from handle_sftp import createSFTPConnection, sftp_showPlugins -from plugin_remover import removePlugin +import time + +from utils.consoleoutput import oColors +from utils.utilities import getHelp +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 def createInputLists(): global COMMANDLIST @@ -34,6 +34,7 @@ def handleInput(inputCommand, inputSelectedObject, inputParams): break else: getSpecificPackage(inputSelectedObject, checkConfig().pathToPluginFolder, inputParams) + break else: searchPackage(inputSelectedObject) break @@ -72,17 +73,7 @@ def getInput(): handleInput(inputCommand, inputSelectedObject, inputParams) -def inputMainMenu(): - consoleTitle() - clearConsole() - checkConfig() - check_requirements() - createInputLists() - printMainMenu() - getInput() - outputTest() - - +# only for testing purposes def outputTest(): print("Hello world") print("Waiting still seconds: 5", end='\r') @@ -97,6 +88,3 @@ def outputTest(): time.sleep(1) print("Done ✅☑✔ ") input("Press key to end program...") - - -inputMainMenu() diff --git a/src/handlers/handle_sftp.py b/src/handlers/handle_sftp.py new file mode 100644 index 0000000..faa0d3c --- /dev/null +++ b/src/handlers/handle_sftp.py @@ -0,0 +1,59 @@ +import sys +import pysftp +import paramiko + +from utils.consoleoutput import oColors +from handlers.handle_config import checkConfig + + +def createSFTPConnection(): + 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) + except paramiko.ssh_exception.AuthenticationException: + print(oColors.brightRed + "[SFTP]: Wrong Username/Password" + oColors.standardWhite) + except paramiko.ssh_exception.SSHException: + print(oColors.brightRed + "[SFTP]: The SFTP server isn't available." + oColors.standardWhite) + try: + return sftp + except UnboundLocalError: + print(oColors.brightRed + "[SFTP]: Check your config.ini!" + oColors.standardWhite) + print(oColors.brightRed + "Exiting program..." + oColors.standardWhite) + sys.exit() + + +def sftp_showPlugins(sftp): + sftp.cd('plugins') + for attr in sftp.listdir_attr(): + print(attr.filename, attr) + + +def sftp_cdPluginDir(sftp): + sftp.cd('plugins') + + +def sftp_upload_file(sftp, itemPath): + try: + sftp.chdir('plugins') + 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') + installedPlugins = sftp.listdir() + + 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) \ No newline at end of file diff --git a/src/plugin/__init__.py b/src/plugin/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/plugin_downloader.py b/src/plugin/plugin_downloader.py similarity index 95% rename from src/plugin_downloader.py rename to src/plugin/plugin_downloader.py index a62b347..d99770b 100644 --- a/src/plugin_downloader.py +++ b/src/plugin/plugin_downloader.py @@ -1,11 +1,12 @@ +import re import urllib.request from urllib.error import HTTPError -import re -from web_request import doAPIRequest -from consoleoutput import oColors -from handle_config import checkConfig -from utilities import createTempPluginFolder, deleteTempPluginFolder -from handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection + +from utils.consoleoutput import oColors +from utils.web_request import doAPIRequest +from utils.utilities import createTempPluginFolder, deleteTempPluginFolder +from handlers.handle_config import checkConfig +from handlers.handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection def calculateFileSize(downloadFileSize): @@ -102,7 +103,6 @@ def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'): print(f"Downloadsize: {filesizeData} KB") print(f"File downloaded here: {downloadPath}") if not checkConfig().localPluginFolder: - print(downloadPath) sftpSession = createSFTPConnection() sftp_upload_file(sftpSession, downloadPath) diff --git a/src/plugin/plugin_remover.py b/src/plugin/plugin_remover.py new file mode 100644 index 0000000..bd569ac --- /dev/null +++ b/src/plugin/plugin_remover.py @@ -0,0 +1,48 @@ +import os +import re + +from utils.consoleoutput import oColors +from handlers.handle_config import checkConfig +from handlers.handle_sftp import createSFTPConnection, sftp_listAll +from plugin.plugin_updatechecker import getFileName, getFileVersion, getInstalledPlugin, createPluginList + + +def removePlugin(pluginToRemove): + createPluginList() + if not checkConfig().localPluginFolder: + sftp = createSFTPConnection() + pluginList = sftp_listAll(sftp) + else: + pluginList = os.listdir(checkConfig().pathToPluginFolder) + i = 0 + try: + for plugin in pluginList: + try: + fileName = getFileName(plugin) + fileVersion = getFileVersion(plugin) + pluginId = getInstalledPlugin(fileName, fileVersion) + except TypeError: + continue + pluginIdStr = str(pluginId) + + if pluginToRemove == pluginIdStr or re.search(pluginToRemove, fileName, re.IGNORECASE): + print(f"Removing: {fileName}") + if not checkConfig().localPluginFolder: + pluginPath = checkConfig().sftp_folderPath + pluginPath = f"{pluginPath}\\{plugin}" + sftp = createSFTPConnection() + sftp.remove(pluginPath) + print(f"Removed: {fileName}") + i += 1 + break + else: + pluginPath = checkConfig().pathToPluginFolder + pluginPath = f"{pluginPath}\\{plugin}" + os.remove(pluginPath) + print(f"Removed: {fileName}") + i += 1 + break + except TypeError: + print(oColors.brightRed + f"Aborted removing of: {pluginToRemove}." + oColors.standardWhite) + if i == 0: + print(oColors.brightRed + f"Couldn't remove plugin: {pluginToRemove}" + oColors.standardWhite) diff --git a/src/plugin_updatechecker.py b/src/plugin/plugin_updatechecker.py similarity index 54% rename from src/plugin_updatechecker.py rename to src/plugin/plugin_updatechecker.py index 83b329a..4e0814f 100644 --- a/src/plugin_updatechecker.py +++ b/src/plugin/plugin_updatechecker.py @@ -1,11 +1,11 @@ import os import re -from consoleoutput import oColors -from plugin_downloader import getSpecificPackage #handleInput -from web_request import doAPIRequest -from handle_config import checkConfig -from handle_sftp import createSFTPConnection, sftp_listAll +from utils.consoleoutput import oColors +from utils.web_request import doAPIRequest +from handlers.handle_config import checkConfig +from handlers.handle_sftp import createSFTPConnection, sftp_listAll +from plugin.plugin_downloader import getSpecificPackage def createPluginList(): @@ -31,7 +31,6 @@ def getFileName(pluginName): def getFileVersion(pluginName): - #pluginVersionString = None pluginNameFull = pluginName pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull) pluginVersionFull = pluginVersion.group() @@ -60,38 +59,40 @@ def checkInstalledPackage(inputSelectedObject="all"): i = 0 oldPackages = 0 print("Index / Name / Installed Version / Update available") + try: + for plugin in pluginList: + try: + fileName = getFileName(plugin) + fileVersion = getFileVersion(plugin) + pluginId = getInstalledPlugin(fileName, fileVersion) + except TypeError: + continue + pluginIdStr = str(pluginId) - for plugin in pluginList: - try: - fileName = getFileName(plugin) - fileVersion = getFileVersion(plugin) - pluginId = getInstalledPlugin(fileName, fileVersion) - except TypeError: - continue - pluginIdStr = str(pluginId) + if fileVersion == '': + fileVersion = 'N/A' - if fileVersion == '': - fileVersion = 'N/A' + try: + pluginIsOutdated = INSTALLEDPLUGINLIST[i][2] + except IndexError: + pluginIsOutdated = 'N/A' - try: - pluginIsOutdated = INSTALLEDPLUGINLIST[i][2] - except IndexError: - pluginIsOutdated = 'N/A' + if pluginIsOutdated == True: + oldPackages = oldPackages + 1 - if pluginIsOutdated == True: - oldPackages = oldPackages + 1 - - if inputSelectedObject != "*": - if inputSelectedObject != "all": - if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): - print(f"[{1}] {fileName} - {fileVersion} - {pluginIsOutdated}") - break + if inputSelectedObject != "*": + if inputSelectedObject != "all": + if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): + print(f"[{1}] {fileName} - {fileVersion} - {pluginIsOutdated}") + break + else: + print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}") # TODO find better way for the 2 else else: - print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}") # TODO find better way for the 2 else - else: - print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}") + print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}") - i = i + 1 + i = i + 1 + except TypeError: + print(oColors.brightRed + "Aborted checking for plugins." + oColors.standardWhite) print(f"Old packages: [{oldPackages}/{i}]") @@ -104,58 +105,61 @@ def updateInstalledPackage(inputSelectedObject='all'): pluginList = os.listdir(checkConfig().pathToPluginFolder) i = 0 pluginsUpdated = 0 - for plugin in pluginList: - print(plugin) - try: - fileName = getFileName(plugin) - fileVersion = getFileVersion(plugin) - pluginId = getInstalledPlugin(fileName, fileVersion) - except TypeError: - continue - pluginIdStr = str(pluginId) + try: + for plugin in pluginList: + print(plugin) + try: + fileName = getFileName(plugin) + fileVersion = getFileVersion(plugin) + pluginId = getInstalledPlugin(fileName, fileVersion) + except TypeError: + continue + pluginIdStr = str(pluginId) - if pluginId == None: - print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite) - continue + if pluginId == None: + print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite) + continue - if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): - print(f"Updating: {fileName}") - if not checkConfig().localPluginFolder: - pluginPath = checkConfig().sftp_folderPath - pluginPath = f"{pluginPath}\\{plugin}" - sftp = createSFTPConnection() - sftp.remove(pluginPath) - getSpecificPackage(pluginId, checkConfig().sftp_folderPath) - pluginsUpdated += 1 - else: - pluginPath = checkConfig().pathToPluginFolder - pluginPath = f"{pluginPath}\\{plugin}" - os.remove(pluginPath) - getSpecificPackage(pluginId, checkConfig().pathToPluginFolder) - pluginsUpdated += 1 - break - - if inputSelectedObject == 'all': - if INSTALLEDPLUGINLIST[i][2] == True: + if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): + print(f"Updating: {fileName}") if not checkConfig().localPluginFolder: pluginPath = checkConfig().sftp_folderPath pluginPath = f"{pluginPath}\\{plugin}" - print("Deleting old plugin...") sftp = createSFTPConnection() sftp.remove(pluginPath) - print("Downloading new plugin...") getSpecificPackage(pluginId, checkConfig().sftp_folderPath) pluginsUpdated += 1 - else: pluginPath = checkConfig().pathToPluginFolder pluginPath = f"{pluginPath}\\{plugin}" - print("Deleting old plugin...") os.remove(pluginPath) - print("Downloading new plugin...") getSpecificPackage(pluginId, checkConfig().pathToPluginFolder) pluginsUpdated += 1 - i = i + 1 + break + + if inputSelectedObject == 'all': + if INSTALLEDPLUGINLIST[i][2] == True: + if not checkConfig().localPluginFolder: + pluginPath = checkConfig().sftp_folderPath + pluginPath = f"{pluginPath}\\{plugin}" + print("Deleting old plugin...") + sftp = createSFTPConnection() + sftp.remove(pluginPath) + print("Downloading new plugin...") + getSpecificPackage(pluginId, checkConfig().sftp_folderPath) + pluginsUpdated += 1 + + else: + pluginPath = checkConfig().pathToPluginFolder + pluginPath = f"{pluginPath}\\{plugin}" + print("Deleting old plugin...") + os.remove(pluginPath) + print("Downloading new plugin...") + getSpecificPackage(pluginId, checkConfig().pathToPluginFolder) + pluginsUpdated += 1 + i = i + 1 + except TypeError: + print(oColors.brightRed + "Aborted updating for plugins." + oColors.standardWhite) print(f"[{pluginsUpdated}/{i}] Plugins updated") diff --git a/src/plugin_remover.py b/src/plugin_remover.py deleted file mode 100644 index 1f16959..0000000 --- a/src/plugin_remover.py +++ /dev/null @@ -1,15 +0,0 @@ -import os -import re -from handle_config import checkConfig -from handle_sftp import createSFTPConnection, sftp_listAll - -def removePlugin(pluginToRemove): - if not checkConfig().localPluginFolder: - sftp = createSFTPConnection() - pluginList = sftp_listAll(sftp) - else: - pluginList = os.listdir(checkConfig().pathToPluginFolder) - - for plugin in pluginList: - #pluginVersion = re.search(pluginToRemove, pluginNameFull) - print(plugin) \ No newline at end of file diff --git a/src/utils/__init__.py b/src/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/consoleoutput.py b/src/utils/consoleoutput.py similarity index 100% rename from src/consoleoutput.py rename to src/utils/consoleoutput.py diff --git a/src/utilities.py b/src/utils/utilities.py similarity index 91% rename from src/utilities.py rename to src/utils/utilities.py index cbcf520..6f12755 100644 --- a/src/utilities.py +++ b/src/utils/utilities.py @@ -1,10 +1,11 @@ # misc functions import os import sys -import requests import shutil -from consoleoutput import oColors -from handle_config import checkConfig +import requests + +from utils.consoleoutput import oColors +from handlers.handle_config import checkConfig def getHelp(): @@ -34,7 +35,7 @@ def apiTest(): try: r = requests.get(apiStatusUrl) except requests.exceptions.HTTPError: - print(oColors.brightRed + "Couldn*t make a connection to the API. Check you connection to the internet!" + oColors.standardWhite) + print(oColors.brightRed + "Couldn't make a connection to the API. Check you connection to the internet!" + oColors.standardWhite) sys.exit() if r.status_code != 200: print(oColors.brightRed + "Problems with the API detected. Plese try it again later!" + oColors.standardWhite) @@ -63,4 +64,4 @@ def deleteTempPluginFolder(tempPluginFolder): try: shutil.rmtree(tempPluginFolder) except OSError as e: - print ("Error: %s - %s." % (e.filename, e.strerror)) \ No newline at end of file + print ("Error: %s - %s." % (e.filename, e.strerror)) diff --git a/src/web_request.py b/src/utils/web_request.py similarity index 90% rename from src/web_request.py rename to src/utils/web_request.py index fffe8f5..b947ab6 100644 --- a/src/web_request.py +++ b/src/utils/web_request.py @@ -1,6 +1,5 @@ # Handles the web requests import requests -#import urllib.request def doAPIRequest(url):