2021-03-01 22:39:48 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
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-03 22:25:44 +00:00
|
|
|
def addToPluginList(pluginId, versionId, plugin_is_outdated):
|
|
|
|
INSTALLEDPLUGINLIST.append([pluginId, versionId, plugin_is_outdated])
|
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
|
2021-03-03 16:13:43 +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-03 16:13:43 +00:00
|
|
|
return pluginNameOnly
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def compareVersions(pluginId, pluginVersion):
|
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"]
|
|
|
|
if pluginVersion != versionLatestUpdate:
|
|
|
|
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
|
|
|
|
print("Index / Name / Installed Version / 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:
|
|
|
|
pluginIsOutdated = INSTALLEDPLUGINLIST[i][2]
|
|
|
|
except IndexError:
|
|
|
|
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
|
2021-03-08 01:56:00 +00:00
|
|
|
else:
|
2021-03-12 00:39:39 +00:00
|
|
|
print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}")
|
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-08 01:56:00 +00:00
|
|
|
print(f"Old packages: [{oldPackages}/{i}]")
|
|
|
|
|
|
|
|
|
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-12 00:39:39 +00:00
|
|
|
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 inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE):
|
|
|
|
print(f"Updating: {fileName}")
|
2021-03-10 01:05:56 +00:00
|
|
|
if not checkConfig().localPluginFolder:
|
|
|
|
pluginPath = checkConfig().sftp_folderPath
|
|
|
|
pluginPath = f"{pluginPath}\\{plugin}"
|
|
|
|
sftp = createSFTPConnection()
|
|
|
|
sftp.remove(pluginPath)
|
|
|
|
getSpecificPackage(pluginId, checkConfig().sftp_folderPath)
|
2021-03-10 22:49:57 +00:00
|
|
|
pluginsUpdated += 1
|
2021-03-10 01:05:56 +00:00
|
|
|
else:
|
|
|
|
pluginPath = checkConfig().pathToPluginFolder
|
|
|
|
pluginPath = f"{pluginPath}\\{plugin}"
|
|
|
|
os.remove(pluginPath)
|
|
|
|
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
|
2021-03-10 22:49:57 +00:00
|
|
|
pluginsUpdated += 1
|
2021-03-12 00:39:39 +00:00
|
|
|
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)
|
2021-03-10 22:49:57 +00:00
|
|
|
print(f"[{pluginsUpdated}/{i}] Plugins updated")
|
2021-03-03 16:13:43 +00:00
|
|
|
|
|
|
|
|
2021-03-03 22:25:44 +00:00
|
|
|
def getInstalledPlugin(localFileName, localFileVersion):
|
2021-03-04 23:09:03 +00:00
|
|
|
url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name"
|
|
|
|
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-08 21:13:57 +00:00
|
|
|
#pName = ressource["name"]
|
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"]
|
|
|
|
if localFileVersion == updateVersion:
|
|
|
|
plugin_match_found = True
|
|
|
|
pluginID = pID
|
2021-03-03 22:25:44 +00:00
|
|
|
updateId = updates["id"]
|
|
|
|
plugin_is_outdated = compareVersions(pID, updateVersion)
|
|
|
|
addToPluginList(pID, updateId, plugin_is_outdated)
|
2021-03-03 16:13:43 +00:00
|
|
|
break
|
2021-03-02 23:17:15 +00:00
|
|
|
i = i + 1
|
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
|