-
Notifications
You must be signed in to change notification settings - Fork 7
/
pybcompgen.py
279 lines (228 loc) · 9.7 KB
/
pybcompgen.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python
"""
https://github.com/mattvonrocketstein/smash
The MIT License (MIT)
Copyright (c) 2014 matthew von rocketstein
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.
"""
""" pybcompgen
Pybcompgen calculates context sensitive tab-completion data which is
derived from environment bash system settings. It doesn't need to know
anything about whether you use /etc/completion or /etc/bash_completion.d,
all that matters is whether *bash* knows about the completion. The benefit
of doing this are obvious: you get access to all the completion features
that the system has installed without caring how the completion features
work. Note that this approach doesn't just work for things in the users
$PATH, it works for arbitrary complex completion. In the default linux
installations, completion normally includes everything from
git-subcommands to debian packages, depending on context.
Example I/O:
$ pybcompgen "/et"
["/etc "]
$ pybcompgen "git lo"
["log"]
$ pybcompgen "apt-ge"
["apt-get "]
$ pybcompgen "apt-get inst"
["install "]
$pybcompgen "apt-get install ubuntu-art"
["ubuntu-artwork "]
"""
"""
Swell.sh changes:
1. Python 3
2. Use specific input rc file
3. Some changes to the parsing of bash output to make it work on more cases
4. Add the void function to prevent accidentally running the command
5. Case insensitivity
"""
import sys
import unicodedata
from subprocess import Popen, PIPE, DEVNULL, TimeoutExpired
import os
import signal
import re
import shutil
import shlex
from functools import reduce
import pprint
# def remove_control_characters(s):
# return "".join(ch for ch in s if unicodedata.category(ch)[0]!="C")
ansi_escape = re.compile(r'\x1b\[C')
def remove_control_characters(s):
s = ansi_escape.sub('', s)
s = s.replace('[1@#', '')
tmp = "".join(ch for ch in s if ch == '\x08' or unicodedata.category(ch)[0] != "C")
ret = ""
for ch in tmp:
if ch == '\x08':
# NOTE: very inefficient
ret = ret[:-1]
else:
ret += ch
return ret
# NOTE: by adding the void here,
# if bash-completion package is not installed, `complete` won't suggest commands
# so in that case make sure {to_complete} already contains command + ' ' + xxx
CMD_TMPL = [
#'cd {cwd}',
'PS1=""',
'void(){{ :; }}',
'declare -F _command &>/dev/null && complete -F _command void',
'echo MARKER',
'void {complete}\t\x01#',
'echo MARKER',
]
CMD_TMPL = '\n'.join(CMD_TMPL)
BASH_PATH = shutil.which('bash')
def complete(to_complete, cwd=None, show_all=False):
""" wow! so this is stupid, but what can you do? to understand
the command required to get completion information out of bash,
start by executing "printf '/et\x09\x09' | bash -i". What this
command does is put bash into interactive mode, then simulate
typing "/et<tab><tab>" inside bash. The tab-completion information
can scraped out of the text, but several things complicate the final
solution:
1) the tab-completion info, apart from being post-processed, must be
scraped from stderr, not from stdout.
2) for post-processing, without knowledge of how the prompt will be
rendered or if there is some kind of banner that will be printed,
it's hard to know where exactly to start capturing tab-completion
options.
3) the method used to get the tab completion involves the bash builtins
"printf", meaning we have to launch subprocess with "bash -c"
4) completion output could be paginated by bash if there are lots of
options. have to account for that and still try to get all the
options instead of just the first page
5) sending EOF does not working unless it comes after a newline. this
means we have to take extra steps to avoid actually executing the
command we want to complete (executing the command is not what the
user expects and is possibly unsafe). to avoid executing the line,
after getting tab completion you have to simulate control-a (go to
start of line) followed by '#'. this comments the line and prevents
it from executing. note: you cannot send control-c to cancel the
execution of the line because we are dealing with pipes, whereas
control-c is processed only by proper tty's.
"""
if not to_complete:
return []
my_env = {}
my_env['HISTFILE'] = '/dev/null'
my_env['HOME'] = os.environ.get('HOME')
if os.environ.get("LD_LIBRARY_PATH"):
my_env['LD_LIBRARY_PATH'] = os.environ.get('LD_LIBRARY_PATH')
if show_all:
my_env['INPUTRC'] = os.path.join(os.getcwd(), 'myinputrc_showall')
else:
my_env['INPUTRC'] = os.path.join(os.getcwd(), 'myinputrc')
# print(my_env)
if not cwd:
cwd = os.getcwd()
cmd = '''{bash} -c "printf '{cmd_tmpl}'| bash -i"'''.format(cmd_tmpl=CMD_TMPL, bash=BASH_PATH)
cmd = cmd.format(cwd=cwd, complete=to_complete)
p1 = Popen(shlex.split(cmd), shell=False, stdout=PIPE, stdin=DEVNULL, stderr=PIPE, env=my_env, cwd=cwd, start_new_session=True)
try:
# print("spawning", p1.pid)
out, err = p1.communicate(timeout=1)
except TimeoutExpired as e:
# print("timeout, terminating", p1.pid)
# p1.terminate()
os.killpg(os.getpgid(p1.pid), signal.SIGTERM) # kill the subprocesses (i.e. bash -i) as well
p1.wait()
out, err = p1.communicate()
raise e
err = err.decode('utf-8')
# import bpdb; bpdb.set_trace()
# print(err)
lines = err.split('\n')
for i in range(len(lines)):
l = lines[i]
idx = l.find('\r')
if idx > -1:
lines[i] = lines[i][:idx]
#lines = err.splitlines()
#pprint.pprint(lines)
first_marker = None
last_marker = None
for i in range(len(lines)):
line = lines[i]
if line.strip().endswith('echo MARKER'):
if first_marker is None:
first_marker = i
else:
last_marker = i
# print('markers:', first_marker, last_marker)
# pprint.pprint(lines)
# SPECIAL CASE: pagination
if last_marker is None:
# when this happens there are too many options,
# ie bash is asking something like this:
# Display all 103 possibilities? (y or n)
# Pagination indicators like '--More--'must be removed
lines = [line for line in lines if not line.startswith('--More')]
last_marker = len(lines) - 3
# first_marker+=1
complete_lines = lines[first_marker+2:last_marker-1]
# print("OSCAR DEBUG")
# pprint.pprint(complete_lines)
#import bpdb; bpdb.set_trace()
#SPECIAL-CASE: no completion options or only one option
if not complete_lines:
# NOTE:
# if there is only one option, readline simply applies it,
# which affects the current line in place. apparently this
# results in tons of control-characters being dumped onto
# the line, and we have to clean those up for the output
try:
the_line = lines[first_marker+1:last_marker][0]
except IndexError as e:
print("IndexError")
return []
the_line = remove_control_characters(the_line)
i1 = the_line.lower().rfind(to_complete.lower())
subs = the_line[i1:i1+len(to_complete)]
if subs.lower() == to_complete.lower():
# print("yes")
to_complete = subs
tmp = the_line[the_line.rfind(to_complete)+len(to_complete):]
if to_complete.endswith(' '):
# result = to_complete.split()[-1] + ' ' + tmp
result = tmp
else:
result = to_complete.split()[-1] + tmp
if '#' in result:
# this seems to only happen for directories. not sure why
result = result[:result.find('#')]
if result == to_complete.split()[-1]:
#SPECIAL-CASE: no completion options at all.
return []
# Sometimes it outputs duplicated things, eg: 'app.py app.py '
parts = result.split(' ')
if len(parts) == 3 and parts[0] == parts[1]:
result = parts[0]
return [result]
else:
# there are multiple completion options
completion_choices_by_row = [x.split() for x in complete_lines]
completion_choices = reduce(lambda x,y:x+y, completion_choices_by_row)
return completion_choices
if __name__=='__main__':
# if being called from the command line, output json
# so it is easier for another application to consume
import json
result = complete(sys.argv[-1])
print(json.dumps(result))