From 3fed1580f8902aecad177f46cea0090656ff5cab Mon Sep 17 00:00:00 2001 From: Neocky Date: Sat, 13 Mar 2021 19:21:03 +0100 Subject: [PATCH] Every plugin has now an array entry; Beautified the output Changes: - added latest version of plugin to installedplugin array - fixed issue when array couldn't be created; Now the array will always be created - added bright Green to console Output - deleted debug output for the innput --- src/handlers/handle_input.py | 6 +- src/plugin/plugin_updatechecker.py | 130 ++++++++++++++++++++--------- src/utils/consoleoutput.py | 1 + 3 files changed, 93 insertions(+), 44 deletions(-) diff --git a/src/handlers/handle_input.py b/src/handlers/handle_input.py index c446393..3827bd7 100644 --- a/src/handlers/handle_input.py +++ b/src/handlers/handle_input.py @@ -67,9 +67,9 @@ def getInput(): print(oColors.brightRed + "Wrong input! Use: > *command* *selectedObject* *optionalParams*" + oColors.standardWhite) inputParams = inputParams[0] if inputParams else None - print(inputCommand) - print(inputSelectedObject) - print(inputParams) + #print(inputCommand) + #print(inputSelectedObject) + #print(inputParams) handleInput(inputCommand, inputSelectedObject, inputParams) diff --git a/src/plugin/plugin_updatechecker.py b/src/plugin/plugin_updatechecker.py index 4e0814f..c689c7f 100644 --- a/src/plugin/plugin_updatechecker.py +++ b/src/plugin/plugin_updatechecker.py @@ -14,11 +14,11 @@ def createPluginList(): return INSTALLEDPLUGINLIST -def addToPluginList(pluginId, versionId, plugin_is_outdated): - INSTALLEDPLUGINLIST.append([pluginId, versionId, plugin_is_outdated]) +def addToPluginList(pluginId, versionId, plugin_latest_version, plugin_is_outdated): + INSTALLEDPLUGINLIST.append([pluginId, versionId, plugin_latest_version, plugin_is_outdated]) -def getFileName(pluginName): +def getFileName(pluginName): # TODO delete end of pluginName -v like BossShopPro-v pluginNameFull = pluginName pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull) try: @@ -38,11 +38,15 @@ def getFileVersion(pluginName): return pluginVersionString -def compareVersions(pluginId, pluginVersion): +def getLatestPluginVersion(pluginId): url = f"https://api.spiget.org/v2/resources/{pluginId}/versions/latest" latestUpdateSearch = doAPIRequest(url) versionLatestUpdate = latestUpdateSearch["name"] - if pluginVersion != versionLatestUpdate: + return versionLatestUpdate + + +def compareVersions(plugin_latest_version, pluginVersion): + if pluginVersion < plugin_latest_version: plugin_is_outdated = True else: plugin_is_outdated = False @@ -58,7 +62,8 @@ def checkInstalledPackage(inputSelectedObject="all"): pluginList = os.listdir(checkConfig().pathToPluginFolder) i = 0 oldPackages = 0 - print("Index / Name / Installed Version / Update available") + print(f"Checking: {inputSelectedObject}") + print("Index | Name | Installed V. | Latest V. | Update available") try: for plugin in pluginList: try: @@ -73,27 +78,43 @@ def checkInstalledPackage(inputSelectedObject="all"): fileVersion = 'N/A' try: - pluginIsOutdated = INSTALLEDPLUGINLIST[i][2] + pluginLatestVersion = INSTALLEDPLUGINLIST[i][2] except IndexError: + pluginLatestVersion = 'N/A' + + if pluginLatestVersion == None: + pluginLatestVersion = 'N/A' + + try: + pluginIsOutdated = INSTALLEDPLUGINLIST[i][3] + except IndexError: + pluginIsOutdated = 'N/A' + + if pluginIsOutdated == None: pluginIsOutdated = 'N/A' 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 - else: - print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}") # TODO find better way for the 2 else + if inputSelectedObject != "*" and inputSelectedObject != "all": + if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): + print(f" [{1}]".ljust(8), end='') + print(f"{fileName}".ljust(33), end='') + print(f"{fileVersion}".ljust(15), end='') + print(f"{pluginLatestVersion}".ljust(12), end='') + print(f" {pluginIsOutdated}".ljust(5)) + break else: - print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}") + print(f" [{i+1}]".ljust(8), end='') + print(f"{fileName}".ljust(33), end='') + print(f"{fileVersion}".ljust(15), end='') + print(f"{pluginLatestVersion}".ljust(12), end='') + print(f" {pluginIsOutdated}".ljust(5)) i = i + 1 except TypeError: print(oColors.brightRed + "Aborted checking for plugins." + oColors.standardWhite) - print(f"Old packages: [{oldPackages}/{i}]") + print(oColors.brightYellow + f"Old packages: [{oldPackages}/{i}]" + oColors.standardWhite) def updateInstalledPackage(inputSelectedObject='all'): @@ -105,15 +126,19 @@ def updateInstalledPackage(inputSelectedObject='all'): pluginList = os.listdir(checkConfig().pathToPluginFolder) i = 0 pluginsUpdated = 0 + print(f"Updating: {inputSelectedObject}") + print("Index | Name | Old V. | New V.") try: for plugin in pluginList: - print(plugin) try: fileName = getFileName(plugin) fileVersion = getFileVersion(plugin) pluginId = getInstalledPlugin(fileName, fileVersion) + latestVersion = getLatestPluginVersion(pluginId) except TypeError: continue + except ValueError: + continue pluginIdStr = str(pluginId) if pluginId == None: @@ -121,46 +146,60 @@ def updateInstalledPackage(inputSelectedObject='all'): 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 INSTALLEDPLUGINLIST[i][3] == True: + print(f" [{pluginsUpdated+1}]".ljust(8), end='') + print(f"{fileName}".ljust(30), end='') + print(f"{fileVersion}".ljust(8), end='') + print(" ", end='') + print(f"{latestVersion}".ljust(8)) + + 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 + else: + print(f"{fileName} is already on {latestVersion}") + print(oColors.brightRed + "Aborting the update process."+ oColors.standardWhite) + break + + if inputSelectedObject == 'all': + if INSTALLEDPLUGINLIST[i][3] == True: + print(f" [{pluginsUpdated+1}]".ljust(8), end='') + print(f"{fileName}".ljust(30), end='') + print(f"{fileVersion}".ljust(8), end='') + print(" ", end='') + print(f"{latestVersion}".ljust(8)) - 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") + if inputSelectedObject =='all' and pluginsUpdated == 0: + print(oColors.brightGreen + "All plugins are on the latest version!" + oColors.standardWhite) def getInstalledPlugin(localFileName, localFileVersion): @@ -172,7 +211,7 @@ def getInstalledPlugin(localFileName, localFileVersion): for ressource in packageName: if plugin_match_found == True: break - #pName = ressource["name"] + pID = ressource["id"] url2 = f"https://api.spiget.org/v2/resources/{pID}/versions?size=100&sort=-name" packageVersions = doAPIRequest(url2) @@ -182,10 +221,19 @@ def getInstalledPlugin(localFileName, localFileVersion): plugin_match_found = True pluginID = pID updateId = updates["id"] - plugin_is_outdated = compareVersions(pID, updateVersion) - addToPluginList(pID, updateId, plugin_is_outdated) + plugin_latest_version = getLatestPluginVersion(pID) + plugin_is_outdated = compareVersions(plugin_latest_version, updateVersion) + addToPluginList(pID, updateId, plugin_latest_version , plugin_is_outdated) break + i = i + 1 + else: + pID = None + updateId = None + plugin_latest_version = None + plugin_is_outdated = None + addToPluginList(pID, updateId, plugin_latest_version , plugin_is_outdated) + return pluginID diff --git a/src/utils/consoleoutput.py b/src/utils/consoleoutput.py index fb7709d..0e0754c 100644 --- a/src/utils/consoleoutput.py +++ b/src/utils/consoleoutput.py @@ -16,6 +16,7 @@ class oColors: brightYellow = "\033[93m" brightMagenta = "\033[95m" brightRed = "\033[91m" + brightGreen = "\033[92m" darkMagenta = "\033[35m"