This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ftp_transfer.py
223 lines (183 loc) · 5.93 KB
/
ftp_transfer.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
@file
@brief provides some functionalities to upload file to a website
.. versionadded:: 1.0
moved from pyensae to pyquickhelper
"""
from ftplib import FTP
import os, io
from ..loghelper.flog import noLOG
class TransferFTP (FTP) :
"""
this class uploads files to a website,
if the remote does not exists, it creates it first
@example(Transfer files to webste through FTP)
Simple sketch to transfer a list of ``files`` to
a website through FTP
@code
ftp = TransferFTP('ftp.<website>', alias, password, fLOG=print)
issues = [ ]
done = [ ]
notdone = [ ]
for file in files :
try :
r = ftp.transfer (file, path)
if r : done.append( (file, path) )
else : notdone.append ( (file, path) )
except Exception as e :
issues.append( (file, e) )
try :
ftp.close()
except Exception as e :
print ("unable to close FTP connection using ftp.close")
@endcode
@endexample
.. versionadded:: 1.0
Moved prom pyense to pyquickhelper.
"""
errorNoDirectory = "Can't change directory"
def __init__ (self, site, login, password, fLOG = noLOG) :
"""
constructor
@param site website
@param login login
@param password password
@param fLOG logging function
"""
FTP.__init__(self, site, login, password)
self.LOG = fLOG
self._atts = dict(site=site)
@property
def Site(self):
"""
return the website
"""
return self._atts["site"]
def _private_login (self) :
"""
logs in
"""
self.LOG ("connecting to ", self.Site)
FTP.login(self)
def run_command (self, command, *args) :
"""
run a FTP command
@param command command
@param args list of argument
@return output of the command or True for success, False for failure
"""
try :
t = command (self, *args)
if command == FTP.pwd or command == FTP.dir:
return t
elif command != FTP.cwd :
pass
return True
except Exception as e:
if TransferFTP.errorNoDirectory in str(e) :
raise e
self.LOG (e)
self.LOG (" ** run exc ", str(command), str(args))
self._private_login()
t = command (self, *args)
self.LOG (" ** run ", str(command), str(args))
return t
def print_list (self) :
"""
return the list of files in the current directory
the function sends eveything to the logging function
@return output of the command or True for success, False for failure
"""
return self.run_command(FTP.retrlines,'LIST')
def mkd (self, path) :
"""
creates a directory
@param path path to the directory
@return True or False
"""
self.LOG("[mkd]", path)
return self.run_command(FTP.mkd, path)
def cwd (self, path, create = False) :
"""
go to a directory, if it does not exist, create it
(if create is True)
@param path path to the directory
@param create True to create it
@return True or False
"""
try :
self.run_command(FTP.cwd, path)
except Exception as e :
if create and TransferFTP.errorNoDirectory in str(e) :
self.mkd (path)
self.cwd (path, create)
else :
raise e
def pwd (self) :
"""
Return the pathname of the current directory on the server.
@return pathname
"""
return self.run_command(FTP.pwd)
def dir(self, path = '.'):
"""
list the content of a path
@param path path
@return list of path
"""
return self.run_command(FTP.dir, path)
def ls(self, path = '.'):
"""
list the content of a path
@param path path
@return list of path
"""
return self.run_command(FTP.dir, path)
def transfer (self, file, to, name, debug = False) :
"""
transfers a file
@param file file name or stream (binary, BytesIO)
@param to destination (a folder)
@param name name of the stream on the website
@param debug if True, displays more information
@return status
.. versionchanged:: 1.0
file can be a file name or a stream,
parameter *name* was added
"""
path = to.split("/")
path = [ _ for _ in path if len(_) > 0 ]
for p in path :
self.cwd(p, True)
if isinstance(file, str):
if not os.path.exists(file):
raise FileNotFoundError(file)
with open(file, "rb") as f :
r = self.run_command(FTP.storbinary, 'STOR ' + name, f)
elif isinstance(file, io.BytesIO):
r = self.run_command(FTP.storbinary, 'STOR ' + name, file)
else:
r = self.run_command(FTP.storbinary, 'STOR ' + name, file)
for p in path :
self.cwd("..")
return r
class MockTransferFTP (TransferFTP) :
"""
mocking FTP transfer
"""
def __init__(self, site, login, password, fLOG = noLOG):
"""
same signature as @see cl TransferFTP
"""
self.LOG = fLOG
self._atts = dict(site=site)
def transfer (self, file, to, debug = False) :
"""
does nothing, returns True
"""
return True
def close(self) :
"""
does noting
"""
pass