blob: da4fbeabb67a49fd5041d952b09e6cffffc51c16 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# BitBake Curses UI Implementation
3#
4# Implements an ncurses frontend for the BitBake utility.
5#
6# Copyright (C) 2006 Michael 'Mickey' Lauer
7# Copyright (C) 2006-2007 Richard Purdie
8#
Brad Bishopc342db32019-05-15 21:57:59 -04009# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011
12"""
13 We have the following windows:
14
15 1.) Main Window: Shows what we are ultimately building and how far we are. Includes status bar
16 2.) Thread Activity Window: Shows one status line for every concurrent bitbake thread.
17 3.) Command Line Window: Contains an interactive command line where you can interact w/ Bitbake.
18
19 Basic window layout is like that:
20
21 |---------------------------------------------------------|
22 | <Main Window> | <Thread Activity Window> |
23 | | 0: foo do_compile complete|
24 | Building Gtk+-2.6.10 | 1: bar do_patch complete |
25 | Status: 60% | ... |
26 | | ... |
27 | | ... |
28 |---------------------------------------------------------|
29 |<Command Line Window> |
30 |>>> which virtual/kernel |
31 |openzaurus-kernel |
32 |>>> _ |
33 |---------------------------------------------------------|
34
35"""
36
37
Patrick Williamsc0f7c042017-02-23 20:41:17 -060038
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039import logging
Andrew Geissler82c905d2020-04-13 13:39:40 -050040import os, sys, itertools, time
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041
42try:
43 import curses
44except ImportError:
45 sys.exit("FATAL: The ncurses ui could not load the required curses python module.")
46
47import bb
Patrick Williamsc0f7c042017-02-23 20:41:17 -060048import xmlrpc.client
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049from bb.ui import uihelper
50
51parsespin = itertools.cycle( r'|/-\\' )
52
53X = 0
54Y = 1
55WIDTH = 2
56HEIGHT = 3
57
58MAXSTATUSLENGTH = 32
59
60class NCursesUI:
61 """
62 NCurses UI Class
63 """
64 class Window:
65 """Base Window Class"""
66 def __init__( self, x, y, width, height, fg=curses.COLOR_BLACK, bg=curses.COLOR_WHITE ):
67 self.win = curses.newwin( height, width, y, x )
68 self.dimensions = ( x, y, width, height )
69 """
70 if curses.has_colors():
71 color = 1
72 curses.init_pair( color, fg, bg )
73 self.win.bkgdset( ord(' '), curses.color_pair(color) )
74 else:
75 self.win.bkgdset( ord(' '), curses.A_BOLD )
76 """
77 self.erase()
78 self.setScrolling()
79 self.win.noutrefresh()
80
81 def erase( self ):
82 self.win.erase()
83
84 def setScrolling( self, b = True ):
85 self.win.scrollok( b )
86 self.win.idlok( b )
87
88 def setBoxed( self ):
89 self.boxed = True
90 self.win.box()
91 self.win.noutrefresh()
92
93 def setText( self, x, y, text, *args ):
94 self.win.addstr( y, x, text, *args )
95 self.win.noutrefresh()
96
97 def appendText( self, text, *args ):
98 self.win.addstr( text, *args )
99 self.win.noutrefresh()
100
101 def drawHline( self, y ):
102 self.win.hline( y, 0, curses.ACS_HLINE, self.dimensions[WIDTH] )
103 self.win.noutrefresh()
104
105 class DecoratedWindow( Window ):
106 """Base class for windows with a box and a title bar"""
107 def __init__( self, title, x, y, width, height, fg=curses.COLOR_BLACK, bg=curses.COLOR_WHITE ):
108 NCursesUI.Window.__init__( self, x+1, y+3, width-2, height-4, fg, bg )
109 self.decoration = NCursesUI.Window( x, y, width, height, fg, bg )
110 self.decoration.setBoxed()
111 self.decoration.win.hline( 2, 1, curses.ACS_HLINE, width-2 )
112 self.setTitle( title )
113
114 def setTitle( self, title ):
115 self.decoration.setText( 1, 1, title.center( self.dimensions[WIDTH]-2 ), curses.A_BOLD )
116
117 #-------------------------------------------------------------------------#
118# class TitleWindow( Window ):
119 #-------------------------------------------------------------------------#
120# """Title Window"""
121# def __init__( self, x, y, width, height ):
122# NCursesUI.Window.__init__( self, x, y, width, height )
123# version = bb.__version__
124# title = "BitBake %s" % version
125# credit = "(C) 2003-2007 Team BitBake"
126# #self.win.hline( 2, 1, curses.ACS_HLINE, width-2 )
127# self.win.border()
128# self.setText( 1, 1, title.center( self.dimensions[WIDTH]-2 ), curses.A_BOLD )
129# self.setText( 1, 2, credit.center( self.dimensions[WIDTH]-2 ), curses.A_BOLD )
130
131 #-------------------------------------------------------------------------#
132 class ThreadActivityWindow( DecoratedWindow ):
133 #-------------------------------------------------------------------------#
134 """Thread Activity Window"""
135 def __init__( self, x, y, width, height ):
136 NCursesUI.DecoratedWindow.__init__( self, "Thread Activity", x, y, width, height )
137
138 def setStatus( self, thread, text ):
139 line = "%02d: %s" % ( thread, text )
140 width = self.dimensions[WIDTH]
141 if ( len(line) > width ):
142 line = line[:width-3] + "..."
143 else:
144 line = line.ljust( width )
145 self.setText( 0, thread, line )
146
147 #-------------------------------------------------------------------------#
148 class MainWindow( DecoratedWindow ):
149 #-------------------------------------------------------------------------#
150 """Main Window"""
151 def __init__( self, x, y, width, height ):
152 self.StatusPosition = width - MAXSTATUSLENGTH
153 NCursesUI.DecoratedWindow.__init__( self, None, x, y, width, height )
154 curses.nl()
155
156 def setTitle( self, title ):
157 title = "BitBake %s" % bb.__version__
158 self.decoration.setText( 2, 1, title, curses.A_BOLD )
159 self.decoration.setText( self.StatusPosition - 8, 1, "Status:", curses.A_BOLD )
160
161 def setStatus(self, status):
162 while len(status) < MAXSTATUSLENGTH:
163 status = status + " "
164 self.decoration.setText( self.StatusPosition, 1, status, curses.A_BOLD )
165
166
167 #-------------------------------------------------------------------------#
168 class ShellOutputWindow( DecoratedWindow ):
169 #-------------------------------------------------------------------------#
170 """Interactive Command Line Output"""
171 def __init__( self, x, y, width, height ):
172 NCursesUI.DecoratedWindow.__init__( self, "Command Line Window", x, y, width, height )
173
174 #-------------------------------------------------------------------------#
175 class ShellInputWindow( Window ):
176 #-------------------------------------------------------------------------#
177 """Interactive Command Line Input"""
178 def __init__( self, x, y, width, height ):
179 NCursesUI.Window.__init__( self, x, y, width, height )
180
181# put that to the top again from curses.textpad import Textbox
182# self.textbox = Textbox( self.win )
183# t = threading.Thread()
184# t.run = self.textbox.edit
185# t.start()
186
187 #-------------------------------------------------------------------------#
188 def main(self, stdscr, server, eventHandler, params):
189 #-------------------------------------------------------------------------#
190 height, width = stdscr.getmaxyx()
191
192 # for now split it like that:
193 # MAIN_y + THREAD_y = 2/3 screen at the top
194 # MAIN_x = 2/3 left, THREAD_y = 1/3 right
195 # CLI_y = 1/3 of screen at the bottom
196 # CLI_x = full
197
198 main_left = 0
199 main_top = 0
200 main_height = ( height // 3 * 2 )
201 main_width = ( width // 3 ) * 2
202 clo_left = main_left
203 clo_top = main_top + main_height
204 clo_height = height - main_height - main_top - 1
205 clo_width = width
206 cli_left = main_left
207 cli_top = clo_top + clo_height
208 cli_height = 1
209 cli_width = width
210 thread_left = main_left + main_width
211 thread_top = main_top
212 thread_height = main_height
213 thread_width = width - main_width
214
215 #tw = self.TitleWindow( 0, 0, width, main_top )
216 mw = self.MainWindow( main_left, main_top, main_width, main_height )
217 taw = self.ThreadActivityWindow( thread_left, thread_top, thread_width, thread_height )
218 clo = self.ShellOutputWindow( clo_left, clo_top, clo_width, clo_height )
219 cli = self.ShellInputWindow( cli_left, cli_top, cli_width, cli_height )
220 cli.setText( 0, 0, "BB>" )
221
222 mw.setStatus("Idle")
223
224 helper = uihelper.BBUIHelper()
225 shutdown = 0
226
227 try:
228 params.updateFromServer(server)
229 cmdline = params.parseActions()
230 if not cmdline:
231 print("Nothing to do. Use 'bitbake world' to build everything, or run 'bitbake --help' for usage information.")
232 return 1
233 if 'msg' in cmdline and cmdline['msg']:
234 logger.error(cmdline['msg'])
235 return 1
236 cmdline = cmdline['action']
237 ret, error = server.runCommand(cmdline)
238 if error:
239 print("Error running command '%s': %s" % (cmdline, error))
240 return
Andrew Geissler82c905d2020-04-13 13:39:40 -0500241 elif not ret:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500242 print("Couldn't get default commandlind! %s" % ret)
243 return
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600244 except xmlrpc.client.Fault as x:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500245 print("XMLRPC Fault getting commandline:\n %s" % x)
246 return
247
248 exitflag = False
249 while not exitflag:
250 try:
251 event = eventHandler.waitEvent(0.25)
252 if not event:
253 continue
254
255 helper.eventHandler(event)
256 if isinstance(event, bb.build.TaskBase):
257 mw.appendText("NOTE: %s\n" % event._message)
258 if isinstance(event, logging.LogRecord):
259 mw.appendText(logging.getLevelName(event.levelno) + ': ' + event.getMessage() + '\n')
260
261 if isinstance(event, bb.event.CacheLoadStarted):
262 self.parse_total = event.total
263 if isinstance(event, bb.event.CacheLoadProgress):
264 x = event.current
265 y = self.parse_total
266 mw.setStatus("Loading Cache: %s [%2d %%]" % ( next(parsespin), x*100/y ) )
267 if isinstance(event, bb.event.CacheLoadCompleted):
268 mw.setStatus("Idle")
269 mw.appendText("Loaded %d entries from dependency cache.\n"
270 % ( event.num_entries))
271
272 if isinstance(event, bb.event.ParseStarted):
273 self.parse_total = event.total
274 if isinstance(event, bb.event.ParseProgress):
275 x = event.current
276 y = self.parse_total
277 mw.setStatus("Parsing Recipes: %s [%2d %%]" % ( next(parsespin), x*100/y ) )
278 if isinstance(event, bb.event.ParseCompleted):
279 mw.setStatus("Idle")
280 mw.appendText("Parsing finished. %d cached, %d parsed, %d skipped, %d masked.\n"
281 % ( event.cached, event.parsed, event.skipped, event.masked ))
282
283# if isinstance(event, bb.build.TaskFailed):
284# if event.logfile:
285# if data.getVar("BBINCLUDELOGS", d):
286# bb.error("log data follows (%s)" % logfile)
287# number_of_lines = data.getVar("BBINCLUDELOGS_LINES", d)
288# if number_of_lines:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500289# subprocess.check_call('tail -n%s %s' % (number_of_lines, logfile), shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500290# else:
291# f = open(logfile, "r")
292# while True:
293# l = f.readline()
294# if l == '':
295# break
296# l = l.rstrip()
297# print '| %s' % l
298# f.close()
299# else:
300# bb.error("see log in %s" % logfile)
301
302 if isinstance(event, bb.command.CommandCompleted):
303 # stop so the user can see the result of the build, but
304 # also allow them to now exit with a single ^C
305 shutdown = 2
306 if isinstance(event, bb.command.CommandFailed):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500307 mw.appendText(str(event))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500308 time.sleep(2)
309 exitflag = True
310 if isinstance(event, bb.command.CommandExit):
311 exitflag = True
312 if isinstance(event, bb.cooker.CookerExit):
313 exitflag = True
314
315 if isinstance(event, bb.event.LogExecTTY):
316 mw.appendText('WARN: ' + event.msg + '\n')
317 if helper.needUpdate:
318 activetasks, failedtasks = helper.getTasks()
319 taw.erase()
320 taw.setText(0, 0, "")
321 if activetasks:
322 taw.appendText("Active Tasks:\n")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600323 for task in activetasks.values():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500324 taw.appendText(task["title"] + '\n')
325 if failedtasks:
326 taw.appendText("Failed Tasks:\n")
327 for task in failedtasks:
328 taw.appendText(task["title"] + '\n')
329
330 curses.doupdate()
331 except EnvironmentError as ioerror:
332 # ignore interrupted io
333 if ioerror.args[0] == 4:
334 pass
335
336 except KeyboardInterrupt:
337 if shutdown == 2:
338 mw.appendText("Third Keyboard Interrupt, exit.\n")
339 exitflag = True
340 if shutdown == 1:
341 mw.appendText("Second Keyboard Interrupt, stopping...\n")
342 _, error = server.runCommand(["stateForceShutdown"])
343 if error:
344 print("Unable to cleanly stop: %s" % error)
345 if shutdown == 0:
346 mw.appendText("Keyboard Interrupt, closing down...\n")
347 _, error = server.runCommand(["stateShutdown"])
348 if error:
349 print("Unable to cleanly shutdown: %s" % error)
350 shutdown = shutdown + 1
351 pass
352
353def main(server, eventHandler, params):
354 if not os.isatty(sys.stdout.fileno()):
355 print("FATAL: Unable to run 'ncurses' UI without a TTY.")
356 return
357 ui = NCursesUI()
358 try:
359 curses.wrapper(ui.main, server, eventHandler, params)
360 except:
361 import traceback
362 traceback.print_exc()