def run_as_command(editor, tool): editor.message('Running ' + tool._title, 'info') fd, filename = tempfile.mkstemp() os.write(fd, tool._script) os.close(fd) stdin = get_stdin(editor, tool._input) command_to_run = ['/usr/bin/env', 'sh', filename] env = {} env.update(os.environ) env['FILENAME'] = editor.uri env['OFFSET'] = str(editor.cursor.get_offset()) env['PYTHON'] = get_executable(editor.conf) def on_finish(): os.remove(filename) if tool._output == 'to-iconsole': return run_cmd_in_tty(command_to_run, editor, env, on_finish) if tool._output == 'to-background': proc = Popen(command_to_run, cwd=editor.project_root, env=env) else: proc = Popen(command_to_run, stdout=PIPE, stderr=PIPE, bufsize=1, stdin=PIPE if stdin else None, cwd=editor.project_root, env=env) if tool._output == 'to-console': from snaked.core.console import consume_output if stdin: proc.stdin.write(stdin) proc.stdin.close() consume_output(editor, proc, on_finish) elif tool._output == 'to-background': from threading import Thread def bg_run(): proc.wait() on_finish() t = Thread(target=bg_run) t.daemon = True t.start() else: stdout, stderr = proc.communicate(stdin) on_finish() process_stdout(editor, stdout, stderr, tool._output)
def run(editor, tool): import os.path from subprocess import Popen, PIPE import tempfile editor.message('Running ' + tool.title) fd, filename = tempfile.mkstemp() os.write(fd, tool.script) os.close(fd) stdin = get_stdin(editor, tool.input) command_to_run = ['/usr/bin/env', 'sh', filename] env = {} env.update(os.environ) env['FILENAME'] = editor.uri env['OFFSET'] = str(editor.cursor.get_offset()) def on_finish(): os.remove(filename) if tool.output == 'to-iconsole': return run_cmd_in_tty(command_to_run, editor, env, on_finish) proc = Popen(command_to_run, stdout=PIPE, stderr=PIPE, bufsize=1, stdin=PIPE if stdin else None, cwd=editor.project_root, env=env) if tool.output == 'to-console': from snaked.core.console import consume_output if stdin: proc.stdin.write(stdin) proc.stdin.close() consume_output(editor, proc, on_finish) else: stdout, stderr = proc.communicate(stdin) on_finish() process_stdout(editor, stdout, stderr, tool.output)