Changeset 30261 in vbox for trunk/src/VBox/Frontends/VBoxShell/vboxshell.py
- Timestamp:
- Jun 16, 2010 5:10:47 PM (15 years ago)
- svn:sync-xref-src-repo-rev:
- 62768
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VBoxShell/vboxshell.py
r30006 r30261 650 650 return mach 651 651 652 class 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 683 class 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 698 class 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 705 class XPathNodeHolderVM(XPathNodeHolder): 706 def __init__(self, parent, vbox): 707 XPathNodeHolder.__init__(self, parent, vbox, 'machines', XPathNodeVM, 'vms') 708 709 class 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 719 class 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 730 class 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 736 class 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 745 def 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 652 761 def argsToMach(ctx,args): 653 762 if len(args) < 2: … … 1189 1298 def verboseCmd(ctx, args): 1190 1299 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 1192 1304 return 0 1193 1305 1194 1306 def colorsCmd(ctx, args): 1195 1307 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 1197 1312 return 0 1198 1313 … … 2649 2764 2650 2765 if len(args) < 3 \ 2651 or not args[2].isdigit() \2652 2766 or int(args[2]) not in range(0, ctx['vb'].systemProperties.networkAdapterCount): 2653 2767 print 'please specify adapter num %d isn\'t in range [0-%d]'%(args[2], ctx['vb'].systemProperties.networkAdapterCount) … … 2677 2791 return 0 2678 2792 2793 def 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 2809 def 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 2679 2819 2680 2820 aliases = {'s':'start', … … 2755 2895 'nic' : ['Network adapter management', nicCmd, 0], 2756 2896 '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], 2757 2899 } 2758 2900
Note:
See TracChangeset
for help on using the changeset viewer.