BmapHelpers: add program_is_available
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Wed, 29 Jan 2014 08:51:20 +0000 (10:51 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Wed, 29 Jan 2014 15:32:59 +0000 (17:32 +0200)
This patch adds the 'program_is_available()' helper function which checks if an
external program is available in the PATH. Also make TransRead use this helper.

Change-Id: Ied8c8b39e7c382bc57fd47b2120e47c7022b6e23
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptools/BmapHelpers.py
bmaptools/TransRead.py

index 21ae05fbc8eb17489ab8e64f77d4d954c5fb63cc..c09189721f9f4bb0da4806b3906fa82bfd3c6545 100644 (file)
@@ -54,3 +54,18 @@ def get_block_size(file_obj):
     # the FIGETBSZ ioctl (number 2).
     binary_data = ioctl(file_obj, 2, struct.pack('I', 0))
     return struct.unpack('I', binary_data)[0]
+
+def program_is_available(name):
+    """
+    This is a helper function which check if the external program 'name' is
+    available in the system.
+    """
+
+    import os
+
+    for path in os.environ["PATH"].split(os.pathsep):
+        program = os.path.join(path.strip('"'), name)
+        if os.path.isfile(program) and os.access(program, os.X_OK):
+            return True
+
+    return False
index 89f63f9d76edc93790cb38cc65d4078d686dfcd8..6cf1e4b896f8f8d385dc1050b87fe3b00a724cbc 100644 (file)
@@ -20,6 +20,8 @@ import os
 import errno
 import urlparse
 import logging
+import subprocess
+import BmapHelpers
 
 # Disable the following pylint errors and recommendations:
 #   * Instance of X has no member Y (E1101), because it produces
@@ -367,8 +369,6 @@ class TransRead(object):
         support password-based authentication.
         """
 
-        import subprocess
-
         username = parsed_url.username
         password = parsed_url.password
         path = parsed_url.path
@@ -377,13 +377,9 @@ class TransRead(object):
             hostname = username + "@" + hostname
 
         # Make sure the ssh client program is installed
-        try:
-            subprocess.Popen("ssh", stderr=subprocess.PIPE,
-                                    stdout=subprocess.PIPE).wait()
-        except OSError as err:
-            if err.errno == os.errno.ENOENT:
-                raise Error("\"sshpass\" program not found, but it is "
-                            "required for downloading over SSH")
+        if not BmapHelpers.program_is_available("ssh"):
+            raise Error("the \"ssh\" program is not available but it is "
+                        "required for downloading over the ssh protocol")
 
         # Prepare the commands that we are going to run
         if password:
@@ -398,13 +394,9 @@ class TransRead(object):
                           hostname]
 
             # Make sure the sshpass program is installed
-            try:
-                subprocess.Popen("sshpass", stderr=subprocess.PIPE,
-                                            stdout=subprocess.PIPE).wait()
-            except OSError as err:
-                if err.errno == os.errno.ENOENT:
-                    raise Error("\"sshpass\" program not found, but it is "
-                                "required for password SSH authentication")
+            if not BmapHelpers.program_is_available("ssh"):
+                raise Error("the \"sshpass\" program is not available but it "
+                            "is required for password-based SSH authentication")
         else:
             popen_args = ["ssh",
                           "-o StrictHostKeyChecking=no",