-
Notifications
You must be signed in to change notification settings - Fork 21
/
lnk.py
executable file
·162 lines (131 loc) · 4.71 KB
/
lnk.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
#!/usr/bin/env python
import sys
import os
from link import lnk
from optparse import OptionParser
from subprocess import Popen
parser = OptionParser()
parser.add_option("-i", "--install", dest="install",
help="install this file in the wrappers directory")
parser.add_option("-r", "--install_git", dest="install_git",
help="install from git location into wrappers file")
parser.add_option("-g", "--global_install",
dest="global_install",action="store_true", default=False,
help="If set to global it will be installed into link in site packages")
def get_shell_commands():
lnk.run_command("find /usr/local/bin/ -perm -u+x,g+x ")
def get_from_git(location):
"""
Run git and put the data in the plugins tmp directory.
From there install it
"""
tmp = lnk.plugins_directory()
name = location.split("/")[-1].split('.')[0]
clone_to = '%s/%s' % (tmp, name)
print "getting the plugin from git and placing in %s" % clone_to
p = Popen("git clone %s %s" % (location ,clone_to), shell=True)
p.wait()
print "done getting file"
return clone_to
class ShCmd(object):
def __init__(self, command, directory):
self.command = command
self.directory = directory
class LnkSh(object):
"""
lnk shell class. Allows you to call either a shell command or
a command configured in lnk.
# start hbase from your shell
shell> lnk dbs.hbase.stop
stopping...
"""
def __init__(self, options=None):
self.options = self._parse_options(options)
self.shell_commands = None
self.path = os.environ.get("PATH", "")
def _parse_options(self, options):
"""
Options may come in as command line args. need to change them
into a dictionary. Also removes any None's from the parser args because
that means they aren't set and we don't want that overriding global args
"""
if not options:
return {}
from optparse import Values
#if straight from a parser use it's dictionary
if isinstance(options, Values):
#remove the nones
return dict(
[(key, value) for key, value in
options.__dict__.iteritems()
if value!=None]
)
return options
def lookup_lnk(self):
"""
Lookup to see if it's something lnk has configured
"""
def get_shell_commands(self):
"""
Get all executable commands in your PATH
"""
self.shell_commands = {}
command_dirs = self.path.split(":")
#you want to look them up backwords so that commands in the beginning of
#the path overwrite the ones in the back
# PATH=/usr/bin:/usr/local/bin
# we want to prefer those in /usr/bin
command_dirs.reverse()
#get all the commands
commands = [os.listdir(dir) for dir in command_dirs]
commands = zip(commands, command_dirs)
final_commands = []
for cmds, dir in commands:
final_commands.extend(
[(cmd, dir) for cmd in cmds
if os.access('%s/%s' % (dir, cmd), os.X_OK)]
)
#return an indexed map
#this is where the overwritting hoppens. dict creation prefers the
#second instance
return dict([(cmd, ShCmd(cmd, dir)) for cmd, dir in final_commands])
def lookup_shell(self, command):
"""
"""
if not self.shell_commands:
self.shell_commands = self.get_shell_commands()
print self.shell_commands
def __call__(self, command, options=None):
"""
call the actual command. Override global options
"""
_options = {}
if options:
_options = self.options.copy()
_options.update(
self._parse_options(options)
)
self.lookup_shell("thaseu")
#TODO: Take care of aliases
print _options
print command
def main():
"""
Take in user input. Start up the vlnk shell
"""
(options, args) = parser.parse_args()
lnk_sh = LnkSh()
lnk_sh(args, options)
sys.exit()
install_file = options.install
install_git = options.install_git
install_global = options.global_install
#if 'commands' in args:
#get_shell_commands(
if install_file:
lnk.install_plugin(install_file, install_global)
elif install_git:
install_file = get_from_git(install_git)
#lnk.install_plugin(install_file, install_global)
if __name__ == '__main__':
main()