Skip to contentSkip to conversation

Add ability to auto spawn screen on a controlling terminal

PATCH Applied Jason Wessel 2 participants 6 messages Sep 17, 2012 – Sep 24, 2012

lore
Share & tools
lore
#0 lore raw

cover Add ability to auto spawn screen on a controlling terminal

This is a series of 4 patches, 2 for bitbake and 2 for oe-core. On the bitbake side the idea is to have a new type of event which can exec a command or print a log message if there is no controlling terminal available.

When a controlling terminal is available the knotty UI can directly execute the command on the controlling terminal, instead of printing something, which occurs in all the other UI's. What is really nice about this is that you don't have to spawn screen in separate terminals when you only want to use a single terminal to a build server.

--

 lib/bb/event.py                  |    9 +++++++++
 lib/bb/ui/crumbs/progress.py     |    3 +++
 lib/bb/ui/crumbs/runningbuild.py |   15 +++++++++++++++
 lib/bb/ui/knotty.py              |   19 +++++++++++++++++++
 lib/bb/ui/ncurses.py             |    2 ++
 meta/classes/terminal.bbclass    |    4 ++--
 meta/lib/oe/terminal.py          |   33 +++++++++++++++++++--------------
 7 files changed, 69 insertions(+), 16 deletions(-)
#1 lore raw

PATCH 1/4 progress.py: Fix traceback when running goggle ui

The following traceback appears when running the following command after the devshell is exited.

bitbake -u goggle -c devshell busybox

-- traceback --
Traceback (most recent call last):
File "/work/bitbake/lib/bb/ui/goggle.py", line 35, in event_handle_idle_func
build.handle_event (event, pbar)
File "/work/bitbake/lib/bb/ui/crumbs/runningbuild.py", line 299, in handle_event
pbar.set_text(event.msg)
AttributeError: 'ProgressBar' object has no attribute 'set_text'

---
 lib/bb/ui/crumbs/progress.py |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
lib/bb/ui/crumbs/progress.py +3 -0
index 0c7ad96..1d28a11 100644
@@ -11,6 +11,9 @@ class ProgressBar(gtk.Dialog):
         self.vbox.pack_start(self.progress)
         self.show_all()
 
+    def set_text(self, msg):
+        self.progress.set_text(msg)
+
     def update(self, x, y):
         self.progress.set_fraction(float(x)/float(y))
         self.progress.set_text("%2d %%" % (x*100/y))
Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/4 event.py, knotty.py, ncurses.py, runningbuild.py: Add support for LogExecTTY event

In reply to Jason Wessel

The LogExecTTY even is intended to provide the ability to spawn a task on a the controlling tty, if a tty is availble. When a controlling tty is not availble the previous behavior is preserved where a warning is issued about the action an end user must execute.

All the available UI's were tested against the new event type.

This feature is primarily intended for hooking up a screen client session automatically on the controlling tty to allow for a more streamlined end user experience when using a pure command line driven environment. The changes that send the LogExecTTY event are in the oe-core side.

---
 lib/bb/event.py                  |    9 +++++++++
 lib/bb/ui/crumbs/runningbuild.py |   15 +++++++++++++++
 lib/bb/ui/knotty.py              |   19 +++++++++++++++++++
 lib/bb/ui/ncurses.py             |    2 ++
 4 files changed, 45 insertions(+), 0 deletions(-)
lib/bb/event.py +9 -0
index ab62d4d..e62f2f8 100644
@@ -509,6 +509,15 @@ class MsgFatal(MsgBase):
 class MsgPlain(MsgBase):
     """General output"""
 
+class LogExecTTY(Event):
+    """Send event containing program to spawn on tty of the logger"""
+    def __init__(self, msg, prog, sleep_delay, retries):
+        Event.__init__(self)
+        self.msg = msg
+        self.prog = prog
+        self.sleep_delay = sleep_delay
+        self.retries = retries
+
 class LogHandler(logging.Handler):
     """Dispatch logging messages as bitbake events"""
 
lib/bb/ui/crumbs/runningbuild.py +15 -0
index a57d6db..700fd65 100644
@@ -375,6 +375,21 @@ class RunningBuild (gobject.GObject):
                     msg += ("%s\n" % reason)
             self.emit("no-provider", msg)
             self.emit("log", msg)
