Skip to content
Snippets Groups Projects
Commit 25e98b57 authored by Sebastien Luttringer's avatar Sebastien Luttringer
Browse files

improve human_size computation

parent a05e7d11
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,7 @@ import hashlib
import shutil
import urllib2
import time
import math
from subprocess import call, check_call, CalledProcessError
......@@ -324,15 +325,17 @@ def getsize(path):
total_sz += filestat.st_size
return total_sz
def human_size(num):
def human_size(num, unit='B'):
'''
Return human readable size
'''
for x in ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']:
if num < 1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0
return "%3.1f%s" % (num, x)
prefixes = ('','Ki', 'Mi', 'Gi', 'Ti','Pi', 'Ei', 'Zi', 'Yi')
power = int(math.log(num, 1024))
# max is YiB
if power >= len(prefixes):
power = len(prefixes) - 1
scaled = num / float(1024 ** power)
return "%3.1f%s%s" % (scaled, prefixes[power], unit)
def guess_distro(path):
'''
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment