[blivet:rhel7/master 08/16] Make an additional class for labeling abstractions (#1038590)

mulhern amulhern at redhat.com
Thu Jan 16 16:10:09 UTC 2014


Related: rhbz#1038590

Many filesystems can be labeled at creation time. Want to distinguish
abstract notion of filesystem labeling from concrete notion of a filesystem
labeling application that can label after the fact.

So, refactor abstraction into a separate class, which contains labeling
application class as a field.

The labeling applications and the filesystem creation apps seem to respect
the same label formatting so labelFormatOK goes in fslabeling.

The behavior hasn't really changed yet, except that some calls are forwarded.
All existing tests are passing.

Signed-off-by: mulhern <amulhern at redhat.com>
---
 blivet/formats/fs.py            | 30 ++++++-------
 blivet/formats/fslabel.py       | 25 -----------
 blivet/formats/fslabeling.py    | 96 +++++++++++++++++++++++++++++++++++++++++
 tests/formats_test/misc_test.py |  6 +--
 4 files changed, 114 insertions(+), 43 deletions(-)
 create mode 100644 blivet/formats/fslabeling.py

diff --git a/blivet/formats/fs.py b/blivet/formats/fs.py
index 7c818a5..1090828 100644
--- a/blivet/formats/fs.py
+++ b/blivet/formats/fs.py
@@ -28,7 +28,7 @@ import sys
 import tempfile
 import selinux
 
-from . import fslabel
+from . import fslabeling
 from ..errors import *
 from . import DeviceFormat, register_device_format
 from .. import util
@@ -79,7 +79,7 @@ class FS(DeviceFormat):
     _mkfs = ""                           # mkfs utility
     _modules = []                        # kernel modules required for support
     _resizefs = ""                       # resize utility
-    _labelfs = None                      # labeling utility
+    _labelfs = None                      # labeling functionality
     _fsck = ""                           # fs check utility
     _fsckErrors = {}                     # fs check command error codes & msgs
     _infofs = ""                         # fs info utility
@@ -617,10 +617,10 @@ class FS(DeviceFormat):
         if not os.path.exists(self.device):
             raise FSError("device does not exist")
 
-        if not self._labelfs or not self._labelfs.reads:
+        if not self._labelfs or not self._labelfs.labelApp or not self._labelfs.labelApp.reads:
             raise FSError("no application to read label for filesystem %s" % self.type)
 
-        (rc, out) = util.run_program_and_capture_output(self._labelfs.readLabelCommand(self))
+        (rc, out) = util.run_program_and_capture_output(self._labelfs.labelApp.readLabelCommand(self))
         if rc:
             raise FSError("read label failed")
 
@@ -629,7 +629,7 @@ class FS(DeviceFormat):
         if label == "":
             return None
         else:
-            label = self._labelfs.extractLabel(label)
+            label = self._labelfs.labelApp.extractLabel(label)
             if label == "":
                 return None
             else:
@@ -647,16 +647,16 @@ class FS(DeviceFormat):
         if not self.exists:
             raise FSError("filesystem has not been created")
 
-        if not self._labelfs:
+        if not self._labelfs or not self._labelfs.labelApp:
             raise FSError("no application to set label for filesystem %s" % self.type)
 
         if self.label and not self.labelFormatOK(self.label):
-            raise FSError("bad label format for labelling application %s" % self._labelfs.name)
+            raise FSError("bad label format for labelling application %s" % self._labelfs.labelApp.name)
 
         if not os.path.exists(self.device):
             raise FSError("device does not exist")
 
-        rc = util.run_program(self._labelfs.setLabelCommand(self))
+        rc = util.run_program(self._labelfs.labelApp.setLabelCommand(self))
         if rc:
             raise FSError("label failed")
 
@@ -694,8 +694,8 @@ class FS(DeviceFormat):
 
             May be None if no such program exists.
         """
-        if self._labelfs:
-            return self._labelfs.name
+        if self._labelfs and self._labelfs.labelApp:
+            return self._labelfs.labelApp.name
         else:
             return None
 
@@ -828,7 +828,7 @@ class Ext2FS(FS):
     _mkfs = "mke2fs"
     _modules = ["ext2"]
     _resizefs = "resize2fs"
-    _labelfs = fslabel.E2Label()
+    _labelfs = fslabeling.Ext2FSLabeling()
     _fsck = "e2fsck"
     _fsckErrors = {4: _("File system errors left uncorrected."),
                    8: _("Operational error."),
@@ -991,7 +991,7 @@ class FATFS(FS):
     _type = "vfat"
     _mkfs = "mkdosfs"
     _modules = ["vfat"]
-    _labelfs = fslabel.DosFsLabel()
+    _labelfs = fslabeling.FATFSLabeling()
     _fsck = "dosfsck"
     _fsckErrors = {1: _("Recoverable errors have been detected or dosfsck has "
                         "discovered an internal inconsistency."),
@@ -1121,7 +1121,7 @@ class JFS(FS):
     _type = "jfs"
     _mkfs = "mkfs.jfs"
     _modules = ["jfs"]
-    _labelfs = fslabel.JFSTune()
+    _labelfs = fslabeling.JFSLabeling()
     _defaultFormatOptions = ["-q"]
     _maxLabelChars = 16
     _maxSize = 8 * 1024 * 1024
@@ -1152,7 +1152,7 @@ class ReiserFS(FS):
     _type = "reiserfs"
     _mkfs = "mkreiserfs"
     _resizefs = "resize_reiserfs"
-    _labelfs = fslabel.ReiserFSTune()
+    _labelfs = fslabeling.ReiserFSLabeling()
     _modules = ["reiserfs"]
     _defaultFormatOptions = ["-f", "-f"]
     _maxLabelChars = 16
@@ -1190,7 +1190,7 @@ class XFS(FS):
     _type = "xfs"
     _mkfs = "mkfs.xfs"
     _modules = ["xfs"]
-    _labelfs = fslabel.XFSAdmin()
+    _labelfs = fslabeling.XFSLabeling()
     _defaultFormatOptions = ["-f"]
     _maxLabelChars = 16
     _maxSize = 16 * 1024 * 1024 * 1024 * 1024
diff --git a/blivet/formats/fslabel.py b/blivet/formats/fslabel.py
index 6b0b48c..3b5f5d0 100644
--- a/blivet/formats/fslabel.py
+++ b/blivet/formats/fslabel.py
@@ -46,16 +46,6 @@ class FSLabelApp(object):
         raise NotImplementedError
 
     @abc.abstractmethod
-    def labelFormatOK(self, label):
-        """Returns True if this label is correctly formatted for this
-           filesystem labeling application, otherwise False.
-
-           :param str label: the label for this filesystem
-           :rtype: bool
-        """
-        raise NotImplementedError
-
-    @abc.abstractmethod
     def _writeLabelArgs(self, fs):
         """Returns a list of the arguments for writing a label.
 
@@ -132,9 +122,6 @@ class E2Label(FSLabelApp):
         else:
             return [fs.device, ""]
 
-    def labelFormatOK(self, label):
-        return len(label) < 17
-
     def _readLabelArgs(self, fs):
         return [fs.device]
 
@@ -158,9 +145,6 @@ class DosFsLabel(FSLabelApp):
         else:
             return [fs.device, ""]
 
-    def labelFormatOK(self, label):
-        return len(label) < 12
-
     def _readLabelArgs(sefl, fs):
         return [fs.device]
 
@@ -184,9 +168,6 @@ class JFSTune(FSLabelApp):
         else:
             return ["-L", "", fs.device]
 
-    def labelFormatOK(self, label):
-        return len(label) < 17
-
     def _readLabelArgs(sefl, fs):
         raise NotImplementedError
 
@@ -210,9 +191,6 @@ class ReiserFSTune(FSLabelApp):
         else:
             return ["-l", "", fs.device]
 
-    def labelFormatOK(self, label):
-        return len(label) < 17
-
     def _readLabelArgs(self, fs):
         raise NotImplementedError
 
@@ -236,9 +214,6 @@ class XFSAdmin(FSLabelApp):
         else:
             return ["-L", "--", fs.device]
 
-    def labelFormatOK(self, label):
-        return ' ' not in label and len(label) < 13
-
     def _readLabelArgs(sefl, fs):
         return ["-l", fs.device]
 
diff --git a/blivet/formats/fslabeling.py b/blivet/formats/fslabeling.py
new file mode 100644
index 0000000..4b67f4c
--- /dev/null
+++ b/blivet/formats/fslabeling.py
@@ -0,0 +1,96 @@
+# fslabeling.py
+# Filesystem labeling classes for anaconda's storage configuration module.
+#
+# Copyright (C) 2014  Red Hat, Inc.
+#
+# This copyrighted material is made available to anyone wishing to use,
+# modify, copy, or redistribute it subject to the terms and conditions of
+# the GNU General Public License v.2, or (at your option) any later version.
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY expressed or implied, including the implied warranties of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+# Public License for more details.  You should have received a copy of the
+# GNU General Public License along with this program; if not, write to the
+# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
+# source code or documentation are not subject to the GNU General Public
+# License and may only be used or replicated with the express permission of
+# Red Hat, Inc.
+#
+# Red Hat Author(s): Anne Mulhern <amulhern at redhat.com>
+
+import abc
+
+import fslabel
+
+class FSLabeling(object):
+    """An abstract class that represents filesystem labeling actions.
+    """
+
+    __metaclass__ = abc.ABCMeta
+
+    @abc.abstractproperty
+    def labelApp(self):
+        """Application that may be used to label filesystem after the
+           filesystem has been created.
+
+           :rtype: object or None
+           :return: the object representing the labeling application or None
+           if no such object exists
+        """
+        raise NotImplementedError
+
+    @abc.abstractmethod
+    def labelFormatOK(self, label):
+        """Returns True if this label is correctly formatted for this
+           filesystem, otherwise False.
+
+           :param str label: the label for this filesystem
+           :rtype: bool
+        """
+        raise NotImplementedError
+
+class Ext2FSLabeling(FSLabeling):
+
+    @property
+    def labelApp(self):
+        return fslabel.E2Label()
+
+    def labelFormatOK(self, label):
+        return len(label) < 17
+
+class FATFSLabeling(FSLabeling):
+
+    @property
+    def labelApp(self):
+        return fslabel.DosFsLabel()
+
+    def labelFormatOK(self, label):
+        return len(label) < 12
+
+class JFSLabeling(FSLabeling):
+
+    @property
+    def labelApp(self):
+        return fslabel.JFSTune()
+
+    def labelFormatOK(self, label):
+        return len(label) < 17
+
+class ReiserFSLabeling(FSLabeling):
+
+    @property
+    def labelApp(self):
+        return fslabel.ReiserFSTune()
+
+    def labelFormatOK(self, label):
+        return len(label) < 17
+
+class XFSLabeling(FSLabeling):
+
+    @property
+    def labelApp(self):
+        return fslabel.XFSAdmin()
+
+    def labelFormatOK(self, label):
+        return ' ' not in label and len(label) < 13
diff --git a/tests/formats_test/misc_test.py b/tests/formats_test/misc_test.py
index 808736e..d282321 100755
--- a/tests/formats_test/misc_test.py
+++ b/tests/formats_test/misc_test.py
@@ -54,18 +54,18 @@ class MethodsTestCase(unittest.TestCase):
 
         # ReiserFS uses a -l flag
         reiserfs = self.fs["reiserfs"]
-        self.assertEqual(reiserfs._labelfs.setLabelCommand(reiserfs),
+        self.assertEqual(reiserfs._labelfs.labelApp.setLabelCommand(reiserfs),
            ["reiserfstune", "-l", "myfs", "/dev"], msg="reiserfs")
 
         # JFS, XFS use a -L flag
         lflag_classes = [fs.JFS, fs.XFS]
         for k, v in [(k, v) for k, v in self.fs.items() if any(isinstance(v, c) for c in lflag_classes)]:
-            self.assertEqual(v._labelfs.setLabelCommand(v), [v._labelfs.name, "-L", "myfs", "/dev"], msg=k)
+            self.assertEqual(v._labelfs.labelApp.setLabelCommand(v), [v._labelfs.labelApp.name, "-L", "myfs", "/dev"], msg=k)
 
         # Ext2FS and descendants and FATFS do not use a flag
         noflag_classes = [fs.Ext2FS, fs.FATFS]
         for k, v in [(k, v) for k, v in self.fs.items() if any(isinstance(v, c) for c in noflag_classes)]:
-            self.assertEqual(v._labelfs.setLabelCommand(v), [v._labelfs.name, "/dev", "myfs"], msg=k)
+            self.assertEqual(v._labelfs.labelApp.setLabelCommand(v), [v._labelfs.labelApp.name, "/dev", "myfs"], msg=k)
 
         # all of the remaining are non-labeling so will accept any label
         label = "Houston, we have a problem!"
-- 
1.8.3.1



More information about the anaconda-patches mailing list