From 30172e7e9bdc6cb0404a6b611fe9bbab5101e44a Mon Sep 17 00:00:00 2001 From: Papoteur Date: Sat, 27 Jan 2024 11:15:56 +0100 Subject: Add a progress bar when uploading a binary file --- MgaRepo/binrepo.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/MgaRepo/binrepo.py b/MgaRepo/binrepo.py index 874a091..d96507c 100644 --- a/MgaRepo/binrepo.py +++ b/MgaRepo/binrepo.py @@ -2,6 +2,7 @@ from MgaRepo import Error, config, mirror, layout from MgaRepo.util import execcmd, rellink, get_helper from MgaRepo.svn import SVN +from tqdm import tqdm import sys import os import string @@ -11,8 +12,8 @@ import re import tempfile import hashlib import urllib.parse -import threading import httplib2 +import subprocess from io import StringIO @@ -101,13 +102,31 @@ def upload_binary(topdir, filename): if binary_exists(sha1sum): return host = config.get("binrepo", "upload_host") + # upload_bin_helper by default: /usr/local/bin/wrapper.upload-bin upload_bin_helper = get_helper("upload-bin") - command = "ssh %s %s %s" % (host, upload_bin_helper, filename) + command = ["ssh", host, upload_bin_helper, filename] try: - filein = open(filepath, 'r') + b = os.path.getsize(filepath) except Error as e: raise Error("Could not open file %s\n" % filepath) - status, output = execcmd(command, show=True, geterr=True, stdin=filein) + with open(filepath, "rb") as f: + # file is sent chunk by chunk to allow displaying of a progress bar + bs = 4096 * 128 + written = 0 + ncuts = int(b / bs) + pbar = tqdm(total=b, unit='B', unit_scale=True, unit_divisor=1024, desc=filename) + while ncuts <= 100: + bs = int(bs / 2) + ncuts = int(b / bs) + p = subprocess.Popen(command, stdin=subprocess.PIPE) + for i in range(0, int(ncuts) + 1): + buf = f.read(bs) + p.stdin.write(buf) + written += len(buf) + pbar.update(len(buf)) + p.stdin.flush() + pbar.close() + p.communicate() def import_binaries(topdir, pkgname): """Import all binaries from a given package checkout -- cgit v1.2.1