Finished paper support; Created independent paths

Changes:
- Created OS independent paths
- finished paper support
- created many catches for paper handling errors
This commit is contained in:
Neocky 2021-03-16 01:04:13 +01:00
parent 5e296e858f
commit 430dbcd8d2
8 changed files with 132 additions and 65 deletions

View File

@ -1,13 +1,13 @@
import os
import sys
import configparser
from pathlib import Path
from utils.consoleoutput import oColors
def checkConfig():
currentFolder = os.getcwd()
os.chdir('..')
configAvailable = os.path.isfile("config.ini")
if not configAvailable:
@ -21,9 +21,9 @@ def checkConfig():
config.sections()
config.read("config.ini")
localPluginFolder = config['General']['LocalPluginFolder']
pathToPluginFolder = config['General']['PathToPluginFolder']
pathToPluginFolder = Path(config['General']['PathToPluginFolder'])
seperateDownloadPath = config['General']['SeperateDownloadPath']
pathToSeperateDownloadPath = config['General']['PathToSeperateDownloadPath']
pathToSeperateDownloadPath = Path(config['General']['PathToSeperateDownloadPath'])
sftp_server = config['SFTP - Remote Server']['Server']
sftp_user = config['SFTP - Remote Server']['Username']
@ -75,5 +75,5 @@ def createConfig():
config['SFTP - Remote Server']['PathToSeperateDownloadPath'] = '.\\plugins'
with open('./config.ini', 'w') as configfile:
with open('config.ini', 'w') as configfile:
config.write(configfile)

View File

@ -1,6 +1,7 @@
import re
import urllib.request
from urllib.error import HTTPError
from pathlib import Path
from utils.consoleoutput import oColors
from utils.web_request import doAPIRequest
@ -119,7 +120,7 @@ def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'):
versionId = getVersionID(ressourceId, inputPackageVersion)
packageVersion = getVersionName(ressourceId, versionId)
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"
downloadPackagePath = f"{downloadPath}\\{packageDownloadName}"
downloadPackagePath = Path(f"{downloadPath}/{packageDownloadName}")
if checkConfig().localPluginFolder:
if inputPackageVersion is None or inputPackageVersion == 'latest':
downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath)

View File

@ -1,5 +1,6 @@
import os
import re
from pathlib import Path
from utils.consoleoutput import oColors
from handlers.handle_config import checkConfig
@ -29,7 +30,7 @@ def removePlugin(pluginToRemove):
print(f"Removing: {fileName}")
if not checkConfig().localPluginFolder:
pluginPath = checkConfig().sftp_folderPath
pluginPath = f"{pluginPath}\\{plugin}"
pluginPath = f"{pluginPath}/{plugin}"
sftp = createSFTPConnection()
sftp.remove(pluginPath)
print(f"Removed: {fileName}")
@ -37,7 +38,7 @@ def removePlugin(pluginToRemove):
break
else:
pluginPath = checkConfig().pathToPluginFolder
pluginPath = f"{pluginPath}\\{plugin}"
pluginPath = Path(f"{pluginPath}/{plugin}")
os.remove(pluginPath)
print(f"Removed: {fileName}")
i += 1

View File

