Added papermc support & fixing some issues

Changes:
- adding papermc support
- paper can be checked for an update
- can be updated to latest or specific version
- download specific paper version for different minecraft versions
This commit is contained in:
Neocky 2021-03-15 16:26:00 +01:00
parent 20261d110e
commit 5e296e858f
5 changed files with 258 additions and 25 deletions

View File

@ -6,6 +6,9 @@ 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
from serverjar.serverjar_checker import checkInstalledServerjar, updateServerjar
from serverjar.serverjar_paper import papermc_downloader
def createInputLists():
global COMMANDLIST
@ -16,7 +19,8 @@ def createInputLists():
'search',
'exit',
'help',
'remove'
'remove',
'get-paper'
]
global INPUTSELECTEDOBJECT
INPUTSELECTEDOBJECT = [
@ -47,10 +51,16 @@ def handleInput(inputCommand, inputSelectedObject, inputParams):
searchPackage(inputSelectedObject)
break
if inputCommand == 'update':
updateInstalledPackage(inputSelectedObject)
if inputSelectedObject == 'serverjar':
updateServerjar(inputParams)
else:
updateInstalledPackage(inputSelectedObject)
break
if inputCommand == 'check':
checkInstalledPackage(inputSelectedObject)
if inputSelectedObject == 'serverjar':
checkInstalledServerjar()
else:
checkInstalledPackage(inputSelectedObject)
break
if inputCommand == 'search':
searchPackage(inputSelectedObject)
@ -63,6 +73,9 @@ def handleInput(inputCommand, inputSelectedObject, inputParams):
if inputCommand == 'remove':
removePlugin(inputSelectedObject)
break
if inputCommand == 'get-paper':
papermc_downloader(inputSelectedObject, inputParams)
break
else:
print(oColors.brightRed + "Command not found. Please try again." + oColors.standardWhite)
getInput()

View File

@ -45,6 +45,17 @@ def sftp_upload_file(sftp, itemPath):
sftp.close()
def sftp_upload_server_jar(sftp, itemPath):
try:
sftp.chdir('.')
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')
@ -56,4 +67,18 @@ def sftp_listAll(sftp):
try:
return installedPlugins
except UnboundLocalError:
print(oColors.brightRed + "No plugins were found." + oColors.standardWhite)
print(oColors.brightRed + "No plugins were found." + oColors.standardWhite)
def sftp_listFilesInServerRoot(sftp):
try:
#sftp.chdir('plugins')
filesInServerRoot = sftp.listdir()
except FileNotFoundError:
print(oColors.brightRed + "The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite)
try:
return filesInServerRoot
except UnboundLocalError:
print(oColors.brightRed + "No Serverjar was found." + oColors.standardWhite)

View File

@ -0,0 +1,85 @@
import os
import sys
from urllib.error import HTTPError
from handlers.handle_sftp import createSFTPConnection, sftp_listFilesInServerRoot
from handlers.handle_config import checkConfig
from utils.consoleoutput import oColors
from serverjar.serverjar_paper import paperCheckForUpdate, papermc_downloader
def checkInstalledServerjar():
if not checkConfig().localPluginFolder:
sftp = createSFTPConnection()
serverRootList = sftp_listFilesInServerRoot(sftp)
else:
serverRootList = os.path.dirname(checkConfig().pathToPluginFolder)
os.chdir('..')
serverRootList = os.listdir(serverRootList)
installedServerjarFullName = None
try:
for files in serverRootList:
try:
if '.jar' in files:
installedServerjarFullName = files
break
except TypeError:
continue
except TypeError:
print(oColors.brightRed + "Serverjar couldn't be found." + oColors.standardWhite)
print(oColors.brightRed + "Aborting the process." + oColors.standardWhite)
if installedServerjarFullName == None:
print(oColors.brightRed + "Serverjar couldn't be found." + oColors.standardWhite)
print(oColors.brightRed + "Aborting the process." + oColors.standardWhite)
input("Press any key + enter to exit...")
sys.exit()
if 'paper' in installedServerjarFullName:
paperCheckForUpdate(installedServerjarFullName)
else:
print(oColors.brightRed + f"{installedServerjarFullName} isn't supported.")
print(oColors.brightRed + "Aborting the process." + oColors.standardWhite)
def updateServerjar(serverJarBuild='latest'):
if serverJarBuild == None:
serverJarBuild = 'latest'
if not checkConfig().localPluginFolder:
sftp = createSFTPConnection()
serverRootList = sftp_listFilesInServerRoot(sftp)
else:
serverRoot = os.path.dirname(checkConfig().pathToPluginFolder)
os.chdir('..')
serverRootList = os.listdir(serverRoot)
installedServerjarFullName = None
try:
for files in serverRootList:
try:
if '.jar' in files:
installedServerjarFullName = files
break
except TypeError:
continue
except TypeError:
print(oColors.brightRed + "Serverjar couldn't be found." + oColors.standardWhite)
print(oColors.brightRed + "Aborting the process." + oColors.standardWhite)
if installedServerjarFullName == None:
print(oColors.brightRed + "Serverjar couldn't be found." + oColors.standardWhite)
print(oColors.brightRed + "Aborting the process." + oColors.standardWhite)
input("Press any key + enter to exit...")
sys.exit()
if 'paper' in installedServerjarFullName:
print(serverJarBuild)
try:
papermc_downloader(serverJarBuild, installedServerjarFullName)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
else:
print(oColors.brightRed + f"{installedServerjarFullName} isn't supported.")
print(oColors.brightRed + "Aborting the process." + oColors.standardWhite)

View File

@ -1,32 +1,142 @@
import os
import sys
import re
import urllib.request
from utils.consoleoutput import oColors
from utils.web_request import doAPIRequest
from handlers.handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection
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
# = 1.16.5
def getInstalledPaperMinecraftVersion(localPaperName):
print("test")
mcVersionFull = re.search(r'(\d*\.*\d)+', localPaperName)
try:
mcVersion = mcVersionFull.group()
except AttributeError:
mcVersion = mcVersionFull
return mcVersion
# = 550
def getInstalledPaperVersion(localPaperName):
print("test")
paperBuildFull = re.search(r'([\d]*.jar)', localPaperName)
try:
paperBuild = paperBuildFull.group()
except AttributeError:
paperBuild = paperBuildFull
paperBuild = paperBuild.replace('.jar', '')
return paperBuild
def findVersionGroup(mcVersion):
versionGroups = ['1.16', '1.15']
versionGroupFound = False
for versionGroup in versionGroups:
if versionGroupFound == True:
break
url = f"https://papermc.io/api/v2/projects/paper/version_group/{versionGroup}/builds"
papermcdetails = doAPIRequest(url)
papermcVersionForMc = papermcdetails["versions"]
for versions in papermcVersionForMc:
if versions == mcVersion:
versionGroupFound = True
paperVersionGroup = versionGroup
break
if versionGroup == mcVersion:
versionGroupFound = True
paperVersionGroup = versionGroup
print(versionGroup)
break
return paperVersionGroup
def findLatestBuild(paperVersionGroup):
url = f"https://papermc.io/api/v2/projects/paper/version_group/{paperVersionGroup}/builds"
papermcbuilds = doAPIRequest(url)
latestPaperBuild = papermcbuilds["builds"][-1]["build"]
return latestPaperBuild
def versionBehind(installedPaperBuild, latestPaperBuild):
installedPaperBuildint = int(installedPaperBuild)
latestPaperBuildint = int(latestPaperBuild)
versionsBehind = latestPaperBuildint - installedPaperBuildint
return versionsBehind
def getDownloadFileName(paperMcVersion, paperBuild):
url = f"https://papermc.io/api/v2/projects/paper/versions/{paperMcVersion}/builds/{paperBuild}"
buildDetails = doAPIRequest(url)
downloadName = buildDetails["downloads"]["application"]["name"]
return downloadName
def paperCheckForUpdate(installedServerjarFullName):
mcVersion = getInstalledPaperMinecraftVersion(installedServerjarFullName)
paperInstalledBuild = getInstalledPaperVersion(installedServerjarFullName)
versionGroup = findVersionGroup(mcVersion)
paperLatestBuild = findLatestBuild(versionGroup)
paperVersionBehind = versionBehind(paperInstalledBuild, paperLatestBuild)
print(f"Paper for {mcVersion}")
print("Index | Name | Old V. | New V. | Versions behind ")
print(f" [1]".ljust(8), end='')
print(f"paper".ljust(24), end='')
print(f"{paperInstalledBuild}".ljust(8), end='')
print(" ", end='')
print(f"{paperLatestBuild}".ljust(8), end='')
print(" ", end='')
print(f"{paperVersionBehind}".ljust(8))
# https://papermc.io/api/docs/swagger-ui/index.html?configUrl=/api/openapi/swagger-config#/
def papermc_downloader(paperBuild, paperVersionGroup='1.16'):
url = f"https://papermc.io/api/v2/projects/paper/{serverVersion}/{inputPackageVersion}" # v1 is deprecated
#https://papermc.io/api/v2/projects/paper/version_group/1.16/builds get all builds for all 1.16 versions
#https://papermc.io/api/v2/projects/paper/versions/1.16.5/builds/450 gets file name
# input serverversion = 1.16.5 inputpackageversion = 450
# regex 1.16 % build url
# search for 450 in version group 1.16
# if found get file name from builds/450
# build url & download package
# https://papermc.io/api/v2/projects/paper/versions/1.16.5/builds/450/downloads/paper-1.16.5-450.jar
def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersion=None):
if checkConfig().localPluginFolder == False:
downloadPath = createTempPluginFolder()
else:
downloadPath = checkConfig().pathToPluginFolder
downloadPath = downloadPath.replace(r'\plugins', '')
if mcVersion == None:
mcVersion = '1.16.5'
if installedServerjarName != None:
mcVersion = getInstalledPaperMinecraftVersion(installedServerjarName)
papermcdetails = doAPIRequest(url)
errorExists = papermcdetails["error"]
if errorExists in papermcdetails:
print(f"PaperMc version: {inputPackageVersion} couldn't be found")
print("Aborting the download of PaperMc.")
input("Press any key + enter to exit...")
sys.exit()
if paperBuild == 'latest':
versionGroup = findVersionGroup(mcVersion)
paperBuild = findLatestBuild(versionGroup)
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)
downloadFileName = getDownloadFileName(mcVersion, paperBuild)
downloadPackagePath = f"{downloadPath}\\{downloadFileName}"
if checkConfig().localPluginFolder == False:
downloadPath = createTempPluginFolder()
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}...")
urllib.request.urlretrieve(url, downloadPackagePath)
filesizeData = calculateFileSize(filesize)
print(f"Downloadsize: {filesizeData} KB")
print(f"File downloaded here: {downloadPackagePath}")
if not checkConfig().localPluginFolder:
sftpSession = createSFTPConnection()
sftp_upload_server_jar(sftpSession, downloadPackagePath)
deleteTempPluginFolder(downloadPath)

View File

@ -59,7 +59,7 @@ def check_requirements():
def createTempPluginFolder():
tempPluginFolder = ".\\plugins"
tempPluginFolder = ".\\TempSFTPUploadFolder"
if not os.path.isdir(tempPluginFolder):
try:
os.mkdir(tempPluginFolder)