From 6d14dacaa72ad4d12488fc83cbdba07c3ffe85d2 Mon Sep 17 00:00:00 2001 From: PhilipTrauner Date: Sun, 3 Apr 2016 22:36:48 +0200 Subject: [PATCH] Added program output streaming --- Wallaby/Wallaby.py | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/Wallaby/Wallaby.py b/Wallaby/Wallaby.py index eb8a0d1..5334e57 100644 --- a/Wallaby/Wallaby.py +++ b/Wallaby/Wallaby.py @@ -2,6 +2,7 @@ from ESock import ESock from Sync import SyncClient from Utils import is_socket_related_error from Utils import capture_trace +from Utils import is_wallaby import Routing import Logging @@ -10,9 +11,10 @@ import time import os import sys import platform +import subprocess CHANNEL = "w" -IS_WALLABY = True if "ARMv7" in platform.uname().version.lower() else False +IS_WALLABY = is_wallaby() PATH = "/home/root/Documents/KISS/" if IS_WALLABY else sys.argv[1] class WallabyControl(Routing.ClientRoute): @@ -25,9 +27,28 @@ class WallabyControl(Routing.ClientRoute): if type(data) is str: if data in self.actions_without_params.keys(): self.actions_without_params[data](handler) - elif type(data[address_pair]) is dict: - if action in self.actions_with_params.keys(): - self.actions_without_params[action](handler, data[action]) + elif type(data) is dict: + for action in data: + print(action) + if action in self.actions_with_params.keys(): + self.actions_with_params[action](handler, data[action]) + + + def run_program(self, handler, program): + if not IS_WALLABY: + command = ["gstdbuf", "-i0", "-o0", "-e0"] + else: + command = ["stdbuf", "-i0", "-o0", "-e0"] + command.append("./%s%s/botball_user_program" % (handler.sync.folder, program)) + process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + + # Poll process for new output until finished + for line in iter(process.stdout.readline, b""): + handler.sock.send(line.decode(), "std_stream") + print(line) + + process.wait() + handler.sock.send({"return_code" : process.returncode}, "std_stream") def stop(self, handler): @@ -35,13 +56,6 @@ class WallabyControl(Routing.ClientRoute): os.system("killall -s 2 botball_user_program") - def run_program(self, handler, program): - # WIP: Subprocess with unbuffered stdout required for output streaming - print(handler) - print(program) - os.system("./%s%s" % (handler.sync.folder, program)) - - def restart(self, handler): Logging.warning("Restart not implemented yet.") @@ -57,10 +71,14 @@ class WallabyControl(Routing.ClientRoute): handler.sock.close() +def get_wallaby_hostname(): + return open("/etc/hostname", "r").read() + class GetInfo(Routing.ClientRoute): def run(self, data, handler): if data == "": - handler.sock.send({"type" : CHANNEL, "name" : platform.node()}, "get_info") + handler.sock.send({"type" : CHANNEL, + "name" : platform.node() if not IS_WALLABY else get_wallaby_hostname()}, "get_info") elif "name" in data: if IS_WALLABY: open("/etc/hostname", "w").write(str(data["name"]))