@ -1,6 +1,7 @@
import os
import re
from urllib.error import HTTPError
from pathlib import Path
from utils.consoleoutput import oColors
from utils.web_request import doAPIRequest
@ -138,6 +139,8 @@ def updateInstalledPackage(inputSelectedObject='all'):
fileVersion = getFileVersion(plugin)
pluginId = getInstalledPlugin(fileName, fileVersion)
latestVersion = getLatestPluginVersion(pluginId)
print(fileName)
print(fileVersion)
except TypeError:
continue
except ValueError:
@ -147,7 +150,7 @@ def updateInstalledPackage(inputSelectedObject='all'):
if pluginId == None:
print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite)
continue
print(INSTALLEDPLUGINLIST[i])
if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE):
if INSTALLEDPLUGINLIST[i][3] == True:
print(f" [{indexNumberUpdated+1}]".ljust(8), end='')
@ -161,7 +164,7 @@ def updateInstalledPackage(inputSelectedObject='all'):
pluginPath = checkConfig().sftp_pathToSeperateDownloadPath
else:
pluginPath = checkConfig().sftp_folderPath
pluginPath = f"{pluginPath}\\{plugin}"
pluginPath = Path(f"{pluginPath}/{plugin}")
sftp = createSFTPConnection()
indexNumberUpdated += 1
pluginsUpdated += 1
@ -173,13 +176,13 @@ def updateInstalledPackage(inputSelectedObject='all'):
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)
print(oColors.brightRed + "Error: Old plugin file coulnd't be deleted" + oColors.standardWhite)
else:
if checkConfig().seperateDownloadPath is True:
pluginPath = checkConfig().pathToSeperateDownloadPath
else:
pluginPath = checkConfig().pathToPluginFolder
pluginPath = f"{pluginPath}\\{plugin}"
pluginPath = Path(f"{pluginPath}/{plugin}")
indexNumberUpdated += 1
pluginsUpdated += 1
try:
@ -211,7 +214,7 @@ def updateInstalledPackage(inputSelectedObject='all'):
else:
pluginPath = checkConfig().sftp_folderPath
pluginPath = checkConfig().sftp_folderPath
pluginPath = f"{pluginPath}\\{plugin}"
pluginPath = f"{pluginPath}/{plugin}"
sftp = createSFTPConnection()
indexNumberUpdated += 1
pluginsUpdated += 1
@ -230,7 +233,7 @@ def updateInstalledPackage(inputSelectedObject='all'):
pluginPath = checkConfig().pathToSeperateDownloadPath
else:
pluginPath = checkConfig().pathToPluginFolder
pluginPath = f"{pluginPath}\\{plugin}"
pluginPath = Path(f"{pluginPath}/{plugin}")
indexNumberUpdated += 1
pluginsUpdated += 1
try:
@ -254,13 +257,13 @@ def updateInstalledPackage(inputSelectedObject='all'):
def getInstalledPlugin(localFileName, localFileVersion):
url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name&sort=-downloads"
packageName = doAPIRequest(url)
i = 1
#i = 1
plugin_match_found = False
pluginID = None
for ressource in packageName:
if plugin_match_found == True:
plugin_match_found = False
break
pID = ressource["id"]
url2 = f"https://api.spiget.org/v2/resources/{pID}/versions?size=100&sort=-name"
packageVersions = doAPIRequest(url2)
@ -273,23 +276,19 @@ def getInstalledPlugin(localFileName, localFileVersion):
plugin_latest_version = getLatestPluginVersion(pID)
plugin_is_outdated = compareVersions(plugin_latest_version, updateVersion)
addToPluginList(pID, updateId, plugin_latest_version , plugin_is_outdated)
break
print("in if")
print(plugin_match_found)
return pluginID # just testing
#break
i = i + 1
else:
else: # TODO fix duplicate entrys when update all
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)
print("in else")
print(plugin_match_found)
return pluginID
# start query
# get id
# search with id for all version upates
# get version that matches installed version
# if match then download latest update
# else get second query

View File

View File

