コード例 #1
0
ファイル: utils.py プロジェクト: bswartz/cinder
def _calculate_count(size_in_m, blocksize):

    # Check if volume_dd_blocksize is valid
    try:
        # Rule out zero-sized/negative/float dd blocksize which
        # cannot be caught by strutils
        if blocksize.startswith(("-", "0")) or "." in blocksize:
            raise ValueError
        bs = strutils.string_to_bytes("%sB" % blocksize)
    except ValueError:
        LOG.warning(
            _LW(
                "Incorrect value error: %(blocksize)s, "
                "it may indicate that 'volume_dd_blocksize' "
                "was configured incorrectly. Fall back to default."
            ),
            {"blocksize": blocksize},
        )
        # Fall back to default blocksize
        CONF.clear_override("volume_dd_blocksize")
        blocksize = CONF.volume_dd_blocksize
        bs = strutils.string_to_bytes("%sB" % blocksize)

    count = math.ceil(size_in_m * units.Mi / bs)

    return blocksize, int(count)
コード例 #2
0
ファイル: zfs.py プロジェクト: galitus/zfs_for_openstack
def _fromsizestr(sizestr):
    # ZFS uses a dash as a 'not applicable' value.
    if sizestr == '-':
        return None

    # ZFS tools give their sizes as 1T, 9.2G. string_to_bytes
    # doesn't accept that format, so we'll have to add a B.
    try:
        return strutils.string_to_bytes(sizestr + "B")
    except ValueError:
        return strutils.string_to_bytes(sizestr)
コード例 #3
0
ファイル: objects.py プロジェクト: jmank/proliantutils
    def __init__(self, id, properties, parent):
        """Constructor for a PhysicalDrive object."""
        self.parent = parent
        self.properties = properties

        # Strip off physicaldrive before storing it in id
        self.id = id[14:]

        size = self.properties['Size'].replace(' ', '')
        # 'string_to_bytes' takes care of converting any returned
        # (like 500MB, 25GB) unit of storage space to bytes (Integer value).
        # It requires space to be stripped.
        try:
            self.size_gb = int(strutils.string_to_bytes(size,
                                                        return_int=True) /
                               (1024*1024*1024))
        except ValueError:
            msg = ("hpssacli returned unknown size '%(size)s' for physical "
                   "disk '%(physical_disk)s' of controller "
                   "'%(controller)s'." %
                   {'size': size, 'physical_disk': self.id,
                    'controller': self.parent.id})
            raise exception.HPSSAOperationError(reason=msg)

        ssa_interface = self.properties['Interface Type']
        self.interface_type = constants.get_interface_type(ssa_interface)
        self.disk_type = constants.get_disk_type(ssa_interface)
        self.model = self.properties.get('Model')
        self.firmware = self.properties.get('Firmware Revision')
コード例 #4
0
ファイル: test_strutils.py プロジェクト: bdrich/neutron-lbaas
    def test_string_to_bytes(self):
        def _get_quantity(sign, magnitude, unit_suffix):
            res = float("%s%s" % (sign, magnitude))
            if unit_suffix in ["b", "bit"]:
                res /= 8
            return res

        def _get_constant(unit_prefix, unit_system):
            if not unit_prefix:
                return 1
            elif unit_system == "SI":
                res = getattr(units, unit_prefix)
            elif unit_system == "IEC":
                if unit_prefix.endswith("i"):
                    res = getattr(units, unit_prefix)
                else:
                    res = getattr(units, "%si" % unit_prefix)
            return res

        text = "".join([self.sign, self.magnitude, self.unit_prefix, self.unit_suffix])
        err_si = self.unit_system == "SI" and (self.unit_prefix == "K" or self.unit_prefix.endswith("i"))
        err_iec = self.unit_system == "IEC" and self.unit_prefix == "k"
        if getattr(self, "assert_error", False) or err_si or err_iec:
            self.assertRaises(
                ValueError, strutils.string_to_bytes, text, unit_system=self.unit_system, return_int=self.return_int
            )
            return
        quantity = _get_quantity(self.sign, self.magnitude, self.unit_suffix)
        constant = _get_constant(self.unit_prefix, self.unit_system)
        expected = quantity * constant
        actual = strutils.string_to_bytes(text, unit_system=self.unit_system, return_int=self.return_int)
        if self.return_int:
            self.assertEqual(actual, int(math.ceil(expected)))
        else:
            self.assertAlmostEqual(actual, expected)
