Handle imported commands that aren't in the PATH
authorNikos Skalkotos <skalkoto@grnet.gr>
Wed, 7 Mar 2012 13:40:05 +0000 (15:40 +0200)
committerNikos Skalkotos <skalkoto@grnet.gr>
Wed, 7 Mar 2012 15:45:35 +0000 (17:45 +0200)
This fixes cases where a normal user tries to execute the program and
the program raises pbs.CommandNotFound exceptions because it tries to
import a command from sbin, which is not in the path.

image_creator/disk.py

index aebddc2..0d5ed48 100644 (file)
@@ -9,8 +9,7 @@ import re
 import sys
 import guestfs
 
-from pbs import dmsetup
-from pbs import blockdev
+import pbs
 from pbs import dd
 
 
@@ -18,6 +17,26 @@ class DiskError(Exception):
     pass
 
 
+def find_sbin_command(command, exception):
+    search_paths = ['/usr/local/sbin', '/usr/sbin', '/sbin']
+    for fullpath in map(lambda x: "%s/%s" % (x, command), search_paths):
+        if os.path.exists(fullpath) and os.access(fullpath, os.X_OK):
+            return pbs.Command(fullpath)
+        continue
+    raise exception
+
+
+try:
+    from pbs import dmsetup
+except pbs.CommandNotFound as e:
+    dmsetup = find_sbin_command('dmsetup', e)
+
+try:
+    from pbs import blockdev
+except pbs.CommandNotFound as e:
+    blockdev = find_sbin_command('blockdev', e)
+
+
 class Disk(object):
     """This class represents a hard disk hosting an Operating System