@ -1,6 +1,7 @@
import os
import sys
from urllib.error import HTTPError
from pathlib import Path
from handlers.handle_sftp import createSFTPConnection, sftp_listFilesInServerRoot
from handlers.handle_config import checkConfig
@ -14,7 +15,6 @@ def checkInstalledServerjar():
serverRootList = sftp_listFilesInServerRoot(sftp)
else:
serverRootList = os.path.dirname(checkConfig().pathToPluginFolder)
os.chdir('..')
serverRootList = os.listdir(serverRootList)
installedServerjarFullName = None
try:
@ -44,16 +44,31 @@ def checkInstalledServerjar():
def updateServerjar(serverJarBuild='latest'):
try:
if serverJarBuild == None:
serverJarBuild = 'latest'
if not checkConfig().localPluginFolder:
sftp = createSFTPConnection()
serverRootPath = checkConfig().sftp_folderPath
serverRootPath = Path(str(serverRootPath).replace(r'/plugins', ''))
serverRootList = sftp_listFilesInServerRoot(sftp)
else:
serverRoot = os.path.dirname(checkConfig().pathToPluginFolder)
os.chdir('..')
serverRootList = os.listdir(serverRoot)
serverRootPath = checkConfig().pathToPluginFolder
helpPath = Path('/plugins')
helpPathstr = str(helpPath)
serverRootPath = Path(str(serverRootPath).replace(helpPathstr, ''))
installedServerjarFullName = None
except FileNotFoundError:
print(oColors.brightRed + "Path couldn't be found!" + oColors.standardWhite)
print(oColors.brightRed + "Check your config!" + oColors.standardWhite)
print(oColors.brightRed + "Aborting the process." + oColors.standardWhite)
input("Press any key + enter to exit...")
sys.exit()
try:
for files in serverRootList:
try:
@ -72,12 +87,27 @@ def updateServerjar(serverJarBuild='latest'):
input("Press any key + enter to exit...")
sys.exit()
serverJarPath = Path(f"{serverRootPath}/{installedServerjarFullName}")
if 'paper' in installedServerjarFullName:
print(serverJarBuild)
print(f"Updating Paper to build: {serverJarBuild}")
if not checkConfig().localPluginFolder:
try:
papermc_downloader(serverJarBuild, installedServerjarFullName)
sftp.remove(serverJarPath)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
except FileNotFoundError:
print(oColors.brightRed + "Error: Old serverjar file coulnd't be deleted" + oColors.standardWhite)
else:
try:
papermc_downloader(serverJarBuild, installedServerjarFullName)
os.remove(serverJarPath)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
except FileNotFoundError:
print(oColors.brightRed + "Error: Old serverjar file coulnd't be deleted" + oColors.standardWhite)
else:
print(oColors.brightRed + f"{installedServerjarFullName} isn't supported.")

View File

