-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.py
160 lines (138 loc) · 5.21 KB
/
index.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
#coding:UTF-8
"""
文件同步小工具
@author:yubang
2015-05-28
"""
from threading import Thread
from config import server,base
import os,ftplib,time,sqlite3,logging,re
import pyinotify,shutil
def sqlDeal(value):
value=value.replace("'","\\'").replace("\"","\\\"")
return value
def executeSql(sql,selectSign):
"执行sql语句"
conn = sqlite3.connect('data/file.db')
cursor = conn.cursor()
cursor.execute(sql)
if selectSign :
r=cursor.fetchall()
else:
r=cursor.rowcount
cursor.close()
conn.commit()
conn.close()
return r
class SendWorker(Thread):
"文件传送类"
def __moveSuccessUploadFile(self,filePath):
"移动上传成功的文件"
oldPath=filePath
filePath=filePath.replace(base.monitorPath,"")
if filePath[0] != "/":
filePath="/"+filePath
targetFile=base.move_dir+filePath
targetDir=os.path.dirname(targetFile)
if not os.path.exists(targetDir):
os.makedirs(targetDir)
try:
shutil.move(oldPath,targetFile)
logging.info(u"文件%s -> %s 移动成功!"%(oldPath,targetFile))
except:
logging.error(u"文件%s -> %s 移动失败!"%(oldPath,targetFile))
def __handle(self,filePath):
"处理要上传的文件"
result=True
for obj in server.serverLists:
remotePath=self.__getRemotePath(filePath,obj['data']['ftpRootPrefix'])
r=self.__sendFileUseFtp(obj['data']['ftpHost'],obj['data']['ftpPort'],obj['data']['ftpUser'],obj['data']['ftpPassword'],filePath,remotePath)
if not r:
result = False
logging.error(u"文件(%s)上传服务器(%s)失败!"%(filePath,obj['data']['ftpHost']))
else:
logging.info(u"文件(%s)上传服务器(%s)成功!"%(filePath,obj['data']['ftpHost']))
if result and base.move_able:
#移动上传后的文件
self.__moveSuccessUploadFile(filePath)
def __getRemotePath(self,filePath,remotePath):
"获取远程服务器文件存放路径"
filePath=filePath.replace(base.monitorPath,"")
if filePath[0] != "/":
filePath="/"+filePath
filePath=remotePath+filePath
filePath=os.path.dirname(filePath)
return filePath
def __sendFileUseFtp(self,serverHost,serverPort,serverUser,serverPassword,filePath,remotePath):
"发送文件"
result = True
ftp=ftplib.FTP()
ftp.connect(serverHost,serverPort)
r=ftp.login(serverUser,serverPassword)
if r == "230 Login successful.":
fp = open(filePath,"r")
#递归在ftp服务器创建文件夹
remotePaths=remotePath.split("/")
tempPath="."
for t in remotePaths:
tempPath+="/"+t
try:
ftp.mkd(tempPath)
except:
logging.info(u"在ftp服务器(%s)创建文件夹失败"%(serverHost))
try:
ftp.cwd(remotePath)
ftp.storbinary('STOR %s' % os.path.basename(filePath),fp,1024)
except:
logging.error(u"在ftp服务器(%s)上传文件失败"%(serverHost))
result=False
fp.close()
else:
result=False
ftp.quit()
return result
def run(self):
while True:
fps=executeSql("select * from files where status = 0 limit 1",True)
if len(fps) == 0:
time.sleep(1.5)
else:
try:
self.__handle(fps[0][0])
except Exception,e:
#raise
logging.error(str(e))
time.sleep(1.5)
executeSql("update files set status = 1 where path = '%s'"%(fps[0][0]),True)
class MyEventHandler(pyinotify.ProcessEvent):
"监控处理类"
def __checkAbleFile(self,filename):
"检测文件是否需要转移"
for r in base.able_lists:
if re.search("\."+r+"$",filename):
return True
return False
def process_IN_CLOSE_WRITE(self, event):
filePath=os.path.join(event.path,event.name)
if self.__checkAbleFile(event.name):
logging.info(u"检测到要上传文件:"+filePath)
executeSql("insert into files(path,status) values('%s',0)"%sqlDeal(filePath),False)
else:
logging.warning(u"检测到不符合规则文件:%s"%(filePath))
def init():
"初始化"
executeSql("create table if not exists files(path varchar(255),status int(1))",False)
logging.basicConfig(filename = "log/tmp.log",level = logging.NOTSET, format = '%(asctime)s - %(levelname)s: %(message)s' )
sendWorker=SendWorker()
sendWorker.setDaemon(True)
sendWorker.start()
def main():
"主函数"
init()
wm = pyinotify.WatchManager()
wm.add_watch(base.monitorPath, pyinotify.ALL_EVENTS, rec=True)
eh = MyEventHandler()
notifier = pyinotify.Notifier(wm, eh)
notifier.loop()
if __name__ == "__main__":
main()