forked from tnhu/SimpleSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleSync.py
102 lines (84 loc) · 2.78 KB
/
SimpleSync.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
#
# Sublime Text SimpleSync plugin
#
# Help the orphans, street children, disadvantaged people
# and physically handicapped in Vietnam (http://bit.ly/LPgJ1m)
#
# @copyright (c) 2012 Tan Nhu, tnhu AT me . COM
# @version 0.0.1
# @licence MIT
# @link https://github.com/tnhu/SimpleSync
#
from __future__ import print_function, unicode_literals
import sublime
import sublime_plugin
import subprocess
import threading
#
# Run a process
# @param cmd process command
#
def runProcess(cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while (True):
retcode = p.poll() #returns None while subprocess is running
line = p.stdout.readline()
yield line.decode('utf-8')
if (retcode is not None):
break
#
# Get sync item(s) for a file
# @param local_file full path of a local file
# @return sync item(s)
#
def getSyncItem(local_file):
# Populate settings
settings = sublime.load_settings("SimpleSync.sublime-settings")
sync = settings.get("sync")
ret = []
for item in sync:
if local_file.startswith(item["local"]):
ret += [item]
return ret
#
# ScpCopier does actual copying using threading to avoid UI blocking
#
class ScpCopier(threading.Thread):
def __init__(self, host, username, local_file, remote_file, port=22):
self.host = host
self.port = port
self.username = username
self.local_file = local_file
self.remote_file = remote_file
threading.Thread.__init__(self)
def run(self):
remote = self.username + "@" + self.host + ":" + self.remote_file
print("SimpleSync: ", self.local_file, " -> ", remote)
for line in runProcess(["scp", "-r", "-P", str(self.port) , self.local_file, remote]):
print(line.encode('ascii', 'ignore'), end='')
#
# LocalCopier does local copying using threading to avoid UI blocking
#
class LocalCopier(threading.Thread):
def __init__(self, local_file, remote_file):
self.local_file = local_file
self.remote_file = remote_file
threading.Thread.__init__(self)
def run(self):
print("SimpleSync: ", self.local_file, " -> ", self.remote_file)
for line in runProcess(['cp', self.local_file, self.remote_file]):
print(line.encode('ascii', 'ignore'), end='')
#
# Subclass sublime_plugin.EventListener
#
class SimpleSync(sublime_plugin.EventListener):
def on_post_save(self, view):
local_file = view.file_name()
syncItems = getSyncItem(local_file)
if (len(syncItems) > 0):
for item in syncItems:
remote_file = local_file.replace(item["local"], item["remote"])
if (item["type"] == "ssh"):
ScpCopier(item["host"], item["username"], local_file, remote_file, port=item["port"]).start()
elif (item["type"] == "local"):
LocalCopier(local_file, remote_file).start()