From 87c2f97197f93d4dc413afed0e0bd51d4f3b075b Mon Sep 17 00:00:00 2001 From: Jan Rydzewski Date: Mon, 19 Aug 2019 22:03:56 +0200 Subject: [PATCH] Use array instead of dicts i/o mapping --- examples/id_explicite.yml | 8 ++++---- examples/join_explicite.yml | 8 ++++---- examples/join_pipe.yml | 8 ++++---- examples/orphaned_read_end.yml | 2 +- examples/orphaned_write_end.yml | 2 +- examples/server.yml | 26 +++++++++++++------------- logfile.txt | 5 +++++ pgspawn.py | 25 ++++++++++++++++--------- test.yml | 15 +++++++++++++++ 9 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 logfile.txt create mode 100644 test.yml diff --git a/examples/id_explicite.yml b/examples/id_explicite.yml index 5a359ed..b797f84 100644 --- a/examples/id_explicite.yml +++ b/examples/id_explicite.yml @@ -1,10 +1,10 @@ inputs: - stdin: 0 + - {fd: 0, name: stdin} outputs: - stdout: 1 + - {fd: 1, name: stdout} nodes: - command: [cat] inputs: - 0: stdin + - {fd: 0, name: stdin} outputs: - 1: stdout + - {fd: 1, name: stdout} diff --git a/examples/join_explicite.yml b/examples/join_explicite.yml index 4af5742..ea7e67d 100644 --- a/examples/join_explicite.yml +++ b/examples/join_explicite.yml @@ -1,12 +1,12 @@ outputs: - stdout: 1 + - {fd: 1, name: stdout} nodes: - command: [echo, one] outputs: - 1: stdout + - {fd: 1, name: stdout} - command: [echo, two] outputs: - 1: stdout + - {fd: 1, name: stdout} - command: [echo, three] outputs: - 1: stdout + - {fd: 1, name: stdout} diff --git a/examples/join_pipe.yml b/examples/join_pipe.yml index f4b0242..f97f290 100644 --- a/examples/join_pipe.yml +++ b/examples/join_pipe.yml @@ -1,13 +1,13 @@ nodes: - command: [echo, one] outputs: - 1: into_cat + - {fd: 1, name: into_cat} - command: [echo, two] outputs: - 1: into_cat + - {fd: 1, name: into_cat} - command: [echo, three] outputs: - 1: into_cat + - {fd: 1, name: into_cat} - command: [cat] inputs: - 0: into_cat + - {fd: 0, name: into_cat} diff --git a/examples/orphaned_read_end.yml b/examples/orphaned_read_end.yml index 6d42da4..d38e50b 100644 --- a/examples/orphaned_read_end.yml +++ b/examples/orphaned_read_end.yml @@ -1,4 +1,4 @@ nodes: - command: [/bin/true] outputs: - 1: a_pipe + - {fd: 1, name: a_pipe} diff --git a/examples/orphaned_write_end.yml b/examples/orphaned_write_end.yml index 783c075..c4506bd 100644 --- a/examples/orphaned_write_end.yml +++ b/examples/orphaned_write_end.yml @@ -1,4 +1,4 @@ nodes: - command: [/bin/true] inputs: - 0: a_pipe + - {fd: 0, name: a_pipe} diff --git a/examples/server.yml b/examples/server.yml index 7ed1198..736b938 100644 --- a/examples/server.yml +++ b/examples/server.yml @@ -2,40 +2,40 @@ nodes: # ncat tcp server waiting for many connections - command: [ncat, -v, -l, -k, -p7000] inputs: - 0: send + - {fd: 0, name: send} outputs: - 2: log - 1: received + - {fd: 2, name: log} + - {fd: 1, name: received} # periodic echo of current date is sent to opened connections - command: [bash, -c, 'while true; do date; sleep 10; done'] outputs: - 1: send + - {fd: 1, name: send} # log being saved to a file - command: [bash, -c, 'cat > logfile.txt'] inputs: - 0: log + - {fd: 0, name: log} # incoming data is echoed back but also processed by our fancy machinery - command: [tee, /proc/self/fd/3] inputs: - 0: received + - {fd: 0, name: received} outputs: - 1: send - 3: unfiltered_exprs + - {fd: 1, name: send} + - {fd: 3, name: unfiltered_exprs} # pass only lines beginning with # - command: [sed, -rn, -u, 's/^#(.*)/\1/p'] inputs: - 0: unfiltered_exprs + - {fd: 0, name: unfiltered_exprs} outputs: - 1: exprs + - {fd: 1, name: exprs} # evaluate expressions and send results - command: [xargs, -L1, expr] inputs: - 0: exprs + - {fd: 0, name: exprs} outputs: - 1: send - 2: send + - {fd: 1, name: send} + - {fd: 2, name: send} diff --git a/logfile.txt b/logfile.txt new file mode 100644 index 0000000..a548490 --- /dev/null +++ b/logfile.txt @@ -0,0 +1,5 @@ +Ncat: Version 7.70 ( https://nmap.org/ncat ) +Ncat: Listening on :::7000 +Ncat: Listening on 0.0.0.0:7000 +Ncat: Connection from 127.0.0.1. +Ncat: Connection from 127.0.0.1:56596. diff --git a/pgspawn.py b/pgspawn.py index a1a8ab9..4129046 100644 --- a/pgspawn.py +++ b/pgspawn.py @@ -8,10 +8,17 @@ logger = logging.getLogger(__name__) -def bimap_dict(key_f, val_f, d): +def connection_arr2dict(arr): return { - key_f(k): val_f(v) - for k, v in d.items() + int(c['fd']): str(c['name']) + for c in arr + } + + +def connection_arr2dict_inv(arr): + return { + str(c['name']): int(c['fd']) + for c in arr } @@ -39,9 +46,9 @@ def from_dict(cls, description): return cls( command=[str(p) for p in description['command']], - inputs=bimap_dict(int, str, description.get('inputs', {})), - outputs=bimap_dict(int, str, description.get('outputs', {})), - sockets=bimap_dict(int, str, description.get('sockets', {})), + inputs=connection_arr2dict(description.get('inputs', [])), + outputs=connection_arr2dict(description.get('outputs', [])), + sockets=connection_arr2dict(description.get('sockets', [])), separate_group=bool(description.get('separate_group', False)), signals=[str2sig(str(s)) for s in description.get('signals', [])], ) @@ -55,9 +62,9 @@ def from_dict(cls, description): logger.warning("Unknown keys in graph description dict: {}".format(unknown_keys)) g = cls( - inputs=bimap_dict(str, int, description.get('inputs', {})), - outputs=bimap_dict(str, int, description.get('outputs', {})), - sockets=bimap_dict(str, int, description.get('sockets', {})), + inputs=connection_arr2dict_inv(description.get('inputs', [])), + outputs=connection_arr2dict_inv(description.get('outputs', [])), + sockets=connection_arr2dict_inv(description.get('sockets', [])), nodes=list(map(Node.from_dict, description.get('nodes', []))), ) diff --git a/test.yml b/test.yml new file mode 100644 index 0000000..4128d57 --- /dev/null +++ b/test.yml @@ -0,0 +1,15 @@ +#!/usr/bin/env pgspawn +nodes: + - command: [minos/minos, 0, 3, "-,1"] + inputs: + 0: program + 3: program_inner + outputs: + #1: test + - command: [cat, programs/cnew.c.bin] + outputs: {1: program} + - command: [cat, programs/hello.asm.bin] + outputs: {1: program_inner} + - command: [./stdin-eq, "parent\nhello world\n"] + inputs: + 0: test