Howdy,
I'm not sure everyone will agree that this is the best way to do it but
it's so simple.
It's very useful to be able to parse the output of xm list from a shell
script (especially for automated Xend tests). In a balloon test I've
written, to get the reservation I have to do this:
res=$(echo $(xm list $VM | tail -n 1 | cut -c21-29))
Very ugly.
The following patch adds a --format option to xm list which let's you
specify a python formatting string to use. Since we're using named
keys, it ends up working very nicely. The above can be done with:
res=`xm list -f "%(mem)d" $VM`
Which is considerably nicer and more robust against slight changes in
the display format.
I did not add docs for it because it seems like it should be reserved as
an expert option only (since it relies on internal naming).
Signed-off-by: Anthony Liguori
Regards,
Anthony Liguori
# HG changeset patch
# User Anthony Liguori <aliguori@xxxxxxxxxx>
# Node ID 8937fe723eef24375a0eae488443e1c5948c1a7a
# Parent fcc272a4f74bfc51f9068867de02a3525b3121d1
Add -f,--format=FMT parameter that uses FMT as the format instead of one of the
stock formats. This is really useful for writing any sort of scripting
interface to xm since you can force the output to look like whatever is easiest
to parse
diff -r fcc272a4f74b -r 8937fe723eef tools/python/xen/xm/main.py
--- a/tools/python/xen/xm/main.py Wed Aug 10 16:34:38 2005
+++ b/tools/python/xen/xm/main.py Wed Aug 10 22:08:12 2005
@@ -200,7 +200,8 @@
def xm_list(args):
use_long = 0
show_vcpus = 0
- (options, params) = getopt(args, 'lv', ['long','vcpus'])
+ format = None
+ (options, params) = getopt(args, 'lvf:', ['long','vcpus','format='])
n = len(params)
for (k, v) in options:
@@ -208,6 +209,8 @@
use_long = 1
if k in ['-v', '--vcpus']:
show_vcpus = 1
+ if k in ['-f', '--format']:
+ format = v
domsinfo = []
from xen.xend.XendClient import server
@@ -225,6 +228,9 @@
PrettyPrint.prettyprint(info)
elif show_vcpus:
xm_show_vcpus(domsinfo)
+ elif format:
+ for dominfo in domsinfo:
+ print format % dominfo
else:
xm_brief_list(domsinfo)
_______________________________________________
Xen-tools mailing list
Xen-tools@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-tools
|