[PATCH] Refactor and make functional the getDmDeps and getDmTarget functions.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Moved the common code to find the map into a separate function, getMap,
which both getDmDeps and getDmTarget now use.
---
 __init__.py |   59 ++++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 42 insertions(+), 17 deletions(-)

diff --git a/__init__.py b/__init__.py
index 7c09a1b..03a4802 100644
--- a/__init__.py
+++ b/__init__.py
@@ -201,6 +201,36 @@ def getRaidSets(*disks):
             rsList.append(set)
     return rsList
 
+def getMap(uuid = None, major = None, minor = None, name = None):
+    """ Return a map that matches the given parameters.
+
+    uuid and name are strings.  major and minor are converted to long before
+    being compared.
+
+    major and minor should be specified as a pair -- that is to say one
+    should either give both of them or neither of them.
+
+    Returns None if the map is not found.
+    """
+    # don't bother if there are no specs to search for
+    if uuid is None and major is None and minor is None and name is None:
+        return None
+
+    # Return None if we don't find the map.
+    map = None
+    for _map in dm.maps():
+        if (name is None or \
+            (_map.name is not None and _map.name == name)) and\
+           (uuid is None or \
+            (_map.uuid is not None and _map.uuid == uuid)) and\
+           ((major is None or minor is None) or \
+            (_map.dev.major is not None and _map.dev.minor is not None and \
+             _map.dev.major == long(major) and _map.dev.minor == long(minor))):
+            map = _map
+            break
+
+    return map
+
 def getDmDeps(uuid = None, major = None, minor = None, name = None):
     """ Retrieve the deps for a specified map/device.
 
@@ -211,14 +241,12 @@ def getDmDeps(uuid = None, major = None, minor = None, name = None):
     Returns () when no deps are found for the specified device.
     Returns None when device was not found.
     """
-    # If has dpes, return a set of maps, else return an empty set.
-    for map in dm.maps():
-        if (map.name and map.name == name) or \
-                (map.uuid is not None and map.uuid == uuid) or \
-                (map.dev.minor is not None and map.dev.minor == long(minor)and\
-                 map.dev.major is not None and map.dev.major == long(major)):
-            return map.deps
-    return None
+    map = getMap(uuid=uuid, major=major, minor=minor, name=name)
+    try:
+        deps = map.deps
+    except AttributeError:
+        deps = ()
+    return deps
 
 def getDmTarget(uuid = None, major = None, minor = None, name = None):
     """ Retrieve the target for a specified map/device.
@@ -231,15 +259,12 @@ def getDmTarget(uuid = None, major = None, minor = None, name = None):
 
     Note: None is returned if map.table.type is None.
     """
-    # Return None if we don't find the map.
-    for map in dm.maps():
-        if (map.name and map.name == name) or \
-                (map.uuid is not None and map.uuid == uuid) or \
-                (map.dev.minor is not None and map.dev.minor == long(minor)and\
-                 map.dev.major is not None and map.dev.major == long(major)):
-            # might be worth validating tupe.
-            return map.table.type
-    return None
+    map = getMap(uuid=uuid, major=major, minor=minor, name=name)
+    try:
+        target = map.table.type
+    except AttributeError:
+        target = None
+    return target
 
 def getNameFromDmNode(dm_node):
     """ Return the related name for the specified node.
-- 
1.6.0.6

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list

[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux