summaryrefslogtreecommitdiff
path: root/morphlib/util.py
diff options
context:
space:
mode:
authorRichard Ipsum <richardipsum@fastmail.co.uk>2015-07-05 12:15:55 +0100
committerRichard Maw <richard.maw@codethink.co.uk>2015-09-23 13:43:18 +0000
commit46511c257999b1c5bdfa02a1ae7d04ee26ffee8f (patch)
tree51537c6e85887dee7a93128521efc2fa6f12bcb9 /morphlib/util.py
parent4549b28282bb4c2591ccd24db5a29970a3e907f2 (diff)
downloadmorph-46511c257999b1c5bdfa02a1ae7d04ee26ffee8f.tar.gz
Display progress bar when fetching to local cache
Looks like, 2015-07-05 16:08:10 [Build 1/304] [stage1-binutils] Fetching to local cache: artifact stage1-binutils-misc stage1-binutils-misc[##################### ] 51.9/73.0 MB Change-Id: Ib10f1cfaa0c1df80ae605ecfeb5b706c8d46c4a4
Diffstat (limited to 'morphlib/util.py')
-rw-r--r--morphlib/util.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/morphlib/util.py b/morphlib/util.py
index a92b7f37..284fe305 100644
--- a/morphlib/util.py
+++ b/morphlib/util.py
@@ -1,4 +1,6 @@
+# -*- coding: utf-8 -*-
# Copyright (C) 2011-2015 Codethink Limited
+# Copyright © 2015 Richard Ipsum
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -731,3 +733,45 @@ def temp_dir(*args, **kwargs): #pragma: no cover
else:
if cleanup_on_success:
shutil.rmtree(td, ignore_errors=True)
+
+def copyfileobj(fsrc, fdst, length=16*1024,
+ callback=lambda x: None): #pragma: no cover
+ ''' This is similar to shutil.copyfileobj
+ except this can be passed a callback to monitor copy progress
+ '''
+
+ count = 0
+
+ while True:
+ buf = fsrc.read(length)
+ if buf == '':
+ break
+
+ fdst.write(buf)
+ count += len(buf)
+ callback(count)
+
+
+class ProgressBar(object):
+ ''' A very simple progress bar '''
+
+ TEMPLATE = '%s[%s%s] %.1f/%.1f %s\r'
+
+ def __init__(self, label, expected_size, unit, width=30,
+ progress_char='#', empty_char=' '): #pragma: no cover
+ self._label = label
+ self._expected_size = expected_size
+ self._width = width
+ self._unit = unit
+ self._block_width = expected_size / float(width);
+ self._progress_char = progress_char
+ self._empty_char = empty_char
+
+ def show(self, progress): #pragma: no cover
+ blocks = int(progress / self._block_width);
+
+ s = self.TEMPLATE % (self._label, self._progress_char * blocks,
+ self._empty_char * (self._width - blocks),
+ progress, self._expected_size, self._unit)
+ sys.stderr.write(s)
+ sys.stderr.flush()