Added sftp & ftp functionality back

This commit is contained in:
Jan-Luca Bogdan | BEL NET GmbH 2022-06-21 17:01:03 +02:00
parent 96b4411020
commit 041d7aa240
6 changed files with 499 additions and 71 deletions

187
src/handlers/handle_ftp.py Normal file
View File

@ -0,0 +1,187 @@
import os
import sys
import ftplib
import re
from src.utils.console_output import rich_print_error
from src.handlers.handle_config import config_value
def ftp_create_connection():
"""
Creates a connection to the ftp server with the given values in the config
:returns: ftp connection type
"""
config_values = config_value()
try:
ftp = ftplib.FTP()
ftp.connect(config_values.server, config_values.ftp_port)
ftp.login(config_values.username, config_values.password)
return ftp
except UnboundLocalError:
rich_print_error("Error: [SFTP]: Check your config file!")
rich_print_error("Exiting program...")
sys.exit()
def ftp_show_plugins(ftp) -> None:
"""
Prints all plugins in the plugin folder
:param ftp: ftp connection
:returns: None
"""
config_values = config_value()
ftp.cwd(config_values.remote_plugin_folder_on_server)
for attr in ftp.dir():
print(attr.filename, attr)
return None
def ftp_upload_file(ftp, path_item) -> None:
"""
Uploads a file to the ftp server
:param ftp: ftp connection
:param path_item: Name of the item which should be uploaded
:returns: None
"""
config_values = config_value()
if config_values.remote_seperate_download_path is True:
path_upload_folder = config_values.remote_path_to_seperate_download_path
else:
path_upload_folder = config_values.remote_plugin_folder_on_server
try:
ftp.cwd(path_upload_folder)
path_item = os.path.relpath(path_item, 'TempSFTPFolder/')
path_item = str(path_item)
current_directory = os.getcwd()
os.chdir('TempSFTPFolder')
with open (path_item, 'rb') as plugin_file:
ftp.storbinary('STOR '+ str(path_item), plugin_file)
except FileNotFoundError:
rich_print_error("Error: [FTP]: The 'plugins' folder couldn't be found on the remote host!")
rich_print_error("Error: [FTP]: Aborting uploading.")
os.chdir(current_directory)
ftp.close()
return None
def ftp_upload_server_jar(ftp, path_item) -> None:
"""
Uploads a serverjar to the root folder of the ftp host
:param ftp: ftp connection
:param path_item: Name of the file which should be uploaded
:returns: None
"""
try:
ftp.cwd('.')
path_item = os.path.relpath(path_item, 'TempSFTPFolder/')
path_item = str(path_item)
current_directory = os.getcwd()
os.chdir('TempSFTPFolder')
with open (path_item, 'rb') as server_jar:
ftp.storbinary('STOR '+ str(path_item), server_jar)
except FileNotFoundError:
rich_print_error("Error: [FTP]: The 'root' folder couldn't be found on the remote host!")
rich_print_error("Error: [FTP]: Aborting uploading.")
os.chdir(current_directory)
ftp.close()
return None
def ftp_list_all(ftp):
"""
Returns a list with all installed plugins in the plugin folder of the ftp host
:param ftp: ftp connection
:returns: List of all plugins in plugin folder
"""
config_values = config_value()
try:
ftp.cwd(config_values.remote_plugin_folder_on_server)
installed_plugins = ftp.nlst()
except FileNotFoundError:
rich_print_error("Error: [FTP]: The 'plugins' folder couldn't be found on the remote host!")
try:
return installed_plugins
except UnboundLocalError:
rich_print_error("Error: [FTP]: No plugins were found.")
def ftp_listFilesInServerRoot(ftp):
"""
Returns a list with all files in the root folder of the ftp host
:param ftp: ftp connection
:returns: List of all files in root folder
"""
try:
ftp.cwd('.')
filesInServerRoot = ftp.nlst()
except FileNotFoundError:
rich_print_error("Error: [FTP]: The 'root' folder couldn't be found on the remote host!")
try:
return filesInServerRoot
except UnboundLocalError:
rich_print_error("Error: [FTP]: No Serverjar was found.")
def ftp_downloadFile(ftp, path_download, file) -> None:
"""
Download a file of the ftp server
:param ftp: ftp connection
:param path_download: Path to save downloaded file to
:param file: File to download
:returns None
"""
config_values = config_value()
ftp.cwd(config_values.remote_plugin_folder_on_server)
filedata = open(path_download,'wb')
ftp.retrbinary('RETR '+file, filedata.write)
filedata.close()
ftp.quit()
return None
def ftp_is_file(ftp, plugin_path) -> bool:
"""
Check if file on ftp host is a file and not a directory
:param ftp: ftp connection
:param plugin_path
:returns: True if file is a file and not a directory
"""
if ftp.nlst(plugin_path) == [plugin_path]:
return True
else:
return False
def ftp_validate_file_attributes(ftp, plugin_path) -> bool:
"""
Check if a file is a legitimate plugin file
:param ftp: ftp connection
:param plugin_path: Path of file to check
:returns: If file is a plugin file or not
"""
if ftp_is_file(ftp, plugin_path) is False:
return False
if re.search(r'.jar$', plugin_path):
return True
else:
return False

