-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add progressbar support to most download actions (#615)
LP: #1597387 Signed-off-by: Sergio Schvezov <sergio.schvezov@ubuntu.com>
- Loading branch information
1 parent
f20b33a
commit c256727
Showing
9 changed files
with
78 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
# | ||
# Copyright (C) 2016 Canonical Ltd | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3 as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import os | ||
|
||
from progressbar import ( | ||
AnimatedMarker, | ||
Bar, | ||
Percentage, | ||
ProgressBar, | ||
UnknownLength, | ||
) | ||
|
||
|
||
def download_requests_stream(request_stream, destination, message=None): | ||
"""This is a facility to download a request with nice progress bars.""" | ||
if not message: | ||
message = 'Downloading {!r}'.format(os.path.basename(destination)) | ||
|
||
total_length = int(request_stream.headers.get('Content-Length', '0')) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
if total_length: | ||
progress_bar = ProgressBar( | ||
widgets=[message, | ||
Bar(marker='=', left='[', right=']'), | ||
' ', Percentage()], | ||
maxval=total_length) | ||
else: | ||
progress_bar = ProgressBar( | ||
widgets=[message, AnimatedMarker()], | ||
maxval=UnknownLength) | ||
|
||
total_read = 0 | ||
progress_bar.start() | ||
with open(destination, 'wb') as destination_file: | ||
for buf in request_stream.iter_content(1024): | ||
destination_file.write(buf) | ||
total_read += len(buf) | ||
progress_bar.update(total_read) | ||
progress_bar.finish() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This approach of finding content length using content length header is making probelm when using http compression
Eg github gist