コード例 #5
0
ファイル: objects.py プロジェクト: yanheven/proliantutils-1
    def __init__(self, id, properties, parent):
        """Constructor for a PhysicalDrive object."""
        self.parent = parent
        self.properties = properties

        # Strip off physicaldrive before storing it in id
        self.id = id[14:]

        size = self.properties['Size'].replace(' ', '')
        # 'string_to_bytes' takes care of converting any returned
        # (like 500MB, 25GB) unit of storage space to bytes (Integer value).
        # It requires space to be stripped.
        try:
            self.size_gb = int(
                strutils.string_to_bytes(size, return_int=True) /
                (1024 * 1024 * 1024))
        except ValueError:
            msg = ("hpssacli returned unknown size '%(size)s' for physical "
                   "disk '%(physical_disk)s' of controller "
                   "'%(controller)s'." % {
                       'size': size,
                       'physical_disk': self.id,
                       'controller': self.parent.id
                   })
            raise exception.HPSSAOperationError(reason=msg)

        ssa_interface = self.properties['Interface Type']
        self.interface_type = constants.get_interface_type(ssa_interface)
        self.disk_type = constants.get_disk_type(ssa_interface)
        self.model = self.properties.get('Model')
        self.firmware = self.properties.get('Firmware Revision')
コード例 #6
0
    def _parse_memory_embedded_health(self, data):
        """Parse the get_host_health_data() for essential properties

        :param data: the output returned by get_host_health_data()
        :returns: memory size in MB.
        :raises IloError, if unable to get the memory details.

        """
        memory_mb = 0
        memory = self.get_value_as_list((data['GET_EMBEDDED_HEALTH_DATA']
                                        ['MEMORY']), 'MEMORY_DETAILS_SUMMARY')
        if memory is None:
            msg = "Unable to get memory data. Error: Data missing"
            raise exception.IloError(msg)

        total_memory_size = 0
        for item in memory:
            for val in item.values():
                memsize = val['TOTAL_MEMORY_SIZE']['VALUE']
                if memsize != 'N/A':
                    memory_bytes = (
                        strutils.string_to_bytes(
                            memsize.replace(' ', ''), return_int=True))
                    memory_mb = int(memory_bytes / (1024 * 1024))
                    total_memory_size = total_memory_size + memory_mb
        return total_memory_size
コード例 #7
0
ファイル: utils.py プロジェクト: Nexenta/cinder
def _check_blocksize(blocksize):

    # Check if volume_dd_blocksize is valid
    try:
        # Rule out zero-sized/negative/float dd blocksize which
        # cannot be caught by strutils
        if blocksize.startswith(('-', '0')) or '.' in blocksize:
            raise ValueError
        strutils.string_to_bytes('%sB' % blocksize)
    except ValueError:
        LOG.warning(_LW("Incorrect value error: %(blocksize)s, "
                        "it may indicate that \'volume_dd_blocksize\' "
                        "was configured incorrectly. Fall back to default."),
                    {'blocksize': blocksize})
        # Fall back to default blocksize
        CONF.clear_override('volume_dd_blocksize')
        blocksize = CONF.volume_dd_blocksize

    return blocksize
