xosski

Local to remote ftp xfer

Dec 13th, 2024
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.81 KB | None | 0 0
  1. import os
  2. from ftp_service import FTPService
  3. import argparse
  4. import time
  5.  
  6. HOST_NAME = None
  7. USER_NAME = None
  8. PASSWORD = None
  9. PORT = None
  10. LOCAL_DIRECTORY = None
  11. WITH_OK_FILE = None
  12. LOCAL_ARCHIVE_DIRECTORY = None
  13. REMOTE_DIRECTORY = None
  14.  
  15. parser = argparse.ArgumentParser(description="Script description")
  16. parser.add_argument(
  17. "--prefix",
  18. type=str,
  19. default="",
  20. help="Prefix for environment variables",
  21. )
  22. parser.add_argument(
  23. "--host",
  24. type=str,
  25. default=HOST_NAME,
  26. nargs="?",
  27. const=True,
  28. help="Name of the SFTP server",
  29. )
  30. parser.add_argument(
  31. "--username",
  32. type=str,
  33. default=USER_NAME,
  34. nargs="?",
  35. const=True,
  36. help="User name to connect to FTP server",
  37. )
  38. parser.add_argument(
  39. "--password",
  40. type=str,
  41. default=PASSWORD,
  42. nargs="?",
  43. const=True,
  44. help="Password to connect to FTP server",
  45. )
  46. parser.add_argument(
  47. "--port",
  48. type=int,
  49. default=PORT,
  50. nargs="?",
  51. const=True,
  52. help="Port number to connect to FTP server",
  53. )
  54. parser.add_argument(
  55. "--local_directory",
  56. type=str,
  57. default=LOCAL_DIRECTORY,
  58. nargs="?",
  59. const=True,
  60. help="Path to local folder for storing recovered files",
  61. )
  62. parser.add_argument(
  63. "--with_ok_file",
  64. type=bool,
  65. default=WITH_OK_FILE,
  66. help="Include .ok files in file recovery",
  67. )
  68. parser.add_argument(
  69. "--remote_directory",
  70. type=str,
  71. default=REMOTE_DIRECTORY,
  72. nargs="?",
  73. const=True,
  74. help='Paths to the remote folder where we will store the files sent by default "./recu_ii/"',
  75. )
  76.  
  77. args = parser.parse_args()
  78.  
  79. # Function to get environment variable with prefix
  80. def get_env_var(var_name, default=None):
  81. return os.getenv(f"{args.prefix}_{var_name}", default)
  82.  
  83. # Check that host is present
  84. if not args.host and not get_env_var("HOST"):
  85. print("The host name of the FTP server is mandatory.")
  86. exit()
  87. # Check that username is present
  88. if not args.username and not get_env_var("USERNAME"):
  89. print("The username to connect to the FTP server is mandatory.")
  90. exit()
  91. # Check that password is present
  92. if not args.password and not get_env_var("PASSWORD"):
  93. print("The password to connect to the SFTP server is mandatory")
  94. exit()
  95. # Check that local_directory is present
  96. if not args.local_directory and not get_env_var("LOCAL_DIRECTORY"):
  97. print("The path to the local folder where the files to be sent are stored is mandatory.")
  98. exit()
  99.  
  100. if args.host:
  101. HOST_NAME = args.host
  102. else:
  103. HOST_NAME = get_env_var("HOST")
  104.  
  105. if args.username:
  106. USER_NAME = args.username
  107. else:
  108. USER_NAME = get_env_var("USERNAME")
  109.  
  110. if args.password:
  111. PASSWORD = args.password
  112. else:
  113. PASSWORD = get_env_var("PASSWORD")
  114.  
  115. if args.port:
  116. PORT = args.port
  117. else:
  118. PORT = int(get_env_var("PORT"))
  119.  
  120. if args.local_directory:
  121. LOCAL_DIRECTORY = args.local_directory
  122. else:
  123. LOCAL_DIRECTORY = get_env_var("LOCAL_DIRECTORY")
  124.  
  125. if args.with_ok_file:
  126. WITH_OK_FILE = args.with_ok_file
  127. else:
  128. WITH_OK_FILE = get_env_var("WITH_OK_FILE", False)
  129.  
  130. if args.remote_directory:
  131. REMOTE_DIRECTORY = args.remote_directory
  132. else:
  133. REMOTE_DIRECTORY = get_env_var("REMOTE_DIRECTORY")
  134.  
  135. LOCAL_ARCHIVE_DIRECTORY = LOCAL_DIRECTORY + "/envoye/" + str(int(time.time()))
  136.  
  137. ftp = FTPService(
  138. host=HOST_NAME,
  139. port=PORT,
  140. identifier=USER_NAME,
  141. password=PASSWORD,
  142. )
  143.  
  144. def get_all_local_files(path, ignore_paths=[]):
  145. all_files = []
  146. for root, dirs, files in os.walk(path):
  147. # ignore folders ignore_paths
  148. dirs[:] = [d for d in dirs if d not in ignore_paths]
  149. for file in files:
  150. # ignore .ok files and .gpg files
  151. if not file.endswith(".ok") and not file.endswith(".gpg"):
  152. file_path = os.path.join(root, file)
  153. all_files.append(file_path)
  154. return all_files
  155.  
  156. def send_local_files_to_ftp(with_ok_file=False):
  157. try:
  158. local_files_paths = get_all_local_files(
  159. LOCAL_DIRECTORY, ignore_paths=["envoye", "recu"]
  160. )
  161. # If local_files_paths is empty, stop sending files
  162. if not local_files_paths:
  163. print("No files to send.")
  164. return False
  165.  
  166. # create a list of remote files from the list of local files by deleting the name of the local folder
  167. remote_files_paths = [
  168. file.replace(LOCAL_DIRECTORY + "/", REMOTE_DIRECTORY)
  169. for file in local_files_paths
  170. ]
  171. for local_path, remote_path in zip(local_files_paths, remote_files_paths):
  172. send_file(
  173. local_path=local_path,
  174. remote_path=remote_path,
  175. with_ok_file=with_ok_file,
  176. )
  177. return local_files_paths
  178. except Exception as e:
  179. print(f"Error sending files : {str(e)}")
  180. return False
  181.  
  182. # Send multiple files over FTP
  183. def send_file(local_path, remote_path, with_ok_file=False):
  184. # Checks if the file exists locally
  185. try:
  186. with open(local_path, "r") as f:
  187. pass
  188. except Exception as e:
  189. print(f"Local file does not exist : {str(e)}")
  190. return False
  191.  
  192. # Checks if remote_file_path is not just a directory
  193. if remote_path.endswith("/"):
  194. # If remote_file_path ends with /, we add the name of the local file
  195. remote_path += local_path.split("/")[-1]
  196. # Writes a message to warn that the file will be sent to the remote directory with the local file name
  197. print(
  198. f'The remote path is a directory. The file will be sent to the directory {remote_path}'
  199. )
  200.  
  201. # Send file
  202. try:
  203. # Retrieves the path of the local file before encrypting it and moves it to the send folder afterwards
  204. original_file_path = local_path
  205.  
  206. # Envoie le fichier sur le sftp
  207. ftp.send_file(local_path, remote_path)
  208. except Exception as e:
  209. print(f"Error sending file : {local_path} error: {str(e)}")
  210. return False
  211.  
  212. # Moves the original file to the sent folder
  213. move_file_to_envoye(original_file_path)
  214.  
  215. # If with_ok_file is True, create an .ok file
  216. if with_ok_file == True:
  217. try:
  218. ok_file_path = local_path
  219. ok_remote_path = remote_path
  220. if not local_path.endswith(".ok"):
  221. ok_file_path = local_path + ".ok"
  222. # Creates an .ok file
  223. with open(ok_file_path, "w") as f:
  224. pass
  225. # If remote_file_path ends with .ok, it is not added
  226. if not remote_path.endswith(".ok"):
  227. ok_remote_path = remote_path + ".ok"
  228. # Send .ok file to ftp
  229. ftp.send_file(ok_file_path, ok_remote_path)
  230. # Moves the original file to the sent folder
  231. move_file_to_envoye(original_file_path)
  232. except Exception as e:
  233. print(
  234. f"Error sending file : {ok_file_path}, error: {str(e)}"
  235. )
  236. return False
  237. finally:
  238. # If the local .ok file exists, delete it
  239. if os.path.exists(ok_file_path):
  240. os.remove(ok_file_path)
  241.  
  242. return original_file_path
  243.  
  244. def move_file_to_envoye(local_file_path):
  245. # Removes the local_directory from the file path and replaces it with send_directory
  246. new_file_path = local_file_path.replace(LOCAL_DIRECTORY, LOCAL_ARCHIVE_DIRECTORY)
  247. # Create send folder if none exists
  248. os.makedirs(os.path.dirname(new_file_path), exist_ok=True)
  249. # Moves the file to the sent folder
  250. os.rename(local_file_path, new_file_path)
  251. return new_file_path
  252.  
  253. # FTP connection
  254. if not ftp.connect():
  255. exit()
  256. else:
  257. print("Sending files over FTP...")
  258. send_local_files_to_ftp(with_ok_file=WITH_OK_FILE)
  259. print("Sending files over FTP complete.")
  260.  
Add Comment
Please, Sign In to add comment