git clone 的 Python 进度条 [英] Python progress bar for git clone

查看:58
本文介绍了git clone 的 Python 进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 GitPython 在我的程序中克隆一个 repo.我想出了如何使用 clone_from 命令显示克隆的状态,但我希望状态看起来更像 tqdm 进度条.我尝试使用请求库来获取文件的大小,但我仍然不确定如何实现它.尝试在下面做这样的事情,但它不起作用.感谢任何帮助,谢谢.

Im using GitPython to clone a repo within my program. I figured how to display the the status of the clone with the clone_from command but I want the status to look more like a tqdm progress bar. I tried using the requests library to get the size of the file but I'm still unsure how to implement it. Tried doing something like this below but it's not working. Any help appreciated, thanks.

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)

推荐答案

这是另一个答案的改进版本.该栏仅在 CloneProgress 类初始化时创建一次.更新时,它会将条形设置为正确的数量.

This is an improved version of the other answer. The bar is created only once when the CloneProgress class is initialized. And when updating, it sets the bar at the correct amount.

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()

这篇关于git clone 的 Python 进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