99import re
1010import os
1111import distutils .dir_util
12- import requests
12+ import time
13+ import logging
14+ import traceback
15+
1316
1417debug = True
1518
1619def download_ftp_file (source , target , ftp ):
20+ logging .debug ('downloading ftp file: ' + source + ' target: ' + target )
1721 basedir = os .path .dirname (target )
1822 distutils .dir_util .mkpath (basedir )
1923
@@ -22,20 +26,36 @@ def download_ftp_file(source, target, ftp):
2226
2327def process_upload_dir (source , target , ftp ):
2428 basename = os .path .basename (source )
29+ logging .debug ('processing upload dir src: ' + source + ' target: ' + target )
30+ logging .debug ('dir basename: ' + basename )
31+ wd = ftp .pwd ()
32+ # does the parent dir exist?
2533 try :
26- print ('trying to create dir: ' + '/' + target + '/' + basename , file = sys .stderr )
34+ ftp .cwd ('/' + target )
35+ except :
36+ logging .error ('Cannot stat parent dir: /' + target )
37+ return 1
38+
39+ ftp .cwd (wd )
40+
41+ try :
42+ logging .debug ('trying to create dir: ' + '/' + target + '/' + basename )
2743 ftp .mkd ('/' + target + '/' + basename )
2844 except ftplib .error_perm :
29- print ('Directory exists, overwriting' )
45+ logging . debug ('Directory exists, overwriting' )
3046
3147 for f in os .listdir (source ):
32- if os .path .isdir (source + '/' + f ):
33- process_upload_dir (source + '/' + f , target + '/' + basename + '/' , ftp )
34- elif os .path .isfile (source + '/' + f ):
35- ftp .storbinary ("STOR " + target + '/' + basename + '/' + f , open (source + '/' + f , 'r' ))
48+ path = source + '/' + f
49+ if os .path .isdir (path ):
50+ process_upload_dir (path , target + '/' + basename + '/' , ftp )
51+ elif os .path .isfile (path ):
52+ logging .debug ('Trying to upload file: ' + path + ' to dest: ' + target + '/' + basename + '/' + f )
53+ ftp .storbinary ("STOR " + target + '/' + basename + '/' + f , open (path , 'r' ))
3654 return 0
3755
3856def process_ftp_dir (source , target , ftp ):
57+ logging .debug ('processing ftp dir: ' + source + ' target: ' + target )
58+ pwd = ftp .pwd ()
3959 ftp .cwd ('/' + source )
4060
4161 ls = []
@@ -53,11 +73,14 @@ def process_ftp_dir(source, target, ftp):
5373 else :
5474 download_ftp_file (name , target + '/' + name , ftp )
5575
76+ ftp .cwd (pwd )
77+
5678def process_ftp_file (ftype , afile ):
5779 p = re .compile ('[a-z]+://([-a-z.]+)/(.*)' )
5880 ftp_baseurl = p .match (afile ['url' ]).group (1 )
5981 ftp_path = p .match (afile ['url' ]).group (2 )
6082
83+ logging .debug ('Connecting to FTP: ' + ftp_baseurl )
6184 ftp = FTP (ftp_baseurl )
6285 if os .environ .get ('TESK_FTP_USERNAME' ) is not None :
6386 try :
@@ -84,22 +107,32 @@ def process_ftp_file(ftype, afile):
84107 elif afile ['type' ] == 'DIRECTORY' :
85108 return process_upload_dir (afile ['path' ], ftp_path , ftp )
86109 else :
87- print ('Unknown file type: ' + afile ['type' ])
110+ logging . error ('Unknown file type: ' + afile ['type' ])
88111 return 1
89112 else :
90- print ('Unknown file action: ' + ftype )
113+ logging . error ('Unknown file action: ' + ftype )
91114 return 1
92115
93116def process_http_file (ftype , afile ):
94117 if ftype == 'inputs' :
95118 r = requests .get (afile ['url' ])
119+
120+ if r .status_code != 200 :
121+ logging .error ('Got status code: ' + str (r .status_code ))
122+ return 1
123+
96124 fp = open (afile ['path' ], 'wb' )
97125 fp .write (r .content )
98126 fp .close
99127 return 0
100128 elif ftype == 'outputs' :
101129 fp = open (afile ['path' ], 'r' )
102130 r = requests .put (afile ['url' ], data = fp .read ())
131+
132+ if r .status_code != 200 or r .status_code != 201 :
133+ logging .error ('Got status code: ' + str (r .status_code ))
134+ return 1
135+
103136 fp .close
104137 return 0
105138 else :
@@ -109,7 +142,7 @@ def process_http_file(ftype, afile):
109142def filefromcontent (afile ):
110143 content = afile .get ('content' )
111144 if content is None :
112- print ('Incorrect file spec format, no content or url specified' , file = sys . stderr )
145+ logging . error ('Incorrect file spec format, no content or url specified' )
113146 return 1
114147
115148 fh = open (afile ['path' ], 'w' )
@@ -125,7 +158,7 @@ def process_file(ftype, afile):
125158
126159 p = re .compile ('([a-z]+)://' )
127160 protocol = p .match (url ).group (1 )
128- debug ('protocol is: ' + protocol )
161+ logging . debug ('protocol is: ' + protocol )
129162
130163 if protocol == 'ftp' :
131164 return process_ftp_file (ftype , afile )
@@ -140,6 +173,8 @@ def debug(msg):
140173 print (msg , file = sys .stderr )
141174
142175def main (argv ):
176+ logging .basicConfig (format = '%(asctime)s %(levelname)s: %(message)s' , datefmt = '%m/%d/%Y %I:%M:%S' , level = logging .DEBUG )
177+ logging .debug ('Starting filer...' )
143178 parser = argparse .ArgumentParser (description = 'Filer script for down- and uploading files' )
144179 parser .add_argument ('filetype' , help = 'filetype to handle, either \' inputs\' or \' outputs\' ' )
145180 parser .add_argument ('data' , help = 'file description data, see docs for structure' )
@@ -148,14 +183,15 @@ def main(argv):
148183 data = json .loads (args .data )
149184
150185 for afile in data [args .filetype ]:
151- debug ('processing file: ' + afile ['path' ])
186+ logging . debug ('processing file: ' + afile ['path' ])
152187 if process_file (args .filetype , afile ):
153- print ('something went wrong' , file = sys . stderr )
188+ logging . error ('something went wrong' )
154189 return 1
155190 # TODO a bit more detailed reporting
156191 else :
157- debug ('Processed file: ' + afile ['path' ])
192+ logging . debug ('Processed file: ' + afile ['path' ])
158193
159194 return 0
195+
160196if __name__ == "__main__" :
161- main (sys .argv )
197+ sys . exit ( main (sys .argv ) )
0 commit comments