View File

@ -18,7 +18,12 @@ from src.plugin.plugin_updatechecker import check_installed_plugins
# search ???
def handle_input(input_command=None, input_selected_object=None, input_parameter=None, arguments_from_console=False) -> None:
def handle_input(
input_command: str=None,
input_selected_object: str=None,
input_parameter: str=None,
arguments_from_console: bool=False
) -> None:
"""
Manages the correct function calling from the given input
"""
@ -50,13 +55,11 @@ def handle_input(input_command=None, input_selected_object=None, input_parameter
#updateInstalledPackage(inputSelectedObject)
case "check":
print("check package")
match input_selected_object:
case "serverjar":
print("check serverjar")
#checkInstalledServerjar()
case _:
print("check plugins")
check_installed_plugins(input_selected_object, input_parameter)
case "search":
@ -81,7 +84,7 @@ def handle_input(input_command=None, input_selected_object=None, input_parameter
def get_input() -> None:
"""
Gets command line input and calls the handle input function
Gets command line input and calls the handle input function
"""
input_command = None
print("\n'STRG + C' to exit")

168
src/handlers/handle_sftp.py Normal file
View File

@ -0,0 +1,168 @@
import sys
import os
import pysftp
import paramiko
import stat
import re
from src.utils.console_output import rich_print_error
from src.handlers.handle_config import config_value
def sftp_create_connection():
"""
Creates a sftp connection with the given values in the config file
:returns: SFTP connection type
"""
config_values = config_value()
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None # TODO fix this
try:
sftp = pysftp.Connection(config_values.server, username=config_values.username, \
password=config_values.password, port=config_values.sftp_port, cnopts=cnopts)
except paramiko.ssh_exception.AuthenticationException:
rich_print_error("Error: [SFTP]: Wrong Username/Password")
except paramiko.ssh_exception.SSHException:
rich_print_error("Error: [SFTP]: The SFTP server isn't available.")
try:
return sftp
except UnboundLocalError:
rich_print_error("Error: [SFTP]: Check your config file!")
rich_print_error("Exiting program...")
sys.exit()
def sftp_show_plugins(sftp) -> None:
"""
Prints all plugins in the sftp folder
:param sftp: sftp connection
:returns: None
"""
config_values = config_value()
sftp.cd(config_values.remote_plugin_folder_on_server)
for attr in sftp.listdir_attr():
print(attr.filename, attr)
sftp.close()
return None
def sftp_upload_file(sftp, path_item) -> None:
"""
Uploads a file to the set folder from the config file
:param sftp: sftp connection
:param path_item: The upload path with the item name
:returns: None
"""
config_values = config_value()
if config_values.remote_seperate_download_path is True:
path_upload_folder = config_values.remote_path_to_seperate_download_path
else:
path_upload_folder = config_values.remote_plugin_folder_on_server
try:
sftp.chdir(path_upload_folder)
sftp.put(path_item)
except FileNotFoundError:
rich_print_error("Error: [SFTP]: The 'plugins' folder couldn't be found on the remote host!")
rich_print_error("Error: [SFTP]: Aborting uploading.")
sftp.close()
return None
def sftp_upload_server_jar(sftp, path_item) -> None:
"""
Uploads the server jar to the root folder
:param sftp: sftp connection
:param path_item: The upload path with the item name
:returns: None
"""
try:
sftp.chdir('.')
sftp.put(path_item)
except FileNotFoundError:
rich_print_error("Error: [SFTP]: The 'root' folder couldn't be found on the remote host!")
rich_print_error("Error: [SFTP]: Aborting uploading.")
sftp.close()
return None
def sftp_list_all(sftp):
"""
List all plugins in the 'plugins' folder on the sftp host
:param sftp: sftp connection
:return: List of plugins in plugin folder
"""
config_values = config_value()
try:
sftp.chdir(config_values.remote_plugin_folder_on_server)
installed_plugins = sftp.listdir()
except FileNotFoundError:
rich_print_error("Error: [SFTP]: The 'plugins' folder couldn't be found on the remote host!")
try:
return installed_plugins
except UnboundLocalError:
rich_print_error("Error: [SFTP]: No plugins were found.")
def sftp_list_files_in_server_root(sftp):
"""
List all files in the root folder on the sftp host
:param sftp: sftp connection
:returns: List of files in root folder
"""
try:
files_in_server_root = sftp.listdir()
except FileNotFoundError:
rich_print_error("Error: [SFTP]: The 'root' folder couldn't be found on the remote host!")
try:
return files_in_server_root
except UnboundLocalError:
rich_print_error("Error: [SFTP]: No Serverjar was found.")
def sftp_download_file(sftp, file) -> None:
"""
Downloads a plugin file from the sftp host to a temporary folder
:param sftp: sftp connection
:param file: Filename of plugin
:returns: None
"""
config_values = config_value()
sftp.cwd(config_values.remote_plugin_folder_on_server)
current_directory = os.getcwd()
os.chdir('TempSFTPFolder')
sftp.get(file)
sftp.close()
os.chdir(current_directory)
return None
def sftp_validate_file_attributes(sftp, plugin_path) -> bool:
"""
Check if the file is a legitimate plugin file
:param sftp: sftp connection
param plugin_path: Path of the single plugin file
:returns: If file is a plugin file or not
"""
plugin_sftp_attribute = sftp.lstat(plugin_path)
if stat.S_ISDIR(plugin_sftp_attribute.st_mode):
return False
elif re.search(r'.jar$', plugin_path):
return True
else:
return False

View File

@ -14,6 +14,8 @@ from src.utils.utilities import convert_file_size_down, remove_temp_plugin_folde
from src.utils.utilities import api_do_request
from src.utils.console_output import rich_print_error
from src.handlers.handle_config import config_value
from src.handlers.handle_sftp import sftp_create_connection, sftp_upload_file
from src.handlers.handle_ftp import ftp_create_connection, ftp_upload_file
def handle_regex_plugin_name(full_plugin_name) -> str:
@ -95,7 +97,7 @@ def download_specific_plugin_version_spiget(plugin_id, download_path, version_id
"""
Download a specific plugin
"""
#config_values = config_value()
config_values = config_value()
if version_id != "latest" and version_id != None:
#url = f"https://spigotmc.org/resources/{plugin_id}/download?version={versionID}"
rich_print_error("Sorry but specific version downloads aren't supported because of cloudflare protection. :(")
@ -142,12 +144,12 @@ def download_specific_plugin_version_spiget(plugin_id, download_path, version_id
console.print(" [not bold][bright_green]Downloaded[bright_magenta] " + (str(file_size_data)).rjust(9) + \
f" KB [cyan]→ [white]{download_path}")
# TODO add sftp and ftp support
#if config_values.connection == "sftp":
# sftp_session = createSFTPConnection()
# sftp_upload_file(sftp_session, download_path)
#elif config_values.connection == "ftp":
# ftp_session = createFTPConnection()
# ftp_upload_file(ftp_session, download_path)
if config_values.connection == "sftp":
sftp_session = sftp_create_connection()
sftp_upload_file(sftp_session, download_path)
elif config_values.connection == "ftp":
ftp_session = ftp_create_connection()
ftp_upload_file(ftp_session, download_path)
return None
@ -238,7 +240,6 @@ def search_specific_plugin(plugin_name) -> None:
except IndexError:
rich_print_error("Error: Number was out of range! Please try again!")
return None
console = Console()
selected_plugin_name = handle_regex_plugin_name(plugin_search_results[plugin_selected]["name"])
console.print(f"\n [bright_white]● [bright_magenta]{selected_plugin_name} [bright_green]latest")
rich_console.print(f"\n [bright_white]● [bright_magenta]{selected_plugin_name} [bright_green]latest")
get_specific_plugin(plugin_selected_id)

View File

@ -6,18 +6,37 @@ import os
import re
from pathlib import Path
from rich.progress import track
from rich.table import Table
from rich.console import Console
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_ftp import ftp_create_connection, ftp_validate_file_attributes, ftp_list_all
from src.utils.utilities import api_do_request
class plugin():
class Plugin():
"""
Create plugin class to store installed plugins inside it
"""
def __init__(self, plugin_name, plugin_file_version) -> None:
def __init__(
self,
plugin_file_name : str,
plugin_name : str,
plugin_file_version : str,
plugin_latest_version : str,
plugin_is_outdated : bool,
plugin_repository : str,
plugin_repository_data : list
) -> None:
self.plugin_file_name = plugin_file_name
self.plugin_name = plugin_name
self.plugin_file_version = plugin_file_version
self.plugin_latest_version = plugin_latest_version
self.plugin_is_outdated = plugin_is_outdated
self.plugin_repository = plugin_repository
self.plugin_repository_data = plugin_repository_data
@staticmethod
@ -31,29 +50,31 @@ class plugin():
@staticmethod
def add_to_plugin_list(plugin_name, plugin_file_version) -> None:
def add_to_plugin_list(
plugin_file_name: str,
plugin_name : str,
plugin_file_version : str,
plugin_latest_version : str,
plugin_is_outdated : bool,
plugin_repository : str,
plugin_repository_data : list
) -> None:
"""
Adds a plugin to global installed plugin lists
"""
INSTALLEDPLUGINLIST.append(plugin(plugin_name, plugin_file_version))
INSTALLEDPLUGINLIST.append(Plugin(
plugin_file_name,
plugin_name,
plugin_file_version,
plugin_latest_version,
plugin_is_outdated,
plugin_repository,
plugin_repository_data
))
return None
@staticmethod
def get_plugin_list_length() -> int:
"""
Returns the lenght of the plugin list
"""
return len(INSTALLEDPLUGINLIST)
#plugin.create_plugin_list()
#plugin.add_to_plugin_list("test2", 12345)
#for i in INSTALLEDPLUGINLIST: print(i.plugin_name)
#print(plugin.get_plugin_list_length())
def get_plugin_file_name(plugin_full_name) -> str:
def get_plugin_file_name(plugin_full_name : str) -> str:
"""
Finds the full plugin name of the given string
Example LuckPerms-5.4.30.jar -> Luckperms
@ -78,7 +99,7 @@ def get_plugin_file_name(plugin_full_name) -> str:
return plugin_name_only
def get_plugin_file_version(plugin_full_name) -> str:
def get_plugin_file_version(plugin_full_name : str) -> str:
"""
Gets the version of the plugin
@ -95,7 +116,7 @@ def get_plugin_file_version(plugin_full_name) -> str:
return plugin_file_version
def get_latest_spigot_plugin_version(plugin_id) -> str:
def get_latest_spigot_plugin_version(plugin_id : str) -> str:
"""
Gets the latest spigot plugin version
@ -108,7 +129,7 @@ def get_latest_spigot_plugin_version(plugin_id) -> str:
return str(latest_update_search["name"])
def create_plugin_version_tuple(plugin_version_string) -> tuple:
def create_plugin_version_tuple(plugin_version_string : str) -> tuple:
"""
Create a tuple of all version numbers
@ -119,25 +140,25 @@ def create_plugin_version_tuple(plugin_version_string) -> tuple:
return tuple(map(int, (plugin_version_string.split("."))))
def get_plugin_version_without_letters(plugin_version_string) -> str:
def get_plugin_version_without_letters(plugin_version_string : str) -> str:
"""
Returns the version without letters from the plugin version
:param plugin_version_string: Plugin Version
:returns: Plugin versioon without letters
:returns: Plugin version without letters
"""
return re.sub(r'([A-Za-z]*)', '', plugin_version_string)
def compare_plugin_version(plugin_latest_version, plugin_file_version) -> bool:
def compare_plugin_version(plugin_latest_version : str, plugin_file_version : str) -> bool:
"""
Check if plugin version is outdated
:param plugin_latest_version: Latest available plugin version
:param plugin_file_version: Installed plugin version
:returns: Bool if plugin version is outdated
:returns: bool if plugin version is outdated
"""
try:
plugin_version_tuple = create_plugin_version_tuple(
@ -152,7 +173,7 @@ def compare_plugin_version(plugin_latest_version, plugin_file_version) -> bool:
return False
def check_installed_plugins(input_selected_object="all", input_parameter=None) -> None:
def check_installed_plugins(input_selected_object : str="all", input_parameter : str=None) -> None:
"""
Gets installed plugins and checks it against the apis if there are updates for the plugins available
@ -162,52 +183,93 @@ def check_installed_plugins(input_selected_object="all", input_parameter=None) -
:returns: None
"""
config_values = config_value()
plugin.create_plugin_list()
Plugin.create_plugin_list()
match config_values.connection:
case "sftp":
# TODO add sftp adn ftp support
#connection = createSFTPConnection()
#pluginList = sftp_listAll(connection)
print("sftp list all")
connection = sftp_create_connection()
plugin_list = sftp_list_all(connection)
case "ftp":
print("ftp list all")
connection = ftp_create_connection()
plugin_list = ftp_list_all(connection)
case _:
plugin_folder_path = config_values.path_to_plugin_folder
plugin_list = os.listdir(plugin_folder_path)
plugin_count = plugins_with_udpates = 0
# create simple progress bar from rich
for plugin_file in track(plugin_list, description="[cyan]Checking...", transient=True, style="bright_yellow"):
plugin_no_attributes = False
plugin_attributes = True
match config_values.connection:
case "sftp":
#plugin_attributes = sftp_validateFileAttributes(connection, f"config_values.remote_plugin_folder_on_server}/{plugin}")
print("sftp check_installed_plugins")
plugin_attributes = sftp_validate_file_attributes(
connection, f"{config_values.remote_plugin_folder_on_server}/{plugin}"
)
case "ftp":
#plugin_attributes = ftp_validateFileAttributes(connection, f"config_values.remote_plugin_folder_on_server}/{plugin}")
print("ftp check_installed_plugins")
plugin_attributes = ftp_validate_file_attributes(
connection, f"{config_values.remote_plugin_folder_on_server}/{plugin}"
)
case _:
if not os.path.isfile(Path(f"{plugin_folder_path}/{plugin_file}")):
plugin_no_attributes = True
plugin_attributes = False
if not re.search(r'.jar$', plugin_file):
plugin_no_attributes = True
plugin_attributes = False
# skip plugin if no attributes were found
if plugin_no_attributes == True:
if plugin_attributes == False:
continue
plugin_file_name = get_plugin_file_name(plugin_file)
# supports command 'check pluginname' and skip the checking 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_file_name, re.IGNORECASE):
continue
plugin_file_version = get_plugin_file_version(plugin_file)
# check repository of plugin
plugin_spigot_id = search_plugin_spigot(plugin_file, plugin_file_name, plugin_file_version)
plugin_spigot_id = search_plugin_spigot(plugin_file, plugin_file_name, plugin_file_version) # plugin_spigot_id isn't needed
# TODO add more plugin repositories here
print(plugin_file_name)
print(plugin_file_version)
print(plugin_spigot_id)
# plugin wasn't found and not added to global plugin list so add
if plugin_file not in INSTALLEDPLUGINLIST[-1].plugin_file_name:
print(f"skipped {plugin_file}")
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:
plugins_with_udpates += 1
plugin_count += 1
# print rich table of found plugins and result
rich_table = Table(box=None)
rich_table.add_column("No.", justify="right", style="cyan", no_wrap=True)
rich_table.add_column("Name", style="bright_magenta")
rich_table.add_column("Installed V.", justify="right", style="bright_green")
rich_table.add_column("Latest V.", justify="right", style="green")
rich_table.add_column("Update available", justify="left", style="white")
rich_table.add_column("Repository", justify="left", style="white")
# start counting at 1 for all my non-programming friends :)
i = 1
for plugin in INSTALLEDPLUGINLIST:
rich_table.add_row(
str(i),
plugin.plugin_name,
plugin.plugin_file_version,
plugin.plugin_latest_version,
str(plugin.plugin_is_outdated),
plugin.plugin_repository
)
i += 1
rich_console = Console()
rich_console.print(rich_table)
if plugins_with_udpates != 0:
rich_console.print(
"[not bold][bright_yellow]Plugins with available updates: [bright_green]" +
f"{plugins_with_udpates}[bright_yellow]/[bright_magenta]{plugin_count}"
)
else:
rich_console.print(f"[bright_green]All found plugins are on the newest version!")
return None
def search_plugin_spigot(plugin_file, plugin_file_name, plugin_file_version) -> 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
@ -245,12 +307,17 @@ def search_plugin_spigot(plugin_file, plugin_file_name, plugin_file_version) ->
for updates in plugin_versions:
update_version_name = updates["name"]
if plugin_file_version2 in update_version_name:
spigot_update_id = updates["id"]
#spigot_update_id = updates["id"]
plugin_latest_version = get_latest_spigot_plugin_version(plugin_id)
plugin_is_outdated = compare_plugin_version(plugin_latest_version, update_version_name)
#addToPluginList(plugin_file, plugin_id, spigot_update_id, plugin_latest_version , plugin_is_outdated)
return plugin_id, plugin_is_outdated
#plugin_id = spigot_update_id = plugin_latest_version = plugin_is_outdated = None
#addToPluginList(plugin_file, plugin_id, spigot_update_id, plugin_latest_version , plugin_is_outdated)
Plugin.add_to_plugin_list(
plugin_file,
plugin_file_name,
plugin_file_version,
plugin_latest_version,
plugin_is_outdated,
"spigot",
(plugin_id)
)
return plugin_id
return None

View File

@ -9,6 +9,8 @@ import shutil
import re
from pathlib import Path
from rich.console import Console
from src.handlers.handle_sftp import sftp_create_connection
from src.handlers.handle_ftp import ftp_create_connection
from src.utils.console_output import rich_print_error
from src.handlers.handle_config import config_value
@ -33,7 +35,7 @@ def check_for_pluGET_update() -> None:
console.print("Couldn't check if new version of pluGET is available")
return None
if pluget_installed_version_tuple < plugin_latest_version_tuple:
print(f"A new version for pluGET is available: {version}")
print(f"A new version of pluGET is available: {version}")
console.print("Download it here: ", end='')
console.print("https://github.com/Neocky/pluGET", style="link https://github.com/Neocky/pluGET")
return None
@ -135,7 +137,7 @@ def check_requirements() -> None:
case "local":
check_local_plugin_folder(config_values)
case "sftp":
print("Check sftp folder")
sftp_create_connection()
case "ftp":
print("check ftp folder")
ftp_create_connection()
return None