diff options
-rw-r--r-- | sos/utilities.py | 9 | ||||
-rw-r--r-- | tests/utilities_tests.py | 9 |
2 files changed, 14 insertions, 4 deletions
diff --git a/sos/utilities.py b/sos/utilities.py index 3b76b999..2657c68d 100644 --- a/sos/utilities.py +++ b/sos/utilities.py @@ -149,6 +149,11 @@ def sosGetCommandOutput(command, timeout=300): cmdfile = command.strip("(").split()[0] if is_executable(cmdfile): + + # use /usr/bin/timeout to implement a timeout + if timeout and is_executable("/usr/bin/timeout"): + command = "/usr/bin/timeout %ds %s" % (timeout, command) + p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, bufsize=-1) stdout, stderr = p.communicate() return (p.returncode, stdout.strip(), 0) @@ -173,9 +178,7 @@ def import_module(module_fqname, superclasses=None): def shell_out(cmd): """Uses subprocess.Popen to make a system call and returns stdout. Does not handle exceptions.""" - p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) - return p.communicate()[0] - + return sosGetCommandOutput(cmd)[1] class Archive(object): diff --git a/tests/utilities_tests.py b/tests/utilities_tests.py index 24bc950b..fd9699fe 100644 --- a/tests/utilities_tests.py +++ b/tests/utilities_tests.py @@ -2,7 +2,7 @@ import os.path import unittest from StringIO import StringIO -from sos.utilities import grep, DirTree, checksum, get_hash_name, is_executable, sosGetCommandOutput, find, tail +from sos.utilities import grep, DirTree, checksum, get_hash_name, is_executable, sosGetCommandOutput, find, tail, shell_out import sos TEST_DIR = os.path.dirname(__file__) @@ -71,6 +71,9 @@ class ExecutableTest(unittest.TestCase): path = os.path.join(TEST_DIR, 'test_exe.py') self.assertTrue(is_executable(path)) + def test_exe_file_abs_path(self): + self.assertTrue(is_executable("/usr/bin/timeout")) + def test_output(self): path = os.path.join(TEST_DIR, 'test_exe.py') ret, out, junk = sosGetCommandOutput(path) @@ -83,6 +86,10 @@ class ExecutableTest(unittest.TestCase): self.assertEquals(ret, 127) self.assertEquals(out, "") + def test_shell_out(self): + path = os.path.join(TEST_DIR, 'test_exe.py') + self.assertEquals("executed", shell_out(path)) + class FindTest(unittest.TestCase): |