From abf5c3a5dc0477a366a4edb3aff636d0b3af86e3 Mon Sep 17 00:00:00 2001 From: "ahrenholz@gmail.com" Date: Thu, 12 Sep 2013 19:07:41 +0000 Subject: [PATCH] (Boeing r1774) added which() function to search full PATH for executable; use it in checkexec() --- trunk/daemon/core/misc/utils.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/trunk/daemon/core/misc/utils.py b/trunk/daemon/core/misc/utils.py index 634a0425..52b18c45 100644 --- a/trunk/daemon/core/misc/utils.py +++ b/trunk/daemon/core/misc/utils.py @@ -15,11 +15,28 @@ import subprocess, os, ast def checkexec(execlist): for bin in execlist: - # note that os.access() uses real uid/gid; that should be okay - # here - if not os.access(bin, os.X_OK): + if which(bin) is None: raise EnvironmentError, "executable not found: %s" % bin +def which(program): + ''' From: http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python + ''' + def is_exe(fpath): + return os.path.isfile(fpath) and os.access(fpath, os.X_OK) + + fpath, fname = os.path.split(program) + if fpath: + if is_exe(program): + return program + else: + for path in os.environ["PATH"].split(os.pathsep): + path = path.strip('"') + exe_file = os.path.join(path, program) + if is_exe(exe_file): + return exe_file + + return None + def ensurepath(pathlist): searchpath = os.environ["PATH"].split(":") for p in set(pathlist):