-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrsync.py
105 lines (89 loc) · 2.67 KB
/
rsync.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
# use rsync to sync code from server
# coding = UTF8
# @author Kleist Zhou
# 2013/2/5
# Need pyinotify, pexpect
# please use easy_install to add in
import os
import sys
import pyinotify
import re
# your sync config
server = '199.245.60.73'
user = 'root'
password = ''
port = ''
source = './'
target = '/srv/www/love/public_html/'
options = "-rtvuC --delete --progress --exclude='rsync.py'"
#custom regular expression
rulers = (r"[#~.][\s\S]*", r"[\s\S]*[#~]", r"[\s\S]*_flymake.[\s\S]*")
if port != '':
port = "-e 'ssh -p %d'" % (port)
def runCmd(cmd):
global password
if password == '':
os.system(cmd)
else:
import pexpect
print cmd
child = pexpect.spawn(cmd)
try:
i = child.expect(['password: ', 'continue connecting (yes/no)?'])
if i == 0:
child.sendline(password)
elif i == 1:
child.sendline('yes')
except pexpect.EOF:
child.close()
else:
child.expect(pexpect.EOF)
child.close()
def sync():
global server, user, port, source, target, option
cmd = "rsync %s %s %s %s@%s:%s" % (port, options, source, user, server, target)
runCmd(cmd)
def download():
global server, user, port, source, target, option
cmd = "rsync %s %s %s@%s:%s" % (port, options, user, server, target)
print cmd
runCmd(cmd)
class OnChangeHandler(pyinotify.ProcessEvent):
def checkFileName(self, fileName):
global rulers
for ruler in rulers:
p = re.compile(ruler)
if p.match(fileName) != None:
return False
return True
def syncFile(self, fileName):
if self.checkFileName(fileName):
sync()
def process_IN_CREATE(self, event):
#print "Create file: %s " % os.path.join(event.path,event.name)
self.syncFile(event.name)
def process_IN_DELETE(self, event):
#print "Delete file: %s " % os.path.join(event.path,event.name)
self.syncFile(event.name)
def process_IN_MODIFY(self, event):
#print "Modify file: %s " % os.path.join(event.path,event.name)
self.syncFile(event.name)
def auto_sync():
global source
wm = pyinotify.WatchManager()
mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY
notifier = pyinotify.Notifier(wm, OnChangeHandler())
wm.add_watch(source, mask, rec=True, auto_add=True)
notifier.loop()
def main():
if len(sys.argv) == 2:
option = sys.argv[1]
if option == '-down':
download()
elif option == '-auto':
auto_sync()
else:
sync()
# main
if __name__ == "__main__":
main()