[rhel7-branch 1/6] Add BTRFSValueError error and use in btrfs related code (#1202877)

mulkieran installerbot-noreply at redhat.com
Tue Jun 2 22:34:47 UTC 2015


From: mulhern <amulhern at redhat.com>

Related: rhbz#1019685
Related: rhbz#1202877

(cherry picked from commit d11015c37f20fbcc25ec28cb48b50542d8e28141)

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/btrfs.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 eeba83b..12612eb 100644
--- a/blivet/devicelibs/btrfs.py
+++ b/blivet/devicelibs/btrfs.py
@@ -25,7 +25,7 @@
 
 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 @@ def delete_subvolume(mountpoint, name):
 # 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)
@@ -170,7 +170,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/btrfs.py b/blivet/devices/btrfs.py
index 23874ae..d1bdf44 100644
--- a/blivet/devices/btrfs.py
+++ b/blivet/devices/btrfs.py
@@ -49,7 +49,7 @@ def __init__(self, *args, **kwargs):
             args = ("btrfs.%d" % self.id,)
 
         if kwargs.get("parents") is None:
-            raise ValueError("BTRFSDevice must have at least one parent")
+            raise errors.BTRFSError("BTRFSDevice must have at least one parent")
 
         self.req_size = kwargs.pop("size", None)
         super(BTRFSDevice, self).__init__(*args, **kwargs)
@@ -263,13 +263,13 @@ def _removeParent(self, member):
 
     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))
@@ -566,13 +566,13 @@ def __init__(self, *args, **kwargs):
         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 """
@@ -584,7 +584,7 @@ def __init__(self, *args, **kwargs):
         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 e48b649..51fd1f0 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


-- 
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/9a9ad170abfe1e5272030792c3a3006dd8e3f446


More information about the anaconda-patches mailing list