+        elif isinstance(event, bb.event.LogExecTTY):
+            icon = "dialog-warning"
+            color = HobColors.WARNING
+            if self.sequential or not parent:
+                tree_add = self.model.append
+            else:
+                tree_add = self.model.prepend
+            tree_add(parent,
+                     (None,
+                      package,
+                      task,
+                      event.msg,
+                      icon,
+                      color,
+                      0))
         else:
             if not isinstance(event, (bb.event.BuildBase,
                                       bb.event.StampUpdate,
lib/bb/ui/knotty.py +19 -0
index 858cacf..d81ad5d 100644
@@ -27,6 +27,7 @@ import logging
 import progressbar
 import signal
 import bb.msg
+import time
 import fcntl
 import struct
 import copy
@@ -216,6 +217,10 @@ def main(server, eventHandler, tf = TerminalFilter):
     includelogs = server.runCommand(["getVariable", "BBINCLUDELOGS"])
     loglines = server.runCommand(["getVariable", "BBINCLUDELOGS_LINES"])
     consolelogfile = server.runCommand(["getVariable", "BB_CONSOLELOG"])
+    if sys.stdin.isatty() and sys.stdout.isatty():
+        log_exec_tty = True
+    else:
+        log_exec_tty = False
 
     helper = uihelper.BBUIHelper()
 
@@ -271,6 +276,20 @@ def main(server, eventHandler, tf = TerminalFilter):
                 if not main.shutdown:
                     main.shutdown = 1
 
+            if isinstance(event, bb.event.LogExecTTY):
+                if log_exec_tty:
+                    tries = event.retries
+                    while tries:
+                        print "Trying to run: %s" % event.prog
+                        if os.system(event.prog) == 0:
+                            break
+                        time.sleep(event.sleep_delay)
+                        tries -= 1
+                    if tries:
+                        continue
+                logger.warn(event.msg)
+                continue
+
             if isinstance(event, logging.LogRecord):
                 if event.levelno >= format.ERROR:
                     errors = errors + 1
lib/bb/ui/ncurses.py +2 -0
index f573b95..f6ea7f9 100644
@@ -318,6 +318,8 @@ class NCursesUI:
                 if isinstance(event, bb.cooker.CookerExit):
                     exitflag = True
 
+                if isinstance(event, bb.event.LogExecTTY):
+                    mw.appendText('WARN: ' + event.msg + '\n')
                 if helper.needUpdate:
                     activetasks, failedtasks = helper.getTasks()
                     taw.erase()
Signature · 1 line
1.7.1
#3 lore raw

PATCH 3/4 terminal: pass data store all the way through to terminal class

In reply to Jason Wessel

Passing the data store will be needed for firing a custom event for the screen class.

---
 meta/classes/terminal.bbclass |    4 ++--
 meta/lib/oe/terminal.py       |   22 +++++++++++-----------
 2 files changed, 13 insertions(+), 13 deletions(-)
meta/classes/terminal.bbclass +2 -2
index 3cfc84b..2cd6f4c 100644
@@ -25,7 +25,7 @@ def oe_terminal(command, title, d):
         bb.fatal('Devshell usage disabled with OE_TERMINAL')
     elif terminal != 'auto':
         try:
-            oe.terminal.spawn(terminal, command, title)
+            oe.terminal.spawn(terminal, command, title, None, d)
             return
         except oe.terminal.UnsupportedTerminal:
             bb.warn('Unsupported terminal "%s", defaulting to "auto"' %
@@ -34,7 +34,7 @@ def oe_terminal(command, title, d):
             bb.fatal('Unable to spawn terminal %s: %s' % (terminal, exc))
 
     try:
-        oe.terminal.spawn_preferred(command, title)
+        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:
meta/lib/oe/terminal.py +11 -11
index d66309f..c71d6ad 100644
@@ -28,7 +28,7 @@ class Registry(oe.classutils.ClassRegistry):
 class Terminal(Popen):
     __metaclass__ = Registry
 
-    def __init__(self, sh_cmd, title=None, env=None):
+    def __init__(self, sh_cmd, title=None, env=None, d=None):
         fmt_sh_cmd = self.format_command(sh_cmd, title)
         try:
             Popen.__init__(self, fmt_sh_cmd, env=env)
@@ -47,7 +47,7 @@ class Terminal(Popen):
             return [element.format(**fmt) for element in self.command]
 
 class XTerminal(Terminal):
-    def __init__(self, sh_cmd, title=None, env=None):
+    def __init__(self, sh_cmd, title=None, env=None, d=None):
         Terminal.__init__(self, sh_cmd, title, env)
         if not os.environ.get('DISPLAY'):
             raise UnsupportedTerminal(self.name)
@@ -60,7 +60,7 @@ class Xfce(XTerminal):
     command = 'Terminal -T "{title}" -e "{command}"'
     priority = 2
 
-    def __init__(self, command, title=None, env=None):
+    def __init__(self, command, title=None, env=None, d=None):
         # Upstream binary name is Terminal but Debian/Ubuntu use
         # xfce4-terminal to avoid possible(?) conflicts
         distro = distro_name()
@@ -68,20 +68,20 @@ class Xfce(XTerminal):
             cmd = 'xfce4-terminal -T "{title}" -e "{command}"'
         else:
             cmd = command
-        XTerminal.__init__(self, cmd, title, env)
+        XTerminal.__init__(self, cmd, title, env, d)
 
 class Konsole(XTerminal):
     command = 'konsole -T "{title}" -e {command}'
     priority = 2
 
-    def __init__(self, sh_cmd, title=None, env=None):
+    def __init__(self, sh_cmd, title=None, env=None, d=None):
         # Check version
         vernum = check_konsole_version("konsole")
         if vernum:
             if vernum.split('.')[0] == "2":
                 logger.debug(1, 'Konsole from KDE 4.x will not work as devshell, skipping')
                 raise UnsupportedTerminal(self.name)
-        XTerminal.__init__(self, sh_cmd, title, env)
+        XTerminal.__init__(self, sh_cmd, title, env, d)
 
 class XTerm(XTerminal):
     command = 'xterm -T "{title}" -e {command}'
@@ -94,7 +94,7 @@ class Rxvt(XTerminal):
 class Screen(Terminal):
     command = 'screen -D -m -t "{title}" -S devshell {command}'
 
-    def __init__(self, sh_cmd, title=None, env=None):
+    def __init__(self, sh_cmd, title=None, env=None, d=None):
         s_id = "devshell_%i" % os.getpid()
         self.command = "screen -D -m -t \"{title}\" -S %s {command}" % s_id
         Terminal.__init__(self, sh_cmd, title, env)
@@ -105,18 +105,18 @@ class Screen(Terminal):
 def prioritized():
     return Registry.prioritized()
 
-def spawn_preferred(sh_cmd, title=None, env=None):
+def spawn_preferred(sh_cmd, title=None, env=None, d=None):
     """Spawn the first supported terminal, by priority"""
     for terminal in prioritized():
         try:
-            spawn(terminal.name, sh_cmd, title, env)
+            spawn(terminal.name, sh_cmd, title, env, d)
             break
         except UnsupportedTerminal:
             continue
     else:
         raise NoSupportedTerminals()
 
-def spawn(name, sh_cmd, title=None, env=None):
+def spawn(name, sh_cmd, title=None, env=None, d=None):
     """Spawn the specified terminal, by name"""
     logger.debug(1, 'Attempting to spawn terminal "%s"', name)
     try:
@@ -124,7 +124,7 @@ def spawn(name, sh_cmd, title=None, env=None):
     except KeyError:
         raise UnsupportedTerminal(name)
 
-    pipe = terminal(sh_cmd, title, env)
+    pipe = terminal(sh_cmd, title, env, d)
     output = pipe.communicate()[0]
     if pipe.returncode != 0:
         raise ExecutionError(sh_cmd, pipe.returncode, output)
Signature · 1 line
1.7.1
#4 lore raw

PATCH 4/4 terminal: Send LogExecTTY event to spawn screen

In reply to Jason Wessel

Bitbake has the ability to request to run a command and if it is not possible fall back to emitting a log message. This can be used to start a screen client automatically on the controling tty if the UI has an interactive tty.

---
 meta/lib/oe/terminal.py |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)
meta/lib/oe/terminal.py +8 -3
index c71d6ad..1e74315 100644
@@ -97,9 +97,14 @@ class Screen(Terminal):
     def __init__(self, sh_cmd, title=None, env=None, d=None):
         s_id = "devshell_%i" % os.getpid()
         self.command = "screen -D -m -t \"{title}\" -S %s {command}" % s_id
-        Terminal.__init__(self, sh_cmd, title, env)
-        logger.warn('Screen started. Please connect in another terminal with '
-                    '"screen -r devshell %s"' % s_id)
+        Terminal.__init__(self, sh_cmd, title, env, d)
+        msg = 'Screen started. Please connect in another terminal with ' \
+            '"screen -r %s"' % s_id
+        if (d):
+            bb.event.fire(bb.event.LogExecTTY(msg, "screen -r %s" % s_id,
+                                              0.5, 10), d)
+        else:
+            logger.warn(msg)
 
 
 def prioritized():
Signature · 1 line
1.7.1
#5 lore raw
In reply to Jason Wessel
Quoted 11 lines · Jason Wessel

This is a series of 4 patches, 2 for bitbake and 2 for oe-core. On the bitbake side the idea is to have a new type of event which can exec a command or print a log message if there is no controlling terminal available.

When a controlling terminal is available the knotty UI can directly execute the command on the controlling terminal, instead of printing something, which occurs in all the other UI's. What is really nice about this is that you don't have to spawn screen in separate terminals when you only want to use a single terminal to a build server.

Merged to master, thanks.

Richard