[PATCH 1/3] Add a guard for testing if we can modify live system

Vratislav Podzimek vpodzime at redhat.com
Fri Sep 21 16:29:47 UTC 2012


Using "if flags.liveCDinstall or flags.imageInstall or flags.testing" over
and over is incovinient, one may forget to involve some of the conditions
and there is no logging of the skipped actions.

This patch adds flags.can_touch_live_system function, that can be used as
a guard and that logs skipped actions. It can also be easily mocked or
masked for the needs of testing.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 pyanaconda/flags.py | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/pyanaconda/flags.py b/pyanaconda/flags.py
index e6d7a67..34130d7 100644
--- a/pyanaconda/flags.py
+++ b/pyanaconda/flags.py
@@ -24,9 +24,13 @@ import glob
 from constants import *
 from collections import OrderedDict
 
+import logging
+log = logging.getLogger("anaconda")
+
 # A lot of effort, but it only allows a limited set of flags to be referenced
 class Flags(object):
     def __setattr__(self, attr, val):
+        # pylint: disable-msg=E1101
         if attr not in self.__dict__ and not self._in_init:
             raise AttributeError, attr
         else:
@@ -167,6 +171,29 @@ class BootArgs(OrderedDict):
                 result = False # XXX: should noarg=off -> True?
         return result
 
+def can_touch_live_system(msg):
+    """
+    Guard that should be used before doing actions that modify live system.
+
+    @param msg: message to be logged in case that live system cannot be touched
+    @rtype: bool
+
+    """
+
+    if flags.livecdInstall:
+        log.info("Not doing '%s' in live installation" % msg)
+        return False
+
+    if flags.imageInstall:
+        log.info("Not doing '%s' in image installation" % msg)
+        return False
+
+    if flags.testing:
+        log.info("Not doing '%s', because we are just testing" % msg)
+        return False
+
+    return True
+
 global flags
 flags = Flags()
 
-- 
1.7.11.4



More information about the anaconda-patches mailing list