[node-patches] Change in ovirt-node[master]: system: Make which() testable

fabiand at fedoraproject.org fabiand at fedoraproject.org
Mon Jul 15 12:15:04 UTC 2013


Fabian Deutsch has uploaded a new change for review.

Change subject: system: Make which() testable
......................................................................

system: Make which() testable

Previously which() used direct FS access, now the File class is used.
Additionally the method was changed to not raise an Exception if a cmd
was not found, instead None is returned.

Change-Id: I37081b4aa34ed3060f1503961d545eb7322bd4b3
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M src/ovirt/node/utils/fs.py
M src/ovirt/node/utils/system.py
2 files changed, 29 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/60/16860/1

diff --git a/src/ovirt/node/utils/fs.py b/src/ovirt/node/utils/fs.py
index 6943202..eb4a58a 100644
--- a/src/ovirt/node/utils/fs.py
+++ b/src/ovirt/node/utils/fs.py
@@ -118,6 +118,11 @@
         """
         return os.unlink(self.filename)
 
+    def access(self, mode):
+        """Check if the file can be accessed
+        """
+        return os.access(self.filename, mode)
+
     def __iter__(self):
         with open(self.filename, "r") as src:
             for line in src:
@@ -196,6 +201,9 @@
             if self.exists():
                 del FakeFs.filemap[self.filename]
 
+        def access(self, mode):
+            return self.filename in FakeFs.filemap
+
         def __iter__(self):
             for line in StringIO.StringIO(self.read()):
                 yield line
diff --git a/src/ovirt/node/utils/system.py b/src/ovirt/node/utils/system.py
index 3bd969d..ead2e68 100644
--- a/src/ovirt/node/utils/system.py
+++ b/src/ovirt/node/utils/system.py
@@ -20,13 +20,14 @@
 # also available at http://www.gnu.org/copyleft/gpl.html.
 from ovirt.node import base, utils
 from ovirt.node.utils import process
-import os
+from ovirt.node.utils.fs import File
 import logging
+import os
 import rpm
-import system_config_keyboard.keyboard
-import time
 import subprocess
 import sys
+import system_config_keyboard.keyboard
+import time
 
 """
 A module to access system wide stuff
@@ -80,18 +81,26 @@
     return os.path.exists("/dev/HostVG")
 
 
-def which(file):
+def which(cmd):
+    """Simulates the behavior of which
+
+    Args:
+        cmd: The cmd to be found in PATH
+
+    Returns:
+        The cmd with the absolute path if it was found in any path given in
+        $PATH. Otherwise None (if not found in any path in $PATHS).
+    """
     ret = None
-    if os.path.abspath(file) and os.path.exists(file):
-        ret = file
+    if os.path.abspath(cmd):
+        if File(cmd).exists():
+            ret = cmd
     else:
-        for dir in os.environ["PATH"].split(":"):
-            f = os.path.join(dir, file)
-            if os.path.exists(f) and os.access(f, os.X_OK):
-                ret = f
+        for dirname in os.environ["PATH"].split(":"):
+            fn = os.path.join(dirname, cmd)
+            if File(fn).exists() and File(fn).access(os.X_OK):
+                ret = fn
                 break
-    if ret is None:
-        raise RuntimeError("Cannot find command '%s'" % file)
     return ret
 
 


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I37081b4aa34ed3060f1503961d545eb7322bd4b3
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