【问题标题】:Pipe STDIN to a script that is itself being piped to the Python interpreter?将 STDIN 传输到本身通过管道传输到 Python 解释器的脚本?
【发布时间】:2012-09-06 00:31:40
【问题描述】:

我需要实现一个 SVN 预提交挂钩,它执行本身存储在 SVN 中的脚本。

我可以使用svn cat 命令将该脚本通过管道传送到 Python 解释器,如下所示:

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

但是,my_script.py 本身需要在 STDIN 上传输数据。

该数据未存储在文件中;它存储在网络上。我宁愿不必将数据下载到临时文件,因为通常我可以将其通过管道传输到 Python 程序:

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

我不确定如何组合这两个管道。

【问题讨论】:

  • 写一个使用pysvn读入文件的包装脚本,然后编译执行,这样你的stdin就免费了吗?

标签: python bash svn pipe pre-commit-hook


【解决方案1】:

我想出了如何在不创建任何临时文件的情况下做到这一点,但不是严格使用“管道”。

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

我在 Bash 中使用了“匿名文件描述符”结构,它可以用来代替任何文件路径。

例如 python my_script.py 相当于 python <(cat my_script.py)

【讨论】:

    【解决方案2】:

    我认为这是不可能的。您将脚本作为标准输入提供给 python 解释器。这意味着 python 解释器已经附加到输入流。我不认为脚本内容可以创建第二个标准输入来读取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多