]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Send 50% less debug information when executing cmd
authorJohn Schwarz <jschwarz@redhat.com>
Thu, 12 Nov 2015 14:05:19 +0000 (16:05 +0200)
committerJohn Schwarz <jschwarz@redhat.com>
Thu, 19 Nov 2015 11:31:55 +0000 (13:31 +0200)
The current code prints out the command it runs twice: once in
'create_process' or 'execute_rootwrap_daemon', and once after the
command has finished executing and a return code is returned. Since this
is synchronous, there is no need to print the command twice in a row, so
this patch deletes the latter one, leaving only the exit code to be
printed.

This patch also refactors the surrounding code to make it more readable.

Change-Id: I22289f838dbc3956a777a78c882e4d5f1e725d0e

neutron/agent/linux/utils.py

index 2148c73e17ddb9e70186b06677c9bf9689f4980c..c291f987df30ff3c668e98710cdb9260a0a47e82 100644 (file)
@@ -125,32 +125,24 @@ def execute(cmd, process_input=None, addl_env=None,
             if isinstance(_stderr, bytes):
                 _stderr = _stderr.decode('utf-8', 'surrogateescape')
 
-        command_str = {
-            'cmd': cmd,
-            'code': returncode
-        }
-        m = _("\nCommand: %(cmd)s"
-              "\nExit code: %(code)d\n") % command_str
-
         extra_ok_codes = extra_ok_codes or []
-        if returncode and returncode in extra_ok_codes:
-            returncode = None
-
-        if returncode and log_fail_as_error:
-            command_str['stdin'] = process_input or ''
-            command_str['stdout'] = _stdout
-            command_str['stderr'] = _stderr
-            m += _("Stdin: %(stdin)s\n"
-                  "Stdout: %(stdout)s\n"
-                  "Stderr: %(stderr)s") % command_str
-            log_msg = m.strip().replace('\n', '; ')
-            LOG.error(log_msg)
+        if returncode and returncode not in extra_ok_codes:
+            msg = _("Exit code: %(returncode)d; "
+                    "Stdin: %(stdin)s; "
+                    "Stdout: %(stdout)s; "
+                    "Stderr: %(stderr)s") % {
+                        'returncode': returncode,
+                        'stdin': process_input or '',
+                        'stdout': _stdout,
+                        'stderr': _stderr}
+
+            if log_fail_as_error:
+                LOG.error(msg)
+            if check_exit_code:
+                raise RuntimeError(msg)
         else:
-            log_msg = m.strip().replace('\n', '; ')
-            LOG.debug(log_msg)
+            LOG.debug("Exit code: %d", returncode)
 
-        if returncode and check_exit_code:
-            raise RuntimeError(m)
     finally:
         # NOTE(termie): this appears to be necessary to let the subprocess
         #               call clean something up in between calls, without