From: Nikos Skalkotos Date: Wed, 7 Mar 2012 13:40:05 +0000 (+0200) Subject: Handle imported commands that aren't in the PATH X-Git-Tag: v0.1~144 X-Git-Url: https://code.grnet.gr/git/snf-image-creator/commitdiff_plain/01a7cff3bae1bac66d8945c810f0eed8b019422d Handle imported commands that aren't in the PATH 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. --- diff --git a/image_creator/disk.py b/image_creator/disk.py index aebddc2..0d5ed48 100644 --- a/image_creator/disk.py +++ b/image_creator/disk.py @@ -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