11 tuerSock = "/run/tuer.sock"
14 histfile = os.path.join(os.path.expanduser("~"), ".tyshellhist")
16 readline.read_history_file(histfile)
20 atexit.register(readline.write_history_file, histfile)
24 print("Available commands: %s" % ", ".join(sorted(commands.keys())))
28 ret = subprocess.call(cmd)
30 print("Command returned non-zero exit statis %d" % ret)
33 def sendcmd(addr, cmd):
35 print("206 Sending command %s..." % (cmd))
36 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
42 print(data.decode('utf-8'))
50 for n in grp.getgrnam("tuer").gr_mem:
52 print (p.pw_name, " - ", p.pw_gecos)
57 'open': sendcmd(tuerSock, 'unlock'),
58 'unlock': sendcmd(tuerSock, 'unlock'),
59 'buzz': sendcmd(tuerSock, 'buzz'),
63 def complete_command(cmd):
64 '''returns a list of commands (as strings) starting with cmd'''
65 return list(filter(lambda x: x.startswith(cmd), commands.keys()))
66 readline.set_completer(lambda cmd, num: (complete_command(cmd)+[None])[num]) # wrap complete_command for readline's weird completer API
67 readline.parse_and_bind("tab: complete") # run completion on tab
70 print("Welcome to tyshell. Use help to see what you can do.")
77 command = shlex.split(command)
78 if not len(command): continue
79 # find suiting commands
80 if command[0] in commands: # needed in case a complete command is a prefix of another one
81 cmdoptions = [command[0]]
83 cmdoptions = complete_command(command[0])
84 # check how many we found
85 if len(cmdoptions) == 0: # no commands fit prefix
86 print("Command %s not found. Use help." % command[0])
87 elif len(cmdoptions) == 1: # exactly one command fits (prefix)
89 res = commands[cmdoptions[0]](command)
91 except Exception as e:
92 print("Error while executing %s: %s" % (command[0], str(e)))
93 else: # multiple commands fit the prefix
94 print("Ambiguous command prefix, please choose one of the following:")
95 print("\t", " ".join(cmdoptions))
96 # TODO: put current "command[0]" into the shell for the next command, but such that it is deletable with backspace