コード例 #8
0
def _check_blocksize(blocksize):

    # Check if volume_dd_blocksize is valid
    try:
        # Rule out zero-sized/negative/float dd blocksize which
        # cannot be caught by strutils
        if blocksize.startswith(('-', '0')) or '.' in blocksize:
            raise ValueError
        strutils.string_to_bytes('%sB' % blocksize)
    except ValueError:
        LOG.warning(_LW("Incorrect value error: %(blocksize)s, "
                        "it may indicate that \'volume_dd_blocksize\' "
                        "was configured incorrectly. Fall back to default."),
                    {'blocksize': blocksize})
        # Fall back to default blocksize
        CONF.clear_override('volume_dd_blocksize')
        blocksize = CONF.volume_dd_blocksize

    return blocksize
コード例 #9
0
 def _get_zpool_attribute(attribute):
     value, err = utils.execute('zpool', 'list',
                                '-o', attribute,
                                '-H', pool,
                                run_as_root=True)
     if err:
         msg = _('Unable to parse zpool output.')
         raise exception.NovaException(msg)
     value = strutils.string_to_bytes('{}B'.format(value.strip()),
                                      return_int=True)
     return value
コード例 #10
0
    def __init__(self, id, properties, parent):
        """Constructor for a LogicalDrive object."""
        # Strip off 'Logical Drive' before storing it in id
        self.id = id[15:]
        self.parent = parent
        self.properties = properties

        # 'string_to_bytes' takes care of converting any returned
        # (like 500MB, 25GB) unit of storage space to bytes (Integer value).
        # It requires space to be stripped.
        try:
            size = self.properties['Size'].replace(' ', '')
            # TODO(rameshg87): Reduce the disk size by 1 to make sure Ironic
            # has enough space to write a config drive. Remove this when
            # Ironic doesn't need it.
            self.size_gb = int(
                strutils.string_to_bytes(size, return_int=True) /
                (1024 * 1024 * 1024)) - 1
        except KeyError:
            msg = ("Can't get 'Size' parameter from ssacli output for logical "
                   "disk '%(logical_disk)s' of RAID array '%(array)s' in "
                   "controller '%(controller)s'." % {
                       'logical_disk': self.id,
                       'array': self.parent.id,
                       'controller': self.parent.parent.id
                   })
            raise exception.HPSSAOperationError(reason=msg)
        except ValueError:
            msg = ("ssacli returned unknown size '%(size)s' for logical "
                   "disk '%(logical_disk)s' of RAID array '%(array)s' in "
                   "controller '%(controller)s'." % {
                       'size': size,
                       'logical_disk': self.id,
                       'array': self.parent.id,
                       'controller': self.parent.parent.id
                   })
            raise exception.HPSSAOperationError(reason=msg)

        self.raid_level = self.properties.get('Fault Tolerance')
        # For RAID levels (like 5+0 and 6+0), HPSSA names them differently.
        # Check if we have mapping stored, otherwise use the same.
        raid_level_mapping = constants.RAID_LEVEL_HPSSA_TO_INPUT_MAPPING
        self.raid_level = raid_level_mapping.get(self.raid_level,
                                                 self.raid_level)

        self.volume_name = self.properties.get('Logical Drive Label')

        # Trim down the WWN to 16 digits (8 bytes) so that it matches
        # lsblk output in Linux.
        wwn = self.properties.get('Unique Identifier')
        if wwn:
            wwn = '0x' + wwn[:16].lower()
            self.wwn = wwn
コード例 #11
0
 def _convert_value(self, v):
     # This currently takes care only on bytes
     try:
         v = v.strip()
         if v[-1] in ["T", "G", "M", "K"]:
             # Assuming this is bytes...
             try:
                 v = "%s%s%s" % (float(v[:-1]), v[-1], "B")
             except ValueError:
                 pass
         try:
             return strutils.string_to_bytes(v, "IEC")
         except ValueError:
             try:
                 return strutils.string_to_bytes(v, "SI")
             except ValueError:
                 try:
                     return self._string_to_any(v)
                 except ValueError:
                     return float(v)
     except ValueError:
         raise MalformedPerfdata("Unknow perfdata value/unit: '%s'" % v)
