-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftpConn.py
executable file
·260 lines (222 loc) · 7.61 KB
/
ftpConn.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#! /usr/bin/env python3
import queue
import heapq
import ftplib
from ftplib import FTP
import json
import pprint
import datetime
import sys
import re
import os
import logging
log = logging.getLogger(__name__)
pp = pprint.PrettyPrinter()
'''
loggingFmt='%(asctime)s %(levelname)-8s : %(message)s'
loggingDatefmt='%Y/%m/%d %I:%M:%S'
myformat = logging.Formatter(fmt=loggingFmt,
datefmt=loggingDatefmt)
logging.basicConfig(filename='LOG',
format=loggingFmt,
datefmt=loggingDatefmt,
level=logging.DEBUG,
)
ch = logging.StreamHandler()
ch.setFormatter(myformat)
logging.getLogger().addHandler(ch)
'''
class ftpConn():
def __init__(self,host,user,passwd,wd):
self.host = host
self.user = user
self.passwd = passwd
self.wd = wd
self.pp = pprint.PrettyPrinter()
self.nowUnixTime = int(datetime.datetime.now().strftime("%s"))
self.connect()
self.mkdir(self.wd)
self.cd(self.wd)
log.info("ftpConn() setup done")
def quit(self):
try:
self.ftp.quit()
log.info("ftp quit()")
except Exception as e:
log.error("ftp quit() error: %s" % e)
def mkdir(self,d):
try:
resp = self.ftp.mkd(d)
log.info("md: %s" % resp)
except ftplib.error_perm as e:
if str(e).startswith('550'):
log.info("%s exists", d)
else:
log.error("UH OH")
except Exception as e:
log.error("mkd exception %s" % (e))
def cd(self,d):
try:
resp = self.ftp.cwd(d)
log.info("cd: %s" % resp)
except Exception as e:
log.error("cd exception: %s" % e)
sys.exit(3)
def godir(self,d):
try:
resp = self.ftp.cwd(d)
log.info("cd: %s" % resp)
except Exception as e:
self.mkdir(d)
self.godir(d)
def uploadFile(self,upfile,path=''):
log.debug("uploadFile() upfile = %s" % upfile)
matches = re.search('([^/]+)\/(.+)', upfile)
if matches:
log.debug("uploadFile() file needs recursion")
self.godir(matches.group(1))
self.uploadFile(matches.group(2),
os.path.join(path,matches.group(1)),
)
self.cd('..')
else:
filename = upfile
upfile = os.path.join(path,upfile)
if not os.path.isfile(upfile):
log.warning("file not exist: %s" % (upfile))
return 1
with open(upfile, 'rb') as f:
resp = self.ftp.storbinary("STOR %s" % (filename),
fp=f
)
log.info("uploadFile resp: %s" % (resp))
def getFile(self,f):
log.debug("getFile() f = %s" % f)
with open(f, 'wb') as fileout:
def callback(data):
f.write(data)
self.ftp.retrbinary('RETR %s' % f, callback)
def download(self,f,fp):
log.debug("download() %s" % f)
self.ftp.retrbinary('RETR ' + f, fp.write)
def connect(self):
self.ftp = FTP(host=self.host,
user=self.user,
passwd=self.passwd,
)
def getUnixTime(self,s):
return int(datetime.datetime( int(s[0:4]),
int(s[4:6]),
int(s[6:8]),
int(s[8:10]),
int(s[10:12]),
int(s[12:14])
).strftime("%s")
)
def rm(self,delFile):
try:
resp = self.ftp.delete(delFile)
log.info("rm() %s" % resp)
except Exception as e:
log.error("rm() exception: %s" % e)
def list(self,d=''):
FileObjs = self.ftp.mlsd(d)
fileList = list()
for fo in FileObjs:
dotfile = re.search('^(\.+)', fo[0])
if dotfile:
continue
fileList.append(fo[0])
return fileList
def listtest(self,d=''):
FileObjs = self.ftp.mlsd(d)
for fo in FileObjs:
try:
re.search('^(\.+)', fo[0]).group(1)
log.debug("ignore dotfiles: %s" % fo[0])
continue
except Exception as e:
pass
log.debug("Filename: %s" % fo[0])
self.pp.pprint(fo[1])
fileUnixTime = self.getUnixTime(fo[1]['modify'])
delta = self.nowUnixTime - fileUnixTime
log.debug("delta: %d" % delta)
if delta < FILE_AGE_LIMIT_IN_SECONDS:
log.info("file is young, keep")
else:
log.info("file is old")
self.rmFile(fo[0])
def findFiles(self,Q=None,path=''):
log.debug("findFiles() begin %s" % path)
if Q == None:
Q = queue.PriorityQueue()
FileObjs = self.ftp.mlsd(path)
for fileObj in FileObjs:
if fileObj[1]['type'] == 'dir':
log.debug("findFiles() keep digging %s" % (fileObj[0],))
self.findFiles(Q,os.path.join(path,fileObj[0]))
elif fileObj[1]['type'] == 'file':
log.debug("findFiles() found file %s" % (fileObj[0],))
fileUnixTime = self.getUnixTime(fileObj[1]['modify'])
Q.put( ( -fileUnixTime,
os.path.join(path,fileObj[0])
) )
else:
#log.debug("findFiles() ignore %s" % (fileObj,))
pass
log.debug("findFiles() done %s" % path)
return Q
def rmFiles(ftp,files):
log.debug("rmFiles()")
for f in files:
try:
ftp.rm(f)
except Exception as e:
log.error("rmFiles() ftp.rm() error: %s" % e)
log.debug("rmFiles() done")
class Creds():
def __init__(self):
self.host = ''
self.user = ''
self.passwd = ''
self.getCreds()
def getCreds(self):
if os.path.isfile('.creds'):
with open('.creds', 'r') as f:
for line in f:
toks = line.strip().split('=')
if toks[0] == 'host':
self.host = toks[1]
if toks[0] == 'user':
self.user= toks[1]
if toks[0] == 'passwd':
self.passwd= toks[1]
#FILE_AGE_LIMIT_IN_DAYS = 14
#FILE_AGE_LIMIT_IN_SECONDS = 60 * 60 * 24 * FILE_AGE_LIMIT_IN_DAYS
def rmOldFiles(ftp,limitSeconds=60*60*24*7):
log.debug("rmOldFiles() limit = %d" % limitSeconds)
Q = ftp.findFiles()
now = int(datetime.datetime.now().strftime("%s"))
log.debug("queue size: %d" % Q.qsize())
while Q.qsize() > 0:
try:
fileTime,f = Q.get(False,5)
except Exception as e:
log.error("rmOldFiles() could not Q.get(): %s" % e)
return
# we store unix time as negative in min heap
# for oldest-file-first sorting
if now + fileTime > limitSeconds:
log.info("rmOldFiles() rm %s" % (f,))
try:
ftp.rm(f)
except Exception as e:
log.error("rmOldFiles() ftp.rm() error: %s" % e)
else:
log.debug("rmOldFiles() keep %s" % (f,))
log.debug("rmOldFiles() done")
if __name__ == '__main__':
c = Creds()
wd = "public_html/python_test/"
ftp = ftpConn(c.host,c.user,c.passwd,wd)