-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·89 lines (67 loc) · 2.21 KB
/
manage.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
#!/usr/bin/env python3
import cmd
import re
from subprocess import check_output
import logging
logging.basicConfig(level=logging.INFO)
log = logging.getLogger()
class ShellCmd:
@classmethod
def run(cls, *args):
log.debug("CMD: %s", " ".join(args))
res = check_output(args).decode('utf8')
log.debug("RESULT: %s", res)
return res
class Git(ShellCmd):
'''
A simple interface to git commands
'''
@classmethod
def submodule(cls, *args):
cmd = ['git', 'submodule'] + list(args)
cls.run(*cmd)
@classmethod
def get_modules(cls):
cmd = ['git', 'config', '--file', '.gitmodules', '--get-regexp',
'path$']
res = cls.run(*cmd)
mods = (m.split()[-1] for m in res.strip().split('\n'))
names = (m.split('/')[-1] for m in mods)
return dict(zip(names, mods))
class Manager(cmd.Cmd, Git):
def __init__(self):
super().__init__()
self._load_modules()
def do_EOF(self, line):
print()
return True
def do_add(self, line):
url = line
name = url.split('/')[-1]
name = "bundle/" + re.sub(
r'(vim-)?(.*?)(.vim)?(.git)?$',
r'\2',
name)
self.submodule('add', url, name)
self.submodule('update', '--recursive', '--init', '--depth', "1", name)
self._load_modules()
def complete_rm(self, text, line, begidx, endidx):
return self._complete_modules(text, line, begidx, endidx)
def do_rm(self, line):
modname = self._modules[line]
self.submodule('deinit', "-f", modname)
self.git('rm', modname)
self.git('rm', '-f', modname)
self.run('rm', '-rf', '.git/modules/' + modname)
self._load_modules()
def do_ls(self, line):
print('list of vim plugins:')
for i, m in enumerate(sorted(self._modules.keys())):
print(' %2d: %s' % (i, m))
def _complete_modules(self, text, line, begidx, endidx):
return [m for m in self._modules if m.startswith(text)]
def _load_modules(self):
self._modules = self.get_modules()
if __name__ == '__main__':
mgr = Manager()
mgr.cmdloop()