2011-10-29 14:58:36 +00:00
|
|
|
|
#/usr/bin/env python3
|
2011-10-29 03:13:12 +00:00
|
|
|
|
# Copyright 2011 Florent Le Coz <louiz@louiz.org>
|
|
|
|
|
#
|
|
|
|
|
# This file is part of Poezio.
|
|
|
|
|
#
|
|
|
|
|
# Poezio is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the zlib license. See the COPYING file.
|
|
|
|
|
|
|
|
|
|
"""
|
2011-10-29 14:58:36 +00:00
|
|
|
|
This file is a standalone program that reads commands on
|
|
|
|
|
stdin and executes them (each line should be a command).
|
2011-10-29 03:13:12 +00:00
|
|
|
|
|
2011-10-29 14:58:36 +00:00
|
|
|
|
Usage: cat some_fifo | ./daemon.py
|
2011-10-29 03:13:12 +00:00
|
|
|
|
|
2011-10-29 14:58:36 +00:00
|
|
|
|
Poezio writes commands in the fifo, and this daemon executes them on the
|
|
|
|
|
local machine.
|
2011-10-29 03:13:12 +00:00
|
|
|
|
Note that you should not start this daemon if you do not trust the remote
|
|
|
|
|
machine that is running poezio, since this could make it run any (dangerous)
|
|
|
|
|
command on your local machine.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
import threading
|
|
|
|
|
import subprocess
|
2012-01-10 15:30:59 +00:00
|
|
|
|
import shlex
|
2011-12-15 19:29:20 +00:00
|
|
|
|
import logging
|
|
|
|
|
|
2014-06-26 15:45:05 +00:00
|
|
|
|
try:
|
|
|
|
|
from subprocess import DEVNULL # Only in python >= 3.3
|
|
|
|
|
except ImportError:
|
|
|
|
|
import os
|
|
|
|
|
DEVNULL = open(os.devnull, 'wb')
|
|
|
|
|
|
2011-12-15 19:29:20 +00:00
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
2011-10-29 03:13:12 +00:00
|
|
|
|
class Executor(threading.Thread):
|
|
|
|
|
"""
|
2012-07-04 22:49:00 +00:00
|
|
|
|
Just a class to execute commands in a thread. This way, the execution
|
|
|
|
|
can totally fail, we don’t care, and we can start commands without
|
|
|
|
|
having to wait for them to return.
|
|
|
|
|
WARNING: Be careful to properly escape what is untrusted by using
|
|
|
|
|
pipes.quote (or shlex.quote with python 3.3) for example.
|
2011-10-29 03:13:12 +00:00
|
|
|
|
"""
|
2012-12-27 17:18:20 +00:00
|
|
|
|
def __init__(self, command, remote=False):
|
2011-10-29 03:13:12 +00:00
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
self.command = command
|
2012-12-27 17:18:20 +00:00
|
|
|
|
self.remote = remote
|
2012-10-22 15:14:21 +00:00
|
|
|
|
# check for > or >> special case
|
|
|
|
|
self.filename = None
|
|
|
|
|
self.redirection_mode = 'w'
|
|
|
|
|
if len(command) >= 3:
|
|
|
|
|
if command[-2] in ('>', '>>'):
|
|
|
|
|
self.filename = command.pop(-1)
|
|
|
|
|
if command[-1] == '>>':
|
|
|
|
|
self.redirection_mode = 'a'
|
|
|
|
|
command.pop(-1)
|
2011-10-29 03:13:12 +00:00
|
|
|
|
|
|
|
|
|
def run(self):
|
2014-04-06 17:52:59 +00:00
|
|
|
|
log.debug('executing %s', self.command)
|
2014-06-26 15:45:05 +00:00
|
|
|
|
stdout = DEVNULL
|
2012-10-22 15:14:21 +00:00
|
|
|
|
if self.filename:
|
|
|
|
|
try:
|
|
|
|
|
stdout = open(self.filename, self.redirection_mode)
|
2014-04-06 17:52:59 +00:00
|
|
|
|
except (OSError, IOError):
|
|
|
|
|
log.error('Could not open redirection file: %s', self.filename, exc_info=True)
|
2012-10-22 15:14:21 +00:00
|
|
|
|
return
|
2012-12-27 17:18:20 +00:00
|
|
|
|
try:
|
2014-06-26 15:45:05 +00:00
|
|
|
|
subprocess.call(self.command, stdout=stdout, stderr=DEVNULL)
|
2012-12-27 17:18:20 +00:00
|
|
|
|
except:
|
|
|
|
|
if self.remote:
|
2014-04-06 17:52:59 +00:00
|
|
|
|
import traceback
|
2012-12-27 17:18:20 +00:00
|
|
|
|
print(traceback.format_exc())
|
|
|
|
|
else:
|
2014-04-06 17:52:59 +00:00
|
|
|
|
log.error('Could not execute %s:', self.command, exc_info=True)
|
2011-10-29 03:13:12 +00:00
|
|
|
|
|
2011-10-29 14:58:36 +00:00
|
|
|
|
def main():
|
2011-10-29 03:13:12 +00:00
|
|
|
|
while True:
|
2011-10-29 14:58:36 +00:00
|
|
|
|
line = sys.stdin.readline()
|
|
|
|
|
if line == '':
|
|
|
|
|
break
|
2012-10-22 15:14:21 +00:00
|
|
|
|
command = shlex.split(line)
|
2012-12-27 17:18:20 +00:00
|
|
|
|
e = Executor(command, remote=True)
|
2011-10-29 14:58:36 +00:00
|
|
|
|
e.start()
|
2011-10-29 03:13:12 +00:00
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2011-10-29 14:58:36 +00:00
|
|
|
|
main()
|