@ -2,14 +2,13 @@ import os
import sys
import re
import urllib.request
from pathlib import Path
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_config import checkConfig
from utils.utilities import createTempPluginFolder, deleteTempPluginFolder
from plugin.plugin_downloader import calculateFileSize
from utils.utilities import createTempPluginFolder, deleteTempPluginFolder, calculateFileSizeMb
# = 1.16.5
@ -50,12 +49,29 @@ def findVersionGroup(mcVersion):
if versionGroup == mcVersion:
versionGroupFound = True
paperVersionGroup = versionGroup
print(versionGroup)
break
return paperVersionGroup
def findBuildVersion(wantedPaperBuild):
versionGroups = ['1.16', '1.15']
paperBuildFound = False
for versionGroup in versionGroups:
if paperBuildFound is True:
break
url = f"https://papermc.io/api/v2/projects/paper/version_group/{versionGroup}/builds"
papermcdetails = doAPIRequest(url)
paperMcBuilds = papermcdetails["builds"]
for build in paperMcBuilds:
paperBuild = str(build["build"])
if paperBuild == wantedPaperBuild:
paperBuildFound = True
paperVersionGroup = build["version"]
break
return paperVersionGroup
def findLatestBuild(paperVersionGroup):
url = f"https://papermc.io/api/v2/projects/paper/version_group/{paperVersionGroup}/builds"
papermcbuilds = doAPIRequest(url)
@ -63,6 +79,13 @@ def findLatestBuild(paperVersionGroup):
return latestPaperBuild
def findLatestBuildForVersion(mcVersion):
url = f"https://papermc.io/api/v2/projects/paper/versions/{mcVersion}"
papermcbuilds = doAPIRequest(url)
latestPaperBuild = papermcbuilds["builds"][-1]
return latestPaperBuild
def versionBehind(installedPaperBuild, latestPaperBuild):
installedPaperBuildint = int(installedPaperBuild)
latestPaperBuildint = int(latestPaperBuild)
@ -70,7 +93,6 @@ def versionBehind(installedPaperBuild, latestPaperBuild):
return versionsBehind
def getDownloadFileName(paperMcVersion, paperBuild):
url = f"https://papermc.io/api/v2/projects/paper/versions/{paperMcVersion}/builds/{paperBuild}"
buildDetails = doAPIRequest(url)
@ -86,9 +108,9 @@ def paperCheckForUpdate(installedServerjarFullName):
paperVersionBehind = versionBehind(paperInstalledBuild, paperLatestBuild)
print(f"Paper for {mcVersion}")
print("Index | Name | Old V. | New V. | Versions behind ")
print("Index | Name | Installed V. | Latest V. | Versions behind ")
print(f" [1]".ljust(8), end='')
print(f"paper".ljust(24), end='')
print(f"paper".ljust(21), end='')
print(f"{paperInstalledBuild}".ljust(8), end='')
print(" ", end='')
print(f"{paperLatestBuild}".ljust(8), end='')
@ -102,27 +124,30 @@ def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersi
downloadPath = createTempPluginFolder()
else:
downloadPath = checkConfig().pathToPluginFolder
downloadPath = downloadPath.replace(r'\plugins', '')
helpPath = Path('/plugins')
helpPathstr = str(helpPath)
downloadPath = Path(str(downloadPath).replace(helpPathstr, ''))
if mcVersion == None:
if paperBuild == 'latest':
mcVersion = '1.16.5'
else:
mcVersion = findBuildVersion(paperBuild)
if installedServerjarName != None:
mcVersion = getInstalledPaperMinecraftVersion(installedServerjarName)
if paperBuild == 'latest':
versionGroup = findVersionGroup(mcVersion)
paperBuild = findLatestBuild(versionGroup)
paperBuild = findLatestBuildForVersion(mcVersion)
try:
downloadFileName = getDownloadFileName(mcVersion, paperBuild)
except KeyError:
print(oColors.brightRed + f"This version wasn't found for {mcVersion}" + oColors.standardWhite)
print(oColors.brightRed + f"Reverting to latest version for {mcVersion}" + oColors.standardWhite)
versionGroup = findVersionGroup(mcVersion)
paperBuild = findLatestBuild(versionGroup)
paperBuild = findLatestBuildForVersion(mcVersion)
downloadFileName = getDownloadFileName(mcVersion, paperBuild)
downloadPackagePath = f"{downloadPath}\\{downloadFileName}"
downloadPackagePath = Path(f"{downloadPath}/{downloadFileName}")
if checkConfig().localPluginFolder == False:
downloadPath = createTempPluginFolder()
@ -130,13 +155,16 @@ def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersi
url = f"https://papermc.io/api/v2/projects/paper/versions/{mcVersion}/builds/{paperBuild}/downloads/{downloadFileName}"
remotefile = urllib.request.urlopen(url)
filesize = remotefile.info()['Content-Length']
print(f"Starting paper-{paperBuild} download for {mcVersion}...")
print(f"Starting Paper-{paperBuild} download for {mcVersion}...")
urllib.request.urlretrieve(url, downloadPackagePath)
filesizeData = calculateFileSize(filesize)
filesizeData = calculateFileSizeMb(filesize)
print(f"Downloadsize: {filesizeData} KB")
print(f"Downloadsize: {filesizeData} MB")
print(f"File downloaded here: {downloadPackagePath}")
if not checkConfig().localPluginFolder:
sftpSession = createSFTPConnection()
sftp_upload_server_jar(sftpSession, downloadPackagePath)
deleteTempPluginFolder(downloadPath)
print(oColors.brightGreen + "Downloaded successfully " + oColors.standardWhite + f"Paper-{paperBuild}" + \
oColors.brightGreen + " for " + oColors.standardWhite + f"{mcVersion}" + oColors.standardWhite)

View File

@ -3,6 +3,7 @@ import os
import sys
import shutil
import requests
from pathlib import Path
from utils.consoleoutput import oColors
from handlers.handle_config import checkConfig
@ -59,7 +60,7 @@ def check_requirements():
def createTempPluginFolder():
tempPluginFolder = ".\\TempSFTPUploadFolder"
tempPluginFolder = Path("./TempSFTPUploadFolder")
if not os.path.isdir(tempPluginFolder):
try:
os.mkdir(tempPluginFolder)
@ -76,3 +77,10 @@ def deleteTempPluginFolder(tempPluginFolder):
shutil.rmtree(tempPluginFolder)
except OSError as e:
print ("Error: %s - %s." % (e.filename, e.strerror))
def calculateFileSizeMb(downloadFileSize):
fileSizeDownload = int(downloadFileSize)
fileSizeMb = fileSizeDownload / 1024 / 1024
roundedFileSize = round(fileSizeMb, 2)
return roundedFileSize