* 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-10 01:26:19 +02:00
commit 98f0ba6bb1
2 changed files with 39 additions and 30 deletions

3
debian/changelog vendored
View File

@ -22,6 +22,9 @@ ubuntu-dev-tools (0.131) UNRELEASED; urgency=low
(LP: #841975) (LP: #841975)
- Use apt-get rather than aptitude. - Use apt-get rather than aptitude.
* Removed get-build-deps, mk-build-deps -ir is equivalent (LP: #158108) * Removed get-build-deps, mk-build-deps -ir is equivalent (LP: #158108)
* 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,43 +306,48 @@ 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 size = [entry['size'] for entry in self.dsc['Files']
size = [entry['size'] for entry in self.dsc['Files'] if entry['name'] == filename]
if entry['name'] == filename] assert len(size) == 1
assert len(size) == 1 size = int(size[0])
size = int(size[0]) host = urlparse.urlparse(url).hostname
Logger.normal('Downloading %s (%0.3f MiB)', logurl, if not self.quiet:
size / 1024.0 / 1024) Logger.normal('Downloading %s from %s (%0.3f MiB)',
else: filename, host, size / 1024.0 / 1024)
Logger.normal('Downloading %s', logurl)
try: try:
in_ = urllib2.build_opener().open(url) in_ = urllib2.build_opener().open(url)
except urllib2.URLError: except urllib2.URLError:
return False return False
with open(pathname, 'wb') as out: downloaded = 0
while True: bar_width = 60
block = in_.read(10240) try:
if block == '': with open(pathname, 'wb') as out:
break while True:
out.write(block) block = in_.read(10240)
Logger.stdout.write('.') if block == '':
break
downloaded += len(block)
out.write(block)
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()
in_.close()
finally:
if not self.quiet:
Logger.stdout.write(' ' * (bar_width + 7) + '\r')
Logger.stdout.flush() Logger.stdout.flush()
in_.close() if not self.dsc.verify_file(pathname):
Logger.stdout.write(' done\n') Logger.error('Checksum for %s does not match.', filename)
Logger.stdout.flush() return False
if self.dsc:
if not self.dsc.verify_file(pathname):
Logger.error('Checksum does not match.')
return False
return True return True
def pull(self): def pull(self):