Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
common_helper_process
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-gitdep
common_helper_process
Commits
33def535
Unverified
Commit
33def535
authored
May 28, 2019
by
Johannes vom Dorp
Committed by
GitHub
May 28, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2 from fkie-cad/add_check_result_option
added check option
parents
42f97ed0
68c5582c
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
35 additions
and
15 deletions
+35
-15
.travis.yml
.travis.yml
+3
-1
fail_safe_subprocess.py
common_helper_process/fail_safe_subprocess.py
+19
-11
setup.py
setup.py
+1
-1
test_fail_safe_subprocess.py
test/test_fail_safe_subprocess.py
+12
-2
No files found.
.travis.yml
View file @
33def535
...
@@ -4,7 +4,9 @@ python:
...
@@ -4,7 +4,9 @@ python:
-
"
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=./"
...
...
common_helper_process/fail_safe_subprocess.py
View file @
33def535
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 ou
t
put 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
)
...
...
setup.py
View file @
33def535
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"
,
...
...
test/test_fail_safe_subprocess.py
View file @
33def535
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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment