[PATCH 5/6] Allow negative sizes.
Anne Mulhern
amulhern at redhat.com
Wed Jan 8 15:05:50 UTC 2014
----- Original Message -----
> From: "David Lehman" <dlehman at redhat.com>
> To: anaconda-patches at lists.fedorahosted.org
> Sent: Tuesday, January 7, 2014 6:51:00 PM
> Subject: [PATCH 5/6] Allow negative sizes.
>
> There are times, especially when reconfiguring a stack via the device
> factory, when a volume group might have a negative amount free space.
> ---
> blivet/errors.py | 3 ---
> blivet/size.py | 11 ++++-------
> tests/size_test.py | 10 +++++-----
> 3 files changed, 9 insertions(+), 15 deletions(-)
>
> diff --git a/blivet/errors.py b/blivet/errors.py
> index 78d8271..03c9d7b 100644
> --- a/blivet/errors.py
> +++ b/blivet/errors.py
> @@ -182,9 +182,6 @@ class DasdFormatError(StorageError):
> class SizeParamsError(StorageError):
> pass
>
> -class SizeNotPositiveError(StorageError):
> - pass
> -
> class SizePlacesError(StorageError):
> pass
>
> diff --git a/blivet/size.py b/blivet/size.py
> index 154693e..b98a52c 100644
> --- a/blivet/size.py
> +++ b/blivet/size.py
> @@ -93,9 +93,6 @@ def _parseSpec(spec, xlate):
> except InvalidOperation:
> raise ValueError("invalid size specification", spec)
>
> - if size < 0:
> - raise SizeNotPositiveError("spec= param must be >=0")
> -
> specifier = m.groups()[1].lower()
> if xlate:
> bytes = [_(b) for b in _bytes]
> @@ -155,10 +152,10 @@ class Size(Decimal):
> raise SizeParamsError("only specify one parameter")
>
> if bytes is not None:
> - if type(bytes).__name__ in ["int", "long", "float", 'Decimal']
> and bytes >= 0:
> + if type(bytes).__name__ in ["int", "long", "float", 'Decimal']:
> value = Decimal(bytes)
> else:
> - raise SizeNotPositiveError("bytes= param must be >=0")
> + raise ValueError("invalid value for bytes param")
> elif spec:
> value = _parseSpec(spec, True)
> elif en_spec:
> @@ -259,14 +256,14 @@ class Size(Decimal):
>
> check = self._trimEnd("%d" % self)
>
> - if Decimal(check) < 1000:
> + if abs(Decimal(check)) < 1000:
> return "%s %s" % (check, _("B"))
>
> prefixes_xlated = [_(p) for p in _prefixes]
> for factor, prefix, abbr in prefixes_xlated:
> newcheck = super(Size, self).__div__(Decimal(factor))
>
> - if newcheck < 1000:
> + if abs(newcheck) < 1000:
> # nice value, use this factor, prefix and abbr
> break
>
> diff --git a/tests/size_test.py b/tests/size_test.py
> index 23c8bce..5680b43 100644
> --- a/tests/size_test.py
> +++ b/tests/size_test.py
> @@ -31,14 +31,9 @@ class SizeTestCase(unittest.TestCase):
> self.assertRaises(SizeParamsError, Size)
> self.assertRaises(SizeParamsError, Size, bytes=500, en_spec="45GB")
>
> - self.assertRaises(SizeNotPositiveError, Size, bytes=-1)
> -
> zero = Size(bytes=0)
> self.assertEqual(zero, 0.0)
>
> - self.assertRaises(SizeNotPositiveError, Size, en_spec="-1 TB")
> - self.assertRaises(SizeNotPositiveError, Size, en_spec="-47kb")
> -
> s = Size(bytes=500)
> self.assertRaises(SizePlacesError, s.humanReadable, places=-1)
>
> @@ -81,6 +76,11 @@ class SizeTestCase(unittest.TestCase):
> s = Size(bytes=478360371L)
> self.assertEquals(s.humanReadable(), "478.36 MB")
>
> + def testNegative(self):
> + s = Size(spec="-500MiB")
> + self.assertEquals(s.humanReadable(), "-500 MiB")
> + self.assertEquals(s.convertTo(spec="b"), -524288000)
> +
> def testPartialBytes(self):
> s = Size(bytes=1024.6)
> self.assertEquals(Size(bytes=1024.6), Size(bytes=1024))
> --
> 1.8.1.4
>
> _______________________________________________
> anaconda-patches mailing list
> anaconda-patches at lists.fedorahosted.org
> https://lists.fedorahosted.org/mailman/listinfo/anaconda-patches
>
Ack.
- mulhern
More information about the anaconda-patches
mailing list