2021-03-30 21:55:04 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import ftplib
|
2021-05-30 16:52:35 +00:00
|
|
|
import stat
|
|
|
|
import re
|
2021-03-30 21:55:04 +00:00
|
|
|
|
|
|
|
from utils.consoleoutput import oColors
|
|
|
|
from handlers.handle_config import configurationValues
|
|
|
|
|
|
|
|
|
|
|
|
def createFTPConnection():
|
|
|
|
configValues = configurationValues()
|
|
|
|
ftp = ftplib.FTP(configValues.sftp_server, user=configValues.sftp_user, \
|
|
|
|
passwd=configValues.sftp_password)
|
|
|
|
try:
|
|
|
|
return ftp
|
|
|
|
except UnboundLocalError:
|
|
|
|
print(oColors.brightRed + "[FTP]: Check your config.ini!" + oColors.standardWhite)
|
|
|
|
print(oColors.brightRed + "Exiting program..." + oColors.standardWhite)
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
def ftp_showPlugins(ftp):
|
|
|
|
configValues = configurationValues()
|
|
|
|
ftp.cwd(configValues.sftp_folderPath)
|
|
|
|
for attr in ftp.dir():
|
|
|
|
print(attr.filename, attr)
|
|
|
|
|
|
|
|
|
|
|
|
def ftp_upload_file(ftp, itemPath):
|
|
|
|
configValues = configurationValues()
|
2021-06-26 23:46:15 +00:00
|
|
|
if configValues.sftp_seperateDownloadPath is True:
|
|
|
|
uploadFolderPath = configValues.sftp_pathToSeperateDownloadPath
|
|
|
|
else:
|
|
|
|
uploadFolderPath = configValues.sftp_folderPath
|
2021-03-30 21:55:04 +00:00
|
|
|
try:
|
2021-06-26 23:46:15 +00:00
|
|
|
ftp.cwd(uploadFolderPath)
|
2021-03-30 21:55:04 +00:00
|
|
|
itemPath = os.path.relpath(itemPath, 'TempSFTPFolder/')
|
|
|
|
itemPath = str(itemPath)
|
|
|
|
currentDirectory = os.getcwd()
|
|
|
|
os.chdir('TempSFTPFolder')
|
|
|
|
with open (itemPath, 'rb') as plugin_file:
|
|
|
|
ftp.storbinary('STOR '+ str(itemPath), plugin_file)
|
|
|
|
except FileNotFoundError:
|
2021-03-31 16:11:41 +00:00
|
|
|
print(oColors.brightRed + "[FTP]: The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite)
|
|
|
|
print(oColors.brightRed + "[FTP]: Aborting uploading." + oColors.standardWhite)
|
2021-03-30 21:55:04 +00:00
|
|
|
os.chdir(currentDirectory)
|
|
|
|
ftp.close()
|
|
|
|
|
|
|
|
|
|
|
|
def ftp_upload_server_jar(ftp, itemPath):
|
|
|
|
try:
|
|
|
|
ftp.cwd('.')
|
|
|
|
itemPath = os.path.relpath(itemPath, 'TempSFTPFolder/')
|
|
|
|
itemPath = str(itemPath)
|
|
|
|
currentDirectory = os.getcwd()
|
|
|
|
os.chdir('TempSFTPFolder')
|
|
|
|
with open (itemPath, 'rb') as server_jar:
|
|
|
|
ftp.storbinary('STOR '+ str(itemPath), server_jar)
|
|
|
|
except FileNotFoundError:
|
2021-03-31 16:11:41 +00:00
|
|
|
print(oColors.brightRed + "[FTP]: The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite)
|
|
|
|
print(oColors.brightRed + "[FTP]: Aborting uploading." + oColors.standardWhite)
|
2021-03-30 21:55:04 +00:00
|
|
|
os.chdir(currentDirectory)
|
|
|
|
ftp.close()
|
|
|
|
|
|
|
|
|
|
|
|
def ftp_listAll(ftp):
|
|
|
|
configValues = configurationValues()
|
|
|
|
try:
|
|
|
|
ftp.cwd(configValues.sftp_folderPath)
|
|
|
|
installedPlugins = ftp.nlst()
|
|
|
|
except FileNotFoundError:
|
2021-03-31 16:11:41 +00:00
|
|
|
print(oColors.brightRed + "[FTP]: The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite)
|
2021-03-30 21:55:04 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
return installedPlugins
|
|
|
|
except UnboundLocalError:
|
2021-03-31 16:11:41 +00:00
|
|
|
print(oColors.brightRed + "[FTP]: No plugins were found." + oColors.standardWhite)
|
2021-03-30 21:55:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ftp_listFilesInServerRoot(ftp):
|
|
|
|
try:
|
|
|
|
ftp.cwd('.')
|
|
|
|
filesInServerRoot = ftp.nlst()
|
|
|
|
except FileNotFoundError:
|
2021-03-31 16:11:41 +00:00
|
|
|
print(oColors.brightRed + "[FTP]: The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite)
|
2021-03-30 21:55:04 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
return filesInServerRoot
|
|
|
|
except UnboundLocalError:
|
2021-03-31 16:11:41 +00:00
|
|
|
print(oColors.brightRed + "[FTP]: No Serverjar was found." + oColors.standardWhite)
|
2021-03-30 21:55:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ftp_downloadFile(ftp, downloadPath, fileToDownload):
|
|
|
|
configValues = configurationValues()
|
|
|
|
ftp.cwd(configValues.sftp_folderPath)
|
|
|
|
filedata = open(downloadPath,'wb')
|
|
|
|
ftp.retrbinary('RETR '+fileToDownload, filedata.write)
|
|
|
|
filedata.close()
|
|
|
|
ftp.quit()
|
2021-05-30 16:52:35 +00:00
|
|
|
|
|
|
|
|
2021-08-01 13:06:55 +00:00
|
|
|
def ftp_is_file(FTP, pluginPath):
|
|
|
|
if FTP.nlst(pluginPath) == [pluginPath]:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-05-30 16:52:35 +00:00
|
|
|
def ftp_validateFileAttributes(ftp, pluginPath):
|
2021-08-01 13:06:55 +00:00
|
|
|
if ftp_is_file(ftp, pluginPath) is False:
|
2021-05-30 16:52:35 +00:00
|
|
|
return False
|
2021-08-01 13:06:55 +00:00
|
|
|
if re.search(r'.jar$', pluginPath):
|
2021-06-12 15:03:55 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|