Added SFTP Support & Requirements check

This commit is contained in:
Neocky 2021-03-10 02:05:56 +01:00
parent db404a7a61
commit 6915b213dc
7 changed files with 223 additions and 41 deletions

View File

@ -1,2 +1,10 @@
urllib3 >= 1.21.1
requests >=2.25.1
paramiko >= 2.7.2
bcrypt >= 3.2.0
cryptography >= 3.4.6
pynacl >= 1.4.0
cffi >= 1.14.5
six >= 1.15.0
pycparser >= 2.20
pysftp >= 0.2.9

View File

@ -18,7 +18,21 @@ def checkConfig():
config = configparser.ConfigParser()
config.sections()
config.read("config.ini")
localPluginFolder = config['General']['LocalPluginFolder']
pathToPluginFolder = config['General']['PathToPluginFolder']
sftp_server = config['Remote Server']['Server']
sftp_user = config['Remote Server']['Username']
sftp_password = config['Remote Server']['Password']
sftp_port = config['Remote Server']['Port']
sftp_folderPath = config['Remote Server']['PluginFolder']
sftp_port = int(sftp_port)
if localPluginFolder == 'True':
localPluginFolder = True
else:
localPluginFolder = False
return configValues
@ -32,6 +46,8 @@ def createConfig():
config['Remote Server']['Server'] = '0.0.0.0'
config['Remote Server']['Username'] = 'user'
config['Remote Server']['Password'] = 'longpassword'
config['Remote Server']['Port'] = '22'
config['Remote Server']['PluginFolder'] = '.\\plugins'
with open('./config.ini', 'w') as configfile:
config.write(configfile)

View File

@ -4,7 +4,8 @@ from consoleoutput import consoleTitle, clearConsole, printMainMenu, oColors
from plugin_downloader import searchPackage, getSpecificPackage
from plugin_updatechecker import updateInstalledPackage, checkInstalledPackage
from handle_config import checkConfig
from utilities import getHelp
from utilities import getHelp, check_requirements
from handle_sftp import createSFTPConnection, sftp_showPlugins
def createInputLists():
global COMMANDLIST
@ -26,22 +27,29 @@ def handleInput(inputCommand, inputSelectedObject, inputParams):
while True:
if inputCommand == 'get':
if inputSelectedObject.isdigit():
getSpecificPackage(inputSelectedObject, checkConfig().pathToPluginFolder, inputParams)
break
if not checkConfig().localPluginFolder:
getSpecificPackage(inputSelectedObject, checkConfig().sftp_folderPath, inputParams)
break
else:
getSpecificPackage(inputSelectedObject, checkConfig().pathToPluginFolder, inputParams)
else:
searchPackage(inputSelectedObject)
break
if inputCommand == 'update':
updateInstalledPackage(checkConfig().pathToPluginFolder, inputSelectedObject)
updateInstalledPackage(inputSelectedObject)
break
if inputCommand == 'check':
checkInstalledPackage(checkConfig().pathToPluginFolder, inputSelectedObject)
checkInstalledPackage(inputSelectedObject)
break
if inputCommand == 'exit':
sys.exit()
if inputCommand == 'help':
getHelp()
break
if inputCommand == 'sftp':
sftp = createSFTPConnection()
sftp_showPlugins(sftp)
break
else:
print(oColors.brightRed + "Command not found. Please try again." + oColors.standardWhite)
getInput()
@ -64,11 +72,14 @@ def getInput():
def inputMainMenu():
consoleTitle()
clearConsole()
checkConfig()
check_requirements()
createInputLists()
printMainMenu()
getInput()
outputTest()
def outputTest():
@ -87,6 +98,4 @@ def outputTest():
input("Press key to end program...")
consoleTitle()
inputMainMenu()
outputTest()

41
src/handle_sftp.py Normal file
View File

@ -0,0 +1,41 @@
import sys
import pysftp
from handle_config import checkConfig
from consoleoutput import oColors
def createSFTPConnection():
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None # TODO fix this
sftp = pysftp.Connection(checkConfig().sftp_server, username=checkConfig().sftp_user, password=checkConfig().sftp_password, port=checkConfig().sftp_port, cnopts=cnopts)
return sftp
def sftp_showPlugins(sftp):
sftp.cd('plugins')
for attr in sftp.listdir_attr():
print(attr.filename, attr)
def sftp_cdPluginDir(sftp):
sftp.cd('plugins')
def sftp_upload_file(sftp, itemPath):
#sftp = createSFTPConnection()
try:
sftp.chdir('plugins')
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')
installedPlugins = sftp.listdir()
except FileNotFoundError:
print(oColors.brightRed + "The *plugins* folder couldn*t be found on the remote host!" + oColors.standardWhite)
return installedPlugins

