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 <[email protected]> --- 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, 78 insertions(+), 24 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/74/24774/1 diff --git a/src/ovirt/node/utils/__init__.py b/src/ovirt/node/utils/__init__.py index b064901..75bb273 100644 --- a/src/ovirt/node/utils/__init__.py +++ b/src/ovirt/node/utils/__init__.py @@ -314,3 +314,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 d25251d..a3955a6 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 StringIO @@ -141,7 +141,7 @@ Returns: The new value """ - flags |= re.MULTILINE + # flags |= re.MULTILINE # py2.7+ newval = re.sub(pat, repl, self.read(), count) if inplace: self.write(newval) @@ -203,8 +203,14 @@ >>> FakeFs.filemap {'foo': 'bar'} - >>> FakeFs.File("foo").sub("b(ar)", r"ro\\1") + >>> b = FakeFs.File("foo") + >>> b.sub("b(ar)", r"ro\\1") 'roar' + + >>> b.findall("oa") + ['oa'] + >>> b.findall("Boa") + [] >>> FakeFs.erase() >>> FakeFs.filemap @@ -493,14 +499,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 a51f38b..aa2e07b 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 @@ -206,8 +208,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 c75acdb..29e08d5 100644 --- a/src/ovirt/node/utils/system.py +++ b/src/ovirt/node/utils/system.py @@ -21,6 +21,7 @@ from ovirt.node import base, utils, log from ovirt.node.utils import process from ovirt.node.utils.fs import File +from ovirt.node.utils.console import CaptureOutput import glob import os import re @@ -519,23 +520,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/24774 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I08d600539c33b3baca9b03f7575cd3199f6bda1e Gerrit-PatchSet: 1 Gerrit-Project: ovirt-node Gerrit-Branch: node-3.0 Gerrit-Owner: Fabian Deutsch <[email protected]> _______________________________________________ node-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/node-patches
