Unverified Commit 33def535 by Johannes vom Dorp Committed by GitHub

Merge pull request #2 from fkie-cad/add_check_result_option

added check option
parents 42f97ed0 68c5582c
...@@ -3,8 +3,10 @@ python: ...@@ -3,8 +3,10 @@ python:
- "3.5" - "3.5"
- "3.6" - "3.6"
# command to install dependencies # command to install dependencies
install: install:
- "python setup.py -q install" - "pip install git+https://github.com/fkie-cad/common_helper_files.git"
- "pip install ."
- "pip install -U pytest"
- "pip install pytest-pep8 pytest-cov codecov" - "pip install pytest-pep8 pytest-cov codecov"
# command to run tests # command to run tests
script: "pytest --cov-config .coveragerc --cov=./" script: "pytest --cov-config .coveragerc --cov=./"
......
from subprocess import Popen, PIPE, STDOUT, TimeoutExpired
from signal import SIGKILL
import logging import logging
import pexpect from signal import SIGKILL
from subprocess import Popen, PIPE, STDOUT, TimeoutExpired, CalledProcessError
from time import sleep from time import sleep
import pexpect
def execute_shell_command(shell_command, timeout=None): def execute_shell_command(shell_command, timeout=None, check=False):
""" """
Execute a shell command and return STDOUT and STDERR in one combined result string. Execute a shell command and return STDOUT and STDERR in one combined result string.
This function shall not raise any errors This function shall not raise any errors
:param shell_command: command to execute :param shell_command: command to execute
:type shell_command: str :type shell_command: str
:param timeout: kill process after timeout seconds
:type: timeout: int, optional
:param check: raise CalledProcessError if the return code is != 0
:type: check: bool
:return: str :return: str
""" """
return execute_shell_command_get_return_code(shell_command, timeout=timeout)[0] output, return_code = execute_shell_command_get_return_code(shell_command, timeout=timeout)
if check and return_code != 0:
raise CalledProcessError(return_code, shell_command)
return output
def execute_shell_command_get_return_code(shell_command, timeout=None): def execute_shell_command_get_return_code(shell_command, timeout=None):
""" """
Execute a shell command and return a tuple (program output, return code) Execute a shell command and return a tuple (program output, return code)
Program ouput includes STDOUT and STDERR. Program output includes STDOUT and STDERR.
This function shall not raise any errors This function shall not raise any errors
:param shell_command: command to execute :param shell_command: command to execute
:type shell_command: str :type shell_command: str
:param timeout: kill process after timeout seconds :param timeout: kill process after timeout seconds
:type: timeout: int :type: timeout: int, optional
:return: str, int :return: str, int
""" """
output = ""
return_code = 1
pl = Popen(shell_command, shell=True, stdout=PIPE, stderr=STDOUT) pl = Popen(shell_command, shell=True, stdout=PIPE, stderr=STDOUT)
try: try:
output = pl.communicate(timeout=timeout)[0].decode('utf-8', errors='replace') output = pl.communicate(timeout=timeout)[0].decode('utf-8', errors='replace')
...@@ -44,7 +50,7 @@ def execute_shell_command_get_return_code(shell_command, timeout=None): ...@@ -44,7 +50,7 @@ def execute_shell_command_get_return_code(shell_command, timeout=None):
return output, return_code return output, return_code
def execute_interactive_shell_command(shell_command, timeout=60, inputs={}): def execute_interactive_shell_command(shell_command, timeout=60, inputs=None):
""" """
Execute an interactive shell command and return a tuple (program output, return code) Execute an interactive shell command and return a tuple (program output, return code)
This function shall not raise any errors This function shall not raise any errors
...@@ -54,9 +60,11 @@ def execute_interactive_shell_command(shell_command, timeout=60, inputs={}): ...@@ -54,9 +60,11 @@ def execute_interactive_shell_command(shell_command, timeout=60, inputs={}):
:param timeout: kill process after timeout seconds :param timeout: kill process after timeout seconds
:type timeout: int :type timeout: int
:param inputs: dictionary {'EXPECTED_CONSOLE_OUTPUT': 'DESIRED_INPUT'} :param inputs: dictionary {'EXPECTED_CONSOLE_OUTPUT': 'DESIRED_INPUT'}
:type inputs: dict :type inputs: dict, optional
:return: str, int :return: str, int
""" """
if inputs is None:
inputs = {}
trigger, inputs = _parse_inputs(inputs) trigger, inputs = _parse_inputs(inputs)
output = b'' output = b''
child = pexpect.spawn(shell_command) child = pexpect.spawn(shell_command)
......
from setuptools import setup, find_packages from setuptools import setup, find_packages
VERSION = 0.3 VERSION = 0.4
setup( setup(
name="common_helper_process", name="common_helper_process",
......
import os
import unittest import unittest
from subprocess import CalledProcessError
from time import time from time import time
import os
from common_helper_files import get_dir_of_file from common_helper_files import get_dir_of_file
from common_helper_process import execute_shell_command, execute_shell_command_get_return_code, execute_interactive_shell_command from common_helper_process import (
execute_interactive_shell_command, execute_shell_command, execute_shell_command_get_return_code
)
from common_helper_process.fail_safe_subprocess import _parse_inputs from common_helper_process.fail_safe_subprocess import _parse_inputs
...@@ -27,6 +31,12 @@ class TestProcessHelperPublic(unittest.TestCase): ...@@ -27,6 +31,12 @@ class TestProcessHelperPublic(unittest.TestCase):
self.assertEqual(output, 'test 123\n', 'result not correct') self.assertEqual(output, 'test 123\n', 'result not correct')
self.assertEqual(rc, 2, 'return code not correct') self.assertEqual(rc, 2, 'return code not correct')
def test_execute_shell_command_with_check(self):
with self.assertRaises(CalledProcessError):
execute_shell_command("exit 1", check=True)
with self.assertRaises(CalledProcessError):
execute_shell_command("exit 255", check=True)
def test_execute_shell_command_time_out(self): def test_execute_shell_command_time_out(self):
start_time = time() start_time = time()
output, rc = execute_shell_command_get_return_code('echo \'test 123\' && for i in {1..10}; do sleep 1; done', timeout=1) output, rc = execute_shell_command_get_return_code('echo \'test 123\' && for i in {1..10}; do sleep 1; done', timeout=1)
......
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