【问题标题】:Python progress bar for git clonegit clone 的 Python 进度条
【发布时间】:2018-12-05 07:57:42
【问题描述】:
我正在使用 GitPython 在我的程序中克隆一个 repo。我想出了如何使用 clone_from 命令显示克隆的状态,但我希望状态看起来更像一个 tqdm 进度条。我尝试使用请求库来获取文件的大小,但我仍然不确定如何实现它。尝试在下面做这样的事情,但它不起作用。任何帮助表示赞赏,谢谢。
url = 'git@github.com:somegithubrepo/repo.git'
r = requests.get(url, stream=True)
total_length = r.headers.get('content-length')
for i in tqdm(range(len(total_length??))):
git.Git(pathName).clone(url)
【问题讨论】:
标签:
python
python-3.x
progress-bar
tqdm
【解决方案1】:
你可以试试这样的:
import git
from git import RemoteProgress
from tqdm import tqdm
class CloneProgress(RemoteProgress):
def update(self, op_code, cur_count, max_count=None, message=''):
pbar = tqdm(total=max_count)
pbar.update(cur_count)
git.Repo.clone_from(project_url, repo_dir, branch='master', progress=CloneProgress()
【解决方案2】:
这是另一个答案的改进版本。当CloneProgress 类初始化时,该栏只创建一次。并且在更新时,它会将栏设置为正确的数量。
import git
from git import RemoteProgress
from tqdm import tqdm
class CloneProgress(RemoteProgress):
def __init__(self):
super().__init__()
self.pbar = tqdm()
def update(self, op_code, cur_count, max_count=None, message=''):
self.pbar.total = max_count
self.pbar.n = cur_count
self.pbar.refresh()
git.Repo.clone_from(project_url, repo_dir, branch='master', progress=CloneProgress()