import tornado.httpclient
import urllib.parse
+from . import _pakfire
from .constants import *
+from .i18n import _
# Setup logging
log = logging.getLogger("pakfire.hub")
# File uploads
- async def upload(self, path, filename=None):
+ async def upload(self, path, filename=None, show_progress=True):
"""
Uploads the file to the hub returning the upload ID
"""
# Determine the filesize
size = os.path.getsize(path)
+ # Make progressbar
+ if show_progress:
+ p = _pakfire.Progressbar()
+ p.add_string(_("Uploading %s") % filename)
+ p.add_percentage()
+ p.add_bar()
+ p.add_transfer_speed()
+ p.add_string("|")
+ p.add_bytes_transferred()
+ p.add_eta()
+ else:
+ p = None
+
# Compute a digest
digest = self._compute_digest("blake2b", path)
# Prepare the file for streaming
- body_producer = functools.partial(self._stream_file, path)
+ body_producer = functools.partial(self._stream_file, path, size, p)
# Perform upload
response = await self._request("PUT", "/upload",
return response.get("id")
@staticmethod
- def _stream_file(path, write):
- with open(path, "rb") as f:
- while True:
- buf = f.read(64 * 1024)
- if not buf:
- break
-
- write(buf)
+ def _stream_file(path, size, p, write):
+ # Start the progressbar
+ if p:
+ p.start(size)
+
+ try:
+ with open(path, "rb") as f:
+ while True:
+ buf = f.read(64 * 1024)
+ if not buf:
+ break
+
+ # Update progressbar
+ if p:
+ l = len(buf)
+ p.increment(l)
+
+ write(buf)
+ finally:
+ # Finish the progressbar
+ if p:
+ p.finish()
@staticmethod
def _compute_digest(algo, path):