Commit a22ebb76 by Peter Weidenbach

intial version

parent 152df403
......@@ -99,3 +99,7 @@ ENV/
# mypy
.mypy_cache/
# pydev
.project
.pydevproject
from .fail_safe_subprocess import execute_shell_command
__all__ = [
'execute_shell_command',
]
from subprocess import Popen, PIPE, STDOUT
def execute_shell_command(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
"""
output = ""
with Popen(shell_command, shell=True, stdout=PIPE, stderr=STDOUT) as pl:
output = pl.communicate()[0].decode('utf-8', errors='replace')
return output
from setuptools import setup, find_packages
VERSION = 0.1
setup(
name="common_helper_process",
version=VERSION,
packages=find_packages(),
description="Helper functions for handling processes.",
author="Fraunhofer FKIE",
author_email="peter.weidenbach@fkie.fraunhofer.de",
url="http://www.fkie.fraunhofer.de",
license="GPL-3.0"
)
import unittest
from common_helper_process import execute_shell_command
class Test(unittest.TestCase):
def test_execute_shell_command(self):
result = execute_shell_command("echo 'test 123'")
self.assertEqual(result, 'test 123\n', 'result not correct')
def test_execute_shell_command_error(self):
result = execute_shell_command("echo 'test 123' 1>&2 && exit 2")
self.assertEqual(result, 'test 123\n', 'result 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