another round of rework
[tests.git] / system / utils.py
1 # $Id$
2 import time
3 import os
4 import re
5 from pprint import PrettyPrinter
6
7 options={}
8
9 def init_options(options_arg):
10     global options
11     options=options_arg
12
13 # how could this accept a list again ?
14 def header(message):
15     now=time.strftime("%H:%M:%S", time.localtime())
16     print "*",now,'--',message
17
18 def pprint(message,spec,depth=2):
19     now=time.strftime("%H:%M:%S", time.localtime())
20     print ">",now,"--",message
21     PrettyPrinter(indent=8,depth=depth).pprint(spec)
22
23 # quick & dirty - should probably use the parseroption object instead
24 # and move to TestMain as well
25 exclude_options_keys = [ 'ensure_value' , 'read_file', 'read_module' ]
26 def show_options (message,options):
27     now=time.strftime("%H:%M:%S", time.localtime())
28     print ">",now,"--",message
29     for k in dir(options):
30         if k.find("_")==0: continue
31         if k in exclude_options_keys: continue
32         print "    ",k,":",getattr(options,k)
33
34 def show_site_spec (site):
35     print '======== site',site['site_fields']['name']
36     for (k,v) in site.iteritems():
37         if k=='nodes':
38             if v: 
39                 print '\t\t','nodes : ',
40                 for node in v:  
41                     print node['node_fields']['hostname'],'',
42                 print ''
43         elif k=='users':
44             if v: 
45                 print '\t\tusers : ',
46                 for user in v:  
47                     print user['name'],'',
48                 print ''
49         elif k == 'site_fields':
50             print '\t\tlogin_base',':',v['login_base']
51         elif k == 'address_fields':
52             pass
53         else:
54             print '\t\t',k,
55             PrettyPrinter(indent=8,depth=2).pprint(v)
56         
57 def show_initscript_spec (initscript):
58     print '======== initscript',initscript['initscript_fields']['name']
59
60 def show_key_spec (key):
61     print '======== key',key['name']
62
63 def show_slice_spec (slice):
64     print '======== slice',slice['slice_fields']['name']
65     for (k,v) in slice.iteritems():
66         if k=='nodenames':
67             if v: 
68                 print '\t\tnodes : ',
69                 for nodename in v:  
70                     print nodename,'',
71                 print ''
72         elif k=='usernames':
73             if v: 
74                 print '\t\tusers : ',
75                 for username in v:  
76                     print username,'',
77                 print ''
78         elif k=='slice_fields':
79             print '\t\tfields',':',
80             print 'max_nodes=',v['max_nodes'],
81             print ''
82         else:
83             print '\t\t',k,v
84
85 def show_test_spec (message,all_plc_specs):
86     now=time.strftime("%H:%M:%S", time.localtime())
87     print ">",now,"--",message
88     for plc_spec in all_plc_specs:
89         show_test_spec_pass (plc_spec,1)
90         show_test_spec_pass (plc_spec,2)
91
92 def show_test_spec_pass (plc_spec,passno):
93     for (key,val) in plc_spec.iteritems():
94         if passno == 2:
95             if key == 'sites':
96                 for site in val:
97                     show_site_spec(site)
98             elif key=='initscripts':
99                 for initscript in val:
100                     show_initscript_spec (initscript)
101             elif key=='slices':
102                 for slice in val:
103                     show_slice_spec (slice)
104             elif key=='keys':
105                 for key in val:
106                     show_key_spec (key)
107         elif passno == 1:
108             if key not in ['sites','initscripts','slices','keys']:
109                 print '\t',key,':',val
110
111 def system(command,background=False):
112     now=time.strftime("%H:%M:%S", time.localtime())
113     if background: command += " &"
114     if options.dry_run:
115         print 'dry_run:',command
116         return 0
117     else:
118         return os.system("set -x; " + command)
119
120 def match (string, pattern):
121     # tmp - there's probably much simpler
122     # rewrite * into .*, ? into .
123     pattern=pattern.replace("*",".*")
124     pattern=pattern.replace("?",".")
125     return re.compile(pattern).match(string)
126