2859642f163e9a7461dda9f2530586b9fcaf1fce
[tests.git] / system / utils.py
1 # $Id$
2 import time, os, re, glob, sys
3 from pprint import PrettyPrinter
4
5 options={}
6
7 def init_options(options_arg):
8     global options
9     options=options_arg
10
11 # how could this accept a list again ?
12 def header(message):
13     now=time.strftime("%H:%M:%S", time.localtime())
14     print "*",now,'--',message
15
16 def pprint(message,spec,depth=2):
17     now=time.strftime("%H:%M:%S", time.localtime())
18     print ">",now,"--",message
19     PrettyPrinter(indent=8,depth=depth).pprint(spec)
20
21
22
23 def system(command,background=False):
24     if background: command += " &"
25     if options.dry_run:
26         print 'dry_run:',command
27         return 0
28     else:
29         now=time.strftime("%H:%M:%S", time.localtime())
30         print "*",now,'--',
31         sys.stdout.flush()
32         return os.system("set -x; " + command)
33
34 ### WARNING : this ALWAYS does its job, even in dry_run mode
35 def output_of (command):
36     import commands
37 #    if options.dry_run:
38 #        print 'dry_run',command
39 #        return (0,'[[dry-run - fake output]]')
40 #    else:
41     (code,string) = commands.getstatusoutput(command)
42     return (code,string)
43
44
45
46 # convenience: translating shell-like pattern into regexp
47 def match (string, pattern):
48     # tmp - there's probably much simpler
49     # rewrite * into .*, ? into .
50     pattern=pattern.replace("*",".*")
51     pattern=pattern.replace("?",".")
52     return re.compile(pattern).match(string)
53     
54 def locate_sanity_scripts (message,path,extensions):
55     print message,'searching',path,'for extensions',extensions
56     scripts=[]
57     for ext in extensions:
58         scripts += glob.glob (path+'/*.'+ext)
59     return scripts
60     
61 # quick & dirty - should probably use the parseroption object instead
62 # and move to TestMain as well
63 exclude_options_keys = [ 'ensure_value' , 'read_file', 'read_module' ]
64 def show_options (message,options):
65     now=time.strftime("%H:%M:%S", time.localtime())
66     print ">",now,"--",message
67     for k in dir(options):
68         if k.find("_")==0: continue
69         if k in exclude_options_keys: continue
70         print "    ",k,":",getattr(options,k)
71
72
73
74 #################### display config
75 # entry point
76 def show_plc_spec (plc_spec):
77     show_plc_spec_pass (plc_spec,1)
78     show_plc_spec_pass (plc_spec,2)
79
80 def show_plc_spec_pass (plc_spec,passno):
81     for (key,val) in plc_spec.iteritems():
82         if passno == 2:
83             if key == 'sites':
84                 for site in val:
85                     show_site_spec(site)
86                     for node in site['nodes']:
87                         show_node_spec(node)
88             elif key=='initscripts':
89                 for initscript in val:
90                     show_initscript_spec (initscript)
91             elif key=='slices':
92                 for slice in val:
93                     show_slice_spec (slice)
94             elif key=='keys':
95                 for key in val:
96                     show_key_spec (key)
97         elif passno == 1:
98             if key not in ['sites','initscripts','slices','keys']:
99                 print '*   ',key,':',val
100
101 def show_site_spec (site):
102     print '* ======== site',site['site_fields']['name']
103     for (k,v) in site.iteritems():
104         if k=='nodes':
105             if v: 
106                 print '*       ','nodes : ',
107                 for node in v:  
108                     print node['node_fields']['hostname'],'',
109                 print ''
110         elif k=='users':
111             if v: 
112                 print '*       users : ',
113                 for user in v:  
114                     print user['name'],'',
115                 print ''
116         elif k == 'site_fields':
117             print '*       login_base',':',v['login_base']
118         elif k == 'address_fields':
119             pass
120         else:
121             print '*       ',k,
122             PrettyPrinter(indent=8,depth=2).pprint(v)
123         
124 def show_initscript_spec (initscript):
125     print '* ======== initscript',initscript['initscript_fields']['name']
126
127 def show_key_spec (key):
128     print '* ======== key',key['name']
129
130 def show_slice_spec (slice):
131     print '* ======== slice',slice['slice_fields']['name']
132     for (k,v) in slice.iteritems():
133         if k=='nodenames':
134             if v: 
135                 print '*       nodes : ',
136                 for nodename in v:  
137                     print nodename,'',
138                 print ''
139         elif k=='usernames':
140             if v: 
141                 print '*       users : ',
142                 for username in v:  
143                     print username,'',
144                 print ''
145         elif k=='slice_fields':
146             print '*       fields',':',
147             print 'max_nodes=',v['max_nodes'],
148             print ''
149         else:
150             print '*       ',k,v
151
152 def show_node_spec (node):
153     print "*           node",node['name'],"host_box=",node['host_box'],
154     print "hostname=",node['node_fields']['hostname'],
155     print "ip=",node['interface_fields']['ip']
156     
157