[node-patches] Change in ovirt-node[master]: utils: Silence some calls

fabiand at fedoraproject.org fabiand at fedoraproject.org
Thu Mar 27 19:53:43 UTC 2014


Fabian Deutsch has uploaded a new change for review.

Change subject: utils: Silence some calls
......................................................................

utils: Silence some calls

Change-Id: I08d600539c33b3baca9b03f7575cd3199f6bda1e
Signed-off-by: Fabian Deutsch <fabiand at redhat.com>
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M src/ovirt/node/utils/__init__.py
M src/ovirt/node/utils/fs.py
M src/ovirt/node/utils/process.py
M src/ovirt/node/utils/system.py
4 files changed, 79 insertions(+), 22 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/81/26181/1

diff --git a/src/ovirt/node/utils/__init__.py b/src/ovirt/node/utils/__init__.py
index 08c8dc7..349dc27 100644
--- a/src/ovirt/node/utils/__init__.py
+++ b/src/ovirt/node/utils/__init__.py
@@ -382,3 +382,30 @@
     """
     return hasattr(filec, "read") \
         and hasattr(filec, "write")
+
+
+def parse_varfile(txt):
+    """Parse a simple shell-var-style lines into a dict:
+
+    >>> import StringIO
+    >>> txt = "# A comment\\n"
+    >>> txt += "A=ah\\n"
+    >>> txt += "B=beh\\n"
+    >>> txt += "C=\\"ceh\\"\\n"
+    >>> txt += "D=\\"more=less\\"\\n"
+    >>> sorted(parse_varfile(txt).items())
+    [('A', 'ah'), ('B', 'beh'), ('C', 'ceh'), ('D', 'more=less')]
+    """
+    cfg = {}
+    for line in txt.split("\n"):
+        line = line.strip()
+        if line == "" or line.startswith("#"):
+            continue
+        try:
+            key, value = line.split("=", 1)
+            cfg[key] = value.strip("\"' \n")
+        except:
+            pass
+            # BAAAAD
+            #raise RuntimeError("Failed to parse line: %s" % line)
+    return cfg
diff --git a/src/ovirt/node/utils/fs.py b/src/ovirt/node/utils/fs.py
index 0cf6385..48d1bab 100644
--- a/src/ovirt/node/utils/fs.py
+++ b/src/ovirt/node/utils/fs.py
@@ -24,7 +24,7 @@
 """
 
 from ovirt.node import log
-from ovirt.node.utils import process
+from ovirt.node.utils import process, parse_varfile
 import shutil
 import os
 import re
@@ -206,6 +206,16 @@
         >>> FakeFs.File("foo").write("bar")
         >>> FakeFs.filemap
         {'foo': 'bar'}
+
+        >>> b = FakeFs.File("foo")
+        >>> b.sub("b(ar)", r"ro\\1")
+        'roar'
+
+        >>> b.findall("oa")
+        ['oa']
+        >>> b.findall("Boa")
+        []
+
         >>> FakeFs.erase()
         >>> FakeFs.filemap
         {}
@@ -486,14 +496,4 @@
         >>> sorted(p._parse_dict(txt).items())
         [('A', 'ah'), ('B', 'beh'), ('C', 'ceh'), ('D', 'more=less')]
         """
-        cfg = {}
-        for line in txt.split("\n"):
-                line = line.strip()
-                if line == "" or line.startswith("#"):
-                    continue
-                try:
-                    key, value = line.split("=", 1)
-                    cfg[key] = value.strip("\"' \n")
-                except:
-                    self.logger.info("Failed to parse line: '%s'" % line)
-        return cfg
+        return parse_varfile(txt)
diff --git a/src/ovirt/node/utils/process.py b/src/ovirt/node/utils/process.py
index 3cee5e5..d5154d8 100644
--- a/src/ovirt/node/utils/process.py
+++ b/src/ovirt/node/utils/process.py
@@ -34,6 +34,8 @@
 
 CalledProcessError = subprocess.CalledProcessError
 
+STDOUT = subprocess.STDOUT
+
 
 def log_call(msg, args=[], kwargs={}, masks=[], logfunc=None):
     logfunc = logfunc or log.getLogger(__name__).debug
@@ -208,8 +210,8 @@
     CalledProcessError: Command 'false' returned non-zero exit status 1
     """
     kwargs.update({"stdin": PIPE,
-                   "stdout": PIPE,
-                   "stderr": STDOUT})
+                   "stderr": STDOUT,
+                   "stdout": PIPE})
     if type(cmd) in [str, unicode]:
         kwargs["shell"] = True
     __check_for_problems(cmd, kwargs)
diff --git a/src/ovirt/node/utils/system.py b/src/ovirt/node/utils/system.py
index 18d66c4..024b222 100644
--- a/src/ovirt/node/utils/system.py
+++ b/src/ovirt/node/utils/system.py
@@ -560,23 +560,51 @@
 
 class Filesystem(base.Base):
     """A class for finding and handling filesystems"""
+    device = None
+
+    def __init__(self, device):
+        self.device = device
+
+    @staticmethod
+    def _flush():
+        """Let all pending operations finish and flush anything to any disks
+        E.g. iscsi etc
+
+        pipe() is used to capture the output of the calls
+        """
+        process.pipe(["partprobe"] + [x for x in glob.glob("/dev/mapper/*")
+                                      if not re.match(r'.*\/control$', x)],
+                     stderr=process.STDOUT, check=False)
+        process.pipe(["udevadm", "settle"],
+                     stderr=process.STDOUT, check=False)
+
     @staticmethod
     def by_label(label):
         """Determines whether a filesystem with a given label is present on
         this system
         """
-        process.call(["partprobe"] + [x for x in glob.glob("/dev/mapper/*")
-                                      if not re.match(r'.*\/control$', x)])
-        process.call(["udevadm", "settle"])
         try:
-            process.check_call(["/sbin/blkid", "-c", "/dev/null", "-l", "-o",
-                                "device", "-t", 'LABEL="%s"' % label])
+            Filesystem._flush()
+            device = process.check_output(["blkid", "-c", "/dev/null",
+                                           "-L", label])
 
-            return True
+            return Filesystem(label, device)
 
         except process.CalledProcessError as e:
-            LOGGER.exception("Failed to resolve disks: %s" % e.cmd)
-            return False
+            self.logger.exception("Failed to resolve disks: %s" % e.cmd)
+            return None
+
+    def _tokens(self):
+        tokens = process.check_output(["blkid", "-o", "export", self.device])
+        return parse_varfile(tokens)
+
+    def label(self):
+        return self._tokens().get("LABEL", None)
+
+    def mountpoints(self):
+        targets = process.check_output(["findmnt", "-o", "target", "-n",
+                                        self.device]).split("\n")
+        return [Mount(t.strip()) for t in targets]
 
 
 class Mount(base.Base):


-- 
To view, visit http://gerrit.ovirt.org/26181
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I08d600539c33b3baca9b03f7575cd3199f6bda1e
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>



More information about the node-patches mailing list