コード例 #12
0
ファイル: ssh.py プロジェクト: petrutlucian94/manila
    def get_share_usage(self, share_id):
        command = ['quota', 'list', self.fs_name, share_id]
        output, err = self._execute(command)

        quota = Quota(output)

        if quota.usage is None:
            msg = (_("Virtual volume %s does not have any quota.") % share_id)
            raise exception.HNASItemNotFoundException(msg=msg)
        else:
            bytes_usage = strutils.string_to_bytes(
                six.text_type(quota.usage) + quota.usage_unit)
            return bytes_usage / units.Gi
コード例 #13
0
ファイル: ssh.py プロジェクト: ISCAS-VDI/manila-base
    def get_share_usage(self, share_id):
        command = ['quota', 'list', self.fs_name, share_id]
        output, err = self._execute(command)

        quota = Quota(output)

        if quota.usage is None:
            msg = (_("Virtual volume %s does not have any quota.") % share_id)
            raise exception.HNASItemNotFoundException(msg=msg)
        else:
            bytes_usage = strutils.string_to_bytes(six.text_type(quota.usage) +
                                                   quota.usage_unit)
            return bytes_usage / units.Gi
コード例 #14
0
 def _extract_bytes(self, details):
     # Replace it with the byte amount
     real_size = self.SIZE_RE.search(details)
     if not real_size:
         raise ValueError(_('Invalid input value "%s".') % details)
     magnitude = real_size.group(1)
     unit_of_measure = real_size.group(2)
     bytes_info = real_size.group(3)
     if bytes_info:
         return int(real_size.group(4))
     elif not unit_of_measure:
         return int(magnitude)
     return strutils.string_to_bytes('%s%sB' % (magnitude, unit_of_measure),
                                     return_int=True)
コード例 #15
0
ファイル: utils.py プロジェクト: TonyChengTW/Cinder_Extend
def _calculate_count(size_in_m, blocksize):

    # Check if volume_dd_blocksize is valid
    try:
        # Rule out zero-sized/negative/float dd blocksize which
        # cannot be caught by strutils
        if blocksize.startswith(('-', '0')) or '.' in blocksize:
            raise ValueError
        bs = strutils.string_to_bytes('%sB' % blocksize)
    except ValueError:
        msg = (_("Incorrect value error: %(blocksize)s, "
                 "it may indicate that \'volume_dd_blocksize\' "
                 "was configured incorrectly. Fall back to default.")
               % {'blocksize': blocksize})
        LOG.warn(msg)
        # Fall back to default blocksize
        CONF.clear_override('volume_dd_blocksize')
        blocksize = CONF.volume_dd_blocksize
        bs = strutils.string_to_bytes('%sB' % blocksize)

    count = math.ceil(size_in_m * units.Mi / bs)

    return blocksize, int(count)
コード例 #16
0
ファイル: imageutils.py プロジェクト: Datera/cinder
 def _extract_bytes(self, details):
     # Replace it with the byte amount
     real_size = self.SIZE_RE.search(details)
     if not real_size:
         raise ValueError(_('Invalid input value "%s".') % details)
     magnitude = real_size.group(1)
     unit_of_measure = real_size.group(2)
     bytes_info = real_size.group(3)
     if bytes_info:
         return int(real_size.group(4))
     elif not unit_of_measure:
         return int(magnitude)
     return strutils.string_to_bytes('%s%sB' % (magnitude, unit_of_measure),
                                     return_int=True)
