Added serverjar updating

Added serverjar updating: paper, waterfall, velocity
This commit is contained in:
Neocky 2022-07-02 19:15:15 +02:00
parent 9e74d480dd
commit 2ba9e6ddb7
6 changed files with 435 additions and 25 deletions

View File

@ -24,7 +24,7 @@ class config_value():
self.local_seperate_download_path = True if data["Local"]["SeperateDownloadPath"] == True else False
self.local_path_to_seperate_download_path = Path(data["Local"]["PathToSeperateDownloadPath"])
self.server = data["Remote"]["Server"]
self.username = data["Remote"]["Server"]
self.username = data["Remote"]["Username"]
self.password = data["Remote"]["Password"]
self.sftp_port = int(data["Remote"]["SFTP_Port"])
self.ftp_port = int(data["Remote"]["FTP_Port"])

View File

@ -5,15 +5,18 @@ Handles the input through the pluGET command line
from src.utils.console_output import rich_print_error
from src.plugin.plugin_downloader import get_specific_plugin_spiget, search_specific_plugin_spiget
from src.plugin.plugin_updatechecker import check_installed_plugins, update_installed_plugins
from src.serverjar.serverjar_updatechecker import check_update_available_installed_server_jar
from src.serverjar.serverjar_updatechecker import \
check_update_available_installed_server_jar, update_installed_server_jar
from src.serverjar.serverjar_paper_velocity_waterfall import serverjar_papermc_update
# check
# update
# get
# get-paper
# get-waterfall
# get-velocity
# get-purpur ???
# get-airplane ???
# exit
# remove
# search
@ -57,8 +60,7 @@ def handle_input(
case "update":
match input_selected_object:
case "serverjar":
print("update serverjar")
#updateServerjar(inputParams)
update_installed_server_jar(input_parameter)
case _:
update_installed_plugins(input_selected_object, no_confirmation)
@ -71,13 +73,16 @@ def handle_input(
case "search":
search_specific_plugin_spiget(input_selected_object)
case "remove":
print("remove package")
#removePlugin(inputSelectedObject)
# TODO add remover
#case "remove":
# print("remove package")
# #removePlugin(inputSelectedObject)
case "get-paper":
# download papermc
print("download papermc")
#papermc_downloader(inputSelectedObject, inputParams)
serverjar_papermc_update(input_selected_object, input_parameter, None, "paper")
case "get-velocity":
serverjar_papermc_update(input_selected_object, input_parameter, None, "velocity")
case "get-waterfall":
serverjar_papermc_update(input_selected_object, input_parameter, None, "waterfall")
case "exit":
return
case _:

View File

@ -0,0 +1,300 @@
"""
Handles the update checking and downloading of these serverjars:
Paper, Velocity, Waterfall
All are from the PaperMC Team and use the same api structure which is the reason these are together
"""
import re
import requests
from pathlib import Path
from rich.table import Table
from rich.console import Console
from rich.progress import Progress
from src.handlers.handle_config import config_value
from src.utils.console_output import rich_print_error
from src.handlers.handle_sftp import sftp_create_connection, sftp_upload_server_jar
from src.handlers.handle_ftp import ftp_create_connection, ftp_upload_server_jar
from src.utils.utilities import \
api_do_request, create_temp_plugin_folder, remove_temp_plugin_folder, convert_file_size_down
def get_installed_mc_version(file_server_jar_full_name) -> str:
"""
Gets the used minecraft version from the file name
:param file_server_jar_full_name: Full file name of the installed serverjar
:returns: Used minecraft version
"""
mc_version_full = re.search(r"(\d*\.*\d)+", file_server_jar_full_name)
try:
mc_version = mc_version_full.group()
except AttributeError:
mc_version = mc_version_full
return mc_version
def get_installed_serverjar_version(file_server_jar_full_name) -> str:
"""
Gets the installed version of the installed serverjar
:param file_server_jar_full_name: Full file name fo the installed serverjar
:returns: Used serverjar version
"""
serverjar_version_full = re.search(r"([\d]*.jar)", file_server_jar_full_name)
try:
serverjar_version = serverjar_version_full.group()
except AttributeError:
serverjar_version = serverjar_version_full
serverjar_version = serverjar_version.replace('.jar', '')
return serverjar_version
def get_version_group(file_server_jar_full_name) -> str:
"""
Gets the version group which is used for the papermc api
:param mc_version: Version of Minecraft in use
:returns: Version group of api
"""
version_group = re.sub(r"-\d*.jar$", "", file_server_jar_full_name)
version_group = re.sub(r"^(\w*\-)", "", version_group)
return version_group
def find_latest_available_version(file_server_jar_full_name, version_group) -> int:
"""
Gets the latest available version of the installed serverjar version
:param version_group: Minecraft version group of the serverjar
:returns: Latest available version as int
"""
if "paper" in file_server_jar_full_name:
url = f"https://papermc.io/api/v2/projects/paper/versions/{version_group}/builds"
elif "waterfall" in file_server_jar_full_name:
url = f"https://papermc.io/api/v2/projects/waterfall/versions/{version_group}/builds"
elif "velocity" in file_server_jar_full_name:
url = f"https://papermc.io/api/v2/projects/velocity/versions/{version_group}/builds"
versions = api_do_request(url)
if "status" in versions: # Checks if the API returns a status. This means that there was an error.
return None
latest_version = versions["builds"][-1]["build"]
return latest_version
def get_versions_behind(serverjar_version, latest_version) -> int:
"""
Gets the number diffference between the two versions
:param serverjar_version: Installed serverjar version
:param latest_version: Latest avaialable serverjar version
:returns: Number difference between the two versions
"""
versions_behind = int(latest_version) - int(serverjar_version)
return versions_behind
def get_papermc_download_file_name(mc_version, serverjar_version, file_server_jar_full_name) -> str:
"""
Gets the download name from the papermc api
:param mc_version: Minecraft version
:param serverjar_version: Version of the serverjar
:param file_server_jar_full_name: Serverjar name
:returns: Download name of the file
"""
if "paper" in file_server_jar_full_name:
url = f"https://papermc.io/api/v2/projects/paper/versions/{mc_version}/builds/{serverjar_version}"
elif "waterfall" in file_server_jar_full_name:
url = f"https://papermc.io/api/v2/projects/waterfall/versions/{mc_version}/builds/{serverjar_version}"
elif "velocity" in file_server_jar_full_name:
url = f"https://papermc.io/api/v2/projects/velocity/versions/{mc_version}/builds/{serverjar_version}"
build_details = api_do_request(url)
download_name = build_details["downloads"]["application"]["name"]
return download_name
def serverjar_papermc_check_update(file_server_jar_full_name) -> None:
"""
Checks the installed paper serverjar if an update is available
:param file_server_jar_full_name: Full name of the paper server jar file name
:returns: None
"""
"""
mc_version = get_installed_mc_version(file_server_jar_full_name)
if mc_version == None:
rich_print_error("Error: An error occured while checking the mc version of the serverjar")
return None
print(mc_version)
"""
serverjar_version = get_installed_serverjar_version(file_server_jar_full_name)
if serverjar_version == None:
rich_print_error("Error: An error occured while checking the installed serverjar version")
return None
version_group = get_version_group(file_server_jar_full_name)
if version_group == None:
rich_print_error(
"Error: An error occured while checking the installed version group of the installed serverjar"
)
return None
latest_version = find_latest_available_version(file_server_jar_full_name, version_group)
if latest_version == None:
rich_print_error("Error: An error occured while checking for the latest available version of the serverjar")
return None
versions_behind = get_versions_behind(serverjar_version, latest_version)
rich_table = Table(box=None)
rich_table.add_column("Name", style="bright_magenta")
rich_table.add_column("Installed V.", justify="right", style="green")
rich_table.add_column("Latest V.", justify="right", style="bright_green")
rich_table.add_column("Versions behind", justify="right", style="cyan")
rich_table.add_row(
file_server_jar_full_name,
serverjar_version,
str(latest_version),
str(versions_behind)
)
rich_console = Console()
rich_console.print(rich_table)
return None
def serverjar_papermc_update(
server_jar_version: str="latest",
mc_version: str=None,
file_server_jar_full_name: str=None,
serverjar_to_download: str=None
) -> bool:
"""
Handles the downloading of the papermc serverjar
:param server_jar_version: Version of the serverjar which should get downloaded
:param mc_version: Minecraft version
:param no_confirmation: If no confirmation message should pop up
:param file_server_jar_full_name: The old serverjar file
:param serverjar_to_download: The serverjar to download because it supports: paper, velocity, waterfall
This is used in the handle_input function
:returns: True/False if the serverjar was downloaded successfully
"""
config_values = config_value()
match config_values.connection:
case "local":
path_server_root = config_values.path_to_plugin_folder
# need help_path or else TypeError will be thrown
help_path = Path('/plugins')
help_path_str = str(help_path)
path_server_root = Path(str(path_server_root).replace(help_path_str, ''))
case _:
path_server_root = create_temp_plugin_folder()
# exit if the mc version can't be found
if file_server_jar_full_name == None and mc_version == None:
rich_print_error("Error: Please specifiy the minecraft version as third argument!")
return False
# if both the file name and the serverjar_to_download are emtpy then exit
if file_server_jar_full_name == None and serverjar_to_download == None:
rich_print_error("Error: Couldn't get serverjar name to download")
return False
if mc_version == None:
mc_version = get_version_group(file_server_jar_full_name)
if file_server_jar_full_name == None:
papermc_serverjar = serverjar_to_download
else:
papermc_serverjar = file_server_jar_full_name
if server_jar_version == "latest" or server_jar_version == None:
server_jar_version = find_latest_available_version(papermc_serverjar, mc_version)
rich_console = Console()
rich_console.print(
f"\n [not bold][bright_white]● [bright_magenta]{papermc_serverjar.capitalize()}" + \
f" [cyan]→ [bright_green]{server_jar_version}"
)
if file_server_jar_full_name != None:
serverjar_version = get_installed_serverjar_version(file_server_jar_full_name)
if get_versions_behind(serverjar_version, server_jar_version) == 0:
rich_console.print(" [not bold][bright_green]No updates currently available!")
return False
try:
download_file_name = get_papermc_download_file_name(mc_version, server_jar_version, papermc_serverjar)
except KeyError:
rich_print_error(f" Error: This version wasn't found for {mc_version}")
rich_print_error(f" Reverting to latest version for {mc_version}")
try:
server_jar_version = find_latest_available_version(papermc_serverjar, mc_version)
download_file_name = get_papermc_download_file_name(mc_version, server_jar_version, papermc_serverjar)
except KeyError:
rich_print_error(
f" Error: Version {mc_version} wasn't found for {papermc_serverjar.capitalize()} in the papermc api"
)
return False
if "paper" in papermc_serverjar:
url = f"https://papermc.io/api/v2/projects/paper/versions/{mc_version}" + \
f"/builds/{server_jar_version}/downloads/{download_file_name}"
elif "waterfall" in papermc_serverjar:
url = f"https://papermc.io/api/v2/projects/waterfall/versions/{mc_version}" + \
f"/builds/{server_jar_version}/downloads/{download_file_name}"
elif "velocity" in papermc_serverjar:
url = f"https://papermc.io/api/v2/projects/velocity/versions/{mc_version}" + \
f"/builds/{server_jar_version}/downloads/{download_file_name}"
download_path = Path(f"{path_server_root}/{download_file_name}")
with Progress(transient=True) as progress:
header = {'user-agent': 'pluGET/1.0'}
r = requests.get(url, headers=header, stream=True)
try:
file_size = int(r.headers.get('Content-Length'))
# create progress bar
download_task = progress.add_task(" [cyan]Downloading...", total=file_size)
except TypeError:
# Content-lenght returned nothing
file_size = 0
with open(download_path, 'wb') as f:
# split downloaded data in chunks of 65536
for data in r.iter_content(chunk_size=65536):
f.write(data)
# don't show progress bar if no content-length was returned
if file_size == 0:
continue
progress.update(download_task, advance=len(data))
#f.flush()
# use rich console for nice colors
file_size_data = convert_file_size_down(convert_file_size_down(file_size))
rich_console.print(" [not bold][bright_green]Downloaded[bright_magenta] " + (str(file_size_data)).rjust(9) + \
f" MB [cyan]→ [white]{download_path}")
if config_values.connection == "sftp":
sftp_session = sftp_create_connection()
sftp_upload_server_jar(sftp_session, download_path)
remove_temp_plugin_folder()
elif config_values.connection == "ftp":
ftp_session = ftp_create_connection()
ftp_upload_server_jar(ftp_session, download_path)
remove_temp_plugin_folder()
return True

View File

@ -3,12 +3,14 @@ Checks the installed serverjar for updates
"""
import os
from pathlib import Path
from rich.console import Console
from src.utils.console_output import rich_print_error
from src.handlers.handle_config import config_value
from src.utils.console_output import rich_print_error
from src.handlers.handle_sftp import sftp_create_connection, sftp_list_files_in_server_root
from src.handlers.handle_ftp import ftp_create_connection, ftp_list_files_in_server_root
from src.serverjar.serverjar_paper_velocity_waterfall import serverjar_papermc_check_update, serverjar_papermc_update
def get_installed_server_jar_file(config_values) -> str:
@ -49,22 +51,75 @@ def check_update_available_installed_server_jar() -> None:
"""
Handles the checking of available updates of the installed server jar
:params: None
:returns: None
"""
config_values = config_value()
rich_console = Console()
file_server_jar_full_name = get_installed_server_jar_file(config_values)
if file_server_jar_full_name == None:
# print error and exit function
rich_print_error("Error: Serverjar couldn't be found")
return None
rich_console.print(f"[not bold][cyan]Checking: [bright_magenta]{file_server_jar_full_name}")
# TODO: Add other serverjars here
if "paper" in file_server_jar_full_name:
print("paper check update")
print(file_server_jar_full_name)
if "paper" in file_server_jar_full_name or \
"waterfall" in file_server_jar_full_name or \
"velocity" in file_server_jar_full_name:
serverjar_papermc_check_update(file_server_jar_full_name)
else:
rich_print_error(f"{file_server_jar_full_name} isn't supported")
return None
def update_installed_server_jar(server_jar_version: str="latest") -> None:
"""
Handles the updating of the installed server jar
:returns: None
"""
config_values = config_value()
file_server_jar_full_name = get_installed_server_jar_file(config_values)
if file_server_jar_full_name == None:
# print error and exit function
rich_print_error("Error: Serverjar couldn't be found")
return None
# finding path which is used for deleting old server jar
match config_values.connection:
case "local":
path_server_root = config_values.path_to_plugin_folder
# need help_path or else TypeError will be thrown
help_path = Path('/plugins')
help_path_str = str(help_path)
path_server_root = Path(str(path_server_root).replace(help_path_str, ''))
case _:
path_server_root = config_values.remote_plugin_folder_on_server
path_server_root = str(path_server_root).replace(r'/plugins', '')
server_jar_path = f"{path_server_root}/{file_server_jar_full_name}"
rich_console = Console()
# TODO: Add other serverjars here
if "paper" in file_server_jar_full_name or \
"waterfall" in file_server_jar_full_name or \
"velocity" in file_server_jar_full_name:
download_successfull = serverjar_papermc_update(server_jar_version, None, file_server_jar_full_name, None)
if download_successfull is True:
match config_values.connection:
case "local":
os.remove(Path(server_jar_path))
case "sftp":
connection = sftp_create_connection()
connection.remove(server_jar_path)
case "ftp":
connection = ftp_create_connection()
connection.delete(server_jar_path)
rich_console.print(
" [not bold][bright_green]Deleted old server file [cyan]→ [white]" +
f"{file_server_jar_full_name}"
)
else:
rich_print_error(f"{file_server_jar_full_name} isn't supported")
return None

View File

@ -5,23 +5,23 @@ from src.utils import utilities
class TestCases(unittest.TestCase):
def test_handle_regex_package_name(self):
def test_handle_regex_plugin_name(self):
# Cropped name -> 'SUPERBPlugin'
plugin_name = "[1.13-5.49 ❤] >|> SUPERB Plugin <<💥| Now 150% OFF IN WINTER SALE IN SUMMER???"
plugin_name_cropped = "SUPERBPlugin"
result = plugin_downloader.handle_regex_package_name(plugin_name)
result = plugin_downloader.handle_regex_plugin_name(plugin_name)
self.assertEqual(result, plugin_name_cropped)
def test_get_version_id(self):
def test_get_version_id_spiget(self):
# 21840 -> "Luckperms" in Version 5.4.30
result = plugin_downloader.get_version_id("28140", "5.4.30")
result = plugin_downloader.get_version_id_spiget("28140", "5.4.30")
self.assertEqual(result, 455966)
def test_get_version_name(self):
def test_get_version_name_spiget(self):
# 455966 -> "5.4.30" from Luckperms
result = plugin_downloader.get_version_name("28140", 455966)
result = plugin_downloader.get_version_name_spiget("28140", 455966)
self.assertEqual(result, "5.4.30")

View File

@ -0,0 +1,50 @@
import unittest
from src.serverjar import serverjar_paper_velocity_waterfall
class TestCases(unittest.TestCase):
def test_get_installed_mc_version(self):
# paper-1.19-40.jar -> 1.19
serverjar_file_name = "paper-1.19-40.jar"
mc_version = "1.19"
result = serverjar_paper_velocity_waterfall.get_installed_mc_version(serverjar_file_name)
self.assertEqual(result, mc_version)
def test_get_installed_serverjar_version(self):
# paper-1.19-40.jar -> 40
serverjar_file_name = "paper-1.19-40.jar"
serverjar_version = "40"
result = serverjar_paper_velocity_waterfall.get_installed_serverjar_version(serverjar_file_name)
self.assertEqual(result, serverjar_version)
def test_get_version_group(self):
# 1.18.2 -> 1.18
mc_version = "1.18.2"
mc_version_group = "1.18.2"
result = serverjar_paper_velocity_waterfall.get_version_group(mc_version)
self.assertEqual(result, mc_version_group)
def test_find_latest_available_version(self):
# Get latest available paper version for 1.15.2 which should be 393
file_server_jar_full_name = "paper-1.15.2-40.jar"
version_group = "1.15.2"
result = serverjar_paper_velocity_waterfall.find_latest_available_version(
file_server_jar_full_name,
version_group
)
self.assertEqual(result, 393)
def test_get_versions_behind(self):
# 161 - 157 = 4
serverjar_version = 157
latest_version = 161
result = serverjar_paper_velocity_waterfall.get_versions_behind(serverjar_version, latest_version)
self.assertEqual(result, 4)
if __name__ == "__main__":
unittest.main()