-
Notifications
You must be signed in to change notification settings - Fork 3
/
serv_switch
executable file
·63 lines (56 loc) · 2.22 KB
/
serv_switch
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/python
# Switches the server config file for vim-syncr
# Copyright (C) 2014 Jacob Malcom, [email protected]
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import glob
import os
import sys
def get_name(f):
"Return the name of the remote machine described in file f."
for line in open(f):
if line.startswith("name"):
return line.rstrip().split()[-1]
def switch(cur, cur_nam, new):
"Rename the .syncrconf files to make the chosen remote active."
old_nam = cur + "_" + cur_nam
os.rename(cur, old_nam)
new_dat = new.split("/")
new_nam = "/".join(new_dat[:-1]) + "/" + new_dat[-1].split("_")[0]
os.rename(new, new_nam)
def main():
"Facilitate simple remote machine switching using vim-syncr."
repository_directory = os.path.dirname(os.path.realpath(__file__))
servers, cnt = {}, 0
for fil in glob.glob(os.path.join(repository_directory, ".syncrconf*")):
if fil == os.path.join(repository_directory, ".syncrconf"):
cur_serv = fil
cur_serv_name = get_name(fil)
cnt += 1
for line in open(fil):
if line.startswith("name"):
servers[fil] = line.rstrip().split()[-1]
print("\nAvailable servers:")
serv_keys = servers.keys()
for i in range(len(serv_keys)):
print("\t%s %s" % (i, servers[serv_keys[i]]))
if len(serv_keys) == 0
print("No servers found")
choice = int(input("\nWhich server to choose?\t"))
if servers[serv_keys[choice]] == cur_serv_name:
print("Server not changed.")
sys.exit()
switch(cur_serv, cur_serv_name, serv_keys[choice])
if __name__ == '__main__':
main()