2 # ex:ts=4:sw=4:sts=4:et
3 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4 ##########################################################################
6 # Copyright (C) 2005 Michael 'Mickey' Lauer <mickey@Vanille.de>, Vanille Media
8 # This program is free software; you can redistribute it and/or modify it under
9 # the terms of the GNU General Public License as published by the Free Software
10 # Foundation; version 2 of the License.
12 # This program is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 # Place, Suite 330, Boston, MA 02111-1307 USA.
20 ##########################################################################
28 * command to reparse just one (or more) bbfile(s)
29 * automatic check if reparsing is necessary (inotify?)
30 * frontend for bb file manipulation?
31 * pipe output of commands into a shell command (i.e grep or sort)?
32 * job control, i.e. bring commands into background with '&', fg, bg, etc.?
33 * start parsing in background right after startup?
34 * use ; to supply more than one commnd per line
35 * command aliases / shortcuts?
37 * capture bb exceptions occuring during task execution
40 ##########################################################################
41 # Import and setup global variables
42 ##########################################################################
47 from sets import Set as set
48 import sys, os, imp, readline
49 imp.load_source( "bitbake", os.path.dirname( sys.argv[0] )+"/bitbake" )
50 from bb import make, data, parse, fatal
53 __credits__ = """BitBake Shell Version %s (C) 2005 Michael 'Mickey' Lauer <mickey@Vanille.de>
54 Type 'help' for more information, press CTRL-D to exit.""" % __version__
57 leave_mainloop = False
60 debug = os.environ.get( "BBSHELL_DEBUG", "" ) != ""
61 history_file = "%s/.bbsh_history" % os.environ.get( "HOME" )
63 ##########################################################################
65 ##########################################################################
67 def buildCommand( params, cmd = "build" ):
68 """Build a providee"""
71 oldcmd = make.options.cmd
72 make.options.cmd = cmd
73 cooker.build_cache = []
74 cooker.build_cache_fail = []
77 print "SHELL: D'oh! The .bb files haven't been parsed yet. Next time call 'parse' before building stuff. This time I'll do it for 'ya."
79 cooker.buildProvider( name )
81 make.options.cmd = oldcmd
83 def cleanCommand( params ):
84 """Clean a providee"""
85 buildCommand( params, "clean" )
87 def editCommand( params ):
88 """Call $EDITOR on a .bb file"""
90 os.system( "%s %s" % ( os.environ.get( "EDITOR" ), completeFilePath( name ) ) )
92 def environmentCommand( params ):
93 """Dump out the outer BitBake environment (see bbread)"""
94 data.emit_env(sys.__stdout__, make.cfg, True)
96 def execCommand( params ):
97 """Execute one line of python code"""
98 exec " ".join( params ) in locals(), globals()
100 def exitShell( params ):
101 """Leave the BitBake Shell"""
102 if debug: print "(setting leave_mainloop to true)"
103 global leave_mainloop
104 leave_mainloop = True
106 def fileBuildCommand( params, cmd = "build" ):
107 """Parse and build a .bb file"""
109 bf = completeFilePath( name )
110 print "SHELL: Calling '%s' on '%s'" % ( cmd, bf )
112 oldcmd = make.options.cmd
113 make.options.cmd = cmd
114 cooker.build_cache = []
115 cooker.build_cache_fail = []
118 bbfile_data = parse.handle( bf, make.cfg )
120 print "SHELL: ERROR: Unable to open %s" % bf
122 item = data.getVar('PN', bbfile_data, 1)
123 data.setVar( "_task_cache", [], bbfile_data ) # force
124 cooker.tryBuildPackage( os.path.abspath( bf ), item, bbfile_data )
126 make.options.cmd = oldcmd
129 def fileCleanCommand( params ):
130 """Clean a .bb file"""
131 fileBuildCommand( params, "clean" )
133 def fileRebuildCommand( params ):
134 """Rebuild (clean & build) a .bb file"""
135 fileCleanCommand( params )
136 fileBuildCommand( params )
138 def newCommand( params ):
139 """Create a new .bb file and open the editor"""
140 dirname, filename = params
141 packages = '/'.join( data.getVar( "BBFILES", make.cfg, 1 ).split('/')[:-2] )
142 fulldirname = "%s/%s" % ( packages, dirname )
144 if not os.path.exists( fulldirname ):
145 print "SHELL: Creating '%s'" % fulldirname
146 os.mkdir( fulldirname )
147 if os.path.exists( fulldirname ) and os.path.isdir( fulldirname ):
148 if os.path.exists( "%s/%s" % ( fulldirname, filename ) ):
149 print "SHELL: ERROR: %s/%s already exists" % ( fulldirname, filename )
151 print "SHELL: Creating '%s/%s'" % ( fulldirname, filename )
152 newpackage = open( "%s/%s" % ( fulldirname, filename ), "w" )
153 print >>newpackage,"""DESCRIPTION = ""
181 os.system( "%s %s/%s" % ( os.environ.get( "EDITOR" ), fulldirname, filename ) )
183 def parseCommand( params ):
184 """(Re-)parse .bb files and calculate the dependency graph"""
185 cooker.status = cooker.ParsingStatus()
186 ignore = data.getVar("ASSUME_PROVIDED", make.cfg, 1) or ""
187 cooker.status.ignored_dependencies = set( ignore.split() )
188 cooker.handleCollections( data.getVar("BBFILE_COLLECTIONS", make.cfg, 1) )
190 make.collect_bbfiles( cooker.myProgressCallback )
191 cooker.buildDepgraph()
196 def printCommand( params ):
197 """Print the contents of an outer BitBake environment variable"""
199 value = data.getVar( var, make.cfg, 1 )
202 def pythonCommand( params ):
203 """Enter the expert mode - an interactive BitBake Python Interpreter"""
204 sys.ps1 = "EXPERT BB>>> "
205 sys.ps2 = "EXPERT BB... "
207 python = code.InteractiveConsole( dict( globals() ) )
208 python.interact( "SHELL: Expert Mode - BitBake Python %s\nType 'help' for more information, press CTRL-D to switch back to BBSHELL." % sys.version )
210 def setVarCommand( params ):
211 """Set an outer BitBake environment variable"""
213 data.setVar( var, value, make.cfg )
216 def rebuildCommand( params ):
217 """Clean and rebuild a .bb file or a providee"""
218 buildCommand( params, "clean" )
219 buildCommand( params, "build" )
221 def statusCommand( params ):
223 print "build cache = '%s'" % cooker.build_cache
224 print "build cache fail = '%s'" % cooker.build_cache_fail
225 print "building list = '%s'" % cooker.building_list
226 print "build path = '%s'" % cooker.build_path
227 print "consider_msgs_cache = '%s'" % cooker.consider_msgs_cache
228 print "build stats = '%s'" % cooker.stats
230 def testCommand( params ):
231 """Just for testing..."""
232 print "testCommand called with '%s'" % params
234 def whichCommand( params ):
235 """Computes the providers for a given providee"""
239 print "SHELL: D'oh! The .bb files haven't been parsed yet. Next time call 'parse' before building stuff. This time I'll do it for 'ya."
242 preferred = data.getVar( "PREFERRED_PROVIDER_%s" % item, make.cfg, 1 )
243 if not preferred: preferred = item
246 lv, lf, pv, pf = cooker.findBestProvider( preferred )
248 lv, lf, pv, pf = (None,)*4
251 providers = cooker.status.providers[item]
253 print "SHELL: ERROR: Nothing provides", preferred
255 for provider in providers:
256 if provider == pf: provider = " (***) %s" % provider
257 else: provider = " %s" % provider
260 ##########################################################################
261 # Common helper functions
262 ##########################################################################
264 def completeFilePath( bbfile ):
265 if not make.pkgdata: return bbfile
266 for key in make.pkgdata.keys():
267 if key.endswith( bbfile ):
271 ##########################################################################
273 ##########################################################################
276 """Register commands and set up readline"""
277 registerCommand( "help", showHelp )
278 registerCommand( "exit", exitShell )
280 registerCommand( "build", buildCommand, 1, "build <providee>" )
281 registerCommand( "clean", cleanCommand, 1, "clean <providee>" )
282 registerCommand( "edit", editCommand, 1, "edit <bbfile>" )
283 registerCommand( "environment", environmentCommand )
284 registerCommand( "exec", execCommand, 1, "exec <one line of pythoncode>" )
285 registerCommand( "filebuild", fileBuildCommand, 1, "filebuild <bbfile>" )
286 registerCommand( "fileclean", fileCleanCommand, 1, "fileclean <bbfile>" )
287 registerCommand( "filerebuild", fileRebuildCommand, 1, "filerebuild <bbfile>" )
288 registerCommand( "new", newCommand, 2, "new <directory> <bbfile>" )
289 registerCommand( "parse", parseCommand )
290 registerCommand( "print", printCommand, 1, "print <variable>" )
291 registerCommand( "python", pythonCommand )
292 registerCommand( "rebuild", rebuildCommand, 1, "rebuild <providee>" )
293 registerCommand( "set", setVarCommand, 2, "set <variable> <value>" )
294 registerCommand( "status", statusCommand )
295 registerCommand( "test", testCommand )
296 registerCommand( "which", whichCommand, 1, "which <providee>" )
298 readline.set_completer( completer )
299 readline.set_completer_delims( " " )
300 readline.parse_and_bind("tab: complete")
304 readline.read_history_file( history_file )
306 pass # It doesn't exist yet.
309 """Write readline history and clean up resources"""
310 if debug: print "(writing command history)"
313 readline.write_history_file( history_file )
315 print "SHELL: Unable to save command history"
317 def completer( text, state ):
318 """Return a possible readline completion"""
319 if debug: print "(completer called with text='%s', state='%d'" % ( text, state )
322 line = readline.get_line_buffer()
325 # we are in second (or more) argument
326 if line[0] == "print" or line[0] == "set":
327 allmatches = make.cfg.keys()
328 elif line[0].startswith( "file" ):
329 if make.pkgdata is None: allmatches = [ "(No Matches Available. Parsed yet?)" ]
330 else: allmatches = [ x.split("/")[-1] for x in make.pkgdata.keys() ]
331 elif line[0] == "build" or line[0] == "clean" or line[0] == "which":
332 if make.pkgdata is None: allmatches = [ "(No Matches Available. Parsed yet?)" ]
333 else: allmatches = cooker.status.providers.iterkeys()
334 else: allmatches = [ "(No tab completion available for this command)" ]
336 # we are in first argument
337 allmatches = cmds.iterkeys()
339 completer.matches = [ x for x in allmatches if x[:len(text)] == text ]
340 #print "completer.matches = '%s'" % completer.matches
341 if len( completer.matches ) > state:
342 return completer.matches[state]
347 """Show credits (sic!)"""
350 def showHelp( *args ):
351 """Show a comprehensive list of commands and their purpose"""
352 print "="*35, "Available Commands", "="*35
353 allcmds = cmds.keys()
356 function,numparams,usage,helptext = cmds[cmd]
357 print "| %s | %s" % (usage.ljust(35), helptext)
360 def registerCommand( command, function, numparams = 0, usage = "", helptext = "" ):
361 """Register a command"""
362 if usage == "": usage = command
363 if helptext == "": helptext = function.__doc__ or "<not yet documented>"
364 cmds[command] = ( function, numparams, usage, helptext )
366 def processCommand( command, params ):
367 """Process a command. Check number of params and print a usage string, if appropriate"""
368 if debug: print "(processing command '%s'...)" % command
370 function, numparams, usage, helptext = cmds[command]
372 print "SHELL: ERROR: '%s' command is not a valid command." % command
374 if not len( params ) == numparams:
375 print "Usage: '%s'" % usage
377 result = function( params )
378 if debug: print "(result was '%s')" % result
381 """The main command loop"""
382 while not leave_mainloop:
384 cmdline = raw_input( "BB>> " )
386 commands = cmdline.split( ';' )
387 for command in commands:
389 processCommand( command.split()[0], command.split()[1:] )
391 processCommand( command, "" )
395 except KeyboardInterrupt:
398 def start( aCooker ):
406 if __name__ == "__main__":
407 print "SHELL: Sorry, this program should only be called by BitBake."