Create a gist now

Instantly share code, notes, and snippets.

@thelostt /fetch_upload.py Secret
Last active Jan 1, 2017

#!/bin/python3
import os
import zipfile
import shutil
import ftplib
import ftputil
import ftputil.session
from argparse import ArgumentParser
def fetch_local_files(folder):
for root, dirs, files in os.walk(folder):
for file in [file for file in files if file.lower().endswith(".zip")]:
realpath_file = os.path.join(root, file)
with zipfile.ZipFile(realpath_file) as zip_f:
zip_f.extractall(os.path.splitext(realpath_file)[0] + u"_zip_fetch")
def upload_fetched_files(folder, remote_path, server, port, user, password):
session = ftputil.session.session_factory(base_class=ftplib.FTP, encrypt_data_channel=False, port=int(port))
with ftputil.FTPHost(server, user, password, session_factory=session) as ftp:
if remote_path != "/":
ftp.makedirs(remote_path)
ftp.chdir(remote_path)
for root, dirs, files in os.walk(folder):
for file in [file for file in files if not file.lower().endswith(".zip")]:
ftp.upload(os.path.join(root, file), file)
if root.endswith("_zip_fetch"):
shutil.rmtree(root)
ftp.chdir('/')
def main(args):
try:
if not os.path.isdir(args.local_path):
raise Exception("invalid directory: " + args.local_path)
print("Fetching all files from ", args.local_path, " ...")
fetch_local_files(args.local_path)
print("Uploading fetched files to remote ", args.remote_path, " ...")
upload_fetched_files(args.local_path, args.remote_path, args.ftp_server,
args.ftp_port, args.ftp_user, args.ftp_password)
print("Done.")
except Exception as err:
print(err)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-p", "--local-path", dest="local_path", required=True)
parser.add_argument("--ftp-server", dest="ftp_server", required=True)
parser.add_argument("--ftp-port", dest="ftp_port", default=21)
parser.add_argument("--remote-path", dest="remote_path", required=True)
parser.add_argument("--user", dest="ftp_user", required=True)
parser.add_argument("--password", dest="ftp_password", required=True)
main(parser.parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment