Python中的Shell脚本:调用使用curses等的程序
这是一个非常具体的问题,但我正在寻找一个稍微更通用的解决方案:
我正在用 Python 编写一个 shell 脚本来协助各种配置任务,包括执行 git clone<各种存储库的 /code>。当我调用 git clone 时,有没有一种好方法可以将 git 显示输出直接提供给终端(进度条等)?
仅将子进程的 stdout 通过管道传输到 sys.stdout 并不能解决问题,因为 git 的行为涉及重写终端的同一部分以指示进度。所以这还不够好:
import sys, subprocess
process = subprocess.Popen("git clone --recursive https://github.com/my/repo.git",
shell=True,
stdout=sys.stdout,
stderr=subprocess.PIPE)
我不是在寻找答案“use git-python”——相反,我正在寻找一种可以应用于此的更通用的技术,并且其他配置任务。
谢谢!
Here's a very specific question, but I'm looking for a slightly more general solution:
I'm writing a shell script in Python to assist in various config tasks, including performing a git clone
of various repositories. When I call git clone
, is there a good way for me to supply git display output directly to the terminal (progress bars, etc)?
Just piping the subprocess's stdout to sys.stdout doesn't cut it, because git
's behavior involves re-writing over the same portion of the terminal to indicate progress. So this isn't really good enough:
import sys, subprocess
process = subprocess.Popen("git clone --recursive https://github.com/my/repo.git",
shell=True,
stdout=sys.stdout,
stderr=subprocess.PIPE)
I'm not looking for the answer "use git-python" -- rather, I'm looking for a more general technique that I can apply to this and other config tasks.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
这适用于
wget
,在屏幕上的一行上重写自身的进度条以这种方式正常工作。我想它也适用于 git 和其他人。Try:
This works for
wget
, the progress-bar that rewrites itself on one line on the screen works correctly this way. I suppose it would work forgit
and others as well.