View File

@ -4,6 +4,8 @@ import re
from web_request import doAPIRequest
from consoleoutput import oColors
from handle_config import checkConfig
from utilities import createTempPluginFolder, deleteTempPluginFolder
from handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection
def calculateFileSize(downloadFileSize):
@ -78,7 +80,10 @@ def searchPackage(ressourceName):
#fileInfo = packageName[ressourceSelected]["file"]
#packageUrl = fileInfo["url"]
ressourceId = packageName[ressourceSelected]["id"]
getSpecificPackage(ressourceId, checkConfig().pathToPluginFolder)
if not checkConfig().localPluginFolder:
getSpecificPackage(ressourceId, checkConfig().sftp_folderPath)
else:
getSpecificPackage(ressourceId, checkConfig().pathToPluginFolder)
def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'):
@ -96,9 +101,16 @@ def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'):
filesizeData = calculateFileSize(filesize)
print(f"Downloadsize: {filesizeData} KB")
print(f"File downloaded here: {downloadPath}")
if not checkConfig().localPluginFolder:
print(downloadPath)
sftpSession = createSFTPConnection()
#sftp_cdPluginDir(sftpSession)
sftp_upload_file(sftpSession, downloadPath)
def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'):
if checkConfig().localPluginFolder == False:
downloadPath = createTempPluginFolder()
url = f"https://api.spiget.org/v2/resources/{ressourceId}"
packageDetails = doAPIRequest(url)
packageName = packageDetails["name"]
@ -108,17 +120,31 @@ def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'):
#packageVersion = getlatestVersion(ressourceId)
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"
downloadPackagePath = f"{downloadPath}\\{packageDownloadName}"
if checkConfig().localPluginFolder:
if inputPackageVersion is None or inputPackageVersion == 'latest':
try:
downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
else:
try:
downloadSpecificVersion(ressourceId, downloadPackagePath, versionId)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
if inputPackageVersion is None or inputPackageVersion == 'latest':
try:
downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
else:
try:
downloadSpecificVersion(ressourceId, downloadPackagePath, versionId)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
if not checkConfig().localPluginFolder:
if inputPackageVersion is None or inputPackageVersion == 'latest':
try:
downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath)
deleteTempPluginFolder(downloadPath)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
else:
try:
downloadSpecificVersion(ressourceId, downloadPackagePath, versionId)
deleteTempPluginFolder(downloadPath)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
# get latest update > https://api.spiget.org/v2/resources/28140/updates/latest
# this also > https://api.spiget.org/v2/resources/28140/versions/latest

View File