コード例 #17
0
    def _parse_storage_embedded_health(self, data):
        """Gets the storage data from get_embedded_health

        Parse the get_host_health_data() for essential properties

        :param data: the output returned by get_host_health_data()
        :returns: disk size in GB.

        """
        local_gb = 0
        storage = self.get_value_as_list(data['GET_EMBEDDED_HEALTH_DATA'],
                                         'STORAGE')
        if storage is None:
            # We dont raise exception because this dictionary
            # is available only when RAID is configured.
            # If we raise error here then we will always fail
            # inspection where this module is consumed. Hence
            # as a workaround just return 0.
            return local_gb

        minimum = local_gb

        for item in storage:
            cntlr = self.get_value_as_list(item, 'CONTROLLER')
            if cntlr is None:
                continue
            for s in cntlr:
                drive = self.get_value_as_list(s, 'LOGICAL_DRIVE')
                if drive is None:
                    continue
                for item in drive:
                    for key, val in item.items():
                        if key == 'CAPACITY':
                            capacity = val['VALUE']
                            local_bytes = (strutils.string_to_bytes(
                                           capacity.replace(' ', ''),
                                           return_int=True))
                            local_gb = int(local_bytes / (1024 * 1024 * 1024))
                            if minimum >= local_gb or minimum == 0:
                                minimum = local_gb

        # Return disk size 1 less than the actual disk size. This prevents
        # the deploy to fail from Nova when root_gb is same as local_gb
        # in Ironic. When the disk size is used as root_device hints,
        # then it should be given as the actual size i.e.
        # ironic (node.properties['local_gb'] + 1) else root device
        # hint will fail.
        if minimum:
            minimum = minimum - 1
        return minimum
コード例 #18
0
ファイル: utils.py プロジェクト: Dynavisor/cinder
def _calculate_count(size_in_m, blocksize):

    # Check if volume_dd_blocksize is valid
    try:
        # Rule out zero-sized/negative/float dd blocksize which
        # cannot be caught by strutils
        if blocksize.startswith(('-', '0')) or '.' in blocksize:
            raise ValueError
        bs = strutils.string_to_bytes('%sB' % blocksize)
    except ValueError:
        msg = (_("Incorrect value error: %(blocksize)s, "
                 "it may indicate that \'volume_dd_blocksize\' "
                 "was configured incorrectly. Fall back to default.")
               % {'blocksize': blocksize})
        LOG.warn(msg)
        # Fall back to default blocksize
        CONF.clear_override('volume_dd_blocksize')
        blocksize = CONF.volume_dd_blocksize
        bs = strutils.string_to_bytes('%sB' % blocksize)

    count = math.ceil(size_in_m * units.Mi / bs)

    return blocksize, int(count)
コード例 #19
0
    def test_string_to_bytes(self):
        def _get_quantity(sign, magnitude, unit_suffix):
            res = float('%s%s' % (sign, magnitude))
            if unit_suffix in ['b', 'bit']:
                res /= 8
            return res

        def _get_constant(unit_prefix, unit_system):
            if not unit_prefix:
                return 1
            elif unit_system == 'SI':
                res = getattr(units, unit_prefix)
            elif unit_system == 'IEC':
                if unit_prefix.endswith('i'):
                    res = getattr(units, unit_prefix)
                else:
                    res = getattr(units, '%si' % unit_prefix)
            elif unit_system == 'mixed':
                # Note: this will return 'i' units as power-of-two,
                # and other units as power-of-ten.  Additionally, for
                # compatability a "K" is interpreted as "k" in mixed
                # mode
                if unit_prefix == 'K':
                    unit_prefix = 'k'
                res = getattr(units, unit_prefix)
            return res

        text = ''.join(
            [self.sign, self.magnitude, self.unit_prefix, self.unit_suffix])
        err_si = self.unit_system == 'SI' and (self.unit_prefix == 'K' or
                                               self.unit_prefix.endswith('i'))
        err_iec = self.unit_system == 'IEC' and self.unit_prefix == 'k'
        if getattr(self, 'assert_error', False) or err_si or err_iec:
            self.assertRaises(ValueError,
                              strutils.string_to_bytes,
                              text,
                              unit_system=self.unit_system,
                              return_int=self.return_int)
            return
        quantity = _get_quantity(self.sign, self.magnitude, self.unit_suffix)
        constant = _get_constant(self.unit_prefix, self.unit_system)
        expected = quantity * constant
        actual = strutils.string_to_bytes(text,
                                          unit_system=self.unit_system,
                                          return_int=self.return_int)
        if self.return_int:
            self.assertEqual(actual, int(math.ceil(expected)))
        else:
            self.assertAlmostEqual(actual, expected)
