-
Notifications
You must be signed in to change notification settings - Fork 0
/
netvimdiff.py
145 lines (121 loc) · 4.96 KB
/
netvimdiff.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
#!/usr/bin/env python
#
# MIT License
#
# Copyright (c) 2019 David Cachau <[email protected]>
# Copyright (c) 2019 CAPSI Informatique
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
from __future__ import print_function
import re
import subprocess
import sys
import os
import tempfile
import shutil
def parse_file(file, index):
remote_match = re.match('^((?:[A-Za-z0-9-_]+@|)[A-Za-z0-9-_.]+):(.+)$', file)
if remote_match is not None:
return {'remote': remote_match.group(1), 'file': remote_match.group(2), 'index': index}
elif file is not None and file != '':
return {'remote': None, 'file': file, 'index': index}
else:
return {'remote': None, 'file': None, 'index': index}
def get_temp_dir():
return tempfile.mkdtemp(prefix='vimdiff-')
def rsync(file_from, file_to):
ret_code = -1
while ret_code != 0:
ret_code = subprocess.call(['rsync', '--verbose', '--inplace', file_from, file_to])
if ret_code != 0:
while True:
yn = raw_input('The rsync command had failed with code %i, retry (yes/no)? ' % ret_code)
if yn == 'yes':
break
elif yn == 'no':
print('Okay, I will NOT send this file:')
print('"%s" to "%s"' % (file_from, file_to))
return False
return True
def download_file(remote, remote_file, local_file):
print('Downloading %s from %s to temp file %s' % (remote_file, remote, local_file))
return rsync('%s:%s' % (remote, remote_file), local_file)
def upload_file(local_file, remote, remote_file):
print('Uploading %s to %s from temp file %s' % (local_file, remote_file, remote))
return rsync(local_file, '%s:%s' % (remote, remote_file))
if len(sys.argv) == 1:
print("Usage: %s [[user@]remote-server:]file [[user@]remote-server:]file " % (sys.argv[0]) +
"[[[user@]remote-server:]file [[[user@]remote-server:]file]]")
print("")
print("Edit and compare files from local and remote sources")
sys.exit(1)
files = []
for i in range(1, 5):
try:
f = parse_file(sys.argv[i], i)
if f['remote'] is None and f['file'] is None:
print("Arg %i is not valid" % i)
sys.exit(2)
files.append(f)
except IndexError:
pass
if len(files) < 2:
print('You had to pass between 2 and 4 files to edit')
exit(3)
has_error = False
tmp_dir = get_temp_dir()
for file in files:
if file['remote'] is not None:
tmp_file = '%s/%i-%s-%s' % (tmp_dir, file['index'], file['remote'], os.path.basename(file['file']))
success = download_file(file['remote'], file['file'], tmp_file)
has_error |= not success
file['temp'] = tmp_file
else:
file['temp'] = None
vim_args = ['vimdiff']
for file in files:
if file['temp'] is not None:
vim_args.append(file['temp'])
else:
vim_args.append(file['file'])
ret_code = subprocess.call(vim_args)
if ret_code != 0:
while True:
yn = raw_input('vim has return with the code %i, do you want to send files to remotes (yes/no)? ' % ret_code)
if yn == 'yes':
print('Okay, I will send all files for you')
break
elif yn == 'no':
print('Okay, I will NOT send files')
print('If you want to find files on the local filesystem, you can find them in:')
print(tmp_dir)
print('You can edit and send files manually, don\'t forget to delete the temporary directory.')
sys.exit(4)
for file in files:
if file['remote'] is not None:
success = upload_file(file['temp'], file['remote'], file['file'])
has_error |= not success
if has_error:
print('The temp dir has not been removed due previous to errors.')
print('Please remove manually the following directory:')
print(tmp_dir)
elif tmp_dir != '' and tmp_dir != '/':
print('Removing temporary directory')
subprocess.call(["rm", "--preserve-root", "-rf", tmp_dir])