aboutsummaryrefslogtreecommitdiffstats
path: root/meta/lib/oe/terminal.py
diff options
context:
space:
mode:
authorChris Larson <chris_larson@mentor.com>2011-03-29 12:53:19 -0700
committerRichard Purdie <richard.purdie@linuxfoundation.org>2011-08-10 13:25:11 +0100
commit69f77f80965fa06a057837f8f49eda06855c4086 (patch)
tree78bb7b633b22a16d2fa4e63b1c4c4dd6dcc6dd4e /meta/lib/oe/terminal.py
parentfa7fbc9695829f7fa981b16d9559e9e984494e09 (diff)
downloadopenembedded-core-contrib-69f77f80965fa06a057837f8f49eda06855c4086.tar.gz
Rework how the devshell functions
In the new implementation, each known terminal is defined as a class in oe.terminal, as a subclass of bb.process.Popen. terminal.bbclass wraps this functionality, providing the metadata pieces. It obeys the OE_TERMINAL variable, which is a 'choice' typed variable. This variable may be 'auto', 'none', or any of the names of the defined terminals. When using 'auto', or requesting an unsupported terminal, we attempt to spawn them in priority order until we get one that's available on this system (and in the case of the X terminals, has DISPLAY defined). The 'none' value is used when we're doing things like automated builds, and want to ensure that no terminal is *ever* spawned, under any circumstances. Current available terminals: gnome konsole xterm rxvt screen Signed-off-by: Chris Larson <chris_larson@mentor.com>
Diffstat (limited to 'meta/lib/oe/terminal.py')
-rw-r--r--meta/lib/oe/terminal.py101
1 files changed, 101 insertions, 0 deletions
diff --git a/meta/lib/oe/terminal.py b/meta/lib/oe/terminal.py
new file mode 100644
index 0000000000..3767935586
--- /dev/null
+++ b/meta/lib/oe/terminal.py
@@ -0,0 +1,101 @@
+import logging
+import os
+import oe.classutils
+import shlex
+from bb.process import Popen, ExecutionError
+
+logger = logging.getLogger('BitBake.OE.Terminal')
+
+
+class UnsupportedTerminal(StandardError):
+ pass
+
+class NoSupportedTerminals(StandardError):
+ pass
+
+
+class Registry(oe.classutils.ClassRegistry):
+ command = None
+
+ def __init__(cls, name, bases, attrs):
+ super(Registry, cls).__init__(name.lower(), bases, attrs)
+
+ @property
+ def implemented(cls):
+ return bool(cls.command)
+
+
+class Terminal(Popen):
+ __metaclass__ = Registry
+
+ def __init__(self, command, title=None, env=None):
+ self.format_command(command, title)
+
+ try:
+ Popen.__init__(self, self.command, env=env)
+ except OSError as exc:
+ import errno
+ if exc.errno == errno.ENOENT:
+ raise UnsupportedTerminal(self.name)
+ else:
+ raise
+
+ def format_command(self, command, title):
+ fmt = {'title': title or 'Terminal', 'command': command}
+ if isinstance(self.command, basestring):
+ self.command = shlex.split(self.command.format(**fmt))
+ else:
+ self.command = [element.format(**fmt) for element in self.command]
+
+class XTerminal(Terminal):
+ def __init__(self, command, title=None, env=None):
+ Terminal.__init__(self, command, title, env)
+ if not os.environ.get('DISPLAY'):
+ raise UnsupportedTerminal(self.name)
+
+class Gnome(XTerminal):
+ command = 'gnome-terminal --disable-factory -t "{title}" -x {command}'
+ priority = 2
+
+class Konsole(XTerminal):
+ command = 'konsole -T "{title}" -e {command}'
+ priority = 2
+
+class XTerm(XTerminal):
+ command = 'xterm -T "{title}" -e {command}'
+ priority = 1
+
+class Rxvt(XTerminal):
+ command = 'rxvt -T "{title}" -e {command}'
+ priority = 1
+
+class Screen(Terminal):
+ command = 'screen -D -m -t "{title}" {command}'
+
+
+def prioritized():
+ return Registry.prioritized()
+
+def spawn_preferred(command, title=None, env=None):
+ """Spawn the first supported terminal, by priority"""
+ for terminal in prioritized():
+ try:
+ spawn(terminal.name, command, title, env)
+ break
+ except UnsupportedTerminal:
+ continue
+ else:
+ raise NoSupportedTerminals()
+
+def spawn(name, command, title=None, env=None):
+ """Spawn the specified terminal, by name"""
+ logger.debug(1, 'Attempting to spawn terminal "%s"', name)
+ try:
+ terminal = Registry.registry[name]
+ except KeyError:
+ raise UnsupportedTerminal(name)
+
+ pipe = terminal(command, title, env)
+ output = pipe.communicate()[0]
+ if pipe.returncode != 0:
+ raise ExecutionError(pipe.command, pipe.returncode, output)