source: trunk/packages/xen-3.1/xen-3.1/tools/python/xen/sv/util.py @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1from xen.xend.XendClient import server
2from xen.xend import sxp
3from xen.xend import PrettyPrint
4
5import types
6
7def getDomInfo( domain ):
8    domInfoHash = {}
9    try:
10        domInfoHash = sxp2hash( server.xend_domain( domain ) )
11        domInfoHash['dom'] = domain
12    except:
13        domInfoHash['name'] = "Error getting domain details"
14    return domInfoHash
15
16def sxp2hash( s ):
17    sxphash = {}
18       
19    for child in sxp.children( s ):
20        if isinstance( child, types.ListType ) and len( child ) > 1:
21            if isinstance( child[1], types.ListType ) and len( child ) > 1:
22                sxphash[ child[0] ] = sxp2hash( child[1] )
23            else:
24                sxphash[ child[0] ] = child[1]
25       
26    return sxphash 
27   
28def ssxp2hash( s ):
29    sxphash = {}
30   
31    for i in s:
32       if isinstance( i, types.ListType ) and len( i ) > 1:
33          sxphash[ i[0] ] = i[1]
34   
35    return sxphash
36   
37def hash2sxp( h ):
38    hashsxp = []
39   
40    for (key, item) in h.items():
41        hashsxp.append( [key, item] )
42       
43    return hashsxp   
44   
45def string2sxp( string ):
46    pin = sxp.Parser()
47    pin.input( string )
48    return pin.get_val()   
49
50def sxp2string( sexp ):
51    return sxp.to_string( sexp )   
52   
53def sxp2prettystring( sxp ):
54    class tmp:
55        def __init__( self ):
56                self.str = ""
57        def write( self, str ):
58                self.str = self.str + str
59    temp = tmp()
60    PrettyPrint.prettyprint( sxp, out=temp )
61    return temp.str
62
63def getVar( var, request, default=None ):
64   
65    arg = request.args.get( var )
66
67    if arg is None:
68        return default
69    else:
70        return arg[ len( arg )-1 ]
71
72def bigTimeFormatter( time ):
73    time = float( time )
74    weeks = time // 604800
75    remainder = time % 604800
76    days = remainder // 86400
77   
78    remainder = remainder % 86400
79
80    hms = smallTimeFormatter( remainder )
81   
82    return "%d weeks, %d days, %s" % ( weeks, days, hms )
83
84def smallTimeFormatter( time ):
85    time = float( time )
86    hours = time // 3600
87    remainder = time % 3600
88    mins = remainder // 60
89    secs = time % 60
90    return "%02d:%02d:%04.1f (hh:mm:ss.s)" % ( hours, mins, secs ) 
91
92def stateFormatter( state ):
93    states = [ 'Running', 'Blocked', 'Paused', 'Shutdown', 'Crashed' ]
94   
95    stateStr = ""
96   
97    for i in range( len( state ) ):
98        if state[i] != "-":
99            stateStr += "%s, " % states[ i ] 
100           
101    return stateStr + " (%s)" % state
102
103def memoryFormatter( mem ):
104    mem = int( mem )
105    if mem >= 1024:
106        mem = float( mem ) / 1024
107        return "%3.2fGb" % mem
108    else:   
109        return "%7dMb" % mem
110
111def cpuFormatter( mhz ):
112    mhz = int( mhz )
113    if mhz > 1000:
114        ghz = float( mhz ) / 1000.0
115        return "%4.2fGHz" % ghz
116    else:
117        return "%4dMHz" % mhz
118       
119def hyperthreadFormatter( threads ):
120    try:
121        if int( threads ) > 1:
122            return "Yes"
123        else:
124            return "No"
125    except:
126        return "No"
Note: See TracBrowser for help on using the repository browser.