terminal.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import logging
  2. import oe.classutils
  3. import shlex
  4. from bb.process import Popen, ExecutionError
  5. from distutils.version import LooseVersion
  6. logger = logging.getLogger('BitBake.OE.Terminal')
  7. class UnsupportedTerminal(Exception):
  8. pass
  9. class NoSupportedTerminals(Exception):
  10. def __init__(self, terms):
  11. self.terms = terms
  12. class Registry(oe.classutils.ClassRegistry):
  13. command = None
  14. def __init__(cls, name, bases, attrs):
  15. super(Registry, cls).__init__(name.lower(), bases, attrs)
  16. @property
  17. def implemented(cls):
  18. return bool(cls.command)
  19. class Terminal(Popen, metaclass=Registry):
  20. def __init__(self, sh_cmd, title=None, env=None, d=None):
  21. fmt_sh_cmd = self.format_command(sh_cmd, title)
  22. try:
  23. Popen.__init__(self, fmt_sh_cmd, env=env)
  24. except OSError as exc:
  25. import errno
  26. if exc.errno == errno.ENOENT:
  27. raise UnsupportedTerminal(self.name)
  28. else:
  29. raise
  30. def format_command(self, sh_cmd, title):
  31. fmt = {'title': title or 'Terminal', 'command': sh_cmd}
  32. if isinstance(self.command, str):
  33. return shlex.split(self.command.format(**fmt))
  34. else:
  35. return [element.format(**fmt) for element in self.command]
  36. class XTerminal(Terminal):
  37. def __init__(self, sh_cmd, title=None, env=None, d=None):
  38. Terminal.__init__(self, sh_cmd, title, env, d)
  39. if not os.environ.get('DISPLAY'):
  40. raise UnsupportedTerminal(self.name)
  41. class Gnome(XTerminal):
  42. command = 'gnome-terminal -t "{title}" -x {command}'
  43. priority = 2
  44. def __init__(self, sh_cmd, title=None, env=None, d=None):
  45. # Recent versions of gnome-terminal does not support non-UTF8 charset:
  46. # https://bugzilla.gnome.org/show_bug.cgi?id=732127; as a workaround,
  47. # clearing the LC_ALL environment variable so it uses the locale.
  48. # Once fixed on the gnome-terminal project, this should be removed.
  49. if os.getenv('LC_ALL'): os.putenv('LC_ALL','')
  50. XTerminal.__init__(self, sh_cmd, title, env, d)
  51. class Mate(XTerminal):
  52. command = 'mate-terminal --disable-factory -t "{title}" -x {command}'
  53. priority = 2
  54. class Xfce(XTerminal):
  55. command = 'xfce4-terminal -T "{title}" -e "{command}"'
  56. priority = 2
  57. class Terminology(XTerminal):
  58. command = 'terminology -T="{title}" -e {command}'
  59. priority = 2
  60. class Konsole(XTerminal):
  61. command = 'konsole --separate --workdir . -p tabtitle="{title}" -e {command}'
  62. priority = 2
  63. def __init__(self, sh_cmd, title=None, env=None, d=None):
  64. # Check version
  65. vernum = check_terminal_version("konsole")
  66. if vernum and LooseVersion(vernum) < '2.0.0':
  67. # Konsole from KDE 3.x
  68. self.command = 'konsole -T "{title}" -e {command}'
  69. elif vernum and LooseVersion(vernum) < '16.08.1':
  70. # Konsole pre 16.08.01 Has nofork
  71. self.command = 'konsole --nofork --workdir . -p tabtitle="{title}" -e {command}'
  72. XTerminal.__init__(self, sh_cmd, title, env, d)
  73. class XTerm(XTerminal):
  74. command = 'xterm -T "{title}" -e {command}'
  75. priority = 1
  76. class Rxvt(XTerminal):
  77. command = 'rxvt -T "{title}" -e {command}'
  78. priority = 1
  79. class Screen(Terminal):
  80. command = 'screen -D -m -t "{title}" -S devshell {command}'
  81. def __init__(self, sh_cmd, title=None, env=None, d=None):
  82. s_id = "devshell_%i" % os.getpid()
  83. self.command = "screen -D -m -t \"{title}\" -S %s {command}" % s_id
  84. Terminal.__init__(self, sh_cmd, title, env, d)
  85. msg = 'Screen started. Please connect in another terminal with ' \
  86. '"screen -r %s"' % s_id
  87. if (d):
  88. bb.event.fire(bb.event.LogExecTTY(msg, "screen -r %s" % s_id,
  89. 0.5, 10), d)
  90. else:
  91. logger.warning(msg)
  92. class TmuxRunning(Terminal):
  93. """Open a new pane in the current running tmux window"""
  94. name = 'tmux-running'
  95. command = 'tmux split-window "{command}"'
  96. priority = 2.75
  97. def __init__(self, sh_cmd, title=None, env=None, d=None):
  98. if not bb.utils.which(os.getenv('PATH'), 'tmux'):
  99. raise UnsupportedTerminal('tmux is not installed')
  100. if not os.getenv('TMUX'):
  101. raise UnsupportedTerminal('tmux is not running')
  102. if not check_tmux_pane_size('tmux'):
  103. raise UnsupportedTerminal('tmux pane too small or tmux < 1.9 version is being used')
  104. Terminal.__init__(self, sh_cmd, title, env, d)
  105. class TmuxNewWindow(Terminal):
  106. """Open a new window in the current running tmux session"""
  107. name = 'tmux-new-window'
  108. command = 'tmux new-window -n "{title}" "{command}"'
  109. priority = 2.70
  110. def __init__(self, sh_cmd, title=None, env=None, d=None):
  111. if not bb.utils.which(os.getenv('PATH'), 'tmux'):
  112. raise UnsupportedTerminal('tmux is not installed')
  113. if not os.getenv('TMUX'):
  114. raise UnsupportedTerminal('tmux is not running')
  115. Terminal.__init__(self, sh_cmd, title, env, d)
  116. class Tmux(Terminal):
  117. """Start a new tmux session and window"""
  118. command = 'tmux new -d -s devshell -n devshell "{command}"'
  119. priority = 0.75
  120. def __init__(self, sh_cmd, title=None, env=None, d=None):
  121. if not bb.utils.which(os.getenv('PATH'), 'tmux'):
  122. raise UnsupportedTerminal('tmux is not installed')
  123. # TODO: consider using a 'devshell' session shared amongst all
  124. # devshells, if it's already there, add a new window to it.
  125. window_name = 'devshell-%i' % os.getpid()
  126. self.command = 'tmux new -d -s {0} -n {0} "{{command}}"'.format(window_name)
  127. Terminal.__init__(self, sh_cmd, title, env, d)
  128. attach_cmd = 'tmux att -t {0}'.format(window_name)
  129. msg = 'Tmux started. Please connect in another terminal with `tmux att -t {0}`'.format(window_name)
  130. if d:
  131. bb.event.fire(bb.event.LogExecTTY(msg, attach_cmd, 0.5, 10), d)
  132. else:
  133. logger.warning(msg)
  134. class Custom(Terminal):
  135. command = 'false' # This is a placeholder
  136. priority = 3
  137. def __init__(self, sh_cmd, title=None, env=None, d=None):
  138. self.command = d and d.getVar('OE_TERMINAL_CUSTOMCMD')
  139. if self.command:
  140. if not '{command}' in self.command:
  141. self.command += ' {command}'
  142. Terminal.__init__(self, sh_cmd, title, env, d)
  143. logger.warning('Custom terminal was started.')
  144. else:
  145. logger.debug(1, 'No custom terminal (OE_TERMINAL_CUSTOMCMD) set')
  146. raise UnsupportedTerminal('OE_TERMINAL_CUSTOMCMD not set')
  147. def prioritized():
  148. return Registry.prioritized()
  149. def get_cmd_list():
  150. terms = Registry.prioritized()
  151. cmds = []
  152. for term in terms:
  153. if term.command:
  154. cmds.append(term.command)
  155. return cmds
  156. def spawn_preferred(sh_cmd, title=None, env=None, d=None):
  157. """Spawn the first supported terminal, by priority"""
  158. for terminal in prioritized():
  159. try:
  160. spawn(terminal.name, sh_cmd, title, env, d)
  161. break
  162. except UnsupportedTerminal:
  163. continue
  164. else:
  165. raise NoSupportedTerminals(get_cmd_list())
  166. def spawn(name, sh_cmd, title=None, env=None, d=None):
  167. """Spawn the specified terminal, by name"""
  168. logger.debug(1, 'Attempting to spawn terminal "%s"', name)
  169. try:
  170. terminal = Registry.registry[name]
  171. except KeyError:
  172. raise UnsupportedTerminal(name)
  173. # We need to know when the command completes but some terminals (at least
  174. # gnome and tmux) gives us no way to do this. We therefore write the pid
  175. # to a file using a "phonehome" wrapper script, then monitor the pid
  176. # until it exits.
  177. import tempfile
  178. import time
  179. pidfile = tempfile.NamedTemporaryFile(delete = False).name
  180. try:
  181. sh_cmd = bb.utils.which(os.getenv('PATH'), "oe-gnome-terminal-phonehome") + " " + pidfile + " " + sh_cmd
  182. pipe = terminal(sh_cmd, title, env, d)
  183. output = pipe.communicate()[0]
  184. if output:
  185. output = output.decode("utf-8")
  186. if pipe.returncode != 0:
  187. raise ExecutionError(sh_cmd, pipe.returncode, output)
  188. while os.stat(pidfile).st_size <= 0:
  189. time.sleep(0.01)
  190. continue
  191. with open(pidfile, "r") as f:
  192. pid = int(f.readline())
  193. finally:
  194. os.unlink(pidfile)
  195. while True:
  196. try:
  197. os.kill(pid, 0)
  198. time.sleep(0.1)
  199. except OSError:
  200. return
  201. def check_tmux_pane_size(tmux):
  202. import subprocess as sub
  203. # On older tmux versions (<1.9), return false. The reason
  204. # is that there is no easy way to get the height of the active panel
  205. # on current window without nested formats (available from version 1.9)
  206. vernum = check_terminal_version("tmux")
  207. if vernum and LooseVersion(vernum) < '1.9':
  208. return False
  209. try:
  210. p = sub.Popen('%s list-panes -F "#{?pane_active,#{pane_height},}"' % tmux,
  211. shell=True,stdout=sub.PIPE,stderr=sub.PIPE)
  212. out, err = p.communicate()
  213. size = int(out.strip())
  214. except OSError as exc:
  215. import errno
  216. if exc.errno == errno.ENOENT:
  217. return None
  218. else:
  219. raise
  220. return size/2 >= 19
  221. def check_terminal_version(terminalName):
  222. import subprocess as sub
  223. try:
  224. cmdversion = '%s --version' % terminalName
  225. if terminalName.startswith('tmux'):
  226. cmdversion = '%s -V' % terminalName
  227. newenv = os.environ.copy()
  228. newenv["LANG"] = "C"
  229. p = sub.Popen(['sh', '-c', cmdversion], stdout=sub.PIPE, stderr=sub.PIPE, env=newenv)
  230. out, err = p.communicate()
  231. ver_info = out.decode().rstrip().split('\n')
  232. except OSError as exc:
  233. import errno
  234. if exc.errno == errno.ENOENT:
  235. return None
  236. else:
  237. raise
  238. vernum = None
  239. for ver in ver_info:
  240. if ver.startswith('Konsole'):
  241. vernum = ver.split(' ')[-1]
  242. if ver.startswith('GNOME Terminal'):
  243. vernum = ver.split(' ')[-1]
  244. if ver.startswith('MATE Terminal'):
  245. vernum = ver.split(' ')[-1]
  246. if ver.startswith('tmux'):
  247. vernum = ver.split()[-1]
  248. return vernum
  249. def distro_name():
  250. try:
  251. p = Popen(['lsb_release', '-i'])
  252. out, err = p.communicate()
  253. distro = out.split(':')[1].strip().lower()
  254. except:
  255. distro = "unknown"
  256. return distro