terminal.py 11 KB

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