Commit 60092602 by Peter Weidenbach

return code variant added

parent a22ebb76
...@@ -10,7 +10,21 @@ def execute_shell_command(shell_command): ...@@ -10,7 +10,21 @@ def execute_shell_command(shell_command):
:type shell_command: str :type shell_command: str
:return: str :return: str
""" """
return execute_shell_command_get_return_code(shell_command)[0]
def execute_shell_command_get_return_code(shell_command):
"""
Execute a shell command and return STDOUT and STDERR in one combined result string.
This function shall not raise any errors
:param shell_command: command to execute
:type shell_command: str
:return: str, int
"""
output = "" output = ""
rc = 1
with Popen(shell_command, shell=True, stdout=PIPE, stderr=STDOUT) as pl: with Popen(shell_command, shell=True, stdout=PIPE, stderr=STDOUT) as pl:
output = pl.communicate()[0].decode('utf-8', errors='replace') output = pl.communicate()[0].decode('utf-8', errors='replace')
return output rc = pl.returncode
return output, rc
from setuptools import setup, find_packages from setuptools import setup, find_packages
VERSION = 0.1 VERSION = 0.2
setup( setup(
name="common_helper_process", name="common_helper_process",
......
import unittest import unittest
from common_helper_process import execute_shell_command from common_helper_process import execute_shell_command
from common_helper_process.fail_safe_subprocess import execute_shell_command_get_return_code
class Test(unittest.TestCase): class TestProcessHelper(unittest.TestCase):
def test_execute_shell_command(self): def test_execute_shell_command(self):
result = execute_shell_command("echo 'test 123'") result = execute_shell_command("echo 'test 123'")
...@@ -12,3 +13,13 @@ class Test(unittest.TestCase): ...@@ -12,3 +13,13 @@ class Test(unittest.TestCase):
def test_execute_shell_command_error(self): def test_execute_shell_command_error(self):
result = execute_shell_command("echo 'test 123' 1>&2 && exit 2") result = execute_shell_command("echo 'test 123' 1>&2 && exit 2")
self.assertEqual(result, 'test 123\n', 'result not correct') self.assertEqual(result, 'test 123\n', 'result not correct')
def test_execute_shell_command_incl_rc(self):
output, rc = execute_shell_command_get_return_code("echo 'test 123'")
self.assertEqual(output, 'test 123\n', 'result not correct')
self.assertEqual(rc, 0, 'return code not correct')
def test_execute_shell_command_error_incl_rc(self):
output, rc = execute_shell_command_get_return_code("echo 'test 123' 1>&2 && exit 2")
self.assertEqual(output, 'test 123\n', 'result not correct')
self.assertEqual(rc, 2, 'return code not correct')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment