-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileupload.py
93 lines (79 loc) · 3.02 KB
/
fileupload.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
import os
import sys
import time
import subprocess
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class MyHandler(PatternMatchingEventHandler):
patterns = ["*.*"]
def process(self, event):
"""
event.event_type
'modified' | 'created' | 'moved' | 'deleted'
event.is_directory
True | False
event.src_path
path/to/observed/file
"""
# the file will be processed there
# print("event src path", event.src_path) # print now only for debug
# print("event type", event.event_type) # print now only for debug
subprocess.call(["rsync", "-avz", "--delete", "--exclude=.*", SOURCE, DEST])
def on_modified(self, event):
self.process(event)
def on_created(self, event):
self.process(event)
if __name__ == '__main__':
SOURCE = ""
DEST = ""
if len(sys.argv) is 1 or len(sys.argv) > 3:
print("------------------------------------------------------------------------------")
print("Specify correct commandline arguments. Use 1 or 2 arguments:")
print("")
print(" Usage with 1 argument:")
print(" Current directory will be used as source")
print(" Specify destination as: /path/to/dest OR user@ip.adr:/path/to/dest")
print("")
print("Example: python projectsync.py user@10.10.10.10:/path/to/dest")
print("")
print(" Usage with 2 arguments:")
print(" First arg: Path to source folder")
print(" Second arg: Path to destination folder")
print("")
print("Example: python projectsync.py /path/to/source user@10.10.10.10:/path/to/dest")
print("")
print("To sync to remote destination use second argument of format:")
print("username@ip.adress.of.host:/path/to/folder")
print("Transfer via rsync, so destination computer should have your ssh key installed")
print("------------------------------------------------------------------------------")
exit()
elif len(sys.argv) is 2:
SOURCE = os.getcwd()
if not SOURCE.endswith("/"):
SOURCE = SOURCE + "/"
DEST = sys.argv[1]
if DEST.endswith("/"):
DEST = DEST[:-1]
elif len(sys.argv) is 3:
SOURCE = sys.argv[1]
if not SOURCE.endswith("/"):
SOURCE = SOURCE + "/"
DEST = sys.argv[2]
if DEST.endswith("/"):
DEST = DEST[:-1]
print("Watcher will copy files from: %s" % SOURCE)
print("Watcher will copy files to: %s" % DEST)
print("")
print("Running inital transfer of existing files.")
subprocess.call(["rsync", "-avz", "--delete", "--exclude=.*", SOURCE, DEST])
print("")
print('Filewatcher started.')
observer = Observer()
observer.schedule(MyHandler(), path=SOURCE, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()