コード例 #20
0
ファイル: test_strutils.py プロジェクト: openstack/oslo.utils
    def test_string_to_bytes(self):

        def _get_quantity(sign, magnitude, unit_suffix):
            res = float('%s%s' % (sign, magnitude))
            if unit_suffix in ['b', 'bit']:
                res /= 8
            return res

        def _get_constant(unit_prefix, unit_system):
            if not unit_prefix:
                return 1
            elif unit_system == 'SI':
                res = getattr(units, unit_prefix)
            elif unit_system == 'IEC':
                if unit_prefix.endswith('i'):
                    res = getattr(units, unit_prefix)
                else:
                    res = getattr(units, '%si' % unit_prefix)
            elif unit_system == 'mixed':
                # Note: this will return 'i' units as power-of-two,
                # and other units as power-of-ten.  Additionally, for
                # compatability a "K" is interpreted as "k" in mixed
                # mode
                if unit_prefix == 'K':
                    unit_prefix = 'k'
                res = getattr(units, unit_prefix)
            return res

        text = ''.join([self.sign, self.magnitude, self.unit_prefix,
                        self.unit_suffix])
        err_si = self.unit_system == 'SI' and (self.unit_prefix == 'K' or
                                               self.unit_prefix.endswith('i'))
        err_iec = self.unit_system == 'IEC' and self.unit_prefix == 'k'
        if getattr(self, 'assert_error', False) or err_si or err_iec:
            self.assertRaises(ValueError, strutils.string_to_bytes,
                              text, unit_system=self.unit_system,
                              return_int=self.return_int)
            return
        quantity = _get_quantity(self.sign, self.magnitude, self.unit_suffix)
        constant = _get_constant(self.unit_prefix, self.unit_system)
        expected = quantity * constant
        actual = strutils.string_to_bytes(text, unit_system=self.unit_system,
                                          return_int=self.return_int)
        if self.return_int:
            self.assertEqual(actual, int(math.ceil(expected)))
        else:
            self.assertAlmostEqual(actual, expected)
コード例 #21
0
 def _extract_bytes(self, details):
     # Replace it with the byte amount
     real_size = self.SIZE_RE.search(details)
     if not real_size:
         raise ValueError(_('Invalid input value "%s".') % details)
     magnitude = real_size.group(1)
     unit_of_measure = real_size.group(2)
     bytes_info = real_size.group(3)
     if bytes_info:
         return int(real_size.group(4))
     elif not unit_of_measure:
         return int(magnitude)
     # Allow abbreviated unit such as K to mean KB for compatibility.
     if len(unit_of_measure) == 1 and unit_of_measure != 'B':
         unit_of_measure += 'B'
     return strutils.string_to_bytes('%s%s' % (magnitude, unit_of_measure),
                                     return_int=True)
