VirtualBox

Changeset 20941 in vbox for trunk


Ignore:
Timestamp:
Jun 25, 2009 1:49:29 PM (16 years ago)
Author:
vboxsync
Message:

Python: support for custom shell extensions, thanks Vasily for idea

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxShell/vboxshell.py

    r20926 r20941  
    647647   return 0
    648648
     649def reloadExtCmd(ctx, args):
     650   # maybe will want more args smartness
     651   checkUserExtensions(ctx, commands, ctx['vb'].homeFolder)
     652   autoCompletion(commands, ctx)
     653   return 0
     654
    649655aliases = {'s':'start',
    650656           'i':'info',
     
    655661           'v':'verbose'}
    656662
    657 commands = {'help':['Prints help information', helpCmd],
    658             'start':['Start virtual machine by name or uuid', startCmd],
    659             'create':['Create virtual machine', createCmd],
    660             'remove':['Remove virtual machine', removeCmd],
    661             'pause':['Pause virtual machine', pauseCmd],
    662             'resume':['Resume virtual machine', resumeCmd],
    663             'stats':['Stats for virtual machine', statsCmd],
    664             'powerdown':['Power down virtual machine', powerdownCmd],
    665             'list':['Shows known virtual machines', listCmd],
    666             'info':['Shows info on machine', infoCmd],
    667             'aliases':['Shows aliases', aliasesCmd],
    668             'verbose':['Toggle verbosity', verboseCmd],
    669             'setvar':['Set VMs variable: setvar Fedora BIOSSettings.ACPIEnabled True', setvarCmd],
    670             'eval':['Evaluate arbitrary Python construction: eval for m in getMachines(ctx): print m.name,"has",m.memorySize,"M"', evalCmd],
    671             'quit':['Exits', quitCmd],
    672             'host':['Show host information', hostCmd],
    673             'guest':['Execute command for guest: guest Win32 console.mouse.putMouseEvent(20, 20, 0, 0)', guestCmd],
    674             'monitorGuest':['Monitor what happens with the guest for some time: monitorGuest Win32 10', monitorGuestCmd],
    675             'monitorVbox':['Monitor what happens with Virtual Box for some time: monitorVbox 10', monitorVboxCmd],
    676             'portForward':['Setup permanent port forwarding for a VM, takes adapter number host port and guest port: portForward Win32 0 8080 80', portForwardCmd],
    677             'showLog':['Show log file of the VM, : showLog Win32', showLogCmd],
     663commands = {'help':['Prints help information', helpCmd, 0],
     664            'start':['Start virtual machine by name or uuid', startCmd, 0],
     665            'create':['Create virtual machine', createCmd, 0],
     666            'remove':['Remove virtual machine', removeCmd, 0],
     667            'pause':['Pause virtual machine', pauseCmd, 0],
     668            'resume':['Resume virtual machine', resumeCmd, 0],
     669            'stats':['Stats for virtual machine', statsCmd, 0],
     670            'powerdown':['Power down virtual machine', powerdownCmd, 0],
     671            'list':['Shows known virtual machines', listCmd, 0],
     672            'info':['Shows info on machine', infoCmd, 0],
     673            'aliases':['Shows aliases', aliasesCmd, 0],
     674            'verbose':['Toggle verbosity', verboseCmd, 0],
     675            'setvar':['Set VMs variable: setvar Fedora BIOSSettings.ACPIEnabled True', setvarCmd, 0],
     676            'eval':['Evaluate arbitrary Python construction: eval for m in getMachines(ctx): print m.name,"has",m.memorySize,"M"', evalCmd, 0],
     677            'quit':['Exits', quitCmd, 0],
     678            'host':['Show host information', hostCmd, 0],
     679            'guest':['Execute command for guest: guest Win32 console.mouse.putMouseEvent(20, 20, 0, 0)', guestCmd, 0],
     680            'monitorGuest':['Monitor what happens with the guest for some time: monitorGuest Win32 10', monitorGuestCmd, 0],
     681            'monitorVbox':['Monitor what happens with Virtual Box for some time: monitorVbox 10', monitorVboxCmd, 0],
     682            'portForward':['Setup permanent port forwarding for a VM, takes adapter number host port and guest port: portForward Win32 0 8080 80', portForwardCmd, 0],
     683            'showLog':['Show log file of the VM, : showLog Win32', showLogCmd, 0],
     684            'reloadExt':['Reload custom extensions: reloadExt', reloadExtCmd, 0],
    678685            }
    679686
     
    691698    return ci[1](ctx, args)
    692699
     700#
     701# To write your own custom commands to vboxshell, create
     702# file ~/.VirtualBox/shellext.py with content like
     703#
     704# def runTestCmd(ctx, args):
     705#    print "Testy test", ctx['vb']
     706#    return 0
     707#
     708# commands = {
     709#    'test': ['Test help', runTestCmd]
     710# }
     711# and issue reloadExt shell command.
     712# This file also will be read automatically on startup.
     713#
     714def checkUserExtensions(ctx, cmds, folder):
     715    name =  os.path.join(folder, "shellext.py")
     716    if not os.path.isfile(name):
     717        return
     718    d = {}
     719    try:
     720        execfile(name, d, d)
     721        for (k,v) in d['commands'].items():
     722            print "customize: adding \"%s\" - %s" %(k, v[0])
     723            cmds[k] = [v[0], v[1], 1]
     724    except:
     725        print "Error loading user extensions:"
     726        traceback.print_exc()
    693727
    694728def interpret(ctx):
     
    696730    print "Running VirtualBox version %s" %(vbox.version)
    697731    ctx['perf'] = ctx['global'].getPerfCollector(ctx['vb'])
     732
     733    checkUserExtensions(ctx, commands, vbox.homeFolder)
    698734
    699735    autoCompletion(commands, ctx)
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette