Pipe STDIN to a script that is itself being piped to the Python interpreter?

坚强是说给别人听的谎言 提交于 2019-12-08 18:38:02

问题


I need to implement an SVN pre-commit hook which executes a script that itself is stored in SVN.

I can use the svn cat command to pipe that script to the Python interpreter, as follows:

svn cat file://$REPO/trunk/my_script.py | python - --argument1 --argument2

However, my_script.py itself requires data to be piped on STDIN.

That data is not stored in a file; it is stored on the network. I would prefer not to have to download the data to a temporary file, as normally I could pipe it to a Python program:

curl http://example.com/huge_file.txt | python my_script.py

I'm not sure how to combine both of these pipes.


回答1:


I figured out how to do this without creating any temporary files, but not strictly with "pipes".

curl http://example.com/huge_file.txt | python <(svn cat file://$REPO/trunk/my_script.py) --argument1 --argument2

I used the "anonymous file descriptor" construct in Bash, which can be used in place of any file path.

E.g. python my_script.py would be equivalent to python <(cat my_script.py)




回答2:


I don't think this is possible. You feed the script as standard input to the python interpreter. This means that the python interpreter is already attached to an input stream. I don't think the script contents can create a second standard input to read from.



来源:https://stackoverflow.com/questions/12397469/pipe-stdin-to-a-script-that-is-itself-being-piped-to-the-python-interpreter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!