Skip to content

Commit 7456a91

Browse files
logger upload done
1 parent 65dfebd commit 7456a91

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

logger_setup.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from logging.handlers import RotatingFileHandler
22
from utilities import *
3+
from constants import *
34

45
# LOGGER CONFIGURATION -------------------------------------------------------------------------------------------------
56

@@ -15,8 +16,7 @@
1516
file_name = create_timestamped_and_named_file_name(APPLICATION_NAME, LOGGER_FILE_END_NAME)
1617

1718
# creation of the first handler which redirect traces to a log file
18-
file_handler = RotatingFileHandler(SLIMER_SCRIPT_ROOT_APP_PATH + '/' + LOGGER_SUBDIRECTORY_NAME +
19-
'/' + file_name, 'a', 1000000, 1)
19+
file_handler = RotatingFileHandler(SLIMER_SCRIPT_ROOT_LOGS_PATH + '/' + file_name, 'a', 1000000, 1)
2020

2121
# set level of the first handler to DEBUG
2222
file_handler.setLevel(logging.INFO)
@@ -35,14 +35,12 @@
3535

3636
# LOGGER SCRIPT --------------------------------------------------------------------------------------------------------
3737

38+
def logger_script():
39+
latest_logger_file = get_the_latest_file_in_a_folder(SLIMER_SCRIPT_ROOT_LOGS_PATH)
3840

39-
# def logger_script():
40-
# latest_logger_file = get_the_latest_file_in_a_folder(PD_SCRIPT_ROOT_LOGS_PATH + "/" + logger_application_name)
41-
#
42-
# # opens the file for reading only in binary format in order to upload
43-
# file = open(latest_logger_file, "rb")
44-
#
45-
# upload_file_to_server_ftp_without_logging_messages(file, file_name, logger_application_name)
46-
#
47-
# file.close()
48-
""
41+
# opens the file for reading only in binary format in order to upload
42+
file = open(latest_logger_file, "rb")
43+
44+
upload_file_to_server_ftp_without_logging_messages(file, file_name, LOGGER_SUBDIRECTORY_NAME)
45+
46+
file.close()

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
logging.error("The program ended unexpectedly !")
3030
logging.error("Error: " + str(e))
3131

32-
# logger_script()
33-
#
32+
logger_script()
33+
3434
# alm = Alerts()
3535
# alm.run_script()

slimer_script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def slimer_script():
1616
backup_file.write("\nList of directories, subdirectories and descendants of the folder " + path + "\n")
1717
parse_directories(path, backup_file)
1818
backup_file.write("\n\n################################################################################ \n")
19-
backup_file.write("\nList of files of the folder " + path + "\n")
19+
backup_file.write("\nList of files in the folder " + path + "\n")
2020
parse_all_folders_and_files(path, backup_file)
2121
backup_file.write("\n\n################################################################################ \n")
2222

utilities.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
import time
55
import os
6+
from credentials import *
67
from constants import *
78
import logging
9+
import ftplib
10+
from ftplib import FTP
811

912

1013
def create_directory(path):
@@ -65,3 +68,31 @@ def parse_all_folders_and_files(path, backup_file):
6568
backup_file.write("\n" + "--- " + filename + " *** " + time_format + " *** " + str(size) + " Ko")
6669
number_of_files += 1
6770
backup_file.write("\n\nNumber of files in the directory " + path + " : " + str(number_of_files))
71+
72+
73+
def get_the_latest_file_in_a_folder(path):
74+
list_of_files = os.listdir(path) # get a list of all file names in a folder
75+
# get a list of absolute paths for previously recovered files
76+
paths = [os.path.join(path, basename) for basename in list_of_files]
77+
# return the latest (most recent modified metadata) file
78+
return max(paths, key=os.path.getctime)
79+
80+
81+
def upload_file_to_server_ftp_without_logging_messages(file, filename, subdirectory):
82+
ftp = FTP(SEEDBOX_DOMAIN_NAME) # connect to host, default port
83+
try:
84+
print("trying to connect the ftp server...")
85+
ftp.login(user=SEEDBOX_USER_NAME, passwd=SEEDBOX_PASSWD) # login with credentials
86+
print('ftp connection succeed !')
87+
try:
88+
# TODO : se placer dans le bon repertoire (ok) du serveur et
89+
# creer un dossier *nom application* s'il n'existe pas
90+
ftp.cwd(SEEDBOX_ROOT_SLIMER_SCRIPT_PATH + "/" + subdirectory) # Set the current directory on the server
91+
print('sending ' + filename + ' file to the ftp server... (' + subdirectory + ' file)')
92+
ftp.storbinary('STOR ' + filename + '', file) # uploading file to the server
93+
print(filename + ' uploaded successfully!')
94+
except ftplib.all_errors:
95+
print('unable to make directories')
96+
except ftplib.all_errors:
97+
print('unable to connect to ftp server')
98+
ftp.quit()

0 commit comments

Comments
 (0)