shell.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. #!/usr/bin/env python
  2. # ex:ts=4:sw=4:sts=4:et
  3. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  4. ##########################################################################
  5. #
  6. # Copyright (C) 2005-2006 Michael 'Mickey' Lauer <mickey@Vanille.de>
  7. # Copyright (C) 2005-2006 Vanille Media
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License version 2 as
  11. # published by the Free Software Foundation.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. ##########################################################################
  23. #
  24. # Thanks to:
  25. # * Holger Freyther <zecke@handhelds.org>
  26. # * Justin Patrin <papercrane@reversefold.com>
  27. #
  28. ##########################################################################
  29. """
  30. BitBake Shell
  31. IDEAS:
  32. * list defined tasks per package
  33. * list classes
  34. * toggle force
  35. * command to reparse just one (or more) bbfile(s)
  36. * automatic check if reparsing is necessary (inotify?)
  37. * frontend for bb file manipulation
  38. * more shell-like features:
  39. - output control, i.e. pipe output into grep, sort, etc.
  40. - job control, i.e. bring running commands into background and foreground
  41. * start parsing in background right after startup
  42. * ncurses interface
  43. PROBLEMS:
  44. * force doesn't always work
  45. * readline completion for commands with more than one parameters
  46. """
  47. ##########################################################################
  48. # Import and setup global variables
  49. ##########################################################################
  50. try:
  51. set
  52. except NameError:
  53. from sets import Set as set
  54. import sys, os, readline, socket, httplib, urllib, commands, popen2, copy, shlex, Queue, fnmatch
  55. from bb import data, parse, build, fatal, cache, taskdata, runqueue, providers as Providers
  56. __version__ = "0.5.3.1"
  57. __credits__ = """BitBake Shell Version %s (C) 2005 Michael 'Mickey' Lauer <mickey@Vanille.de>
  58. Type 'help' for more information, press CTRL-D to exit.""" % __version__
  59. cmds = {}
  60. leave_mainloop = False
  61. last_exception = None
  62. cooker = None
  63. parsed = False
  64. initdata = None
  65. debug = os.environ.get( "BBSHELL_DEBUG", "" )
  66. ##########################################################################
  67. # Class BitBakeShellCommands
  68. ##########################################################################
  69. class BitBakeShellCommands:
  70. """This class contains the valid commands for the shell"""
  71. def __init__( self, shell ):
  72. """Register all the commands"""
  73. self._shell = shell
  74. for attr in BitBakeShellCommands.__dict__:
  75. if not attr.startswith( "_" ):
  76. if attr.endswith( "_" ):
  77. command = attr[:-1].lower()
  78. else:
  79. command = attr[:].lower()
  80. method = getattr( BitBakeShellCommands, attr )
  81. debugOut( "registering command '%s'" % command )
  82. # scan number of arguments
  83. usage = getattr( method, "usage", "" )
  84. if usage != "<...>":
  85. numArgs = len( usage.split() )
  86. else:
  87. numArgs = -1
  88. shell.registerCommand( command, method, numArgs, "%s %s" % ( command, usage ), method.__doc__ )
  89. def _checkParsed( self ):
  90. if not parsed:
  91. print "SHELL: This command needs to parse bbfiles..."
  92. self.parse( None )
  93. def _findProvider( self, item ):
  94. self._checkParsed()
  95. preferred = data.getVar( "PREFERRED_PROVIDER_%s" % item, cooker.configuration.data, 1 )
  96. if not preferred: preferred = item
  97. try:
  98. lv, lf, pv, pf = Providers.findBestProvider(preferred, cooker.configuration.data, cooker.status, cooker.build_cache_fail)
  99. except KeyError:
  100. if item in cooker.status.providers:
  101. pf = cooker.status.providers[item][0]
  102. else:
  103. pf = None
  104. return pf
  105. def alias( self, params ):
  106. """Register a new name for a command"""
  107. new, old = params
  108. if not old in cmds:
  109. print "ERROR: Command '%s' not known" % old
  110. else:
  111. cmds[new] = cmds[old]
  112. print "OK"
  113. alias.usage = "<alias> <command>"
  114. def buffer( self, params ):
  115. """Dump specified output buffer"""
  116. index = params[0]
  117. print self._shell.myout.buffer( int( index ) )
  118. buffer.usage = "<index>"
  119. def buffers( self, params ):
  120. """Show the available output buffers"""
  121. commands = self._shell.myout.bufferedCommands()
  122. if not commands:
  123. print "SHELL: No buffered commands available yet. Start doing something."
  124. else:
  125. print "="*35, "Available Output Buffers", "="*27
  126. for index, cmd in enumerate( commands ):
  127. print "| %s %s" % ( str( index ).ljust( 3 ), cmd )
  128. print "="*88
  129. def build( self, params, cmd = "build" ):
  130. """Build a providee"""
  131. globexpr = params[0]
  132. self._checkParsed()
  133. names = globfilter( cooker.status.pkg_pn.keys(), globexpr )
  134. if len( names ) == 0: names = [ globexpr ]
  135. print "SHELL: Building %s" % ' '.join( names )
  136. oldcmd = cooker.configuration.cmd
  137. cooker.configuration.cmd = cmd
  138. cooker.build_cache = []
  139. cooker.build_cache_fail = []
  140. td = taskdata.TaskData(cooker.configuration.abort)
  141. try:
  142. tasks = []
  143. for name in names:
  144. td.add_provider(cooker.configuration.data, cooker.status, name)
  145. providers = td.get_provider(name)
  146. if len(providers) == 0:
  147. raise Providers.NoProvider
  148. tasks.append([name, "do_%s" % cooker.configuration.cmd])
  149. td.add_unresolved(cooker.configuration.data, cooker.status)
  150. rq = runqueue.RunQueue()
  151. rq.prepare_runqueue(cooker.configuration.data, cooker.status, td, tasks)
  152. rq.execute_runqueue(cooker, cooker.configuration.data, cooker.status, td, tasks)
  153. except Providers.NoProvider:
  154. print "ERROR: No Provider"
  155. global last_exception
  156. last_exception = Providers.NoProvider
  157. except runqueue.TaskFailure, fnids:
  158. for fnid in fnids:
  159. print "ERROR: '%s' failed" % td.fn_index[fnid]
  160. global last_exception
  161. last_exception = runqueue.TaskFailure
  162. except build.EventException, e:
  163. print "ERROR: Couldn't build '%s'" % names
  164. global last_exception
  165. last_exception = e
  166. cooker.configuration.cmd = oldcmd
  167. build.usage = "<providee>"
  168. def clean( self, params ):
  169. """Clean a providee"""
  170. self.build( params, "clean" )
  171. clean.usage = "<providee>"
  172. def compile( self, params ):
  173. """Execute 'compile' on a providee"""
  174. self.build( params, "compile" )
  175. compile.usage = "<providee>"
  176. def configure( self, params ):
  177. """Execute 'configure' on a providee"""
  178. self.build( params, "configure" )
  179. configure.usage = "<providee>"
  180. def edit( self, params ):
  181. """Call $EDITOR on a providee"""
  182. name = params[0]
  183. bbfile = self._findProvider( name )
  184. if bbfile is not None:
  185. os.system( "%s %s" % ( os.environ.get( "EDITOR", "vi" ), bbfile ) )
  186. else:
  187. print "ERROR: Nothing provides '%s'" % name
  188. edit.usage = "<providee>"
  189. def environment( self, params ):
  190. """Dump out the outer BitBake environment (see bbread)"""
  191. data.emit_env(sys.__stdout__, cooker.configuration.data, True)
  192. def exit_( self, params ):
  193. """Leave the BitBake Shell"""
  194. debugOut( "setting leave_mainloop to true" )
  195. global leave_mainloop
  196. leave_mainloop = True
  197. def fetch( self, params ):
  198. """Fetch a providee"""
  199. self.build( params, "fetch" )
  200. fetch.usage = "<providee>"
  201. def fileBuild( self, params, cmd = "build" ):
  202. """Parse and build a .bb file"""
  203. name = params[0]
  204. bf = completeFilePath( name )
  205. print "SHELL: Calling '%s' on '%s'" % ( cmd, bf )
  206. oldcmd = cooker.configuration.cmd
  207. cooker.configuration.cmd = cmd
  208. cooker.build_cache = []
  209. cooker.build_cache_fail = []
  210. thisdata = copy.deepcopy( initdata )
  211. # Caution: parse.handle modifies thisdata, hence it would
  212. # lead to pollution cooker.configuration.data, which is
  213. # why we use it on a safe copy we obtained from cooker right after
  214. # parsing the initial *.conf files
  215. try:
  216. bbfile_data = parse.handle( bf, thisdata )
  217. except parse.ParseError:
  218. print "ERROR: Unable to open or parse '%s'" % bf
  219. else:
  220. item = data.getVar('PN', bbfile_data, 1)
  221. data.setVar( "_task_cache", [], bbfile_data ) # force
  222. try:
  223. cooker.tryBuildPackage( os.path.abspath( bf ), item, cmd, bbfile_data, True )
  224. except build.EventException, e:
  225. print "ERROR: Couldn't build '%s'" % name
  226. global last_exception
  227. last_exception = e
  228. cooker.configuration.cmd = oldcmd
  229. fileBuild.usage = "<bbfile>"
  230. def fileClean( self, params ):
  231. """Clean a .bb file"""
  232. self.fileBuild( params, "clean" )
  233. fileClean.usage = "<bbfile>"
  234. def fileEdit( self, params ):
  235. """Call $EDITOR on a .bb file"""
  236. name = params[0]
  237. os.system( "%s %s" % ( os.environ.get( "EDITOR", "vi" ), completeFilePath( name ) ) )
  238. fileEdit.usage = "<bbfile>"
  239. def fileRebuild( self, params ):
  240. """Rebuild (clean & build) a .bb file"""
  241. self.fileBuild( params, "rebuild" )
  242. fileRebuild.usage = "<bbfile>"
  243. def fileReparse( self, params ):
  244. """(re)Parse a bb file"""
  245. bbfile = params[0]
  246. print "SHELL: Parsing '%s'" % bbfile
  247. parse.update_mtime( bbfile )
  248. cooker.bb_cache.cacheValidUpdate(bbfile)
  249. fromCache = cooker.bb_cache.loadData(bbfile, cooker.configuration.data)
  250. cooker.bb_cache.sync()
  251. if False: #fromCache:
  252. print "SHELL: File has not been updated, not reparsing"
  253. else:
  254. print "SHELL: Parsed"
  255. fileReparse.usage = "<bbfile>"
  256. def abort( self, params ):
  257. """Toggle abort task execution flag (see bitbake -k)"""
  258. cooker.configuration.abort = not cooker.configuration.abort
  259. print "SHELL: Abort Flag is now '%s'" % repr( cooker.configuration.abort )
  260. def force( self, params ):
  261. """Toggle force task execution flag (see bitbake -f)"""
  262. cooker.configuration.force = not cooker.configuration.force
  263. print "SHELL: Force Flag is now '%s'" % repr( cooker.configuration.force )
  264. def help( self, params ):
  265. """Show a comprehensive list of commands and their purpose"""
  266. print "="*30, "Available Commands", "="*30
  267. allcmds = cmds.keys()
  268. allcmds.sort()
  269. for cmd in allcmds:
  270. function,numparams,usage,helptext = cmds[cmd]
  271. print "| %s | %s" % (usage.ljust(30), helptext)
  272. print "="*78
  273. def lastError( self, params ):
  274. """Show the reason or log that was produced by the last BitBake event exception"""
  275. if last_exception is None:
  276. print "SHELL: No Errors yet (Phew)..."
  277. else:
  278. reason, event = last_exception.args
  279. print "SHELL: Reason for the last error: '%s'" % reason
  280. if ':' in reason:
  281. msg, filename = reason.split( ':' )
  282. filename = filename.strip()
  283. print "SHELL: Dumping log file for last error:"
  284. try:
  285. print open( filename ).read()
  286. except IOError:
  287. print "ERROR: Couldn't open '%s'" % filename
  288. def match( self, params ):
  289. """Dump all files or providers matching a glob expression"""
  290. what, globexpr = params
  291. if what == "files":
  292. self._checkParsed()
  293. for key in globfilter( cooker.status.pkg_fn.keys(), globexpr ): print key
  294. elif what == "providers":
  295. self._checkParsed()
  296. for key in globfilter( cooker.status.pkg_pn.keys(), globexpr ): print key
  297. else:
  298. print "Usage: match %s" % self.print_.usage
  299. match.usage = "<files|providers> <glob>"
  300. def new( self, params ):
  301. """Create a new .bb file and open the editor"""
  302. dirname, filename = params
  303. packages = '/'.join( data.getVar( "BBFILES", cooker.configuration.data, 1 ).split('/')[:-2] )
  304. fulldirname = "%s/%s" % ( packages, dirname )
  305. if not os.path.exists( fulldirname ):
  306. print "SHELL: Creating '%s'" % fulldirname
  307. os.mkdir( fulldirname )
  308. if os.path.exists( fulldirname ) and os.path.isdir( fulldirname ):
  309. if os.path.exists( "%s/%s" % ( fulldirname, filename ) ):
  310. print "SHELL: ERROR: %s/%s already exists" % ( fulldirname, filename )
  311. return False
  312. print "SHELL: Creating '%s/%s'" % ( fulldirname, filename )
  313. newpackage = open( "%s/%s" % ( fulldirname, filename ), "w" )
  314. print >>newpackage,"""DESCRIPTION = ""
  315. SECTION = ""
  316. AUTHOR = ""
  317. HOMEPAGE = ""
  318. MAINTAINER = ""
  319. LICENSE = "GPL"
  320. PR = "r0"
  321. SRC_URI = ""
  322. #inherit base
  323. #do_configure() {
  324. #
  325. #}
  326. #do_compile() {
  327. #
  328. #}
  329. #do_stage() {
  330. #
  331. #}
  332. #do_install() {
  333. #
  334. #}
  335. """
  336. newpackage.close()
  337. os.system( "%s %s/%s" % ( os.environ.get( "EDITOR" ), fulldirname, filename ) )
  338. new.usage = "<directory> <filename>"
  339. def pasteBin( self, params ):
  340. """Send a command + output buffer to the pastebin at http://rafb.net/paste"""
  341. index = params[0]
  342. contents = self._shell.myout.buffer( int( index ) )
  343. sendToPastebin( "output of " + params[0], contents )
  344. pasteBin.usage = "<index>"
  345. def pasteLog( self, params ):
  346. """Send the last event exception error log (if there is one) to http://rafb.net/paste"""
  347. if last_exception is None:
  348. print "SHELL: No Errors yet (Phew)..."
  349. else:
  350. reason, event = last_exception.args
  351. print "SHELL: Reason for the last error: '%s'" % reason
  352. if ':' in reason:
  353. msg, filename = reason.split( ':' )
  354. filename = filename.strip()
  355. print "SHELL: Pasting log file to pastebin..."
  356. file = open( filename ).read()
  357. sendToPastebin( "contents of " + filename, file )
  358. def patch( self, params ):
  359. """Execute 'patch' command on a providee"""
  360. self.build( params, "patch" )
  361. patch.usage = "<providee>"
  362. def parse( self, params ):
  363. """(Re-)parse .bb files and calculate the dependency graph"""
  364. cooker.status = cache.CacheData()
  365. ignore = data.getVar("ASSUME_PROVIDED", cooker.configuration.data, 1) or ""
  366. cooker.status.ignored_dependencies = set( ignore.split() )
  367. cooker.handleCollections( data.getVar("BBFILE_COLLECTIONS", cooker.configuration.data, 1) )
  368. (filelist, masked) = cooker.collect_bbfiles()
  369. cooker.parse_bbfiles(filelist, masked, cooker.myProgressCallback)
  370. cooker.buildDepgraph()
  371. global parsed
  372. parsed = True
  373. print
  374. def reparse( self, params ):
  375. """(re)Parse a providee's bb file"""
  376. bbfile = self._findProvider( params[0] )
  377. if bbfile is not None:
  378. print "SHELL: Found bbfile '%s' for '%s'" % ( bbfile, params[0] )
  379. self.fileReparse( [ bbfile ] )
  380. else:
  381. print "ERROR: Nothing provides '%s'" % params[0]
  382. reparse.usage = "<providee>"
  383. def getvar( self, params ):
  384. """Dump the contents of an outer BitBake environment variable"""
  385. var = params[0]
  386. value = data.getVar( var, cooker.configuration.data, 1 )
  387. print value
  388. getvar.usage = "<variable>"
  389. def peek( self, params ):
  390. """Dump contents of variable defined in providee's metadata"""
  391. name, var = params
  392. bbfile = self._findProvider( name )
  393. if bbfile is not None:
  394. the_data = cooker.bb_cache.loadDataFull(bbfile, cooker.configuration.data)
  395. value = the_data.getVar( var, 1 )
  396. print value
  397. else:
  398. print "ERROR: Nothing provides '%s'" % name
  399. peek.usage = "<providee> <variable>"
  400. def poke( self, params ):
  401. """Set contents of variable defined in providee's metadata"""
  402. name, var, value = params
  403. bbfile = self._findProvider( name )
  404. if bbfile is not None:
  405. print "ERROR: Sorry, this functionality is currently broken"
  406. #d = cooker.pkgdata[bbfile]
  407. #data.setVar( var, value, d )
  408. # mark the change semi persistant
  409. #cooker.pkgdata.setDirty(bbfile, d)
  410. #print "OK"
  411. else:
  412. print "ERROR: Nothing provides '%s'" % name
  413. poke.usage = "<providee> <variable> <value>"
  414. def print_( self, params ):
  415. """Dump all files or providers"""
  416. what = params[0]
  417. if what == "files":
  418. self._checkParsed()
  419. for key in cooker.status.pkg_fn.keys(): print key
  420. elif what == "providers":
  421. self._checkParsed()
  422. for key in cooker.status.providers.keys(): print key
  423. else:
  424. print "Usage: print %s" % self.print_.usage
  425. print_.usage = "<files|providers>"
  426. def python( self, params ):
  427. """Enter the expert mode - an interactive BitBake Python Interpreter"""
  428. sys.ps1 = "EXPERT BB>>> "
  429. sys.ps2 = "EXPERT BB... "
  430. import code
  431. interpreter = code.InteractiveConsole( dict( globals() ) )
  432. interpreter.interact( "SHELL: Expert Mode - BitBake Python %s\nType 'help' for more information, press CTRL-D to switch back to BBSHELL." % sys.version )
  433. def showdata( self, params ):
  434. """Execute 'showdata' on a providee"""
  435. self.build( params, "showdata" )
  436. showdata.usage = "<providee>"
  437. def setVar( self, params ):
  438. """Set an outer BitBake environment variable"""
  439. var, value = params
  440. data.setVar( var, value, cooker.configuration.data )
  441. print "OK"
  442. setVar.usage = "<variable> <value>"
  443. def rebuild( self, params ):
  444. """Clean and rebuild a .bb file or a providee"""
  445. self.build( params, "clean" )
  446. self.build( params, "build" )
  447. rebuild.usage = "<providee>"
  448. def shell( self, params ):
  449. """Execute a shell command and dump the output"""
  450. if params != "":
  451. print commands.getoutput( " ".join( params ) )
  452. shell.usage = "<...>"
  453. def stage( self, params ):
  454. """Execute 'stage' on a providee"""
  455. self.build( params, "stage" )
  456. stage.usage = "<providee>"
  457. def status( self, params ):
  458. """<just for testing>"""
  459. print "-" * 78
  460. print "build cache = '%s'" % cooker.build_cache
  461. print "build cache fail = '%s'" % cooker.build_cache_fail
  462. print "building list = '%s'" % cooker.building_list
  463. print "build path = '%s'" % cooker.build_path
  464. print "consider_msgs_cache = '%s'" % cooker.consider_msgs_cache
  465. print "build stats = '%s'" % cooker.stats
  466. if last_exception is not None: print "last_exception = '%s'" % repr( last_exception.args )
  467. print "memory output contents = '%s'" % self._shell.myout._buffer
  468. def test( self, params ):
  469. """<just for testing>"""
  470. print "testCommand called with '%s'" % params
  471. def unpack( self, params ):
  472. """Execute 'unpack' on a providee"""
  473. self.build( params, "unpack" )
  474. unpack.usage = "<providee>"
  475. def which( self, params ):
  476. """Computes the providers for a given providee"""
  477. item = params[0]
  478. self._checkParsed()
  479. preferred = data.getVar( "PREFERRED_PROVIDER_%s" % item, cooker.configuration.data, 1 )
  480. if not preferred: preferred = item
  481. try:
  482. lv, lf, pv, pf = Providers.findBestProvider(preferred, cooker.configuration.data, cooker.status,
  483. cooker.build_cache_fail)
  484. except KeyError:
  485. lv, lf, pv, pf = (None,)*4
  486. try:
  487. providers = cooker.status.providers[item]
  488. except KeyError:
  489. print "SHELL: ERROR: Nothing provides", preferred
  490. else:
  491. for provider in providers:
  492. if provider == pf: provider = " (***) %s" % provider
  493. else: provider = " %s" % provider
  494. print provider
  495. which.usage = "<providee>"
  496. ##########################################################################
  497. # Common helper functions
  498. ##########################################################################
  499. def completeFilePath( bbfile ):
  500. """Get the complete bbfile path"""
  501. if not cooker.status.pkg_fn: return bbfile
  502. for key in cooker.status.pkg_fn.keys():
  503. if key.endswith( bbfile ):
  504. return key
  505. return bbfile
  506. def sendToPastebin( desc, content ):
  507. """Send content to http://oe.pastebin.com"""
  508. mydata = {}
  509. mydata["lang"] = "Plain Text"
  510. mydata["desc"] = desc
  511. mydata["cvt_tabs"] = "No"
  512. mydata["nick"] = "%s@%s" % ( os.environ.get( "USER", "unknown" ), socket.gethostname() or "unknown" )
  513. mydata["text"] = content
  514. params = urllib.urlencode( mydata )
  515. headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
  516. host = "rafb.net"
  517. conn = httplib.HTTPConnection( "%s:80" % host )
  518. conn.request("POST", "/paste/paste.php", params, headers )
  519. response = conn.getresponse()
  520. conn.close()
  521. if response.status == 302:
  522. location = response.getheader( "location" ) or "unknown"
  523. print "SHELL: Pasted to http://%s%s" % ( host, location )
  524. else:
  525. print "ERROR: %s %s" % ( response.status, response.reason )
  526. def completer( text, state ):
  527. """Return a possible readline completion"""
  528. debugOut( "completer called with text='%s', state='%d'" % ( text, state ) )
  529. if state == 0:
  530. line = readline.get_line_buffer()
  531. if " " in line:
  532. line = line.split()
  533. # we are in second (or more) argument
  534. if line[0] in cmds and hasattr( cmds[line[0]][0], "usage" ): # known command and usage
  535. u = getattr( cmds[line[0]][0], "usage" ).split()[0]
  536. if u == "<variable>":
  537. allmatches = cooker.configuration.data.keys()
  538. elif u == "<bbfile>":
  539. if cooker.status.pkg_fn is None: allmatches = [ "(No Matches Available. Parsed yet?)" ]
  540. else: allmatches = [ x.split("/")[-1] for x in cooker.status.pkg_fn.keys() ]
  541. elif u == "<providee>":
  542. if cooker.status.pkg_fn is None: allmatches = [ "(No Matches Available. Parsed yet?)" ]
  543. else: allmatches = cooker.status.providers.iterkeys()
  544. else: allmatches = [ "(No tab completion available for this command)" ]
  545. else: allmatches = [ "(No tab completion available for this command)" ]
  546. else:
  547. # we are in first argument
  548. allmatches = cmds.iterkeys()
  549. completer.matches = [ x for x in allmatches if x[:len(text)] == text ]
  550. #print "completer.matches = '%s'" % completer.matches
  551. if len( completer.matches ) > state:
  552. return completer.matches[state]
  553. else:
  554. return None
  555. def debugOut( text ):
  556. if debug:
  557. sys.stderr.write( "( %s )\n" % text )
  558. def columnize( alist, width = 80 ):
  559. """
  560. A word-wrap function that preserves existing line breaks
  561. and most spaces in the text. Expects that existing line
  562. breaks are posix newlines (\n).
  563. """
  564. return reduce(lambda line, word, width=width: '%s%s%s' %
  565. (line,
  566. ' \n'[(len(line[line.rfind('\n')+1:])
  567. + len(word.split('\n',1)[0]
  568. ) >= width)],
  569. word),
  570. alist
  571. )
  572. def globfilter( names, pattern ):
  573. return fnmatch.filter( names, pattern )
  574. ##########################################################################
  575. # Class MemoryOutput
  576. ##########################################################################
  577. class MemoryOutput:
  578. """File-like output class buffering the output of the last 10 commands"""
  579. def __init__( self, delegate ):
  580. self.delegate = delegate
  581. self._buffer = []
  582. self.text = []
  583. self._command = None
  584. def startCommand( self, command ):
  585. self._command = command
  586. self.text = []
  587. def endCommand( self ):
  588. if self._command is not None:
  589. if len( self._buffer ) == 10: del self._buffer[0]
  590. self._buffer.append( ( self._command, self.text ) )
  591. def removeLast( self ):
  592. if self._buffer:
  593. del self._buffer[ len( self._buffer ) - 1 ]
  594. self.text = []
  595. self._command = None
  596. def lastBuffer( self ):
  597. if self._buffer:
  598. return self._buffer[ len( self._buffer ) -1 ][1]
  599. def bufferedCommands( self ):
  600. return [ cmd for cmd, output in self._buffer ]
  601. def buffer( self, i ):
  602. if i < len( self._buffer ):
  603. return "BB>> %s\n%s" % ( self._buffer[i][0], "".join( self._buffer[i][1] ) )
  604. else: return "ERROR: Invalid buffer number. Buffer needs to be in (0, %d)" % ( len( self._buffer ) - 1 )
  605. def write( self, text ):
  606. if self._command is not None and text != "BB>> ": self.text.append( text )
  607. if self.delegate is not None: self.delegate.write( text )
  608. def flush( self ):
  609. return self.delegate.flush()
  610. def fileno( self ):
  611. return self.delegate.fileno()
  612. def isatty( self ):
  613. return self.delegate.isatty()
  614. ##########################################################################
  615. # Class BitBakeShell
  616. ##########################################################################
  617. class BitBakeShell:
  618. def __init__( self ):
  619. """Register commands and set up readline"""
  620. self.commandQ = Queue.Queue()
  621. self.commands = BitBakeShellCommands( self )
  622. self.myout = MemoryOutput( sys.stdout )
  623. self.historyfilename = os.path.expanduser( "~/.bbsh_history" )
  624. self.startupfilename = os.path.expanduser( "~/.bbsh_startup" )
  625. readline.set_completer( completer )
  626. readline.set_completer_delims( " " )
  627. readline.parse_and_bind("tab: complete")
  628. try:
  629. readline.read_history_file( self.historyfilename )
  630. except IOError:
  631. pass # It doesn't exist yet.
  632. print __credits__
  633. # save initial cooker configuration (will be reused in file*** commands)
  634. global initdata
  635. initdata = copy.deepcopy( cooker.configuration.data )
  636. def cleanup( self ):
  637. """Write readline history and clean up resources"""
  638. debugOut( "writing command history" )
  639. try:
  640. readline.write_history_file( self.historyfilename )
  641. except:
  642. print "SHELL: Unable to save command history"
  643. def registerCommand( self, command, function, numparams = 0, usage = "", helptext = "" ):
  644. """Register a command"""
  645. if usage == "": usage = command
  646. if helptext == "": helptext = function.__doc__ or "<not yet documented>"
  647. cmds[command] = ( function, numparams, usage, helptext )
  648. def processCommand( self, command, params ):
  649. """Process a command. Check number of params and print a usage string, if appropriate"""
  650. debugOut( "processing command '%s'..." % command )
  651. try:
  652. function, numparams, usage, helptext = cmds[command]
  653. except KeyError:
  654. print "SHELL: ERROR: '%s' command is not a valid command." % command
  655. self.myout.removeLast()
  656. else:
  657. if (numparams != -1) and (not len( params ) == numparams):
  658. print "Usage: '%s'" % usage
  659. return
  660. result = function( self.commands, params )
  661. debugOut( "result was '%s'" % result )
  662. def processStartupFile( self ):
  663. """Read and execute all commands found in $HOME/.bbsh_startup"""
  664. if os.path.exists( self.startupfilename ):
  665. startupfile = open( self.startupfilename, "r" )
  666. for cmdline in startupfile:
  667. debugOut( "processing startup line '%s'" % cmdline )
  668. if not cmdline:
  669. continue
  670. if "|" in cmdline:
  671. print "ERROR: '|' in startup file is not allowed. Ignoring line"
  672. continue
  673. self.commandQ.put( cmdline.strip() )
  674. def main( self ):
  675. """The main command loop"""
  676. while not leave_mainloop:
  677. try:
  678. if self.commandQ.empty():
  679. sys.stdout = self.myout.delegate
  680. cmdline = raw_input( "BB>> " )
  681. sys.stdout = self.myout
  682. else:
  683. cmdline = self.commandQ.get()
  684. if cmdline:
  685. allCommands = cmdline.split( ';' )
  686. for command in allCommands:
  687. pipecmd = None
  688. #
  689. # special case for expert mode
  690. if command == 'python':
  691. sys.stdout = self.myout.delegate
  692. self.processCommand( command, "" )
  693. sys.stdout = self.myout
  694. else:
  695. self.myout.startCommand( command )
  696. if '|' in command: # disable output
  697. command, pipecmd = command.split( '|' )
  698. delegate = self.myout.delegate
  699. self.myout.delegate = None
  700. tokens = shlex.split( command, True )
  701. self.processCommand( tokens[0], tokens[1:] or "" )
  702. self.myout.endCommand()
  703. if pipecmd is not None: # restore output
  704. self.myout.delegate = delegate
  705. pipe = popen2.Popen4( pipecmd )
  706. pipe.tochild.write( "\n".join( self.myout.lastBuffer() ) )
  707. pipe.tochild.close()
  708. sys.stdout.write( pipe.fromchild.read() )
  709. #
  710. except EOFError:
  711. print
  712. return
  713. except KeyboardInterrupt:
  714. print
  715. ##########################################################################
  716. # Start function - called from the BitBake command line utility
  717. ##########################################################################
  718. def start( aCooker ):
  719. global cooker
  720. cooker = aCooker
  721. bbshell = BitBakeShell()
  722. bbshell.processStartupFile()
  723. bbshell.main()
  724. bbshell.cleanup()
  725. if __name__ == "__main__":
  726. print "SHELL: Sorry, this program should only be called by BitBake."