[blivet:rhel7/master 1/2] Add BTRFSValueError error and use in btrfs related code (#1019685)

mulhern amulhern at redhat.com
Thu Sep 11 17:34:13 UTC 2014


Related: rhbz#1019685

>From BTRFS related methods that have ValueError-like exceptions raise
BTRFSValueError instead of ValueError. Note that this will not change the
behavior of code that calls these method and catches a ValueError since
BTRFSValueError extends ValueError. It can change the behavior of calling
code where a BTRFSError is expected; since BTRFSValueError is also a subtype
of BTRFSError the formerly ValueError exception will be caught where
previously it would not. There are no instances like this in anaconda.
There are two in blivet, in devices.py, and in both cases, this change
seems to be an improvement.

Signed-off-by: mulhern <amulhern at redhat.com>
---
 blivet/devicelibs/btrfs.py | 20 ++++++++++----------
 blivet/devices.py          | 14 +++++++-------
 blivet/errors.py           |  3 +++
 3 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/blivet/devicelibs/btrfs.py b/blivet/devicelibs/btrfs.py
index 0e1171d..ea8a984 100644
--- a/blivet/devicelibs/btrfs.py
+++ b/blivet/devicelibs/btrfs.py
@@ -25,7 +25,7 @@ import re
 
 from . import raid
 from .. import util
-from ..errors import BTRFSError
+from ..errors import BTRFSError, BTRFSValueError
 
 import logging
 log = logging.getLogger("blivet")
@@ -64,9 +64,9 @@ def create_volume(devices, label=None, data=None, metadata=None):
        recognizes the string.
     """
     if not devices:
-        raise ValueError("no devices specified")
+        raise BTRFSValueError("no devices specified")
     elif any([not os.path.exists(d) for d in devices]):
-        raise ValueError("one or more specified devices not present")
+        raise BTRFSValueError("one or more specified devices not present")
 
     args = []
     if data:
@@ -90,20 +90,20 @@ def create_volume(devices, label=None, data=None, metadata=None):
 
 def add(mountpoint, device):
     if not os.path.ismount(mountpoint):
-        raise ValueError("volume not mounted")
+        raise BTRFSValueError("volume not mounted")
 
     return btrfs(["device", "add", device, mountpoint])
 
 def remove(mountpoint, device):
     if not os.path.ismount(mountpoint):
-        raise ValueError("volume not mounted")
+        raise BTRFSValueError("volume not mounted")
 
     return btrfs(["device", "delete", device, mountpoint])
 
 def create_subvolume(mountpoint, name):
     """Create a subvolume named name below mountpoint mountpoint."""
     if not os.path.ismount(mountpoint):
-        raise ValueError("volume not mounted")
+        raise BTRFSValueError("volume not mounted")
 
     path = os.path.normpath("%s/%s" % (mountpoint, name))
     args = ["subvol", "create", path]
@@ -111,7 +111,7 @@ def create_subvolume(mountpoint, name):
 
 def delete_subvolume(mountpoint, name):
     if not os.path.ismount(mountpoint):
-        raise ValueError("volume not mounted")
+        raise BTRFSValueError("volume not mounted")
 
     path = os.path.normpath("%s/%s" % (mountpoint, name))
     args = ["subvol", "delete", path]
@@ -123,7 +123,7 @@ _SUBVOL_REGEX = re.compile(_SUBVOL_REGEX_STR)
 # get a list of subvolumes from a mounted btrfs filesystem
 def list_subvolumes(mountpoint, snapshots_only=False):
     if not os.path.ismount(mountpoint):
-        raise ValueError("volume not mounted")
+        raise BTRFSValueError("volume not mounted")
 
     args = ["subvol", "list", "-p", mountpoint]
     if snapshots_only:
@@ -139,7 +139,7 @@ def list_subvolumes(mountpoint, snapshots_only=False):
 
 def get_default_subvolume(mountpoint):
     if not os.path.ismount(mountpoint):
-        raise ValueError("volume not mounted")
+        raise BTRFSValueError("volume not mounted")
 
     args = ["subvol", "get-default", mountpoint]
     buf = btrfs(args, capture=True)
@@ -158,7 +158,7 @@ def create_snapshot(source, dest, ro=False):
         :keyword bool ro: whether to create a read-only snapshot
     """
     if not os.path.ismount(source):
-        raise ValueError("source is not a mounted subvolume")
+        raise BTRFSValueError("source is not a mounted subvolume")
 
     args = ["subvol", "snapshot"]
     if ro:
diff --git a/blivet/devices.py b/blivet/devices.py
index 7e4279c..fe87226 100644
--- a/blivet/devices.py
+++ b/blivet/devices.py
@@ -4996,7 +4996,7 @@ class BTRFSDevice(StorageDevice):
             args = ("btrfs.%d" % self.id,)
 
         if kwargs.get("parents") is None:
-            raise ValueError("BTRFSDevice must have at least one parent")
+            raise errors.BTRFSValueError("BTRFSDevice must have at least one parent")
 
         self.req_size = kwargs.pop("size", None)
         super(BTRFSDevice, self).__init__(*args, **kwargs)
@@ -5206,13 +5206,13 @@ class BTRFSVolumeDevice(BTRFSDevice, ContainerDevice):
 
     def _addSubVolume(self, vol):
         if vol.name in [v.name for v in self.subvolumes]:
-            raise ValueError("subvolume %s already exists" % vol.name)
+            raise errors.BTRFSValueError("subvolume %s already exists" % vol.name)
 
         self.subvolumes.append(vol)
 
     def _removeSubVolume(self, name):
         if name not in [v.name for v in self.subvolumes]:
-            raise ValueError("cannot remove non-existent subvolume %s" % name)
+            raise errors.BTRFSValueError("cannot remove non-existent subvolume %s" % name)
 
         names = [v.name for v in self.subvolumes]
         self.subvolumes.pop(names.index(name))
@@ -5454,13 +5454,13 @@ class BTRFSSnapShotDevice(BTRFSSubVolumeDevice):
         source = kwargs.pop("source", None)
         if not kwargs.get("exists") and not source:
             # it is possible to remove a source subvol and keep snapshots of it
-            raise ValueError("non-existent btrfs snapshots must have a source")
+            raise errors.BTRFSValueError("non-existent btrfs snapshots must have a source")
 
         if source and not isinstance(source, BTRFSDevice):
-            raise ValueError("btrfs snapshot source must be a btrfs subvolume")
+            raise errors.BTRFSValueError("btrfs snapshot source must be a btrfs subvolume")
 
         if source and not source.exists:
-            raise ValueError("btrfs snapshot source must already exist")
+            raise errors.BTRFSValueError("btrfs snapshot source must already exist")
 
         self.source = source
         """ the snapshot's source subvolume """
@@ -5472,7 +5472,7 @@ class BTRFSSnapShotDevice(BTRFSSubVolumeDevice):
         if source and getattr(source, "volume", source) != self.volume:
             self.volume._removeSubVolume(self.name)
             self.parents = []
-            raise ValueError("btrfs snapshot and source must be in the same volume")
+            raise errors.BTRFSValueError("btrfs snapshot and source must be in the same volume")
 
     def _create(self):
         log_method_call(self, self.name, status=self.status)
diff --git a/blivet/errors.py b/blivet/errors.py
index e0e5871..4c4fe30 100644
--- a/blivet/errors.py
+++ b/blivet/errors.py
@@ -146,6 +146,9 @@ class LoopError(StorageError):
 class BTRFSError(StorageError):
     pass
 
+class BTRFSValueError(BTRFSError, ValueError):
+    pass
+
 # DeviceTree
 class DeviceTreeError(StorageError):
     pass
-- 
1.9.3



More information about the anaconda-patches mailing list