Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Trevor Cappallo
OperationalLogging
Commits
65e7bcd0
Commit
65e7bcd0
authored
Aug 17, 2015
by
Trevor Cappallo
Browse files
add options to log_shell for check_call and check_output
parent
f2744d9f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
5 deletions
+17
-5
lib/OperationalLogging.py
lib/OperationalLogging.py
+17
-5
No files found.
lib/OperationalLogging.py
View file @
65e7bcd0
...
...
@@ -232,8 +232,14 @@ def long_debug(message):
long_log
(
logging
.
DEBUG
,
message
)
def
log_shell
(
arg_list
,
**
kwargs
):
"""Pass arg_list to a subprocess call and log all output."""
def
log_shell
(
arg_list
,
raise_err
=
False
,
return_output
=
False
,
**
kwargs
):
"""Pass arg_list to a subprocess call and log all output.
raise_err: (like subprocess.check_call) if True,
raises a RuntimeError if exit code is non-zero
return_output: (like subprocess.check_output) returns any output
from shell call instead of the exit code
"""
logging
.
info
(
'Starting child process of ppid={}'
.
format
(
os
.
getppid
()))
logging
.
info
(
'Executing: {}'
.
format
(
' '
.
join
(
arg_list
)))
try
:
...
...
@@ -243,21 +249,27 @@ def log_shell(arg_list, **kwargs):
parent_name
,
line_num
=
os
.
path
.
basename
(
f
[
1
]),
f
[
2
]
break
proc_output
=
""
proc
=
subprocess
.
Popen
(
arg_list
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
STDOUT
,
**
kwargs
)
for
line
in
iter
(
proc
.
stdout
.
readline
,
b
''
):
if
not
line
:
break
proc_output
+=
line
logging
.
getLogger
(
'_sub'
).
info
(
line
.
rstrip
(),
extra
=
{
'parentname'
:
parent_name
,
'parentline'
:
line_num
})
return_code
=
proc
.
wait
()
if
return_code
:
logging
.
warning
(
'{} failed with exit code {}'
.
format
(
' '
.
join
(
arg_list
),
return_code
))
return
return_code
if
raise_err
:
logging
.
error
(
'{} failed with exit code {}'
.
format
(
' '
.
join
(
arg_list
),
return_code
))
raise
RuntimeError
(
'{} failed with exit code {}'
.
format
(
' '
.
join
(
arg_list
),
return_code
))
else
:
logging
.
warning
(
'{} failed with exit code {}'
.
format
(
' '
.
join
(
arg_list
),
return_code
))
return
proc_output
if
return_output
else
return_code
except
OSError
as
err
:
logging
.
error
(
'Error while executing %s: %s'
,
' '
.
join
(
arg_list
),
str
(
err
))
return
err
else
:
logging
.
info
(
'Successfully finished: %s'
,
' '
.
join
(
arg_list
))
return
0
return
proc_output
if
return_output
else
return_code
def
stack_trace
(
log_level
=
logging
.
ERROR
):
...
...
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