How to do process substitution, e.g. "echo hi | tee >( gzip > /tmp/1 ) > /tmp/2" in plumbum?

274 Views Asked by At

I need to figure out how to call pipe substitution in plumbum. Specifically, how to construct chains such as echo hi | tee >( gzip > /tmp/1 ) > /tmp/2? Or, to illustrate the illustrate the idea better, find / | tee >( grep hi > /tmp/grepped ) > /tmp/nongrepped? I need this kind of approach because the equivalent of find / that I'm going to use is very expensive to run and I can't save it on disk, so I need to apply two different filters in parallel. Is there a way to avoid mkfifo?

Note: I'm aware that the question is similar to "How to pipe many bash commands from python?". The difference, though, is that I'm asking specifically about plumbum and an attempt to write a plumbum-related answer there is flawed, as described in its comments.

1

There are 1 best solutions below

2
On

To replicate the command in the title of the question (echo hi | tee >( gzip > /tmp/1 ) > /tmp/2), you can do the following with plumbum

from plumbum.cmd import echo, tee, gzip

(echo["hi"] | tee["/tmp/2"] | gzip > "/tmp/1")()

This pipes the string "hi" to the tee function which writes it to "/tmp/2" and also copies it to stdout. Then, stdout is piped to gzip whose output is redirected to "/tmp/1".

To achieve something similar with your other command (find / | tee >( grep hi > /tmp/grepped ) > /tmp/nongrepped)

you can do:

from plumbum.cmd import find, grep, tee

(find["/"] | tee["/tmp/nongrepped"] | grep["hi"] > "/tmp/grepped")()