@ -5,6 +5,7 @@ from consoleoutput import oColors
from plugin_downloader import getSpecificPackage #handleInput
from web_request import doAPIRequest
from handle_config import checkConfig
from handle_sftp import createSFTPConnection, sftp_listAll
def createPluginList():
@ -19,10 +20,13 @@ def addToPluginList(pluginId, versionId, plugin_is_outdated):
def getFileName(pluginName):
pluginNameFull = pluginName
pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull)
pluginVersionFull = pluginVersion.group()
pluginNameOnly = pluginNameFull.replace(pluginVersionFull, '')
pluginNameOnly = re.sub(r'(\-$)', '', pluginNameOnly)
pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull) #errors out when for example .yaml file in plugins
try:
pluginVersionFull = pluginVersion.group() # TODO fix this
except AttributeError:
pluginVersionFull = pluginVersion
pluginNameOnlyy = pluginNameFull.replace(pluginVersionFull, '')
pluginNameOnly = re.sub(r'(\-$)', '', pluginNameOnlyy)
return pluginNameOnly
@ -46,9 +50,13 @@ def compareVersions(pluginId, pluginVersion):
return plugin_is_outdated
def checkInstalledPackage(pluginFolderPath, inputSelectedObject="all"):
def checkInstalledPackage(inputSelectedObject="all"):
createPluginList()
pluginList = os.listdir(pluginFolderPath)
if not checkConfig().localPluginFolder:
sftp = createSFTPConnection()
pluginList = sftp_listAll(sftp)
else:
pluginList = os.listdir(checkConfig().pathToPluginFolder)
i = 0
oldPackages = 0
print("Index / Name / Installed Version / Update available")
@ -84,10 +92,13 @@ def checkInstalledPackage(pluginFolderPath, inputSelectedObject="all"):
print(f"Old packages: [{oldPackages}/{i}]")
def updateInstalledPackage(pluginFolderPath, inputSelectedObject='all'):
def updateInstalledPackage(inputSelectedObject='all'):
createPluginList()
pluginList = os.listdir(pluginFolderPath)
print(pluginList)
if not checkConfig().localPluginFolder:
sftp = createSFTPConnection()
pluginList = sftp_listAll(sftp)
else:
pluginList = os.listdir(checkConfig().pathToPluginFolder)
i = 0
for plugin in pluginList:
print(plugin)
@ -102,29 +113,43 @@ def updateInstalledPackage(pluginFolderPath, inputSelectedObject='all'):
if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE):
print(f"Updating: {fileName}")
pluginPath = checkConfig().pathToPluginFolder
pluginPath = f"{pluginPath}\\{plugin}"
os.remove(pluginPath)
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
if not checkConfig().localPluginFolder:
pluginPath = checkConfig().sftp_folderPath
pluginPath = f"{pluginPath}\\{plugin}"
sftp = createSFTPConnection()
sftp.remove(pluginPath)
getSpecificPackage(pluginId, checkConfig().sftp_folderPath)
else:
pluginPath = checkConfig().pathToPluginFolder
pluginPath = f"{pluginPath}\\{plugin}"
os.remove(pluginPath)
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
break
if inputSelectedObject == 'all':
if INSTALLEDPLUGINLIST[i][2] == True:
print("Deleting old plugin...")
pluginPath = checkConfig().pathToPluginFolder
pluginPath = f"{pluginPath}\\{plugin}"
os.remove(pluginPath)
print("Downloading new plugin...")
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
if not checkConfig().localPluginFolder:
pluginPath = checkConfig().sftp_folderPath
pluginPath = f"{pluginPath}\\{plugin}"
print("Deleting old plugin...")
sftp = createSFTPConnection()
sftp.remove(pluginPath)
print("Downloading new plugin...")
getSpecificPackage(pluginId, checkConfig().sftp_folderPath)
else:
pluginPath = checkConfig().pathToPluginFolder
pluginPath = f"{pluginPath}\\{plugin}"
print("Deleting old plugin...")
os.remove(pluginPath)
print("Downloading new plugin...")
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
i = i + 1
#print(INSTALLEDPLUGINLIST[1][0])
#getLatestPackageVersion(pluginID, r"C:\\Users\USER\Desktop\\plugins\\")
def getInstalledPlugin(localFileName, localFileVersion):
url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name"
packageName = doAPIRequest(url)
#packageName = response.json()
i = 1
plugin_match_found = False
pluginID = None

View File

@ -1,5 +1,11 @@
# misc functions
import os
import sys
import requests
import shutil
from consoleoutput import oColors
from handle_config import checkConfig
def getHelp():
print(oColors.brightYellow+ "Need help?" + oColors.standardWhite)
@ -7,3 +13,54 @@ def getHelp():
print("https://github.com/Neocky/pluGET")
print("Or go to the official discord.")
print("The link for discord can also be found on Github!")
def check_local_plugin_folder():
if checkConfig().localPluginFolder:
if not os.path.isdir(checkConfig().pathToPluginFolder):
print(oColors.brightRed + "Plugin folder coulnd*t be found. Creating one..." + oColors.standardWhite)
try:
os.mkdir(checkConfig().pathToPluginFolder)
except OSError:
print(oColors.brightRed + "Creation of directory %s failed" % checkConfig().pathToPluginFolder)
print(oColors.brightRed + "Please check the config file!" + oColors.standardWhite)
sys.exit()
else:
print("Created directory %s" % checkConfig().pathToPluginFolder)
def apiTest():
apiStatusUrl = 'https://api.spiget.org/v2/status'
try:
r = requests.get(apiStatusUrl)
except requests.exceptions.HTTPError:
print(oColors.brightRed + "Couldn*t make a connection to the API. Check you connection to the internet!" + oColors.standardWhite)
sys.exit()
if r.status_code != 200:
print(oColors.brightRed + "Problems with the API detected. Plese try it again later!" + oColors.standardWhite)
sys.exit()
def check_requirements():
apiTest()
check_local_plugin_folder()
# sftp test
def createTempPluginFolder():
tempPluginFolder = ".\\plugins"
if not os.path.isdir(tempPluginFolder):
#print(oColors.brightRed + "Plugin folder coulnd*t be found. Creating one..." + oColors.standardWhite)
try:
os.mkdir(tempPluginFolder)
except OSError:
print(oColors.brightRed + "Creation of directory %s failed" % checkConfig().pathToPluginFolder)
print(oColors.brightRed + "Please check the config file!" + oColors.standardWhite)
sys.exit()
return tempPluginFolder
def deleteTempPluginFolder(tempPluginFolder):
try:
shutil.rmtree(tempPluginFolder)
except OSError as e:
print ("Error: %s - %s." % (e.filename, e.strerror))