Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3 fetcher: add progress handler for S3 cp command #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion lib/bb/fetch2/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,47 @@
import os
import bb
import urllib.request, urllib.parse, urllib.error
import re
from bb.fetch2 import FetchMethod
from bb.fetch2 import FetchError
from bb.fetch2 import runfetchcmd

def convertToBytes(value, unit):
value = float(value)
if (unit == "KiB"):
value = value*1024.0;
elif (unit == "MiB"):
value = value*1024.0*1024.0;
elif (unit == "GiB"):
value = value*1024.0*1024.0*1024.0;
return value

class S3ProgressHandler(bb.progress.LineFilterProgressHandler):
"""
Extract progress information from s3 cp output, e.g.:
Completed 5.1 KiB/8.8 GiB (12.0 MiB/s) with 1 file(s) remaining
"""
def __init__(self, d):
super(S3ProgressHandler, self).__init__(d)
# Send an initial progress event so the bar gets shown
self._fire_progress(0)

def writeline(self, line):
percs = re.findall(r'^Completed (\d+.{0,1}\d*) (\w+)\/(\d+.{0,1}\d*) (\w+) (\(.+\)) with\s+', line)
if percs:
completed = (percs[-1][0])
completedUnit = (percs[-1][1])
total = (percs[-1][2])
totalUnit = (percs[-1][3])
completed = convertToBytes(completed, completedUnit)
total = convertToBytes(total, totalUnit)
progress = (completed/total)*100.0
rate = percs[-1][4]
self.update(progress, rate)
return False
return True


class S3(FetchMethod):
"""Class to fetch urls via 'aws s3'"""

Expand Down Expand Up @@ -52,7 +89,9 @@ def download(self, ud, d):

cmd = '%s cp s3://%s%s %s' % (ud.basecmd, ud.host, ud.path, ud.localpath)
bb.fetch2.check_network_access(d, cmd, ud.url)
runfetchcmd(cmd, d)

progresshandler = S3ProgressHandler(d)
runfetchcmd(cmd, d, False, log=progresshandler)

# Additional sanity checks copied from the wget class (although there
# are no known issues which mean these are required, treat the aws cli
Expand Down