Patchwork [bitbake-devel] build.py: Provide useful diagnostics when exiting.

login
register
mail settings
Submitter Mark Hatle
Date May 29, 2013, 4 a.m.
Message ID <1369800010-1541-1-git-send-email-mark.hatle@windriver.com>
Download mbox | patch
Permalink /patch/50659/
State New
Headers show

Comments

Mark Hatle - May 29, 2013, 4 a.m.
From: Peter Seebach <peter.seebach@windriver.com>

Running scripts with 'set -e' produces silent failures with no
diagnostic. Add an exit handler which produces diagnostics, including
details of what was running if the shell seems to be bash.

Signed-off-by: Peter Seebach <peter.seebach@windriver.com>
Signed-off-by: Randy MacLeod <Randy.MacLeod@windriver.com>
---
 lib/bb/build.py | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

Patch

diff --git a/lib/bb/build.py b/lib/bb/build.py
index 725eb02..dae6d92 100644
--- a/lib/bb/build.py
+++ b/lib/bb/build.py
@@ -254,7 +254,23 @@  def exec_func_shell(func, d, runfile, cwd=None):
     d.delVarFlag('PWD', 'export')
 
     with open(runfile, 'w') as script:
-        script.write('#!/bin/sh -e\n')
+        script.write('''#!/bin/sh\n
+# Emit a useful diagnostic if something fails:
+bb_exit_handler() {
+    ret=$?
+    case $ret in
+    0)  ;;
+    *)  case $BASH_VERSION in
+        "")   echo "WARNING: exit code $ret from a shell command.";;
+        *)    echo "WARNING: ${BASH_SOURCE[0]}:${BASH_LINENO[1]} exit $ret from
+  \"$BASH_COMMAND\"";;
+        esac
+        exit $ret
+    esac
+}
+trap 'bb_exit_handler' 0
+set -e
+''')
         data.emit_func(func, script, d)
 
         if bb.msg.loggerVerboseLogs:
@@ -262,6 +278,12 @@  def exec_func_shell(func, d, runfile, cwd=None):
         if cwd:
             script.write("cd %s\n" % cwd)
         script.write("%s\n" % func)
+        script.write('''
+# cleanup
+ret=$?
+trap '' 0
+exit $?
+''')
 
     os.chmod(runfile, 0775)