Python中的Shell脚本:调用使用curses等的程序

发布于 2024-12-10 14:03:25 字数 610 浏览 0 评论 0原文

这是一个非常具体的问题,但我正在寻找一个稍微更通用的解决方案:

我正在用 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少钕鈤記 2024-12-17 14:03:25

尝试:

import os
os.system(mycommand)

这适用于 wget,在屏幕上的一行上重写自身的进度条以这种方式正常工作。我想它也适用于 git 和其他人。

Try:

import os
os.system(mycommand)

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 for git and others as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文