~raharper/curtin/trunk.lp1641661

« back to all changes in this revision

Viewing changes to curtin/util.py

  • Committer: Ryan Harper
  • Date: 2016-09-19 19:11:43 UTC
  • mfrom: (423.3.1 trunk.32bit-py2-long)
  • Revision ID: ryan.harper@canonical.com-20160919191143-8md5xy9pa7bnwsjn
bytes2human: fix for values larger than 32 bit int on 32 bit python2.

This fixes a bug in bytes2human when running on 32 bit system.
the bytes2human tests would fail because we were not testing for a
type 'long' and integers over 32 bit get converted to long.

    $ uname -m
    i386
    $ python2 -c 'print(type(1024 ** 3 * 10))'
    <type 'long'>

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
except NameError:
46
46
    string_types = (str,)
47
47
 
 
48
try:
 
49
    numeric_types = (int, float, long)
 
50
except NameError:
 
51
    # python3 does not have a long type.
 
52
    numeric_types = (int, float)
 
53
 
48
54
from .log import LOG
49
55
 
50
56
_INSTALLED_HELPERS_PATH = '/usr/lib/curtin/helpers'
871
877
 
872
878
def bytes2human(size):
873
879
    """convert size in bytes to human readable"""
874
 
    if not (isinstance(size, (int, float)) and
875
 
            int(size) == size and
876
 
            int(size) >= 0):
877
 
        raise ValueError('size must be a integral value')
 
880
    if not isinstance(size, numeric_types):
 
881
        raise ValueError('size must be a numeric value, not %s', type(size))
 
882
    isize = int(size)
 
883
    if isize != size:
 
884
        raise ValueError('size "%s" is not a whole number.' % size)
 
885
    if isize < 0:
 
886
        raise ValueError('size "%d" < 0.' % isize)
878
887
    mpliers = {'B': 1, 'K': 2 ** 10, 'M': 2 ** 20, 'G': 2 ** 30, 'T': 2 ** 40}
879
888
    unit_order = sorted(mpliers, key=lambda x: -1 * mpliers[x])
880
 
    unit = next((u for u in unit_order if (size / mpliers[u]) >= 1), 'B')
881
 
    return str(int(size / mpliers[unit])) + unit
 
889
    unit = next((u for u in unit_order if (isize / mpliers[u]) >= 1), 'B')
 
890
    return str(int(isize / mpliers[unit])) + unit
882
891
 
883
892
 
884
893
def import_module(import_str):