コード例 #22
0
ファイル: objects.py プロジェクト: jmank/proliantutils
    def __init__(self, id, properties, parent):
        """Constructor for a LogicalDrive object."""
        # Strip off 'Logical Drive' before storing it in id
        self.id = id[15:]
        self.parent = parent
        self.properties = properties

        # 'string_to_bytes' takes care of converting any returned
        # (like 500MB, 25GB) unit of storage space to bytes (Integer value).
        # It requires space to be stripped.
        size = self.properties['Size'].replace(' ', '')
        try:
            # TODO(rameshg87): Reduce the disk size by 1 to make sure Ironic
            # has enough space to write a config drive. Remove this when
            # Ironic doesn't need it.
            self.size_gb = int(strutils.string_to_bytes(size,
                                                        return_int=True) /
                               (1024*1024*1024)) - 1
        except ValueError:
            msg = ("hpssacli returned unknown size '%(size)s' for logical "
                   "disk '%(logical_disk)s' of RAID array '%(array)s' in "
                   "controller '%(controller)s'." %
                   {'size': size, 'logical_disk': self.id,
                    'array': self.parent.id,
                    'controller': self.parent.parent.id})
            raise exception.HPSSAOperationError(reason=msg)

        self.raid_level = self.properties.get('Fault Tolerance')
        # For RAID levels (like 5+0 and 6+0), HPSSA names them differently.
        # Check if we have mapping stored, otherwise use the same.
        raid_level_mapping = constants.RAID_LEVEL_HPSSA_TO_INPUT_MAPPING
        self.raid_level = raid_level_mapping.get(self.raid_level,
                                                 self.raid_level)

        self.volume_name = self.properties.get('Logical Drive Label')

        # Trim down the WWN to 16 digits (8 bytes) so that it matches
        # lsblk output in Linux.
        wwn = self.properties.get('Unique Identifier')
        if wwn:
            wwn = '0x' + wwn[:16].lower()
            self.wwn = wwn
コード例 #23
0
    def test_string_to_bytes(self):
        def _get_quantity(sign, magnitude, unit_suffix):
            res = float('%s%s' % (sign, magnitude))
            if unit_suffix in ['b', 'bit']:
                res /= 8
            return res

        def _get_constant(unit_prefix, unit_system):
            if not unit_prefix:
                return 1
            elif unit_system == 'SI':
                res = getattr(units, unit_prefix)
            elif unit_system == 'IEC':
                if unit_prefix.endswith('i'):
                    res = getattr(units, unit_prefix)
                else:
                    res = getattr(units, '%si' % unit_prefix)
            return res

        text = ''.join(
            [self.sign, self.magnitude, self.unit_prefix, self.unit_suffix])
        err_si = self.unit_system == 'SI' and (self.unit_prefix == 'K' or
                                               self.unit_prefix.endswith('i'))
        err_iec = self.unit_system == 'IEC' and self.unit_prefix == 'k'
        if getattr(self, 'assert_error', False) or err_si or err_iec:
            self.assertRaises(ValueError,
                              strutils.string_to_bytes,
                              text,
                              unit_system=self.unit_system,
                              return_int=self.return_int)
            return
        quantity = _get_quantity(self.sign, self.magnitude, self.unit_suffix)
        constant = _get_constant(self.unit_prefix, self.unit_system)
        expected = quantity * constant
        actual = strutils.string_to_bytes(text,
                                          unit_system=self.unit_system,
                                          return_int=self.return_int)
        if self.return_int:
            self.assertEqual(actual, int(math.ceil(expected)))
        else:
            self.assertAlmostEqual(actual, expected)
コード例 #24
0
ファイル: hardware.py プロジェクト: vbalachandran/nova
    def check_and_return_pages_size(request):
        if request == "any":
            return MEMPAGES_ANY
        elif request == "large":
            return MEMPAGES_LARGE
        elif request == "small":
            return MEMPAGES_SMALL
        else:
            try:
                request = int(request)
            except ValueError:
                try:
                    request = strutils.string_to_bytes(
                        request, return_int=True) / units.Ki
                except ValueError:
                    request = 0

        if request <= 0:
            raise exception.MemoryPageSizeInvalid(pagesize=request)

        return request
