-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRSync.py
327 lines (282 loc) · 12.2 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import os
from subprocess import Popen, PIPE
import sublime
import sublime_plugin
DEFAULT_CONF = {
'strsync.local_path' : False,
'hosts' : [ False ],
'strsync.use_ssh' : True,
'strsync.delete_slave' : True,
'strsync.remote_is_master' : True,
'strsync.excludes' : [],
'strsync.check_remote_git' : False,
}
# SPINNER_RESOURCES = [
# '[= ]',
# '[ = ]',
# '[ = ]',
# '[ = ]',
# '[ = ]',
# '[ = ]',
# '[ = ]',
# '[ =]',
# ]
PREF_PREFIX = 'strsync.'
annoy_on_rsync_error = True
annoy_on_hash_different = []
git_hash_per_path = {}
def run_executable(call_params):
result_mesg = ""
stderr = False
try:
p = Popen(call_params, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate(None)
except Exception as exc_err:
result_mesg = " EXCEPTION: \n{}\n".format(exc_err)
if stderr or result_mesg:
if stderr:
result_mesg += "Calling eternal command returned an error: \n{0}\n {1}".format(stderr.decode("utf-8") ," ".join(call_params))
print (result_mesg)
return (False, result_mesg)
else:
return (True, stdout.decode("utf-8"))
def get_path_for(executable_name):
(ran_ok, executable_path) = run_executable(['which', executable_name])
if not ran_ok or not executable_path or len(executable_path) <= 1:
print( " Can't find {} ... ".format(executable_name)) ## not trying too hard, though :)
return False
else:
return executable_path[:-1]
rsyncpath = get_path_for('rsync')
sshpath = get_path_for('ssh')
gitpath = get_path_for('git')
class RsyncTreeCommand(sublime_plugin.WindowCommand):
def run(sef):
STRSync(sublime.active_window().active_view()).sync_structure()
class RsyncFileFromRemoteCommand(sublime_plugin.WindowCommand):
def run(sef):
STRSync(sublime.active_window().active_view()).sync_remote_local()
class RsyncFileToRemoteCommand(sublime_plugin.WindowCommand):
def run(sef):
STRSync(sublime.active_window().active_view()).sync_local_remote()
class RSyncCommand(sublime_plugin.EventListener):
def on_load_async(self, view):
STRSync(view).sync_remote_local()
def on_post_save_async(self, view):
STRSync(view).sync_local_remote()
def on_activated_async(self, view):
STRSync(view).check_remote_local_git_hash()
class STRSHost(dict):
def excludes(self):
return self.get('excludes', [])
def host_name(self):
return self.get('remote_host', False)
def user_name(self):
return self.get('remote_host', False)
def path(self):
return self.get('remote_path', False)
# def local_path(self):
# return self.prefs('local_path')
# def use_ssh(self):
# return self.prefs('use_ssh')
# def remote_is_master(self):
# return self.prefs('remote_is_master')
# def delete_slave(self):
# return self.prefs('delete_slave')
def remote_host(self, relative_path=''):
if self:
return "{user}{host}".format(
user=self['remote_user'] + '@' if self.get('remote_user', False) else '',
host=self['remote_host'] if self.get('remote_host', False) else '',
)
else:
return False
def remote_path(self, relative_path=''):
if self:
path = os.path.normpath(self.get('remote_path','')) + relative_path
return "{remote_host}{path}".format(
remote_host=self.remote_host(),
path=':'+ path if self.get('remote_path', False) else '',
)
else:
return False
class STRSync:
def __init__(self, view=sublime.active_window().active_view()):
self.view = view
self.remote_hash = False
# self.spinner_step = 1
# self.spinner_direction = 1
# self.spinner_running = False
#################################
# settings and preferences handling
def prefs(self, preference):
return self.view.settings().get(PREF_PREFIX + preference, DEFAULT_CONF.get(PREF_PREFIX + preference, None))
def hosts(self):
for this_host in self.prefs('hosts'):
yield STRSHost(this_host)
def main_host(self):
for this_host in self.hosts():
if this_host.get('main', False):
return this_host
return this_host
# these are here just to make code shorter and easier to read ...
def excludes(self):
return self.prefs('excludes')
def local_path(self):
this_local_path = self.prefs('local_path')
return os.path.normpath(this_local_path) if this_local_path else ''
def use_ssh(self):
return self.prefs('use_ssh')
def remote_is_master(self):
return self.prefs('remote_is_master')
def delete_slave(self):
return self.prefs('delete_slave')
#################################
# the work itself
def check_remote_local_git_hash(self):
if not self.valid_file_to_process():
return
if not self.prefs('check_remote_git'):
return
(ran_ok, local_hash) = run_executable([gitpath, 'rev-parse', 'HEAD'])
if not ran_ok:
raise Exception(local_hash)
(ran_ok, self.remote_hash) = run_executable(
[
sshpath,
self.main_host().remote_host(),
'cd {}; git rev-parse HEAD'.format(self.main_host().path()),
])
if not ran_ok:
raise Exception(self.remote_hash)
if self.remote_hash in annoy_on_hash_different:
return
if self.remote_hash != local_hash:
print ("Remote Git hash ({}) is diferent from local ({})".format(self.remote_hash, local_hash))
self.view.window().show_quick_panel(
[
'Remote Git hash is diferent from local hash. FULL Rsync ?',
'No, don''t RSync just now. (This can get annoying)'
],
lambda s: self.handle_hash_is_different(s),
selected_index=1,
)
def handle_hash_is_different(self, answer):
if answer in [0, 1]:
annoy_on_hash_different.append(self.remote_hash)
if answer == 0:
self.sync_structure()
def sync_local_remote(self):
self.sync_file()
def sync_remote_local(self):
self.sync_file(False)
def valid_file_to_process(self):
local_file = self.view.file_name()
local_path = self.local_path()
if not local_file or not rsyncpath or not local_path:
return False
if not local_path.upper() in local_file.upper():
return False
return True
def sync_file(self, to_server=True):
# Need to add some checks on whether file changed before syncing
# right now, we sync way too often...
if not self.valid_file_to_process():
return
local_file = self.view.file_name()
local_path = self.local_path()
relative_path = local_file[len(os.path.normpath(local_path)) : ]
for this_host in self.hosts():
remote_path = this_host.remote_path(relative_path)
if not remote_path:
return
(first, second) = (local_file,remote_path) if to_server else (remote_path, local_file)
call_params = self.call_params(this_host, to_server, [first, second])
self.log_status('RSync: {}'.format(this_host.host_name()) )
self.run_rsync(call_params)
def sync_structure(self):
if not self.valid_file_to_process():
return
local_file = self.view.file_name()
local_path = self.local_path()
main_host = self.main_host()
if main_host:
remote_path = main_host.remote_path()
if not remote_path:
return
(first, second) = (remote_path + '/', local_path) if self.remote_is_master() else (local_path + '/',remote_path)
call_params = self.call_params(main_host, not self.remote_is_master(), ['-r', first, second])
self.log_status('RSync: {} [FULL SYNC: Please wait ... ]'.format(main_host.host_name()) )
sublime.set_timeout_async(lambda: self.run_rsync(call_params), 10)
def call_params(self, this_host, to_server=True, others=[]):
call_params = [rsyncpath ,'-a']
if self.use_ssh():
call_params.append('-e ssh')
if not( to_server and self.remote_is_master()) and self.delete_slave():
call_params.append('--delete')
excludes = self.excludes()
excludes.extend(this_host.excludes())
# damn it, I've been coding in perl too long
# this will introduce a '--exclude' for each excluded path.
# for a list such as [ '/bla/ble', '/doo/bi', '/a/b/'], the end result
# will be something like ['--exclude', '/bla/ble','--exclude', '/doo/bi', '--exclude', '/a/b/']
excludes = [ item for this_exclude in excludes for item in ['--exclude', '{}'.format(this_exclude)]]
call_params.extend(excludes)
call_params.extend(others)
return call_params
def run_rsync(self,call_params):
global annoy_on_rsync_error
(rsynced_ok, strMessage) = run_executable(call_params)
if not rsynced_ok:
if annoy_on_rsync_error:
self.log_error_message(strMessage)
self.view.window().show_quick_panel(
[
'Stop annoying me, for now, about network errors (a successful RSync will reset this) ',
# 'Ignore network errors for this Sublime session (you''ll need to reload to reset this)',
'Oh... golly! Please poke me each time this happens.'
],
lambda s: self.handle_error_reponse(s),
selected_index=0,
)
else:
annoy_on_rsync_error = True
print ("End rsync ")
self.clear_status()
def handle_error_reponse(self, answer):
global annoy_on_rsync_error
if answer == 0:
annoy_on_rsync_error = False
def log_status(self, message):
print (message)
self.view.set_status('_rsync_running{}'.format(self), message )
# self.start_spinner()
def clear_status(self):
old_status = self.view.get_status('_rsync_running{}'.format(self))
self.view.set_status('_rsync_running{}'.format(self), 'Ended [{}] '.format(old_status))
# self.stop_spinner()
sublime.set_timeout_async(lambda: self.view.erase_status('_rsync_running{}'.format(self)), 2000)
# def stop_spinner(self):
# self.spinner_running = False
# def start_spinner(self):
# self.spinner_running = True
# sublime.set_timeout_async(lambda: self.progress_spinner(), 100)
# def progress_spinner(self):
# print ("running")
# if not self.spinner_running :
# self.view.erase_status('__rsync_spinner{}'.format(self))
# return
# self.spinner_step += self.spinner_direction
# if self.spinner_step in [-1 , len(SPINNER_RESOURCES)]:
# self.spinner_direction = self.spinner_direction * -1
# self.spinner_step += self.spinner_direction * 2
# self.view.set_status('__rsync_spinner{}'.format(self), SPINNER_RESOURCES[self.spinner_step] )
# sublime.set_timeout_async(lambda: self.progress_spinner(), 100)
def log_error_message(self, message):
if self.view:
self.view.output_view = self.view.window().get_output_panel("textarea")
self.view.window().run_command("show_panel", {"panel": "output.textarea"})
self.view.output_view.set_read_only(False)
self.view.output_view.run_command("append", {"characters": message})
self.view.output_view.set_read_only(True)
print(message)