aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Jaggars <jhjaggars@gmail.com>2012-03-21 07:04:59 -0700
committerJesse Jaggars <jhjaggars@gmail.com>2012-03-21 07:04:59 -0700
commit3a09cb48bc8781f4d8693debec33e8c304096e08 (patch)
tree7c8f5e3b21f3af87f5b66f21cb7d612253b2a799
parent79b22b9dcca60f5d2b8d455d226a2bdb30c15fe2 (diff)
parent550a9574348fcf09099610ba1501b17cb7477f0d (diff)
downloadsos-3a09cb48bc8781f4d8693debec33e8c304096e08.tar.gz
Merge pull request #54 from jhjaggars/timeouts
Timeouts
-rw-r--r--sos/plugins/__init__.py5
-rw-r--r--sos/utilities.py9
-rw-r--r--tests/utilities_tests.py9
3 files changed, 16 insertions, 7 deletions
diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py
index f06c1fba..500f813f 100644
--- a/sos/plugins/__init__.py
+++ b/sos/plugins/__init__.py
@@ -403,13 +403,12 @@ class Plugin(object):
if filespec not in self.copyPaths:
self.copyPaths.append((filespec, sub))
- def callExtProg(self, prog):
+ def callExtProg(self, prog, timeout=300):
"""Execute a command independantly of the output gathering part of
sosreport.
"""
# pylint: disable-msg = W0612
- status, shout, runtime = sosGetCommandOutput(prog)
- return (status, shout, runtime)
+ return sosGetCommandOutput(prog, timeout)
def checkExtprog(self, prog):
"""Execute a command independently of the output gathering part of
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):