From 62198e59738972dd7d589823d28ec7457dfaa9b8 Mon Sep 17 00:00:00 2001 From: "Martin A. Brown" Date: Thu, 18 Feb 2016 09:13:46 -0800 Subject: [PATCH] in execute, before Popen, ensure cmd[0] has +x rename is_executable() to isexecutable() adjust testing code to match change to execute() --- tests/test_utils.py | 12 ++++++++---- tldp/utils.py | 8 +++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 705e2d9..cca62bd 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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) diff --git a/tldp/utils.py b/tldp/utils.py index 557aaa1..62cc0a9 100644 --- a/tldp/utils.py +++ b/tldp/utils.py @@ -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