From 2d58f095bdc2f5e33ba17424e2befd94d5913a47 Mon Sep 17 00:00:00 2001 From: Philip Trauner Date: Thu, 23 Feb 2017 12:04:05 +0100 Subject: [PATCH] Create fl0w user by default --- Wallaby/Wallaby.py | 69 +++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/Wallaby/Wallaby.py b/Wallaby/Wallaby.py index 24b5e99..0b5e85a 100644 --- a/Wallaby/Wallaby.py +++ b/Wallaby/Wallaby.py @@ -1,6 +1,6 @@ from Highway import Route, Pipe, Client +from Meh import Config, Option, ExceptionInConfigError import Logging -import Config import Utils import socket @@ -10,29 +10,28 @@ import sys import subprocess from random import randint from _thread import start_new_thread +from ctypes import cdll import threading +import json CHANNEL = 2 IS_WALLABY = Utils.is_wallaby() -PATH = "/home/root/Documents/KISS/bin/" if IS_WALLABY else (sys.argv[1] if len(sys.argv) > 1 else None) - -PATH = os.path.abspath(PATH) - -if PATH[-1] != "/": - PATH = PATH + "/" +USERS_LOCATION = "/home/root/Documents/KISS/users.json" LIB_WALLABY = "/usr/lib/libwallaby.so" -WALLABY_PROGRAMS = "/root/Documents/KISS/bin/" -if not PATH: - Logging.error("No path specified. (Necessary on simulated Wallaby controllers.)") - exit(1) if not IS_WALLABY: Logging.warning("Binaries that were created for Wallaby Controllers will not run on a simulated Wallaby.") +def get_users(): + if IS_WALLABY: + return list(json.loads(open(USERS_LOCATION, "r").read()).keys()) + return ["Default User"] + + class SensorReadout: ANALOG = 1 DIGITAL = 2 @@ -40,7 +39,7 @@ class SensorReadout: MODES = tuple(NAMED_MODES.keys()) - def __init__(self, handler, poll_rate=0.5): + def __init__(self, handler, poll_rate=0.2): self.poll_rate = poll_rate self.handler = handler self.peer_lock = threading.Lock() @@ -159,6 +158,7 @@ class Identify(Pipe): Logging.success("I was identified!") + class ListPrograms(Pipe): def run(self, data, peer, handler): programs = [] @@ -178,7 +178,7 @@ class StopPrograms(Pipe): def run(self, data, peer, handler): if handler.debug: Logging.info("Stopping all botball programs.") - if subprocess.call(["killall", "botball_user_program"], + if subprocess.call(["killall", "botball_user_program"], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT): handler.pipe(self.__class__.NO_PROGRAMS_RUNNING, "stop_programs", peer) @@ -196,7 +196,7 @@ class RunProgram(Pipe): data = data + "/" path = "%s%s/botball_user_program" % (PATH, data) if os.path.isfile(path): - program = subprocess.Popen(self.command + [path], + program = subprocess.Popen(self.command + [path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) start_new_thread(self.stream_stdout, (program, peer, handler)) else: @@ -313,21 +313,40 @@ class Handler(Client): Logging.info("Unsubscribing '%s' from all sensor updates." % peer) self.routes["sensor"].sensor_readout.unsubscribe_all(peer) +if IS_WALLABY: + if not "fl0w" in get_users(): + json.loads(open(USERS_LOCATION, "r").read())["fl0w"] = {"mode" : "Advanced"} + try: + os.mkdir(FL0W_USER_PATH) + except FileExistsError: + pass + PATH = FL0W_USER_PATH + "bin/" +else: + if len(sys.argv) == 2: + if os.path.exists(sys.argv[1]): + PATH = os.path.abspath(sys.argv[1]) + else: + Logging.error("Location has to be provided in dev-env.") + exit(1) + +if PATH[-1] != "/": + PATH = PATH + "/" CONFIG_PATH = "wallaby.cfg" -config = Config.Config() -config.add(Config.Option("server_address", "ws://127.0.0.1:3077")) -config.add(Config.Option("debug", False, validator=lambda x: True if True or False else False)) -config.add(Config.Option("output_unbuffer", "stdbuf")) -config.add(Config.Option("identify_sound", "Wallaby/identify.wav", - validator=lambda x: os.path.isfile(x))) +config = Config() +config.add(Option("server_address", "ws://127.0.0.1:3077")) +config.add(Option("debug", True, validator=lambda x: True if True or False else False)) +config.add(Option("output_unbuffer", "stdbuf")) +config.add(Option("identify_sound", "Wallaby/identify.wav", + validator=lambda sound: os.path.isfile(sound))) + try: - config = config.read_from_file(CONFIG_PATH) -except FileNotFoundError: - config.write_to_file(CONFIG_PATH) - config = config.read_from_file(CONFIG_PATH) + config = config.load(CONFIG_PATH) +except (IOError, ExceptionInConfigError): + config.dump(CONFIG_PATH) + config = config.load(CONFIG_PATH) try: @@ -335,7 +354,7 @@ try: # setup has to be called before the connection is established ws.setup({"subscribe" : Subscribe(), "hostname" : Hostname(), "processes" : Processes(), "sensor" : Sensor(), - "identify" : Identify(), "list_programs" : ListPrograms(), + "identify" : Identify(), "list_programs" : ListPrograms(), "whoami" : WhoAmI(), "run_program" : RunProgram(config.output_unbuffer), "stop_programs" : StopPrograms(), "shutdown" : Shutdown(), "reboot" : Reboot()},