/
sync
executable file
·63 lines (51 loc) · 2.01 KB
/
sync
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
#!/usr/bin/env python3
import os
import queue
import subprocess
import sys
import threading
sync_queue = queue.Queue()
def do_sync():
while True:
path = sync_queue.get()
local_path = path.replace(local_directory, '').lstrip('/')
remote_path = remote_directory.rstrip('/') + '/' + local_path
print('[{} remaining] {} ... '.format(sync_queue.qsize(), local_path), end = '')
sys.stdout.flush()
try:
subprocess.check_call('scp "{local_path}" "{remote_host}:{remote_path}"'.format(
local_path = local_path,
remote_host = remote_host,
remote_path = remote_path
), shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
except Exception as ex:
print('Failed to copy "{local_path}": {exception}'.format(local_path = local_path, exception = ex))
finally:
print('done')
def sync_command(command):
process = subprocess.Popen(command, shell = True, stdout = subprocess.PIPE)
for line in process.stdout:
for file in line.decode().strip().split():
sync_queue.put(file)
if __name__ == '__main__':
try:
remote_host, remote_directory = sys.argv[1].split(':')
except:
print('Usage: sync HOST:PATH')
sys.exit(0)
local_directory = os.getcwd()
print('Syncing files from {local_directory} to {remote_host}:{remote_directory}\n'.format(
local_directory = local_directory,
remote_host = remote_host,
remote_directory = remote_directory
))
# Start up the sync thread
sync_thread = threading.Thread(target = do_sync)
sync_thread.daemon = True
sync_thread.start()
# Sync any differences on the current branch (first changed, then new files)
if '--skip' not in sys.argv:
sync_command('git diff --name-only master')
sync_command('git ls-files --others --exclude-standard')
# Sync any changes to files as they happen
sync_command('fswatch -r -e ".git" .')