-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp_to_highwire.py
76 lines (59 loc) · 1.76 KB
/
ftp_to_highwire.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from ftplib import FTP
import ftplib
import settings as settings
import glob
import os
"""
Will deliver to the named HW ftp bucket.
## Gotchas
I noted that on one run with a large number of PDFs the upload speed
was very slow, might need to be investigated
# TODO - elife - imulvany - put in place a check to see that zip files contain all expected data
# TODO - elife - imulvany - find out the final location for FTP delivery
"""
source_dir = settings.FTP_TO_HW_DIR
ftpuri = settings.FTP_URI
ftpusername = settings.FTP_USERNAME
ftppassword = settings.FTP_PASSWORD
ftpcwd = settings.FTP_CWD
def upload(ftp, file):
ext = os.path.splitext(file)[1]
print file
uploadname = file.split(os.sep)[-1]
if ext in (".txt", ".htm", ".html"):
ftp.storlines("STOR " + file, open(file))
else:
print "uploading " + uploadname
ftp.storbinary("STOR " + uploadname, open(file, "rb"), 1024)
print "uploaded " + uploadname
def ftp_cwd_mkd(ftp, sub_dir):
"""
Given an FTP connection and a sub_dir name
try to cwd to the directory. If the directory
does not exist, create it, then cwd again
"""
cwd_success = None
try:
ftp.cwd(sub_dir)
cwd_success = True
except ftplib.error_perm:
# Directory probably does not exist, create it
ftp.mkd(sub_dir)
cwd_success = False
if cwd_success is not True:
ftp.cwd(sub_dir)
return cwd_success
def ftp_to_endpoint(zipfiles, sub_dir = None):
for zipfile in zipfiles:
ftp = FTP(ftpuri, ftpusername, ftppassword)
ftp_cwd_mkd(ftp, "/")
if ftpcwd != "":
ftp_cwd_mkd(ftp, ftpcwd)
if sub_dir is not None:
ftp_cwd_mkd(ftp, sub_dir)
upload(ftp, zipfile)
ftp.quit()
if __name__ == "__main__":
zipfiles = glob.glob(source_dir + "/*.zip")
ftp_to_endpoint(zipfiles)
workflow_logger.info("files uploaded to endpoint using ftp")