Added support for purpur serverjar updating

- Added support for purpur updating and checking #43
- Removed get_installed_mc_version function which isn't needed anymore
This commit is contained in:
Jan-Luca Bogdan | BEL NET GmbH 2022-07-04 12:36:34 +02:00
parent 2e9973a24f
commit 119d44631f
7 changed files with 285 additions and 51 deletions

View File

@ -8,6 +8,7 @@ from src.plugin.plugin_updatechecker import check_installed_plugins, update_inst
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
from src.serverjar.serverjar_purpur import serverjar_purpur_update
# check
@ -83,6 +84,8 @@ def handle_input(
serverjar_papermc_update(input_selected_object, input_parameter, None, "velocity")
case "get-waterfall":
serverjar_papermc_update(input_selected_object, input_parameter, None, "waterfall")
case "get-purpur":
serverjar_purpur_update(input_selected_object, input_parameter, None)
case "exit":
return
case _:

View File

@ -20,22 +20,6 @@ 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
@ -50,6 +34,7 @@ def get_installed_serverjar_version(file_server_jar_full_name) -> str:
except AttributeError:
serverjar_version = serverjar_version_full
serverjar_version = serverjar_version.replace('.jar', '')
return serverjar_version
@ -130,14 +115,6 @@ def serverjar_papermc_check_update(file_server_jar_full_name) -> None:
: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")
@ -224,6 +201,7 @@ def serverjar_papermc_update(
if server_jar_version == "latest" or server_jar_version == None:
server_jar_version = find_latest_available_version(papermc_serverjar, mc_version)
# use rich console for nice colors
rich_console = Console()
rich_console.print(
f"\n [not bold][bright_white]● [bright_magenta]{papermc_serverjar.capitalize()}" + \
@ -282,7 +260,6 @@ def serverjar_papermc_update(
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) + \

View File

@ -0,0 +1,208 @@
"""
Handles the update checking and downloading of these serverjars:
Purpur
"""
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
from src.serverjar.serverjar_paper_velocity_waterfall import \
get_installed_serverjar_version, get_version_group, get_versions_behind
def find_latest_available_version(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
"""
url = f"https://api.purpurmc.org/v2/purpur/{version_group}/"
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"]["all"][-1]
return latest_version
def get_purpur_download_file_name(mc_version, serverjar_version) -> str:
"""
Gets the download name from the purpur api and merge it together in the right format
: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
"""
url = f"https://api.purpurmc.org/v2/purpur/{mc_version}/{serverjar_version}/"
build_details = api_do_request(url)
purpur_build_version = build_details["build"]
purpur_project_name = build_details["project"]
purpur_mc_version = build_details["version"]
download_name = f"{purpur_project_name}-{purpur_mc_version}-{purpur_build_version}.jar"
return download_name
def serverjar_purpur_check_update(file_server_jar_full_name) -> None:
"""
Checks the installed purpur serverjar if an update is available
:param file_server_jar_full_name: Full name of the purpu server jar file name
:returns: None
"""
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(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_purpur_update(
server_jar_version: str="latest",
mc_version: str=None,
file_server_jar_full_name: 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
: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 mc_version == None:
mc_version = get_version_group(file_server_jar_full_name)
if server_jar_version == "latest" or server_jar_version == None:
server_jar_version = find_latest_available_version(mc_version)
if file_server_jar_full_name == None:
serverjar_name = "purpur"
else:
serverjar_name = file_server_jar_full_name
# use rich console for nice colors
rich_console = Console()
rich_console.print(
f"\n [not bold][bright_white]● [bright_magenta]{serverjar_name.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_purpur_download_file_name(mc_version, server_jar_version)
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(mc_version)
download_file_name = get_purpur_download_file_name(mc_version, server_jar_version)
except KeyError:
rich_print_error(
f" Error: Version {mc_version} wasn't found for {serverjar_name.capitalize()} in the purpur api"
)
return False
url = f"https://api.purpurmc.org/v2/purpur/{mc_version}/{server_jar_version}/download/"
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()
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

@ -11,6 +11,7 @@ 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
from src.serverjar.serverjar_purpur import serverjar_purpur_check_update, serverjar_purpur_update
def get_installed_server_jar_file(config_values) -> str:
@ -61,14 +62,19 @@ def check_update_available_installed_server_jar() -> None:
return None
# TODO: Add other serverjars here
# Paper / Velocity / Waterfall
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)
# Purpur
elif "purpur" in file_server_jar_full_name:
serverjar_purpur_check_update(file_server_jar_full_name)
else:
rich_print_error(f"{file_server_jar_full_name} isn't supported")
rich_print_error(f"[not bold]Error: [bright_magenta]{file_server_jar_full_name} [bright_red]isn't supported")
return None
@ -99,27 +105,35 @@ def update_installed_server_jar(server_jar_version: str="latest") -> None:
server_jar_path = f"{path_server_root}/{file_server_jar_full_name}"
rich_console = Console()
download_successfull = False
# TODO: Add other serverjars here
# Paper / Velocity / Waterfall
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}"
)
# Purpur
elif "purpur" in file_server_jar_full_name:
download_successfull = serverjar_purpur_update(server_jar_version, None, file_server_jar_full_name)
else:
rich_print_error(f"{file_server_jar_full_name} isn't supported")
rich_print_error(f"[not bold]Error: [bright_magenta]{file_server_jar_full_name} [bright_red]isn't supported")
# remove old serverjar when the serverjar was sucessfully updated
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}"
)
return None

View File

@ -5,4 +5,4 @@ PLUGETVERSION = current version of pluGET
"""
# constant values
PLUGETVERSION = "1.7.0"
PLUGETVERSION = "1.7.1"

View File

@ -3,14 +3,6 @@ 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"

View File

@ -0,0 +1,40 @@
import unittest
from src.serverjar import serverjar_purpur
from src.serverjar import serverjar_paper_velocity_waterfall
class TestCases(unittest.TestCase):
def test_get_installed_serverjar_version(self):
# purpur-1.19-40.jar -> 40
serverjar_file_name = "purpur-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 purpur version for 1.15.2 which should be '606'
file_server_jar_full_name = "purpur-1.15.2-40.jar"
version_group = "1.15.2"
result = serverjar_purpur.find_latest_available_version(version_group)
self.assertEqual(result, str(606))
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()