in execute, before Popen, ensure cmd[0] has +x

rename is_executable() to isexecutable()
adjust testing code to match change to execute()
This commit is contained in:
Martin A. Brown 2016-02-18 09:13:46 -08:00
parent 2550047d23
commit 62198e5973
2 changed files with 13 additions and 7 deletions

View File

@ -16,23 +16,27 @@ from tldp.utils import statfiles
class Test_execute(TestToolsFilesystem):
def test_execute_returns_zero(self):
result = execute(['true'], logdir=self.tempdir)
exe = which ('true')
result = execute([exe], logdir=self.tempdir)
self.assertEqual(0, result)
def test_execute_returns_nonzero(self):
result = execute(['false'], logdir=self.tempdir)
exe = which ('false')
result = execute([exe], logdir=self.tempdir)
self.assertEqual(1, result)
def test_execute_exception_when_logdir_none(self):
exe = which ('true')
with self.assertRaises(Exception) as ecm:
execute(['true'], logdir=None)
execute([exe], logdir=None)
e = ecm.exception
self.assertTrue('Missing' in e.message)
def test_execute_exception_when_logdir_enoent(self):
exe = which ('true')
logdir = os.path.join(self.tempdir, 'nonexistent-directory')
with self.assertRaises(IOError) as ecm:
execute(['true'], logdir=logdir)
execute([exe], logdir=logdir)
e = ecm.exception
self.assertTrue('nonexistent' in e.filename)

View File

@ -28,6 +28,8 @@ def execute(cmd, stdin=None, stdout=None, stderr=None,
logdir=None, env=os.environ):
prefix = os.path.basename(cmd[0]) + '.' + str(os.getpid()) + '-'
assert isexecutable(cmd[0])
if logdir is None:
raise Exception("Missing required parameter: logdir.")
@ -53,7 +55,7 @@ def execute(cmd, stdin=None, stdout=None, stderr=None,
return result
def is_executable(fpath):
def isexecutable(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
@ -62,13 +64,13 @@ def which(program):
http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python/377028#377028
'''
fpath, fname = os.path.split(program)
if fpath and is_executable(program):
if fpath and isexecutable(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
sut = os.path.join(path, program)
if is_executable(sut):
if isexecutable(sut):
return sut
return None