summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-02-03 13:41:40 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2017-02-03 13:41:40 +0100
commit7200a7c06af21aeb24adf28f4186f467ceb7314c (patch)
tree8b0d689764c4decc2b218de7a0c122ec98871da4
parent0efe637c5e6370f142da2f6ba54122015ca7dcc3 (diff)
downloadpsutil-7200a7c06af21aeb24adf28f4186f467ceb7314c.tar.gz
download_exes.py; show file size
-rwxr-xr-xscripts/internal/download_exes.py24
1 files changed, 22 insertions, 2 deletions
diff --git a/scripts/internal/download_exes.py b/scripts/internal/download_exes.py
index d8d2768b..e607b87d 100755
--- a/scripts/internal/download_exes.py
+++ b/scripts/internal/download_exes.py
@@ -85,16 +85,36 @@ def safe_rmtree(path):
shutil.rmtree(path, onerror=onerror)
+def bytes2human(n):
+ """
+ >>> bytes2human(10000)
+ '9.8 K'
+ >>> bytes2human(100001221)
+ '95.4 M'
+ """
+ symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
+ prefix = {}
+ for i, s in enumerate(symbols):
+ prefix[s] = 1 << (i + 1) * 10
+ for s in reversed(symbols):
+ if n >= prefix[s]:
+ value = float(n) / prefix[s]
+ return '%.2f %s' % (value, s)
+ return '%.2f B' % (n)
+
+
def download_file(url):
local_fname = url.split('/')[-1]
local_fname = os.path.join('dist', local_fname)
- print(local_fname)
safe_makedirs('dist')
r = requests.get(url, stream=True)
+ tot_bytes = 0
with open(local_fname, 'wb') as f:
- for chunk in r.iter_content(chunk_size=1024):
+ for chunk in r.iter_content(chunk_size=16384):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
+ tot_bytes += len(chunk)
+ print("downloaded %-45s %s" % (local_fname, bytes2human(tot_bytes)))
return local_fname