terminal: Run command using a wrapper script
authorTyler Hall <tylerwhall@gmail.com>
Sun, 30 Jun 2013 20:16:19 +0000 (16:16 -0400)
committerRichard Purdie <richard.purdie@linuxfoundation.org>
Tue, 2 Jul 2013 21:23:51 +0000 (22:23 +0100)
Some terminals may not pass the environment into the child process. This
is true when using "tmux split-window." If tmux is already running, it
will start the command with the tmux session environment, ignoring the
environment where the command was issued.

This could possibly be worked around when launching tmux by injecting
variables into the user's session environment or adding the variables to
the "update-environment" tmux setting. However, both methods would
permanently alter the user's session, which is undesirable.

By using a wrapper script, we have full control over the final
environment. Replace the env dictionary with an empty data smart that
will contain the exported variables and a wrapper function that execs
the original command.

(From OE-Core rev: 3bb96671e987ce8110ce98b9f6d9efc093f8d20e)

Signed-off-by: Tyler Hall <tylerwhall@gmail.com>
Signed-off-by: Saul Wold <sgw@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
meta/classes/terminal.bbclass

index 8cebad4..ae338e9 100644 (file)
@@ -11,43 +11,66 @@ XAUTHORITY ?= "${HOME}/.Xauthority"
 SHELL ?= "bash"
 
 
+def emit_terminal_func(command, envdata, d):
+    cmd_func = 'do_terminal'
+
+    envdata.setVar(cmd_func, 'exec ' + command)
+    envdata.setVarFlag(cmd_func, 'func', 1)
+
+    runfmt = d.getVar('BB_RUNFMT', True) or "run.{func}.{pid}"
+    runfile = runfmt.format(func=cmd_func, pid=os.getpid())
+    runfile = os.path.join(d.getVar('T', True), runfile)
+    with open(runfile, 'w') as script:
+        script.write('#!/bin/sh -e\n')
+        bb.data.emit_func(cmd_func, script, envdata)
+        script.write(cmd_func)
+        script.write("\n")
+    os.chmod(runfile, 0755)
+
+    return runfile
+
 def oe_terminal(command, title, d):
     import oe.data
     import oe.terminal
 
-    env = dict()
+    envdata = bb.data.init()
 
     for v in os.environ:
-        env[v] = os.environ[v]
+        envdata.setVar(v, os.environ[v])
+        envdata.setVarFlag(v, 'export', 1)
 
     for export in oe.data.typed_value('OE_TERMINAL_EXPORTS', d):
         value = d.getVar(export, True)
         if value is not None:
             os.environ[export] = str(value)
-            env[export] = str(value)
+            envdata.setVar(export, str(value))
+            envdata.setVarFlag(export, 'export', 1)
         if export == "PSEUDO_DISABLED":
             if "PSEUDO_UNLOAD" in os.environ:
                 del os.environ["PSEUDO_UNLOAD"]
-            if "PSEUDO_UNLOAD" in env:
-                del env["PSEUDO_UNLOAD"]
+            envdata.delVar("PSEUDO_UNLOAD")
 
     # Add in all variables from the user's original environment which
     # haven't subsequntly been set/changed
     origbbenv = d.getVar("BB_ORIGENV", False) or {}
     for key in origbbenv:
-        if key in env:
+        if key in envdata:
             continue
         value = origbbenv.getVar(key, True)
         if value is not None:
             os.environ[key] = str(value)
-            env[key] = str(value)
+            envdata.setVar(key, str(value))
+            envdata.setVarFlag(key, 'export', 1)
+
+    # Replace command with an executable wrapper script
+    command = emit_terminal_func(command, envdata, d)
 
     terminal = oe.data.typed_value('OE_TERMINAL', d).lower()
     if terminal == 'none':
         bb.fatal('Devshell usage disabled with OE_TERMINAL')
     elif terminal != 'auto':
         try:
-            oe.terminal.spawn(terminal, command, title, env, d)
+            oe.terminal.spawn(terminal, command, title, None, d)
             return
         except oe.terminal.UnsupportedTerminal:
             bb.warn('Unsupported terminal "%s", defaulting to "auto"' %
@@ -56,7 +79,7 @@ def oe_terminal(command, title, d):
             bb.fatal('Unable to spawn terminal %s: %s' % (terminal, exc))
 
     try:
-        oe.terminal.spawn_preferred(command, title, env, d)
+        oe.terminal.spawn_preferred(command, title, None, d)
     except oe.terminal.NoSupportedTerminals:
         bb.fatal('No valid terminal found, unable to open devshell')
     except oe.terminal.ExecutionError as exc: