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 ...@@ -12,6 +12,7 @@ import hashlib
import shutil import shutil
import urllib2 import urllib2
import time import time
import math
from subprocess import call, check_call, CalledProcessError from subprocess import call, check_call, CalledProcessError
...@@ -324,15 +325,17 @@ def getsize(path): ...@@ -324,15 +325,17 @@ def getsize(path):
total_sz += filestat.st_size total_sz += filestat.st_size
return total_sz return total_sz
def human_size(num): def human_size(num, unit='B'):
''' '''
Return human readable size Return human readable size
''' '''
for x in ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']: prefixes = ('','Ki', 'Mi', 'Gi', 'Ti','Pi', 'Ei', 'Zi', 'Yi')
if num < 1024.0: power = int(math.log(num, 1024))
return "%3.1f%s" % (num, x) # max is YiB
num /= 1024.0 if power >= len(prefixes):
return "%3.1f%s" % (num, x) power = len(prefixes) - 1
scaled = num / float(1024 ** power)
return "%3.1f%s%s" % (scaled, prefixes[power], unit)
def guess_distro(path): 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