Added part of update functionality

This commit is contained in:
Jan-Luca Bogdan | BEL NET GmbH 2022-06-22 12:58:48 +02:00
parent 041d7aa240
commit 3219e32351
3 changed files with 181 additions and 28 deletions

View File

@ -20,11 +20,10 @@ except TypeError:
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Arguments for pluGET", parser = argparse.ArgumentParser(description="Arguments for pluGET",
formatter_class=argparse.ArgumentDefaultsHelpFormatter) formatter_class=argparse.ArgumentDefaultsHelpFormatter)
#parser.add_argument("-a", "--archive", action="store_true", help="archive mode")
#parser.add_argument("--exclude", help="files to exclude")
parser.add_argument("mode", help="Mode (install/update/etc.)", nargs='?', default=None) parser.add_argument("mode", help="Mode (install/update/etc.)", nargs='?', default=None)
parser.add_argument("object", help="Object/Plugin Name", nargs='?', default=None) parser.add_argument("object", help="Object/Plugin Name", nargs='?', default=None)
parser.add_argument("version", help="Version", nargs='?', default=None) parser.add_argument("version", help="Version", nargs='?', default=None)
parser.add_argument("--no-confirmation", action="store_true", help="Skip confirmation messages")
args = vars(parser.parse_args()) args = vars(parser.parse_args())
rename_console_title() rename_console_title()
@ -32,12 +31,13 @@ if __name__ == "__main__":
validate_config() validate_config()
api_test_spiget() api_test_spiget()
check_requirements() check_requirements()
print(args)
if args["mode"] is not None and args["object"] is not None: if args["mode"] is not None and args["object"] is not None:
# arguments were used so call the handle_input function to get the right function call # arguments were used so call the handle_input function to get the right function call
print_console_logo() print_console_logo()
check_for_pluGET_update() check_for_pluGET_update()
handle_input(args["mode"], args["object"], args["version"], arguments_from_console=True) handle_input(args["mode"], args["object"], args["version"], args["no_confirmation"], arguments_from_console=True)
else: else:
# no arguments were used so start pluGET console # no arguments were used so start pluGET console
clear_console() clear_console()

View File

@ -4,25 +4,26 @@ Handles the input through the pluGET command line
from src.utils.console_output import rich_print_error from src.utils.console_output import rich_print_error
from src.plugin.plugin_downloader import get_specific_plugin, search_specific_plugin from src.plugin.plugin_downloader import get_specific_plugin, search_specific_plugin
from src.plugin.plugin_updatechecker import check_installed_plugins from src.plugin.plugin_updatechecker import check_installed_plugins, update_installed_plugins
# check # check
# update # update
# get # get
# get-paper # get-paper
# get-purpur # get-purpur ???
# get-airplane # get-airplane ???
# exit # exit
# remove # remove
# search ??? # search
def handle_input( def handle_input(
input_command: str=None, input_command : str=None,
input_selected_object: str=None, input_selected_object : str=None,
input_parameter: str=None, input_parameter : str=None,
arguments_from_console: bool=False no_confirmation : bool=False,
arguments_from_console : bool=False
) -> None: ) -> None:
""" """
Manages the correct function calling from the given input Manages the correct function calling from the given input
@ -45,14 +46,12 @@ def handle_input(
search_specific_plugin(input_selected_object) search_specific_plugin(input_selected_object)
case "update": case "update":
print("update package")
match input_selected_object: match input_selected_object:
case "serverjar": case "serverjar":
print("update serverjar") print("update serverjar")
#updateServerjar(inputParams) #updateServerjar(inputParams)
case _: case _:
print("update package") update_installed_plugins(input_selected_object, no_confirmation)
#updateInstalledPackage(inputSelectedObject)
case "check": case "check":
match input_selected_object: match input_selected_object:

View File

@ -8,13 +8,17 @@ from pathlib import Path
from rich.progress import track from rich.progress import track
from rich.table import Table from rich.table import Table
from rich.console import Console from rich.console import Console
from urllib.error import HTTPError
from src.handlers.handle_config import config_value from src.handlers.handle_config import config_value
from src.handlers.handle_sftp import sftp_create_connection, sftp_validate_file_attributes, sftp_list_all from src.handlers.handle_sftp import sftp_create_connection, sftp_validate_file_attributes, sftp_list_all
from src.handlers.handle_ftp import ftp_create_connection, ftp_validate_file_attributes, ftp_list_all from src.handlers.handle_ftp import ftp_create_connection, ftp_validate_file_attributes, ftp_list_all
from src.plugin.plugin_downloader import get_specific_plugin, get_download_path
from src.utils.console_output import rich_print_error
from src.utils.utilities import api_do_request from src.utils.utilities import api_do_request
class Plugin(): class Plugin():
""" """
Create plugin class to store installed plugins inside it Create plugin class to store installed plugins inside it
@ -173,16 +177,15 @@ def compare_plugin_version(plugin_latest_version : str, plugin_file_version : st
return False return False
def check_installed_plugins(input_selected_object : str="all", input_parameter : str=None) -> None: def check_update_available_installed_plugins(input_selected_object : str, config_values: config_value) -> str:
""" """
Gets installed plugins and checks it against the apis if there are updates for the plugins available Gets installed plugins and checks it against the apis if there are updates for the plugins available
:param input_selected_object: Which plugin should be checked :param input_selected_object: Command line input (default: all)
:param input_parameter: Optional parameters :param config_values: Config values from config file
:returns: None :returns: Count of plugins, Count of plugins with available updates
""" """
config_values = config_value()
Plugin.create_plugin_list() Plugin.create_plugin_list()
match config_values.connection: match config_values.connection:
case "sftp": case "sftp":
@ -202,18 +205,18 @@ def check_installed_plugins(input_selected_object : str="all", input_parameter :
match config_values.connection: match config_values.connection:
case "sftp": case "sftp":
plugin_attributes = sftp_validate_file_attributes( plugin_attributes = sftp_validate_file_attributes(
connection, f"{config_values.remote_plugin_folder_on_server}/{plugin}" connection, f"{config_values.remote_plugin_folder_on_server}/{plugin_file}"
) )
case "ftp": case "ftp":
plugin_attributes = ftp_validate_file_attributes( plugin_attributes = ftp_validate_file_attributes(
connection, f"{config_values.remote_plugin_folder_on_server}/{plugin}" connection, f"{config_values.remote_plugin_folder_on_server}/{plugin_file}"
) )
case _: case _:
if not os.path.isfile(Path(f"{plugin_folder_path}/{plugin_file}")): if not os.path.isfile(Path(f"{plugin_folder_path}/{plugin_file}")):
plugin_attributes = False plugin_attributes = False
if not re.search(r'.jar$', plugin_file): if not re.search(r'.jar$', plugin_file):
plugin_attributes = False plugin_attributes = False
# skip plugin if no attributes were found # skip plugin if no attributes were found to skip not valid plugin files
if plugin_attributes == False: if plugin_attributes == False:
continue continue
@ -229,12 +232,28 @@ def check_installed_plugins(input_selected_object : str="all", input_parameter :
# TODO add more plugin repositories here # TODO add more plugin repositories here
# plugin wasn't found and not added to global plugin list so add # plugin wasn't found and not added to global plugin list so add
if plugin_file not in INSTALLEDPLUGINLIST[-1].plugin_file_name: try:
print(f"skipped {plugin_file}") if plugin_file not in INSTALLEDPLUGINLIST[-1].plugin_file_name:
Plugin.add_to_plugin_list(plugin_file, plugin_file_name, plugin_file_version, 'N/A', False, 'N/A', ())
except IndexError:
Plugin.add_to_plugin_list(plugin_file, plugin_file_name, plugin_file_version, 'N/A', False, 'N/A', ()) Plugin.add_to_plugin_list(plugin_file, plugin_file_name, plugin_file_version, 'N/A', False, 'N/A', ())
if INSTALLEDPLUGINLIST[-1].plugin_is_outdated == True: if INSTALLEDPLUGINLIST[-1].plugin_is_outdated == True:
plugins_with_udpates += 1 plugins_with_udpates += 1
plugin_count += 1 plugin_count += 1
return plugin_count, plugins_with_udpates
def check_installed_plugins(input_selected_object : str="all", input_parameter : str=None) -> None:
"""
Prints table overview of installed plugins with versions and available updates
:param input_selected_object: Which plugin should be checked
:param input_parameter: Optional parameters
:returns: None
"""
config_values = config_value()
plugin_count, plugins_with_udpates = check_update_available_installed_plugins(input_selected_object, config_values)
# print rich table of found plugins and result # print rich table of found plugins and result
rich_table = Table(box=None) rich_table = Table(box=None)
@ -269,6 +288,141 @@ def check_installed_plugins(input_selected_object : str="all", input_parameter :
return None return None
def ask_update_confirmation(input_selected_object : str) -> bool:
"""
Prints confirmation message of plugins which get updated and ask for confirmation
:param input_selected_object: Command line input
:returns: True or False if plugins should be udpated
"""
rich_console = Console()
rich_console.print("Selected plugins with available Updates:")
for plugin_file in INSTALLEDPLUGINLIST:
if plugin_file.plugin_is_outdated == False:
continue
if input_selected_object != "all" and input_selected_object != "*":
if re.search(input_selected_object, plugin_file.plugin_file_name, re.IGNORECASE):
rich_console.print(f"[not bold][bright_magenta]{plugin_file.plugin_name}", end=' ')
break
rich_console.print(f"[not bold][bright_magenta]{plugin_file.plugin_name}", end=' ')
rich_console.print()
update_confirmation = input("Update these plugins [y/n] ? ")
if str.lower(update_confirmation) != "y":
rich_print_error("Aborting the update process")
return False
return True
def update_installed_plugins(input_selected_object : str="all", no_confirmation : bool=False) -> None:
"""
Checks if a plugin list exists and if so updates the selected plugins if there is an update available
:param input_selected_object: Plugin name to update (use 'all' or '*' for everything)
:param no_confirmation: Don't ask for confirmation if pluGET was called with param: --no-confirmation
:returns: None
"""
config_values = config_value()
match config_values.connection:
case "sftp":
connection = sftp_create_connection()
case "ftp":
connection = ftp_create_connection()
# if INSTALLEDPLUGINLIST was not previously filled by 'check' command call the command to fill plugin list
try:
if len(INSTALLEDPLUGINLIST) == 0:
check_update_available_installed_plugins(input_selected_object, config_values)
except NameError:
check_update_available_installed_plugins(input_selected_object, config_values)
# if argument 'all' was given recheck all plugins to avoid having only a few plugins from previously cached checks
if input_selected_object == "all" or input_selected_object == "*":
check_update_available_installed_plugins(input_selected_object, config_values)
# skip confirmation message if pluGET was called with --no-confirmation
if no_confirmation == False:
if ask_update_confirmation(input_selected_object) == False:
return None
# used later for output as stats
plugins_updated = 0
#for plugin in track(INSTALLEDPLUGINLIST, description="[cyan]Updating...", transient=True, style="bright_yellow"):
for plugin in INSTALLEDPLUGINLIST:
# supports command 'update pluginname' and skip the updating of every other plugin to speed things up a bit
if input_selected_object != "all" and input_selected_object != "*":
if not re.search(input_selected_object, plugin.plugin_file_name, re.IGNORECASE):
continue
if plugin.plugin_is_outdated == False:
continue
plugin_path = get_download_path(config_values)
match config_values.connection:
# local plugin folder
case "local":
match (plugin.plugin_repository):
case "spigot":
try:
plugins_updated += 1
get_specific_plugin(plugin.plugin_repository_data[0])
except HTTPError as err:
rich_print_error(f"HTTPError: {err.code} - {err.reason}")
plugins_updated -= 1
except TypeError:
rich_print_error(
f"Error: TypeError > Couldn't download new version. Is the file available on spigotmc?"
)
plugins_updated -= 1
case _:
rich_print_error("Error: Plugin repository wasn't found")
return None
try:
os.remove(Path(f"{plugin_path}/{plugin.plugin_file_name}"))
except FileNotFoundError:
rich_print_error("Error: Old plugin file couldn't be deleted")
# plugin folder is on sftp or ftp server
case _:
plugin_path = f"{plugin_path}/{plugin.plugin_file_name}"
match (plugin.plugin_repository):
case "spigot":
try:
plugins_updated += 1
get_specific_plugin(plugin.plugin_repository_data[0])
except HTTPError as err:
rich_print_error(f"HTTPError: {err.code} - {err.reason}")
plugins_updated -= 1
except TypeError:
rich_print_error(
f"Error: TypeError > Couldn't download new version. Is the file available on spigotmc?"
)
plugins_updated -= 1
case _:
rich_print_error("Error: Plugin repository wasn't found")
return None
match config_values.connection:
case "sftp":
try:
connection.remove(plugin_path)
except FileNotFoundError:
rich_print_error("Error: Old plugin file couldn't be deleted")
case "ftp":
try:
connection.delete(plugin_path)
except FileNotFoundError:
rich_print_error("Error: Old plugin file couldn't be deleted")
def search_plugin_spigot(plugin_file : str, plugin_file_name : str, plugin_file_version : str) -> int: def search_plugin_spigot(plugin_file : str, plugin_file_name : str, plugin_file_version : str) -> int:
""" """
Search the spigot api for the installed plugin and add it to the installed plugin list Search the spigot api for the installed plugin and add it to the installed plugin list
@ -317,7 +471,7 @@ def search_plugin_spigot(plugin_file : str, plugin_file_name : str, plugin_file_
plugin_latest_version, plugin_latest_version,
plugin_is_outdated, plugin_is_outdated,
"spigot", "spigot",
(plugin_id) [plugin_id]
) )
return plugin_id return plugin_id
return None return None