command.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. """
  2. BitBake 'Command' module
  3. Provide an interface to interact with the bitbake server through 'commands'
  4. """
  5. # Copyright (C) 2006-2007 Richard Purdie
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License version 2 as
  9. # published by the Free Software Foundation.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. """
  20. The bitbake server takes 'commands' from its UI/commandline.
  21. Commands are either synchronous or asynchronous.
  22. Async commands return data to the client in the form of events.
  23. Sync commands must only return data through the function return value
  24. and must not trigger events, directly or indirectly.
  25. Commands are queued in a CommandQueue
  26. """
  27. import bb.event
  28. import bb.cooker
  29. class CommandCompleted(bb.event.Event):
  30. pass
  31. class CommandExit(bb.event.Event):
  32. def __init__(self, exitcode):
  33. bb.event.Event.__init__(self)
  34. self.exitcode = int(exitcode)
  35. class CommandFailed(CommandExit):
  36. def __init__(self, message):
  37. self.error = message
  38. CommandExit.__init__(self, 1)
  39. class CommandError(Exception):
  40. pass
  41. class Command:
  42. """
  43. A queue of asynchronous commands for bitbake
  44. """
  45. def __init__(self, cooker):
  46. self.cooker = cooker
  47. self.cmds_sync = CommandsSync()
  48. self.cmds_async = CommandsAsync()
  49. # FIXME Add lock for this
  50. self.currentAsyncCommand = None
  51. def runCommand(self, commandline):
  52. command = commandline.pop(0)
  53. if hasattr(CommandsSync, command):
  54. # Can run synchronous commands straight away
  55. command_method = getattr(self.cmds_sync, command)
  56. try:
  57. result = command_method(self, commandline)
  58. except CommandError as exc:
  59. return None, exc.args[0]
  60. except Exception:
  61. return None, traceback.format_exc()
  62. else:
  63. return result, None
  64. if self.currentAsyncCommand is not None:
  65. return None, "Busy (%s in progress)" % self.currentAsyncCommand[0]
  66. if command not in CommandsAsync.__dict__:
  67. return None, "No such command"
  68. self.currentAsyncCommand = (command, commandline)
  69. self.cooker.server_registration_cb(self.cooker.runCommands, self.cooker)
  70. return True, None
  71. def runAsyncCommand(self):
  72. try:
  73. if self.currentAsyncCommand is not None:
  74. (command, options) = self.currentAsyncCommand
  75. commandmethod = getattr(CommandsAsync, command)
  76. needcache = getattr( commandmethod, "needcache" )
  77. if (needcache and self.cooker.state in
  78. (bb.cooker.state.initial, bb.cooker.state.parsing)):
  79. self.cooker.updateCache()
  80. return True
  81. else:
  82. commandmethod(self.cmds_async, self, options)
  83. return False
  84. else:
  85. return False
  86. except KeyboardInterrupt as exc:
  87. self.finishAsyncCommand("Interrupted")
  88. return False
  89. except SystemExit as exc:
  90. arg = exc.args[0]
  91. if isinstance(arg, basestring):
  92. self.finishAsyncCommand(arg)
  93. else:
  94. self.finishAsyncCommand("Exited with %s" % arg)
  95. return False
  96. except Exception as exc:
  97. import traceback
  98. if isinstance(exc, bb.BBHandledException):
  99. self.finishAsyncCommand("")
  100. else:
  101. self.finishAsyncCommand(traceback.format_exc())
  102. return False
  103. def finishAsyncCommand(self, msg=None, code=None):
  104. if msg:
  105. bb.event.fire(CommandFailed(msg), self.cooker.configuration.event_data)
  106. elif code:
  107. bb.event.fire(CommandExit(code), self.cooker.configuration.event_data)
  108. else:
  109. bb.event.fire(CommandCompleted(), self.cooker.configuration.event_data)
  110. self.currentAsyncCommand = None
  111. class CommandsSync:
  112. """
  113. A class of synchronous commands
  114. These should run quickly so as not to hurt interactive performance.
  115. These must not influence any running synchronous command.
  116. """
  117. def stateShutdown(self, command, params):
  118. """
  119. Trigger cooker 'shutdown' mode
  120. """
  121. command.cooker.shutdown()
  122. def stateStop(self, command, params):
  123. """
  124. Stop the cooker
  125. """
  126. command.cooker.stop()
  127. def getCmdLineAction(self, command, params):
  128. """
  129. Get any command parsed from the commandline
  130. """
  131. cmd_action = command.cooker.commandlineAction
  132. if cmd_action['msg']:
  133. raise CommandError(msg)
  134. else:
  135. return cmd_action['action']
  136. def getVariable(self, command, params):
  137. """
  138. Read the value of a variable from configuration.data
  139. """
  140. varname = params[0]
  141. expand = True
  142. if len(params) > 1:
  143. expand = params[1]
  144. return command.cooker.configuration.data.getVar(varname, expand)
  145. def setVariable(self, command, params):
  146. """
  147. Set the value of variable in configuration.data
  148. """
  149. varname = params[0]
  150. value = params[1]
  151. command.cooker.configuration.data.setVar(varname, value)
  152. def initCooker(self, command, params):
  153. """
  154. Init the cooker to initial state with nothing parsed
  155. """
  156. command.cooker.initialize()
  157. def resetCooker(self, command, params):
  158. """
  159. Reset the cooker to its initial state, thus forcing a reparse for
  160. any async command that has the needcache property set to True
  161. """
  162. command.cooker.reset()
  163. def getCpuCount(self, command, params):
  164. """
  165. Get the CPU count on the bitbake server
  166. """
  167. return bb.utils.cpu_count()
  168. def setConfFilter(self, command, params):
  169. """
  170. Set the configuration file parsing filter
  171. """
  172. filterfunc = params[0]
  173. bb.parse.parse_py.ConfHandler.confFilters.append(filterfunc)
  174. class CommandsAsync:
  175. """
  176. A class of asynchronous commands
  177. These functions communicate via generated events.
  178. Any function that requires metadata parsing should be here.
  179. """
  180. def buildFile(self, command, params):
  181. """
  182. Build a single specified .bb file
  183. """
  184. bfile = params[0]
  185. task = params[1]
  186. command.cooker.buildFile(bfile, task)
  187. buildFile.needcache = False
  188. def buildTargets(self, command, params):
  189. """
  190. Build a set of targets
  191. """
  192. pkgs_to_build = params[0]
  193. task = params[1]
  194. command.cooker.buildTargets(pkgs_to_build, task)
  195. buildTargets.needcache = True
  196. def generateDepTreeEvent(self, command, params):
  197. """
  198. Generate an event containing the dependency information
  199. """
  200. pkgs_to_build = params[0]
  201. task = params[1]
  202. command.cooker.generateDepTreeEvent(pkgs_to_build, task)
  203. command.finishAsyncCommand()
  204. generateDepTreeEvent.needcache = True
  205. def generateDotGraph(self, command, params):
  206. """
  207. Dump dependency information to disk as .dot files
  208. """
  209. pkgs_to_build = params[0]
  210. task = params[1]
  211. command.cooker.generateDotGraphFiles(pkgs_to_build, task)
  212. command.finishAsyncCommand()
  213. generateDotGraph.needcache = True
  214. def generateTargetsTree(self, command, params):
  215. """
  216. Generate a tree of buildable targets.
  217. If klass is provided ensure all recipes that inherit the class are
  218. included in the package list.
  219. If pkg_list provided use that list (plus any extras brought in by
  220. klass) rather than generating a tree for all packages.
  221. """
  222. klass = params[0]
  223. pkg_list = params[1]
  224. command.cooker.generateTargetsTree(klass, pkg_list)
  225. command.finishAsyncCommand()
  226. generateTargetsTree.needcache = True
  227. def findCoreBaseFiles(self, command, params):
  228. """
  229. Find certain files in COREBASE directory. i.e. Layers
  230. """
  231. subdir = params[0]
  232. filename = params[1]
  233. command.cooker.findCoreBaseFiles(subdir, filename)
  234. command.finishAsyncCommand()
  235. findCoreBaseFiles.needcache = False
  236. def findConfigFiles(self, command, params):
  237. """
  238. Find config files which provide appropriate values
  239. for the passed configuration variable. i.e. MACHINE
  240. """
  241. varname = params[0]
  242. command.cooker.findConfigFiles(varname)
  243. command.finishAsyncCommand()
  244. findConfigFiles.needcache = False
  245. def findFilesMatchingInDir(self, command, params):
  246. """
  247. Find implementation files matching the specified pattern
  248. in the requested subdirectory of a BBPATH
  249. """
  250. pattern = params[0]
  251. directory = params[1]
  252. command.cooker.findFilesMatchingInDir(pattern, directory)
  253. command.finishAsyncCommand()
  254. findFilesMatchingInDir.needcache = False
  255. def findConfigFilePath(self, command, params):
  256. """
  257. Find the path of the requested configuration file
  258. """
  259. configfile = params[0]
  260. command.cooker.findConfigFilePath(configfile)
  261. command.finishAsyncCommand()
  262. findConfigFilePath.needcache = False
  263. def showVersions(self, command, params):
  264. """
  265. Show the currently selected versions
  266. """
  267. command.cooker.showVersions()
  268. command.finishAsyncCommand()
  269. showVersions.needcache = True
  270. def showEnvironmentTarget(self, command, params):
  271. """
  272. Print the environment of a target recipe
  273. (needs the cache to work out which recipe to use)
  274. """
  275. pkg = params[0]
  276. command.cooker.showEnvironment(None, pkg)
  277. command.finishAsyncCommand()
  278. showEnvironmentTarget.needcache = True
  279. def showEnvironment(self, command, params):
  280. """
  281. Print the standard environment
  282. or if specified the environment for a specified recipe
  283. """
  284. bfile = params[0]
  285. command.cooker.showEnvironment(bfile)
  286. command.finishAsyncCommand()
  287. showEnvironment.needcache = False
  288. def parseFiles(self, command, params):
  289. """
  290. Parse the .bb files
  291. """
  292. command.cooker.updateCache()
  293. command.finishAsyncCommand()
  294. parseFiles.needcache = True
  295. def reparseFiles(self, command, params):
  296. """
  297. Reparse .bb files
  298. """
  299. command.cooker.reparseFiles()
  300. command.finishAsyncCommand()
  301. reparseFiles.needcache = True
  302. def compareRevisions(self, command, params):
  303. """
  304. Parse the .bb files
  305. """
  306. if bb.fetch.fetcher_compare_revisions(command.cooker.configuration.data):
  307. command.finishAsyncCommand(code=1)
  308. else:
  309. command.finishAsyncCommand()
  310. compareRevisions.needcache = True
  311. def parseConfigurationFiles(self, command, params):
  312. """
  313. Parse the configuration files
  314. """
  315. prefiles = params[0]
  316. postfiles = params[1]
  317. command.cooker.parseConfigurationFiles(prefiles, postfiles)
  318. command.finishAsyncCommand()
  319. parseConfigurationFiles.needcache = False
  320. def triggerEvent(self, command, params):
  321. """
  322. Trigger a certain event
  323. """
  324. event = params[0]
  325. bb.event.fire(eval(event), command.cooker.configuration.data)
  326. command.currentAsyncCommand = None
  327. triggerEvent.needcache = False