summaryrefslogtreecommitdiff
path: root/poezio/daemon.py
diff options
context:
space:
mode:
authorEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2016-03-31 18:54:41 +0100
committerEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2016-06-11 20:49:43 +0100
commit332a5c2553db41de777473a1e1be9cd1522c9496 (patch)
tree3ee06a59f147ccc4009b35cccfbe2461bcd18310 /poezio/daemon.py
parentcf44cf7cdec9fdb35caa372563d57e7045dc29dd (diff)
downloadpoezio-332a5c2553db41de777473a1e1be9cd1522c9496.tar.gz
poezio-332a5c2553db41de777473a1e1be9cd1522c9496.tar.bz2
poezio-332a5c2553db41de777473a1e1be9cd1522c9496.tar.xz
poezio-332a5c2553db41de777473a1e1be9cd1522c9496.zip
Move the src directory to poezio, for better cython compatibility.
Diffstat (limited to 'poezio/daemon.py')
-rwxr-xr-xpoezio/daemon.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/poezio/daemon.py b/poezio/daemon.py
new file mode 100755
index 00000000..6325d8df
--- /dev/null
+++ b/poezio/daemon.py
@@ -0,0 +1,82 @@
+#/usr/bin/env python3
+# 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.
+
+"""
+This file is a standalone program that reads commands on
+stdin and executes them (each line should be a command).
+
+Usage: cat some_fifo | ./daemon.py
+
+Poezio writes commands in the fifo, and this daemon executes them on the
+local machine.
+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
+import shlex
+import logging
+
+from subprocess import DEVNULL
+
+log = logging.getLogger(__name__)
+
+class Executor(threading.Thread):
+ """
+ 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.
+ """
+ def __init__(self, command, remote=False):
+ threading.Thread.__init__(self)
+ self.command = command
+ self.remote = remote
+ # 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)
+
+ def run(self):
+ log.debug('executing %s', self.command)
+ stdout = DEVNULL
+ if self.filename:
+ try:
+ stdout = open(self.filename, self.redirection_mode)
+ except (OSError, IOError):
+ log.error('Could not open redirection file: %s', self.filename, exc_info=True)
+ return
+ try:
+ subprocess.call(self.command, stdout=stdout, stderr=DEVNULL)
+ except:
+ if self.remote:
+ import traceback
+ print(traceback.format_exc())
+ else:
+ log.error('Could not execute %s:', self.command, exc_info=True)
+
+def main():
+ while True:
+ line = sys.stdin.readline()
+ if line == '':
+ break
+ command = shlex.split(line)
+ e = Executor(command, remote=True)
+ e.start()
+
+if __name__ == '__main__':
+ main()