aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Jaggars <jjaggars@redhat.com>2012-03-20 13:37:34 -0500
committerJesse Jaggars <jjaggars@redhat.com>2012-03-20 13:37:34 -0500
commit2300c2e0d61ad6a241e82ffbcfcb72822f1f70ee (patch)
treeb0732216bcd649c20b7a8cd5e448709255ba4b04
parent8e1e383fe952c661f1e3feda68b31f82268e65e3 (diff)
downloadsos-2300c2e0d61ad6a241e82ffbcfcb72822f1f70ee.tar.gz
using /usr/bin/timeout if it's there
-rw-r--r--sos/utilities.py9
-rw-r--r--tests/utilities_tests.py9
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):