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