コード例 #25
0
    def check_and_return_pages_size(request):
        if request == "any":
            return MEMPAGES_ANY
        elif request == "large":
            return MEMPAGES_LARGE
        elif request == "small":
            return MEMPAGES_SMALL
        else:
            try:
                request = int(request)
            except ValueError:
                try:
                    request = strutils.string_to_bytes(
                        request, return_int=True) / units.Ki
                except ValueError:
                    request = 0

        if request <= 0:
            raise exception.MemoryPageSizeInvalid(pagesize=request)

        return request
コード例 #26
0
    def test_string_to_bytes(self):

        def _get_quantity(sign, magnitude, unit_suffix):
            res = float('%s%s' % (sign, magnitude))
            if unit_suffix in ['b', 'bit']:
                res /= 8
            return res

        def _get_constant(unit_prefix, unit_system):
            if not unit_prefix:
                return 1
            elif unit_system == 'SI':
                res = getattr(units, unit_prefix)
            elif unit_system == 'IEC':
                if unit_prefix.endswith('i'):
                    res = getattr(units, unit_prefix)
                else:
                    res = getattr(units, '%si' % unit_prefix)
            return res

        text = ''.join([self.sign, self.magnitude, self.unit_prefix,
                        self.unit_suffix])
        err_si = self.unit_system == 'SI' and (self.unit_prefix == 'K' or
                                               self.unit_prefix.endswith('i'))
        err_iec = self.unit_system == 'IEC' and self.unit_prefix == 'k'
        if getattr(self, 'assert_error', False) or err_si or err_iec:
            self.assertRaises(ValueError, strutils.string_to_bytes,
                              text, unit_system=self.unit_system,
                              return_int=self.return_int)
            return
        quantity = _get_quantity(self.sign, self.magnitude, self.unit_suffix)
        constant = _get_constant(self.unit_prefix, self.unit_system)
        expected = quantity * constant
        actual = strutils.string_to_bytes(text, unit_system=self.unit_system,
                                          return_int=self.return_int)
        if self.return_int:
            self.assertEqual(actual, int(math.ceil(expected)))
        else:
            self.assertAlmostEqual(actual, expected)
コード例 #27
0
    def _get_info_from_qemu_img(self):
        output = subprocess.check_output(["qemu-img", "info", self.path])
        allocation = None
        capacity = None

        for line in output.splitlines():
            if line.startswith("virtual size"):
                # it looks like `virtual size: 4.0G (4294967296 bytes)`
                _w1, size, _w2 = line.rsplit(" ", 2)
                allocation = int(size.replace("(", ""))
            elif line.startswith("disk size"):
                size = line.split(" ")[2]
                try:
                    capacity = float(size)
                except ValueError:
                    from oslo_utils import strutils
                    capacity = strutils.string_to_bytes("%sB" % size,
                                                        return_int=True)

        if allocation is None or capacity is None:
            raise Exception("Failed to parse output of `qemu-img info %s`." %
                            self.path)

        return capacity, allocation
コード例 #28
0
    def _parse_memory_embedded_health(self, data):
        """Parse the get_host_health_data() for essential properties

        :param data: the output returned by get_host_health_data()
        :returns: memory size in MB.
        :raises IloError, if unable to get the memory details.
        """
        memory_mb = 0
        memory = self._get_memory_details_value_based_on_model(data)

        if memory is None:
            msg = "Unable to get memory data. Error: Data missing"
            raise exception.IloError(msg)

        total_memory_size = 0
        for memory_item in memory:
            memsize = memory_item[self.MEMORY_SIZE_TAG]["VALUE"]
            if memsize != self.MEMORY_SIZE_NOT_PRESENT_TAG:
                memory_bytes = (strutils.string_to_bytes(memsize.replace(
                    ' ', ''),
                                                         return_int=True))
                memory_mb = int(memory_bytes / (1024 * 1024))
                total_memory_size = total_memory_size + memory_mb
        return total_memory_size
コード例 #29
0
 def _fromsizestr(self, sizestr):
     return strutils.string_to_bytes(sizestr + 'B')