VirtualBox

Ignore:
Timestamp:
Jun 16, 2010 5:10:47 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
62768
Message:

VBoxShell: generic foreach

File:
1 edited

Legend:

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

    r30006 r30261  
    650650    return mach
    651651
     652class XPathNode:
     653    def __init__(self, parent, obj, type):
     654        self.parent = parent
     655        self.obj = obj
     656        self.type = type
     657    def lookup(self, subpath):
     658        children = self.enum()
     659        matches = []
     660        for e in children:
     661            if e.matches(subpath):
     662                matches.append(e)
     663        return matches
     664    def enum(self):
     665        return []
     666    def matches(self,subexp):
     667#        m = re.search(r"@(?P<a>\w+)=(?P<v>\w+)", subexp)
     668        matches = False
     669#        try:
     670#        if m is not None:
     671#        else:
     672#            print "no match in ",subexp
     673#       except:
     674
     675        return matches
     676    def apply(self, cmd):
     677        exec(cmd, {'obj':self.obj,'node':self,'ctx':self.getCtx()}, {})
     678    def getCtx(self):
     679        if hasattr(self,'ctx'):
     680            return self.ctx
     681        return self.parent.getCtx()
     682
     683class XPathNodeHolder(XPathNode):
     684    def __init__(self, parent, obj, attr, heldClass, xpathname):
     685        XPathNode.__init__(self, parent, obj, 'hld '+xpathname)
     686        self.attr = attr
     687        self.heldClass = heldClass
     688        self.xpathname = xpathname
     689    def enum(self):
     690        children = []
     691        for n in self.getCtx()['global'].getArray(self.obj, self.attr):
     692            node = self.heldClass(self, n)
     693            children.append(node)
     694        return children
     695    def matches(self,subexp):
     696        return subexp == self.xpathname
     697
     698class XPathNodeValue(XPathNode):
     699    def __init__(self, parent, obj, xpathname):
     700        XPathNode.__init__(self, parent, obj, 'val '+xpathname)
     701        self.xpathname = xpathname
     702    def matches(self,subexp):
     703        return subexp == self.xpathname
     704
     705class XPathNodeHolderVM(XPathNodeHolder):
     706    def __init__(self, parent, vbox):
     707        XPathNodeHolder.__init__(self, parent, vbox, 'machines', XPathNodeVM, 'vms')
     708
     709class XPathNodeVM(XPathNode):
     710    def __init__(self, parent, obj):
     711        XPathNode.__init__(self, parent, obj, 'vm')
     712    def matches(self,subexp):
     713        return subexp=='vm'
     714    def enum(self):
     715        return [XPathNodeHolderNIC(self, self.obj),
     716                XPathNodeValue(self, self.obj.BIOSSettings,  'bios'),
     717                XPathNodeValue(self, self.obj.USBController, 'usb')]
     718
     719class XPathNodeHolderNIC(XPathNodeHolder):
     720    def __init__(self, parent, mach):
     721        XPathNodeHolder.__init__(self, parent, mach, 'nics', XPathNodeVM, 'nics')
     722        self.maxNic = self.getCtx()['vb'].systemProperties.networkAdapterCount
     723    def enum(self):
     724        children = []
     725        for i in range(0, self.maxNic):
     726            node = XPathNodeNIC(self, self.obj.getNetworkAdapter(i))
     727            children.append(node)
     728        return children
     729
     730class XPathNodeNIC(XPathNode):
     731    def __init__(self, parent, obj):
     732        XPathNode.__init__(self, parent, obj, 'nic')
     733    def matches(self,subexp):
     734        return subexp=='nic'
     735
     736class XPathNodeRoot(XPathNode):
     737    def __init__(self, ctx):
     738        XPathNode.__init__(self, None, None, 'root')
     739        self.ctx = ctx
     740    def enum(self):
     741        return [XPathNodeHolderVM(self, self.ctx['vb'])]
     742    def matches(self,subexp):
     743        return True
     744
     745def eval_xpath(ctx,scope):
     746    pathnames = scope.split("/")[2:]
     747    nodes = [XPathNodeRoot(ctx)]
     748    for p in pathnames:
     749        seen = []
     750        while len(nodes) > 0:
     751            n = nodes.pop()
     752            seen.append(n)
     753        for s in seen:
     754            matches = s.lookup(p)
     755            for m in matches:
     756                nodes.append(m)
     757        if len(nodes) == 0:
     758            break
     759    return nodes
     760
    652761def argsToMach(ctx,args):
    653762    if len(args) < 2:
     
    11891298def verboseCmd(ctx, args):
    11901299    global g_verbose
    1191     g_verbose = not g_verbose
     1300    if (len(args) > 1):
     1301        g_verbose  = (args[1]=='on')
     1302    else:
     1303        g_verbose = not g_verbose
    11921304    return 0
    11931305
    11941306def colorsCmd(ctx, args):
    11951307    global g_hascolors
    1196     g_hascolors = not g_hascolors
     1308    if (len(args) > 1):
     1309        g_hascolors = (args[1]=='on')
     1310    else:
     1311        g_hascolors = not g_hascolors
    11971312    return 0
    11981313
     
    26492764
    26502765    if    len(args) < 3 \
    2651        or not args[2].isdigit() \
    26522766       or int(args[2]) not in range(0, ctx['vb'].systemProperties.networkAdapterCount):
    26532767            print 'please specify adapter num %d isn\'t in range [0-%d]'%(args[2], ctx['vb'].systemProperties.networkAdapterCount)
     
    26772791    return 0
    26782792
     2793def foreachCmd(ctx, args):
     2794    if len(args) < 3:
     2795        print "foreach scope command"
     2796        return 0
     2797
     2798    scope = args[1]
     2799    cmd = args[2]
     2800    elems = eval_xpath(ctx,scope)
     2801    try:
     2802        for e in elems:
     2803            e.apply(cmd)
     2804    except:
     2805        print "Error executing"
     2806        traceback.print_exc()
     2807    return 0
     2808
     2809def foreachvmCmd(ctx, args):
     2810    if len(args) < 2:
     2811        print "foreachvm command <args>"
     2812        return 0
     2813    cmdargs = args[1:]
     2814    cmdargs.insert(1, '')
     2815    for m in getMachines(ctx):
     2816        cmdargs[1] = m.id
     2817        runCommandArgs(ctx, cmdargs)
     2818    return 0
    26792819
    26802820aliases = {'s':'start',
     
    27552895            'nic' : ['Network adapter management', nicCmd, 0],
    27562896            'prompt' : ['Control prompt', promptCmd, 0],
     2897            'foreachvm' : ['Perform command for each VM', foreachvmCmd, 0],
     2898            'foreach' : ['Generic "for each" construction, using XPath-like notation: foreach //vms/vm "print obj.name"', foreachCmd, 0],
    27572899            }
    27582900
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