2021-03-01 22:39:48 +00:00
|
|
|
import os
|
|
|
|
import re
|
2021-03-14 19:21:26 +00:00
|
|
|
from urllib.error import HTTPError
|
2021-03-01 22:39:48 +00:00
|
|
|
|
2021-03-12 00:39:39 +00:00
|
|
|
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
|
2021-03-01 22:39:48 +00:00
|
|
|
|
|
|
|
|
2021-03-03 22:25:44 +00:00
|
|
|
def createPluginList():
|
|
|
|
global INSTALLEDPLUGINLIST
|
|
|
|
INSTALLEDPLUGINLIST = []
|
|
|
|
return INSTALLEDPLUGINLIST
|
2021-03-03 16:13:43 +00:00
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
|
2021-03-13 18:21:03 +00:00
|
|
|
def addToPluginList(pluginId, versionId, plugin_latest_version, plugin_is_outdated):
|
|
|
|
INSTALLEDPLUGINLIST.append([pluginId, versionId, plugin_latest_version, plugin_is_outdated])
|
2021-03-03 22:25:44 +00:00
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
|
2021-03-13 21:09:32 +00:00
|
|
|
def getFileName(pluginName):
|
2021-03-01 22:39:48 +00:00
|
|
|
pluginNameFull = pluginName
|
2021-03-10 22:49:57 +00:00
|
|
|
pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull)
|
2021-03-10 01:05:56 +00:00
|
|
|
try:
|
2021-03-10 22:49:57 +00:00
|
|
|
pluginVersionFull = pluginVersion.group()
|
2021-03-10 01:05:56 +00:00
|
|
|
except AttributeError:
|
|
|
|
pluginVersionFull = pluginVersion
|
|
|
|
pluginNameOnlyy = pluginNameFull.replace(pluginVersionFull, '')
|
|
|
|
pluginNameOnly = re.sub(r'(\-$)', '', pluginNameOnlyy)
|
2021-03-13 21:09:32 +00:00
|
|
|
pluginNameOnlyy = re.sub(r'(\-v$)', '', pluginNameOnly)
|
|
|
|
return pluginNameOnlyy
|
2021-03-03 16:13:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def getFileVersion(pluginName):
|
|
|
|
pluginNameFull = pluginName
|
|
|
|
pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull)
|
|
|
|
pluginVersionFull = pluginVersion.group()
|
|
|
|
pluginVersionString = pluginVersionFull.replace('.jar', '')
|
|
|
|
return pluginVersionString
|
|
|
|
|
2021-03-03 22:25:44 +00:00
|
|
|
|
2021-03-13 18:21:03 +00:00
|
|
|
def getLatestPluginVersion(pluginId):
|
2021-03-04 23:09:03 +00:00
|
|
|
url = f"https://api.spiget.org/v2/resources/{pluginId}/versions/latest"
|
|
|
|
latestUpdateSearch = doAPIRequest(url)
|
2021-03-03 16:13:43 +00:00
|
|
|
versionLatestUpdate = latestUpdateSearch["name"]
|
2021-03-13 18:21:03 +00:00
|
|
|
return versionLatestUpdate
|
|
|
|
|
|
|
|
|
|
|
|
def compareVersions(plugin_latest_version, pluginVersion):
|
|
|
|
if pluginVersion < plugin_latest_version:
|
2021-03-03 16:13:43 +00:00
|
|
|
plugin_is_outdated = True
|
|
|
|
else:
|
|
|
|
plugin_is_outdated = False
|
|
|
|
return plugin_is_outdated
|
|
|
|
|
|
|
|
|
2021-03-10 01:05:56 +00:00
|
|
|
def checkInstalledPackage(inputSelectedObject="all"):
|
2021-03-08 01:56:00 +00:00
|
|
|
createPluginList()
|
2021-03-10 01:05:56 +00:00
|
|
|
if not checkConfig().localPluginFolder:
|
|
|
|
sftp = createSFTPConnection()
|
|
|
|
pluginList = sftp_listAll(sftp)
|
|
|
|
else:
|
|
|
|
pluginList = os.listdir(checkConfig().pathToPluginFolder)
|
2021-03-08 01:56:00 +00:00
|
|
|
i = 0
|
|
|
|
oldPackages = 0
|
2021-03-13 18:21:03 +00:00
|
|
|
print(f"Checking: {inputSelectedObject}")
|
|
|
|
print("Index | Name | Installed V. | Latest V. | Update available")
|
2021-03-12 00:39:39 +00:00
|
|
|
try:
|
|
|
|
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'
|
|
|
|
|
|
|
|
try:
|
2021-03-13 18:21:03 +00:00
|
|
|
pluginLatestVersion = INSTALLEDPLUGINLIST[i][2]
|
2021-03-12 00:39:39 +00:00
|
|
|
except IndexError:
|
2021-03-13 18:21:03 +00:00
|
|
|
pluginLatestVersion = 'N/A'
|
|
|
|
|
|
|
|
if pluginLatestVersion == None:
|
|
|
|
pluginLatestVersion = 'N/A'
|
|
|
|
|
|
|
|
try:
|
|
|
|
pluginIsOutdated = INSTALLEDPLUGINLIST[i][3]
|
|
|
|
except IndexError:
|
|
|
|
pluginIsOutdated = 'N/A'
|
|
|
|
|
|
|
|
if pluginIsOutdated == None:
|
2021-03-12 00:39:39 +00:00
|
|
|
pluginIsOutdated = 'N/A'
|
|
|
|
|
|
|
|
if pluginIsOutdated == True:
|
|
|
|
oldPackages = oldPackages + 1
|
|
|
|
|
2021-03-13 18:21:03 +00:00
|
|
|
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
|
2021-03-08 01:56:00 +00:00
|
|
|
else:
|
2021-03-13 18:21:03 +00:00
|
|
|
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))
|
2021-03-08 01:56:00 +00:00
|
|
|
|
2021-03-12 00:39:39 +00:00
|
|
|
i = i + 1
|
|
|
|
except TypeError:
|
|
|
|
print(oColors.brightRed + "Aborted checking for plugins." + oColors.standardWhite)
|
2021-03-13 18:21:03 +00:00
|
|
|
print(oColors.brightYellow + f"Old packages: [{oldPackages}/{i}]" + oColors.standardWhite)
|
2021-03-08 01:56:00 +00:00
|
|
|
|
|
|
|
|
2021-03-10 01:05:56 +00:00
|
|
|
def updateInstalledPackage(inputSelectedObject='all'):
|
2021-03-03 22:25:44 +00:00
|
|
|
createPluginList()
|
2021-03-10 01:05:56 +00:00
|
|
|
if not checkConfig().localPluginFolder:
|
|
|
|
sftp = createSFTPConnection()
|
|
|
|
pluginList = sftp_listAll(sftp)
|
|
|
|
else:
|
|
|
|
pluginList = os.listdir(checkConfig().pathToPluginFolder)
|
2021-03-03 22:25:44 +00:00
|
|
|
i = 0
|
2021-03-10 22:49:57 +00:00
|
|
|
pluginsUpdated = 0
|
2021-03-14 19:21:26 +00:00
|
|
|
indexNumberUpdated = 0
|
2021-03-13 18:21:03 +00:00
|
|
|
print(f"Updating: {inputSelectedObject}")
|
|
|
|
print("Index | Name | Old V. | New V.")
|
2021-03-12 00:39:39 +00:00
|
|
|
try:
|
|
|
|
for plugin in pluginList:
|
|
|
|
try:
|
|
|
|
fileName = getFileName(plugin)
|
|
|
|
fileVersion = getFileVersion(plugin)
|
|
|
|
pluginId = getInstalledPlugin(fileName, fileVersion)
|
2021-03-13 18:21:03 +00:00
|
|
|
latestVersion = getLatestPluginVersion(pluginId)
|
2021-03-12 00:39:39 +00:00
|
|
|
except TypeError:
|
|
|
|
continue
|
2021-03-13 18:21:03 +00:00
|
|
|
except ValueError:
|
|
|
|
continue
|
2021-03-12 00:39:39 +00:00
|
|
|
pluginIdStr = str(pluginId)
|
|
|
|
|
|
|
|
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):
|
2021-03-13 18:21:03 +00:00
|
|
|
if INSTALLEDPLUGINLIST[i][3] == True:
|
2021-03-14 19:21:26 +00:00
|
|
|
print(f" [{indexNumberUpdated+1}]".ljust(8), end='')
|
2021-03-13 18:21:03 +00:00
|
|
|
print(f"{fileName}".ljust(30), end='')
|
|
|
|
print(f"{fileVersion}".ljust(8), end='')
|
|
|
|
print(" ", end='')
|
|
|
|
print(f"{latestVersion}".ljust(8))
|
|
|
|
|
|
|
|
if not checkConfig().localPluginFolder:
|
2021-03-14 19:21:26 +00:00
|
|
|
if checkConfig().sftp_pathToSeperateDownloadPath is True:
|
|
|
|
pluginPath = checkConfig().sftp_pathToSeperateDownloadPath
|
|
|
|
else:
|
|
|
|
pluginPath = checkConfig().sftp_folderPath
|
2021-03-13 18:21:03 +00:00
|
|
|
pluginPath = f"{pluginPath}\\{plugin}"
|
|
|
|
sftp = createSFTPConnection()
|
2021-03-14 19:21:26 +00:00
|
|
|
indexNumberUpdated += 1
|
2021-03-13 18:21:03 +00:00
|
|
|
pluginsUpdated += 1
|
2021-03-14 19:21:26 +00:00
|
|
|
try:
|
|
|
|
getSpecificPackage(pluginId, checkConfig().sftp_folderPath)
|
|
|
|
if checkConfig().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)
|
2021-03-13 18:21:03 +00:00
|
|
|
else:
|
2021-03-14 12:25:13 +00:00
|
|
|
if checkConfig().seperateDownloadPath is True:
|
|
|
|
pluginPath = checkConfig().pathToSeperateDownloadPath
|
|
|
|
else:
|
|
|
|
pluginPath = checkConfig().pathToPluginFolder
|
2021-03-13 18:21:03 +00:00
|
|
|
pluginPath = f"{pluginPath}\\{plugin}"
|
2021-03-14 19:21:26 +00:00
|
|
|
indexNumberUpdated += 1
|
2021-03-13 18:21:03 +00:00
|
|
|
pluginsUpdated += 1
|
2021-03-14 19:21:26 +00:00
|
|
|
try:
|
|
|
|
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
|
|
|
|
if checkConfig().seperateDownloadPath is False:
|
|
|
|
os.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)
|
2021-03-13 18:21:03 +00:00
|
|
|
break
|
2021-03-10 01:05:56 +00:00
|
|
|
else:
|
2021-03-13 18:21:03 +00:00
|
|
|
print(f"{fileName} is already on {latestVersion}")
|
|
|
|
print(oColors.brightRed + "Aborting the update process."+ oColors.standardWhite)
|
|
|
|
break
|
2021-03-12 00:39:39 +00:00
|
|
|
|
|
|
|
if inputSelectedObject == 'all':
|
2021-03-13 18:21:03 +00:00
|
|
|
if INSTALLEDPLUGINLIST[i][3] == True:
|
2021-03-14 19:21:26 +00:00
|
|
|
print(f" [{indexNumberUpdated+1}]".ljust(8), end='')
|
2021-03-13 18:21:03 +00:00
|
|
|
print(f"{fileName}".ljust(30), end='')
|
|
|
|
print(f"{fileVersion}".ljust(8), end='')
|
|
|
|
print(" ", end='')
|
|
|
|
print(f"{latestVersion}".ljust(8))
|
|
|
|
|
2021-03-12 00:39:39 +00:00
|
|
|
if not checkConfig().localPluginFolder:
|
2021-03-14 12:25:13 +00:00
|
|
|
if checkConfig().sftp_seperateDownloadPath is True:
|
|
|
|
pluginPath = checkConfig().sftp_pathToSeperateDownloadPath
|
|
|
|
else:
|
|
|
|
pluginPath = checkConfig().sftp_folderPath
|
2021-03-12 00:39:39 +00:00
|
|
|
pluginPath = checkConfig().sftp_folderPath
|
|
|
|
pluginPath = f"{pluginPath}\\{plugin}"
|
|
|
|
sftp = createSFTPConnection()
|
2021-03-14 19:21:26 +00:00
|
|
|
indexNumberUpdated += 1
|
2021-03-12 00:39:39 +00:00
|
|
|
pluginsUpdated += 1
|
2021-03-14 19:21:26 +00:00
|
|
|
try:
|
|
|
|
getSpecificPackage(pluginId, checkConfig().sftp_folderPath)
|
|
|
|
if checkConfig().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)
|
2021-03-12 00:39:39 +00:00
|
|
|
|
|
|
|
else:
|
2021-03-14 12:25:13 +00:00
|
|
|
if checkConfig().seperateDownloadPath is True:
|
|
|
|
pluginPath = checkConfig().pathToSeperateDownloadPath
|
|
|
|
else:
|
|
|
|
pluginPath = checkConfig().pathToPluginFolder
|
2021-03-12 00:39:39 +00:00
|
|
|
pluginPath = f"{pluginPath}\\{plugin}"
|
2021-03-14 19:21:26 +00:00
|
|
|
indexNumberUpdated += 1
|
2021-03-12 00:39:39 +00:00
|
|
|
pluginsUpdated += 1
|
2021-03-14 19:21:26 +00:00
|
|
|
try:
|
|
|
|
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
|
|
|
|
if checkConfig().seperateDownloadPath is False:
|
|
|
|
os.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)
|
|
|
|
|
2021-03-12 00:39:39 +00:00
|
|
|
i = i + 1
|
|
|
|
except TypeError:
|
|
|
|
print(oColors.brightRed + "Aborted updating for plugins." + oColors.standardWhite)
|
2021-03-10 22:49:57 +00:00
|
|
|
print(f"[{pluginsUpdated}/{i}] Plugins updated")
|
2021-03-13 18:21:03 +00:00
|
|
|
if inputSelectedObject =='all' and pluginsUpdated == 0:
|
|
|
|
print(oColors.brightGreen + "All plugins are on the latest version!" + oColors.standardWhite)
|
2021-03-03 16:13:43 +00:00
|
|
|
|
|
|
|
|
2021-03-03 22:25:44 +00:00
|
|
|
def getInstalledPlugin(localFileName, localFileVersion):
|
2021-03-13 22:32:52 +00:00
|
|
|
url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name&sort=-downloads"
|
2021-03-04 23:09:03 +00:00
|
|
|
packageName = doAPIRequest(url)
|
2021-03-02 23:17:15 +00:00
|
|
|
i = 1
|
2021-03-03 16:13:43 +00:00
|
|
|
plugin_match_found = False
|
|
|
|
pluginID = None
|
2021-03-02 23:17:15 +00:00
|
|
|
for ressource in packageName:
|
2021-03-03 16:13:43 +00:00
|
|
|
if plugin_match_found == True:
|
|
|
|
break
|
2021-03-13 18:21:03 +00:00
|
|
|
|
2021-03-02 23:17:15 +00:00
|
|
|
pID = ressource["id"]
|
2021-03-04 23:09:03 +00:00
|
|
|
url2 = f"https://api.spiget.org/v2/resources/{pID}/versions?size=100&sort=-name"
|
|
|
|
packageVersions = doAPIRequest(url2)
|
2021-03-03 16:13:43 +00:00
|
|
|
for updates in packageVersions:
|
|
|
|
updateVersion = updates["name"]
|
2021-03-13 21:09:32 +00:00
|
|
|
if localFileVersion in updateVersion:
|
2021-03-03 16:13:43 +00:00
|
|
|
plugin_match_found = True
|
|
|
|
pluginID = pID
|
2021-03-03 22:25:44 +00:00
|
|
|
updateId = updates["id"]
|
2021-03-13 18:21:03 +00:00
|
|
|
plugin_latest_version = getLatestPluginVersion(pID)
|
|
|
|
plugin_is_outdated = compareVersions(plugin_latest_version, updateVersion)
|
|
|
|
addToPluginList(pID, updateId, plugin_latest_version , plugin_is_outdated)
|
2021-03-03 16:13:43 +00:00
|
|
|
break
|
2021-03-14 12:25:13 +00:00
|
|
|
|
2021-03-02 23:17:15 +00:00
|
|
|
i = i + 1
|
2021-03-13 18:21:03 +00:00
|
|
|
else:
|
2021-03-14 12:25:13 +00:00
|
|
|
if plugin_match_found != True:
|
|
|
|
pID = None
|
|
|
|
updateId = None
|
|
|
|
plugin_latest_version = None
|
|
|
|
plugin_is_outdated = None
|
|
|
|
addToPluginList(pID, updateId, plugin_latest_version , plugin_is_outdated)
|
2021-03-13 18:21:03 +00:00
|
|
|
|
2021-03-03 16:13:43 +00:00
|
|
|
return pluginID
|
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
|
2021-03-04 23:09:03 +00:00
|
|
|
# start query
|
2021-03-01 22:39:48 +00:00
|
|
|
# get id
|
2021-03-03 16:13:43 +00:00
|
|
|
# search with id for all version upates
|
2021-03-01 22:39:48 +00:00
|
|
|
# get version that matches installed version
|
|
|
|
# if match then download latest update
|
2021-03-08 01:56:00 +00:00
|
|
|
# else get second query
|