* ubuntutools.archive:

- Add quiet option to silence downloading.
  - Use wget-style progress bar (fixed width) (LP: #845787)
This commit is contained in:
Stefano Rivera 2011-09-09 22:44:16 +02:00
parent 29f1538d90
commit ca2abaa613
2 changed files with 31 additions and 24 deletions

3
debian/changelog vendored
View File

@ -13,6 +13,9 @@ ubuntu-dev-tools (0.131) UNRELEASED; urgency=low
past illegal version numbers (LP: #727314) past illegal version numbers (LP: #727314)
* ubuntutools.update_maintainer: Don't use strict changelog parsing * ubuntutools.update_maintainer: Don't use strict changelog parsing
(LP: #806633) (LP: #806633)
* ubuntutools.archive:
- Add quiet option to silence downloading.
- Use wget-style progress bar (fixed width) (LP: #845787)
[ Colin Watson ] [ Colin Watson ]
* syncpackage: Fix typo. * syncpackage: Fix typo.

View File

@ -128,7 +128,7 @@ class SourcePackage(object):
distribution = None distribution = None
def __init__(self, package=None, version=None, component=None, def __init__(self, package=None, version=None, component=None,
dscfile=None, lp=None, mirrors=(), workdir='.'): dscfile=None, lp=None, mirrors=(), workdir='.', quiet=False):
"Can be initialised either using package, version or dscfile" "Can be initialised either using package, version or dscfile"
assert ((package is not None and version is not None) assert ((package is not None and version is not None)
or dscfile is not None) or dscfile is not None)
@ -136,6 +136,7 @@ class SourcePackage(object):
self.source = package self.source = package
self._lp = lp self._lp = lp
self.workdir = workdir self.workdir = workdir
self.quiet = quiet
# Cached values: # Cached values:
self._component = component self._component = component
@ -305,11 +306,7 @@ class SourcePackage(object):
def _download_file(self, url, filename): def _download_file(self, url, filename):
"Download url to filename in workdir." "Download url to filename in workdir."
logurl = url
if os.path.basename(url) != filename:
logurl += ' -> ' + filename
pathname = os.path.join(self.workdir, filename) pathname = os.path.join(self.workdir, filename)
if self.dsc:
if self.dsc.verify_file(pathname): if self.dsc.verify_file(pathname):
Logger.debug('Using existing %s', filename) Logger.debug('Using existing %s', filename)
return True return True
@ -317,30 +314,37 @@ class SourcePackage(object):
if entry['name'] == filename] if entry['name'] == filename]
assert len(size) == 1 assert len(size) == 1
size = int(size[0]) size = int(size[0])
Logger.normal('Downloading %s (%0.3f MiB)', logurl, host = urlparse.urlparse(url).hostname
size / 1024.0 / 1024) if not self.quiet:
else: Logger.normal('Downloading %s from %s (%0.3f MiB)',
Logger.normal('Downloading %s', logurl) filename, host, size / 1024.0 / 1024)
try: try:
in_ = urllib2.build_opener().open(url) in_ = urllib2.build_opener().open(url)
except urllib2.URLError: except urllib2.URLError:
return False return False
downloaded = 0
bar_width = 60
with open(pathname, 'wb') as out: with open(pathname, 'wb') as out:
while True: while True:
block = in_.read(10240) block = in_.read(10240)
if block == '': if block == '':
break break
downloaded += len(block)
out.write(block) out.write(block)
Logger.stdout.write('.') if not self.quiet:
percent = downloaded * 100 // size
bar = '=' * int(round(downloaded * bar_width / size))
bar = (bar + '>' + ' ' * bar_width)[:bar_width]
Logger.stdout.write('[%s] %#3i%%\r' % (bar, percent))
Logger.stdout.flush() Logger.stdout.flush()
in_.close() in_.close()
Logger.stdout.write(' done\n') if not self.quiet:
Logger.stdout.write(' ' * (bar_width + 7) + '\r')
Logger.stdout.flush() Logger.stdout.flush()
if self.dsc:
if not self.dsc.verify_file(pathname): if not self.dsc.verify_file(pathname):
Logger.error('Checksum does not match.') Logger.error('Checksum for %s does not match.', filename)
return False return False
return True return True