forked from mike-zhang/autoSync
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathautoSync.py
More file actions
197 lines (171 loc) · 6.31 KB
/
Copy pathautoSync.py
File metadata and controls
197 lines (171 loc) · 6.31 KB
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
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import time,os,sys
import paramiko
import posixpath, traceback
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class ConfigData():
def __init__(self,_fileName):
self.fileName = _fileName
self.docTree = None
self.ssh_host = "127.0.0.1"
self.ssh_port = 22
self.ssh_user = "root"
self.ssh_passwd = ""
self.currentDir = "."
self.remoteDir = "/tmp/t1"
self.arrFileExcept = []
self.getConfigFromFile()
def show(self):
print (self.ssh_host)
print (self.ssh_port)
print (self.ssh_user)
print (self.ssh_passwd)
print (self.currentDir)
print (self.remoteDir)
print (self.arrFileExcept)
def getSectiontText(self,path):
retText = ""
if self.docTree :
objTmp = self.docTree.find(path)
if objTmp != None :
retText = objTmp.text or ""
return retText
def getFileExcept(self):
if not self.docTree :
return None
objTmp = self.docTree.findall("fileExcept/file")
if objTmp :
self.arrFileExcept += [os.path.join(self.currentDir,item.text) for item in objTmp]
return None
def getSectiontInt(self,path):
strTmp = self.getSectiontText(path).strip()
return (int(strTmp) if strTmp.isdigit() else 0)
def getConfigFromFile(self):
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
if not os.path.exists(self.fileName) :
print ("file ", self.fileName, " not exists")
return None
try:
self.docTree = ET.ElementTree(file=self.fileName)
except Exception as e:
print ("%s is NOT well-formed : %s "%(self.fileName,e))
return None
self.ssh_host = self.getSectiontText("host").strip()
self.ssh_port = self.getSectiontInt("sshPort")
self.ssh_user = self.getSectiontText("user").strip()
self.ssh_passwd = self.getSectiontText("password").strip()
self.currentDir = self.getSectiontText("localDir").strip()
self.currentDir = os.path.abspath(self.currentDir)
self.remoteDir = self.getSectiontText("remoteDir").strip()
self.getFileExcept()
return None
def getSSHInstance(cnf):
ssh=paramiko.SSHClient()
#ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=cnf.ssh_host,
port=cnf.ssh_port,
username=cnf.ssh_user,
password=cnf.ssh_passwd,
timeout=5)
return ssh
def doScp(srcPath,cnf):
bRet = False
try:
print ("srcPath : {0}".format(srcPath))
if srcPath in cnf.arrFileExcept :
print ("{0} in arrFilesExcept".format(srcPath))
return bRet
srcFile = os.path.relpath(srcPath,cnf.currentDir)
dstFile = posixpath.join(cnf.remoteDir,srcFile.replace(os.path.sep, posixpath.sep))
print (dstFile)
print("doScp : {0},{1}".format(srcPath,dstFile))
if not os.path.exists(srcPath) :
print("doScp : file {0} not exist".format(srcPath))
bRet = False
else :
ssh = getSSHInstance(cnf)
strcmd = "mkdir -p {0}".format(posixpath.split(dstFile)[0])
print (strcmd)
stdin,stdout,stderr=ssh.exec_command(strcmd)
sftp = paramiko.SFTPClient.from_transport(ssh.get_transport())
sftp = ssh.open_sftp()
sftp.put(srcPath,dstFile)
ssh.close()
bRet = True
except :
print ("error occur")
print (traceback.format_exc())
bRet = False
return bRet
def doRemoteCmd(cnf,strcmd):
bRet = False
try:
ssh = getSSHInstance(cnf)
print (strcmd)
stdin,stdout,stderr=ssh.exec_command(strcmd)
bRet = True
except :
print (traceback.format_exc())
bRet = False
return bRet
class SyncHandler(FileSystemEventHandler):
def __init__(self, conf):
self.conf = conf
def doFileSync(self,event):
if not event.is_directory :
srcFile = event.src_path
srcFile = os.path.abspath(srcFile)
doScp(srcFile,self.conf)
return None
def doFileDelete(self, event):
if not event.is_directory :
srcPath = os.path.abspath(event.src_path)
srcFile = os.path.relpath(srcPath,self.conf.currentDir)
dstFile = posixpath.join(self.conf.remoteDir,srcFile.replace(os.path.sep, posixpath.sep))
print (dstFile)
if dstFile :
strcmd = "rm -f {0}".format(dstFile)
doRemoteCmd(self.conf, strcmd)
return None
def on_modified(self, event):
print (event.key)
self.doFileSync(event)
def on_deleted(self, event):
print (event.key)
self.doFileDelete(event)
def on_moved(self,event):
print (event.key,"moved")
self.doFileDelete(event)
self.doFileSync(event)
if __name__ == "__main__":
if len(sys.argv) < 2 :
print ("usage : %s conf.xml" % sys.argv[0])
sys.exit(1)
confFile = sys.argv[1]
conf = ConfigData(confFile)
conf.show()
print (conf.arrFileExcept)
# do sync in start
for root, dirs, files in os.walk(conf.currentDir):
for name in files:
t_path = os.path.join(root, name)
t_path = os.path.abspath(t_path)
print (t_path, name)
doScp(t_path,conf)
# do sync on modify
event_handler = SyncHandler(conf)
observer = Observer()
observer.schedule(event_handler, path=conf.currentDir, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()