Changeset 47979 in vbox for trunk/src/VBox/Frontends
- Timestamp:
- Aug 21, 2013 6:44:35 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VBoxShell/vboxshell.py
r47848 r47979 1 #!/usr/bin/python 2 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # $Id$ 4 """ 5 VirtualBox Python Shell. 6 7 This program is a simple interactive shell for VirtualBox. You can query 8 information and issue commands from a simple command line. 9 10 It also provides you with examples on how to use VirtualBox's Python API. 11 This shell is even somewhat documented, supports TAB-completion and 12 history if you have Python readline installed. 13 14 Finally, shell allows arbitrary custom extensions, just create 15 .VirtualBox/shexts/ and drop your extensions there. 16 Enjoy. 17 18 P.S. Our apologies for the code quality. 19 """ 20 21 __copyright__ = \ 3 22 """ 4 23 Copyright (C) 2009-2013 Oracle Corporation … … 12 31 hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. 13 32 """ 14 15 ################################################################################# 16 # This program is a simple interactive shell for VirtualBox. You can query # 17 # information and issue commands from a simple command line. # 18 # # 19 # It also provides you with examples on how to use VirtualBox's Python API. # 20 # This shell is even somewhat documented, supports TAB-completion and # 21 # history if you have Python readline installed. # 22 # # 23 # Finally, shell allows arbitrary custom extensions, just create # 24 # .VirtualBox/shexts/ and drop your extensions there. # 25 # Enjoy. # 26 ################################################################################ 33 __version__ = "$Revision$" 34 27 35 28 36 import os, sys … … 35 43 from pprint import pprint 36 44 45 46 47 # 48 # Global Variables 49 # 37 50 g_fBatchMode = False 38 51 g_sScriptFile = None … … 47 60 g_sPrompt = "vbox> " 48 61 49 g_fHasColors = True 50 g_aTermColors = { 51 'red':'\033[31m', 52 'blue':'\033[94m', 53 'green':'\033[92m', 54 'yellow':'\033[93m', 55 'magenta':'\033[35m', 56 'cyan':'\033[36m' 57 } 62 g_fHasColors = True 63 g_dTermColors = { 64 'red': '\033[31m', 65 'blue': '\033[94m', 66 'green': '\033[92m', 67 'yellow': '\033[93m', 68 'magenta': '\033[35m', 69 'cyan': '\033[36m' 70 } 71 72 73 58 74 def colored(strg, color): 59 75 """ … … 62 78 if not g_fHasColors: 63 79 return strg 64 col = g_ aTermColors.get(color, None)80 col = g_dTermColors.get(color, None) 65 81 if col: 66 82 return col+str(strg)+'\033[0m' 67 else: 68 return strg 83 return strg 69 84 70 85 if g_fHasReadline: … … 238 253 239 254 def startVm(ctx, mach, vmtype): 240 mgr = ctx['mgr']241 255 vbox = ctx['vb'] 242 256 perf = ctx['perf'] 243 session = mgr.getSessionObject(vbox)257 session = ctx['global'].getSessionObject(vbox) 244 258 progress = mach.launchVMProcess(session, vmtype, "") 245 259 if progressBar(ctx, progress, 100) and int(progress.resultCode) == 0: … … 670 684 try: 671 685 vbox = ctx['vb'] 672 session = ctx[' mgr'].getSessionObject(vbox)686 session = ctx['global'].getSessionObject(vbox) 673 687 mach.lockMachine(session, ctx['global'].constants.LockType_Shared) 674 688 except Exception, e: … … 3471 3485 3472 3486 def main(argv): 3473 style = None 3474 params = None3475 autopath = False3476 script_file = None3487 3488 # 3489 # Parse command line arguments. 3490 # 3477 3491 parse = OptionParser() 3478 3492 parse.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False, help = "switch on verbose") … … 3495 3509 g_fHasReadline = False 3496 3510 g_sCmd = options.command_line 3511 3512 params = None 3497 3513 if options.opt_line is not None: 3498 3514 params = {} … … 3502 3518 (key, value) = strparam.split('=') 3503 3519 params[key] = value 3504 else:3505 params = None3506 3520 3507 3521 if options.autopath: 3508 3522 cwd = os.getcwd() 3509 3523 vpp = os.environ.get("VBOX_PROGRAM_PATH") 3510 if vpp is None and (os.path.isfile(os.path.join(cwd, "VirtualBox")) or os.path.isfile(os.path.join(cwd, "VirtualBox.exe"))) : 3524 if vpp is None \ 3525 and ( os.path.isfile(os.path.join(cwd, "VirtualBox")) \ 3526 or os.path.isfile(os.path.join(cwd, "VirtualBox.exe")) ): 3511 3527 vpp = cwd 3512 3528 print "Autodetected VBOX_PROGRAM_PATH as", vpp … … 3516 3532 if vsp is None and os.path.isfile(os.path.join(cwd, "sdk", "bindings", "VirtualBox.xidl")) : 3517 3533 vsp = os.path.join(cwd, "sdk") 3518 if vsp is None and os.path.isfile(os.path.join(vpp, "sdk", "bindings", "VirtualBox.xidl")) :3534 if vsp is None and vpp is not None and os.path.isfile(os.path.join(vpp, "sdk", "bindings", "VirtualBox.xidl")) : 3519 3535 vsp = os.path.join(vpp, "sdk") 3520 3536 if vsp is not None : … … 3522 3538 os.environ["VBOX_SDK_PATH"] = vsp 3523 3539 3540 # 3541 # Set up the shell interpreter context and 3542 # 3524 3543 from vboxapi import VirtualBoxManager 3525 virtualBoxManager = VirtualBoxManager(style, params)3526 ctx = { 'global':virtualBoxManager,3527 'mgr':virtualBoxManager.mgr,3528 'vb':virtualBoxManager.vbox,3529 'const':virtualBoxManager.constants,3530 'remote':virtualBoxManager.remote,3531 'type':virtualBoxManager.type,3532 'run':lambda cmd, args: runCommandCb(ctx, cmd, args),3533 'guestlambda':lambda uuid, guestLambda, args: runGuestCommandCb(ctx, uuid, guestLambda, args),3534 'machById':lambda uuid: machById(ctx, uuid),3535 'argsToMach':lambda args: argsToMach(ctx, args),3536 'progressBar':lambda p: progressBar(ctx, p),3537 'typeInGuest':typeInGuest,3538 '_machlist':None,3539 'prompt':g_sPrompt,3540 'scriptLine':0,3541 'interrupt': False3542 3544 oVBoxMgr = VirtualBoxManager(style, params) 3545 ctx = { 3546 'global': oVBoxMgr, 3547 'vb': oVBoxMgr.vbox, 3548 'const': oVBoxMgr.constants, 3549 'remote': oVBoxMgr.remote, 3550 'type': oVBoxMgr.type, 3551 'run': lambda cmd, args: runCommandCb(ctx, cmd, args), 3552 'guestlambda': lambda uuid, guestLambda, args: runGuestCommandCb(ctx, uuid, guestLambda, args), 3553 'machById': lambda uuid: machById(ctx, uuid), 3554 'argsToMach': lambda args: argsToMach(ctx, args), 3555 'progressBar': lambda p: progressBar(ctx, p), 3556 'typeInGuest': typeInGuest, 3557 '_machlist': None, 3558 'prompt': g_sPrompt, 3559 'scriptLine': 0, 3560 'interrupt': False, 3561 } 3543 3562 interpret(ctx) 3544 virtualBoxManager.deinit()3545 del virtualBoxManager3563 oVBoxMgr.deinit() 3564 del oVBoxMgr 3546 3565 3547 3566 if __name__ == '__main__': 3548 3567 main(sys.argv) 3568
Note:
See TracChangeset
for help on using the changeset viewer.