knotty.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #
  2. # BitBake (No)TTY UI Implementation
  3. #
  4. # Handling output to TTYs or files (no TTY)
  5. #
  6. # Copyright (C) 2006-2012 Richard Purdie
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License version 2 as
  10. # published by the Free Software Foundation.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. from __future__ import division
  21. import os
  22. import sys
  23. import xmlrpclib
  24. import logging
  25. import progressbar
  26. import bb.msg
  27. from bb.ui import uihelper
  28. logger = logging.getLogger("BitBake")
  29. interactive = sys.stdout.isatty()
  30. class BBProgress(progressbar.ProgressBar):
  31. def __init__(self, msg, maxval):
  32. self.msg = msg
  33. widgets = [progressbar.Percentage(), ' ', progressbar.Bar(), ' ',
  34. progressbar.ETA()]
  35. progressbar.ProgressBar.__init__(self, maxval, [self.msg + ": "] + widgets)
  36. class NonInteractiveProgress(object):
  37. fobj = sys.stdout
  38. def __init__(self, msg, maxval):
  39. self.msg = msg
  40. self.maxval = maxval
  41. def start(self):
  42. self.fobj.write("%s..." % self.msg)
  43. self.fobj.flush()
  44. return self
  45. def update(self, value):
  46. pass
  47. def finish(self):
  48. self.fobj.write("done.\n")
  49. self.fobj.flush()
  50. def new_progress(msg, maxval):
  51. if interactive:
  52. return BBProgress(msg, maxval)
  53. else:
  54. return NonInteractiveProgress(msg, maxval)
  55. def pluralise(singular, plural, qty):
  56. if(qty == 1):
  57. return singular % qty
  58. else:
  59. return plural % qty
  60. class TerminalFilter(object):
  61. def __init__(self, main, helper, console, format):
  62. self.main = main
  63. self.helper = helper
  64. def clearFooter(self):
  65. return
  66. def updateFooter(self):
  67. if not main.shutdown or not self.helper.needUpdate:
  68. return
  69. activetasks = self.helper.running_tasks
  70. runningpids = self.helper.running_pids
  71. if len(runningpids) == 0:
  72. return
  73. self.helper.getTasks()
  74. tasks = []
  75. for t in runningpids:
  76. tasks.append("%s (pid %s)" % (activetasks[t]["title"], t))
  77. if main.shutdown:
  78. print("Waiting for %s running tasks to finish:" % len(activetasks))
  79. else:
  80. print("Currently %s running tasks (%s of %s):" % (len(activetasks), self.helper.tasknumber_current, self.helper.tasknumber_total))
  81. for tasknum, task in enumerate(tasks):
  82. print("%s: %s" % (tasknum, task))
  83. def finish(self):
  84. return
  85. def main(server, eventHandler, tf = TerminalFilter):
  86. # Get values of variables which control our output
  87. includelogs, error = server.runCommand(["getVariable", "BBINCLUDELOGS"])
  88. if error:
  89. logger.error("Unable to get the value of BBINCLUDELOGS variable: %s" % error)
  90. return 1
  91. loglines, error = server.runCommand(["getVariable", "BBINCLUDELOGS_LINES"])
  92. if error:
  93. logger.error("Unable to get the value of BBINCLUDELOGS_LINES variable: %s" % error)
  94. return 1
  95. consolelogfile, error = server.runCommand(["getVariable", "BB_CONSOLELOG"])
  96. if error:
  97. logger.error("Unable to get the value of BB_CONSOLELOG variable: %s" % error)
  98. return 1
  99. helper = uihelper.BBUIHelper()
  100. console = logging.StreamHandler(sys.stdout)
  101. format = bb.msg.BBLogFormatter("%(levelname)s: %(message)s")
  102. bb.msg.addDefaultlogFilter(console)
  103. console.setFormatter(format)
  104. logger.addHandler(console)
  105. if consolelogfile:
  106. consolelog = logging.FileHandler(consolelogfile)
  107. bb.msg.addDefaultlogFilter(consolelog)
  108. consolelog.setFormatter(format)
  109. logger.addHandler(consolelog)
  110. try:
  111. cmdline, error = server.runCommand(["getCmdLineAction"])
  112. if error:
  113. logger.error("Unable to get bitbake commandline arguments: %s" % error)
  114. return 1
  115. elif not cmdline:
  116. print("Nothing to do. Use 'bitbake world' to build everything, or run 'bitbake --help' for usage information.")
  117. return 1
  118. ret, error = server.runCommand(cmdline)
  119. if error:
  120. logger.error("Command '%s' failed: %s" % (cmdline, error))
  121. return 1
  122. elif ret != True:
  123. logger.error("Command '%s' failed: returned %s" % (cmdline, ret))
  124. return 1
  125. except xmlrpclib.Fault as x:
  126. logger.error("XMLRPC Fault getting commandline:\n %s" % x)
  127. return 1
  128. parseprogress = None
  129. cacheprogress = None
  130. main.shutdown = 0
  131. interrupted = False
  132. return_value = 0
  133. errors = 0
  134. warnings = 0
  135. taskfailures = []
  136. termfilter = tf(main, helper, console, format)
  137. while True:
  138. try:
  139. termfilter.updateFooter()
  140. event = eventHandler.waitEvent(0.25)
  141. if event is None:
  142. if main.shutdown > 1:
  143. break
  144. continue
  145. helper.eventHandler(event)
  146. if isinstance(event, bb.runqueue.runQueueExitWait):
  147. if not main.shutdown:
  148. main.shutdown = 1
  149. if isinstance(event, logging.LogRecord):
  150. if event.levelno >= format.ERROR:
  151. errors = errors + 1
  152. return_value = 1
  153. elif event.levelno == format.WARNING:
  154. warnings = warnings + 1
  155. # For "normal" logging conditions, don't show note logs from tasks
  156. # but do show them if the user has changed the default log level to
  157. # include verbose/debug messages
  158. if event.taskpid != 0 and event.levelno <= format.NOTE:
  159. continue
  160. logger.handle(event)
  161. continue
  162. if isinstance(event, bb.build.TaskFailed):
  163. return_value = 1
  164. logfile = event.logfile
  165. if logfile and os.path.exists(logfile):
  166. termfilter.clearFooter()
  167. print("ERROR: Logfile of failure stored in: %s" % logfile)
  168. if includelogs and not event.errprinted:
  169. print("Log data follows:")
  170. f = open(logfile, "r")
  171. lines = []
  172. while True:
  173. l = f.readline()
  174. if l == '':
  175. break
  176. l = l.rstrip()
  177. if loglines:
  178. lines.append(' | %s' % l)
  179. if len(lines) > int(loglines):
  180. lines.pop(0)
  181. else:
  182. print('| %s' % l)
  183. f.close()
  184. if lines:
  185. for line in lines:
  186. print(line)
  187. if isinstance(event, bb.build.TaskBase):
  188. logger.info(event._message)
  189. continue
  190. if isinstance(event, bb.event.ParseStarted):
  191. if event.total == 0:
  192. continue
  193. parseprogress = new_progress("Parsing recipes", event.total).start()
  194. continue
  195. if isinstance(event, bb.event.ParseProgress):
  196. parseprogress.update(event.current)
  197. continue
  198. if isinstance(event, bb.event.ParseCompleted):
  199. if not parseprogress:
  200. continue
  201. parseprogress.finish()
  202. print(("Parsing of %d .bb files complete (%d cached, %d parsed). %d targets, %d skipped, %d masked, %d errors."
  203. % ( event.total, event.cached, event.parsed, event.virtuals, event.skipped, event.masked, event.errors)))
  204. continue
  205. if isinstance(event, bb.event.CacheLoadStarted):
  206. cacheprogress = new_progress("Loading cache", event.total).start()
  207. continue
  208. if isinstance(event, bb.event.CacheLoadProgress):
  209. cacheprogress.update(event.current)
  210. continue
  211. if isinstance(event, bb.event.CacheLoadCompleted):
  212. cacheprogress.finish()
  213. print("Loaded %d entries from dependency cache." % event.num_entries)
  214. continue
  215. if isinstance(event, bb.command.CommandFailed):
  216. return_value = event.exitcode
  217. errors = errors + 1
  218. logger.error("Command execution failed: %s", event.error)
  219. main.shutdown = 2
  220. continue
  221. if isinstance(event, bb.command.CommandExit):
  222. if not return_value:
  223. return_value = event.exitcode
  224. continue
  225. if isinstance(event, (bb.command.CommandCompleted, bb.cooker.CookerExit)):
  226. main.shutdown = 2
  227. continue
  228. if isinstance(event, bb.event.MultipleProviders):
  229. logger.info("multiple providers are available for %s%s (%s)", event._is_runtime and "runtime " or "",
  230. event._item,
  231. ", ".join(event._candidates))
  232. logger.info("consider defining a PREFERRED_PROVIDER entry to match %s", event._item)
  233. continue
  234. if isinstance(event, bb.event.NoProvider):
  235. return_value = 1
  236. errors = errors + 1
  237. if event._runtime:
  238. r = "R"
  239. else:
  240. r = ""
  241. if event._dependees:
  242. logger.error("Nothing %sPROVIDES '%s' (but %s %sDEPENDS on or otherwise requires it)", r, event._item, ", ".join(event._dependees), r)
  243. else:
  244. logger.error("Nothing %sPROVIDES '%s'", r, event._item)
  245. if event._reasons:
  246. for reason in event._reasons:
  247. logger.error("%s", reason)
  248. continue
  249. if isinstance(event, bb.runqueue.sceneQueueTaskStarted):
  250. logger.info("Running setscene task %d of %d (%s)" % (event.stats.completed + event.stats.active + event.stats.failed + 1, event.stats.total, event.taskstring))
  251. continue
  252. if isinstance(event, bb.runqueue.runQueueTaskStarted):
  253. if event.noexec:
  254. tasktype = 'noexec task'
  255. else:
  256. tasktype = 'task'
  257. logger.info("Running %s %s of %s (ID: %s, %s)",
  258. tasktype,
  259. event.stats.completed + event.stats.active +
  260. event.stats.failed + 1,
  261. event.stats.total, event.taskid, event.taskstring)
  262. continue
  263. if isinstance(event, bb.runqueue.runQueueTaskFailed):
  264. taskfailures.append(event.taskstring)
  265. logger.error("Task %s (%s) failed with exit code '%s'",
  266. event.taskid, event.taskstring, event.exitcode)
  267. continue
  268. if isinstance(event, bb.runqueue.sceneQueueTaskFailed):
  269. logger.warn("Setscene task %s (%s) failed with exit code '%s' - real task will be run instead",
  270. event.taskid, event.taskstring, event.exitcode)
  271. continue
  272. # ignore
  273. if isinstance(event, (bb.event.BuildBase,
  274. bb.event.StampUpdate,
  275. bb.event.ConfigParsed,
  276. bb.event.RecipeParsed,
  277. bb.event.RecipePreFinalise,
  278. bb.runqueue.runQueueEvent,
  279. bb.runqueue.runQueueExitWait,
  280. bb.event.OperationStarted,
  281. bb.event.OperationCompleted,
  282. bb.event.OperationProgress)):
  283. continue
  284. logger.error("Unknown event: %s", event)
  285. except EnvironmentError as ioerror:
  286. termfilter.clearFooter()
  287. # ignore interrupted io
  288. if ioerror.args[0] == 4:
  289. pass
  290. except KeyboardInterrupt:
  291. import time
  292. termfilter.clearFooter()
  293. if main.shutdown == 1:
  294. print("\nSecond Keyboard Interrupt, stopping...\n")
  295. _, error = server.runCommand(["stateStop"])
  296. if error:
  297. logger.error("Unable to cleanly stop: %s" % error)
  298. if main.shutdown == 0:
  299. print("\nKeyboard Interrupt, closing down...\n")
  300. interrupted = True
  301. _, error = server.runCommand(["stateShutdown"])
  302. if error:
  303. logger.error("Unable to cleanly shutdown: %s" % error)
  304. main.shutdown = main.shutdown + 1
  305. pass
  306. summary = ""
  307. if taskfailures:
  308. summary += pluralise("\nSummary: %s task failed:",
  309. "\nSummary: %s tasks failed:", len(taskfailures))
  310. for failure in taskfailures:
  311. summary += "\n %s" % failure
  312. if warnings:
  313. summary += pluralise("\nSummary: There was %s WARNING message shown.",
  314. "\nSummary: There were %s WARNING messages shown.", warnings)
  315. if return_value:
  316. summary += pluralise("\nSummary: There was %s ERROR message shown, returning a non-zero exit code.",
  317. "\nSummary: There were %s ERROR messages shown, returning a non-zero exit code.", errors)
  318. if summary:
  319. print(summary)
  320. if interrupted:
  321. print("Execution was interrupted, returning a non-zero exit code.")
  322. if return_value == 0:
  323. return_value = 1
  324. termfilter.finish()
  325. return return_value