2009-03-21 15:41:33 -04:00
|
|
|
import os
|
2008-03-17 03:16:15 -04:00
|
|
|
from subprocess import Popen, PIPE, STDOUT
|
|
|
|
|
2009-03-21 15:41:33 -04:00
|
|
|
import dirutil
|
|
|
|
import regex
|
|
|
|
from buffer.emul import XTermBuffer
|
|
|
|
from method import Method, arg
|
|
|
|
from term import XTerm
|
|
|
|
from window import Window
|
2008-03-17 03:16:15 -04:00
|
|
|
|
|
|
|
class Exec(Method):
|
|
|
|
'''Execute a command in a shell and put the output in a new buffer'''
|
2008-03-20 21:58:30 -04:00
|
|
|
show_success = True
|
2009-03-21 15:41:33 -04:00
|
|
|
args = [arg('cmd', dt='shell', p="Exec: ")]
|
2008-10-04 12:09:15 -04:00
|
|
|
def _doit(self, w, path, cmd, cmdname=None, bufname=None, cmddir=None, opts={}):
|
2009-03-21 15:41:33 -04:00
|
|
|
if cmddir: cmd = "cd %r && %s" % (cmddir, cmd)
|
2008-10-04 12:09:15 -04:00
|
|
|
d = dict(opts)
|
2009-03-21 15:41:33 -04:00
|
|
|
if path: d['path'] = path
|
|
|
|
|
2008-10-04 12:09:15 -04:00
|
|
|
try:
|
|
|
|
cmd = cmd % d
|
|
|
|
except:
|
|
|
|
pass
|
2008-03-18 10:51:47 -04:00
|
|
|
|
2008-11-13 23:44:41 -05:00
|
|
|
if bufname is None: bufname = '*%s*' % self.name.title()
|
|
|
|
if cmdname is None: cmdname = cmd.split(None, 1)[0]
|
|
|
|
|
|
|
|
p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
|
|
|
|
output = p.stdout.read()
|
|
|
|
result = p.wait()
|
|
|
|
status = os.WEXITSTATUS(result)
|
2008-03-20 21:58:30 -04:00
|
|
|
|
|
|
|
if not os.WIFEXITED(result):
|
2009-03-14 22:36:27 -04:00
|
|
|
err = True
|
|
|
|
msg = "%s: killed by signal %r" % (cmdname, os.WTERMSIG(result))
|
2008-03-20 21:58:30 -04:00
|
|
|
elif status != 0:
|
2009-03-14 22:36:27 -04:00
|
|
|
err = True
|
|
|
|
msg = "%s: failed with status %r" % (cmdname, status)
|
2008-03-20 21:58:30 -04:00
|
|
|
else:
|
2009-03-14 22:36:27 -04:00
|
|
|
err = False
|
|
|
|
msg = "%s: ok" % (cmdname,)
|
2008-03-20 21:58:30 -04:00
|
|
|
|
|
|
|
if output:
|
|
|
|
switch_to = err or self.show_success
|
|
|
|
w.application.data_buffer(bufname, output, switch_to=switch_to)
|
2009-03-14 22:36:27 -04:00
|
|
|
w.set_error(msg)
|
2008-03-18 10:51:47 -04:00
|
|
|
|
2008-03-17 03:16:15 -04:00
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
if w.buffer.btype == 'dir':
|
2009-03-21 15:41:33 -04:00
|
|
|
name, path = dirutil.resolve_name_path(w)
|
2008-03-17 03:16:15 -04:00
|
|
|
self._doit(w, path, vargs['cmd'])
|
|
|
|
dirutil.find_name(w, name)
|
|
|
|
elif hasattr(w.buffer, 'path'):
|
|
|
|
path = w.buffer.path
|
|
|
|
self._doit(w, path, vargs['cmd'])
|
|
|
|
else:
|
|
|
|
self._doit(w, None, vargs['cmd'])
|
|
|
|
|
2008-10-19 20:01:58 -04:00
|
|
|
class Man(Exec):
|
|
|
|
'''Execute a command in a shell and put the output in a new buffer'''
|
2009-03-21 15:41:33 -04:00
|
|
|
args = [arg('name', p="Program: ")]
|
2008-10-19 20:01:58 -04:00
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
name = vargs['name']
|
|
|
|
cmd = 'man %r' % name
|
2009-03-14 19:50:16 -04:00
|
|
|
p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
|
2008-10-19 20:01:58 -04:00
|
|
|
output = p.stdout.read()
|
|
|
|
result = p.wait()
|
|
|
|
status = os.WEXITSTATUS(result)
|
|
|
|
if not os.WIFEXITED(result):
|
|
|
|
err = True
|
|
|
|
errmsg = "man: killed by signal %r" % os.WTERMSIG(result)
|
|
|
|
elif status != 0:
|
|
|
|
err = True
|
|
|
|
errmsg = "man: failed with status %r" % status
|
|
|
|
else:
|
|
|
|
err = False
|
|
|
|
errmsg = "man: ok"
|
|
|
|
if output:
|
2009-03-21 15:41:33 -04:00
|
|
|
xterm = XTerm(cbuf=True)
|
|
|
|
s = xterm.term_filter(output)
|
|
|
|
switch = err or self.show_success
|
|
|
|
w.application.color_data_buffer('*Manpage*', s, switch_to=switch)
|
2008-10-19 20:01:58 -04:00
|
|
|
w.set_error(errmsg)
|
|
|
|
|
2008-03-17 03:16:15 -04:00
|
|
|
class Pipe(Method):
|
|
|
|
'''Pipe the buffer's contents through the command, and display the output in a new buffer'''
|
2009-03-21 15:41:33 -04:00
|
|
|
args = [arg('cmd', dt="shell", p="Pipe: ")]
|
2008-03-17 03:16:15 -04:00
|
|
|
def _parse(self, w, **vargs):
|
|
|
|
# return 3 things: prog name, cmd, and whether to use the shell
|
|
|
|
m = regex.shell_command.match(vargs['cmd'])
|
|
|
|
if m:
|
|
|
|
prog = m.group(0)
|
|
|
|
return (prog, vargs['cmd'], True)
|
|
|
|
else:
|
|
|
|
return (None, None, False)
|
2008-03-18 10:51:47 -04:00
|
|
|
|
|
|
|
def _display(self, w, data, status, prog, cmd, shell):
|
2008-03-20 09:31:43 -04:00
|
|
|
lines = data.split('\n')
|
|
|
|
if lines and lines[-1] == '':
|
|
|
|
lines = lines[:-1]
|
2009-04-26 22:13:57 -04:00
|
|
|
bufname = '*%s*' % self.name.title()
|
|
|
|
b = w.application.data_buffer(bufname, data, switch_to=True, modename='error')
|
|
|
|
b.orig_path = w.buffer.path
|
|
|
|
w.set_error("%s exited with status %d" % (prog, status))
|
2008-03-18 10:51:47 -04:00
|
|
|
|
2008-03-17 03:16:15 -04:00
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
(prog, cmd, shell) = self._parse(w, **vargs)
|
|
|
|
if prog is None or not cmd:
|
|
|
|
return
|
2009-04-26 22:13:57 -04:00
|
|
|
|
2008-10-06 00:55:38 -04:00
|
|
|
self._dopipe(w, prog, cmd, shell)
|
2008-03-17 03:16:15 -04:00
|
|
|
|
2008-10-06 00:55:38 -04:00
|
|
|
def _dopipe(self, w, prog, cmd, shell):
|
2008-03-17 03:16:15 -04:00
|
|
|
pipe = Popen(cmd, shell=shell, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
|
|
|
|
pid = pipe.pid
|
|
|
|
|
|
|
|
indata = w.buffer.make_string()
|
|
|
|
pipe.stdin.write(indata)
|
|
|
|
pipe.stdin.close()
|
|
|
|
|
|
|
|
outdata = pipe.stdout.read()
|
2008-03-18 10:51:47 -04:00
|
|
|
status = pipe.wait() >> 8
|
2008-03-17 03:16:15 -04:00
|
|
|
|
2008-03-18 10:51:47 -04:00
|
|
|
self._display(w, outdata, status, prog, cmd, shell)
|
2008-03-17 03:16:15 -04:00
|
|
|
|
|
|
|
class Grep(Pipe):
|
|
|
|
'''Grep the buffer's contents for instances of a pattern, and display them in a new buffer'''
|
2009-03-21 15:41:33 -04:00
|
|
|
args = [arg('pattern', dt="str", p="Pattern: ")]
|
2008-03-17 03:16:15 -04:00
|
|
|
def _parse(self, w, **vargs):
|
|
|
|
return ('grep', ('grep', '-E', '-n', vargs['pattern']), False)
|
2009-04-26 22:13:57 -04:00
|
|
|
|
2008-03-17 03:16:15 -04:00
|
|
|
class Sed(Pipe):
|
|
|
|
'''Push the buffer's contents through a sed expression'''
|
2009-03-21 15:41:33 -04:00
|
|
|
args = [arg('expression', dt="str", p="Expression: ")]
|
2008-03-17 03:16:15 -04:00
|
|
|
def _parse(self, w, **vargs):
|
2009-04-26 22:13:57 -04:00
|
|
|
return ('sed', ('sed', '-r', '-e', vargs['expression']), False)
|
2009-03-03 21:10:32 -05:00
|
|
|
|
|
|
|
class Interact(Method):
|
|
|
|
'''Interact with a program via a PTY'''
|
2009-03-21 15:41:33 -04:00
|
|
|
args = [arg('bname', dt="str", p="Buffer Name: ", dv=lambda w: '*Interact*'),
|
|
|
|
arg('cmd', dt="shell", p="Command: ", dv=lambda w: 'bash')]
|
2009-03-11 23:53:56 -04:00
|
|
|
modename = None
|
2009-04-06 02:23:41 -04:00
|
|
|
reuse = False
|
2009-03-03 21:10:32 -05:00
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
bname = vargs['bname']
|
|
|
|
cmd = vargs['cmd']
|
|
|
|
a = w.application
|
2009-04-06 02:23:41 -04:00
|
|
|
if self.reuse and a.has_buffer_name(bname):
|
|
|
|
a.switch_buffer(a.get_buffer_by_name(bname))
|
|
|
|
return
|
|
|
|
|
2009-03-03 21:10:32 -05:00
|
|
|
a.close_buffer_by_name(bname)
|
2009-04-06 02:20:43 -04:00
|
|
|
b = XTermBuffer(a, 'bash', ['-c', cmd], name=bname,
|
|
|
|
modename=self.modename)
|
2009-03-03 21:10:32 -05:00
|
|
|
a.add_buffer(b)
|
2009-03-21 15:41:33 -04:00
|
|
|
Window(b, a)
|
2009-03-03 21:10:32 -05:00
|
|
|
if a.window().buffer is not b:
|
|
|
|
a.switch_buffer(b)
|