various changes all over the place
[tests.git] / system / utils.py
1 # $Id$
2 import time
3 import os
4 import re
5 from pprint import PrettyPrinter
6
7 # how could this accept a list again ?
8 def header(message):
9     now=time.strftime("%H:%M:%S", time.localtime())
10     print "*",now,'--',message
11
12 def pprint(message,spec,depth=2):
13     now=time.strftime("%H:%M:%S", time.localtime())
14     print ">",now,"--",message
15     PrettyPrinter(indent=8,depth=depth).pprint(spec)
16
17 def show_site_spec (site):
18     print '======== site',site['site_fields']['name']
19     for (k,v) in site.iteritems():
20         if k=='nodes':
21             if v: 
22                 print '\t\t','nodes : ',
23                 for node in v:  
24                     print node['node_fields']['hostname'],'',
25                 print ''
26         elif k=='users':
27             if v: 
28                 print '\t\tusers : ',
29                 for user in v:  
30                     print user['name'],'',
31                 print ''
32         elif k == 'site_fields':
33             print '\t\tlogin_base',':',v['login_base']
34         elif k == 'address_fields':
35             pass
36         else:
37             print '\t\t',k,
38             PrettyPrinter(indent=8,depth=2).pprint(v)
39         
40 def show_initscript_spec (initscript):
41     print '======== initscript',initscript['initscript_fields']['name']
42
43 def show_key_spec (key):
44     print '======== key',key['name']
45
46 def show_slice_spec (slice):
47     print '======== slice',slice['slice_fields']['name']
48     for (k,v) in slice.iteritems():
49         if k=='nodenames':
50             if v: 
51                 print '\t\tnodes : ',
52                 for nodename in v:  
53                     print nodename,'',
54                 print ''
55         elif k=='usernames':
56             if v: 
57                 print '\t\tusers : ',
58                 for username in v:  
59                     print username,'',
60                 print ''
61         elif k=='slice_fields':
62             print '\t\tfields',':',
63             print 'max_nodes=',v['max_nodes'],
64             print ''
65         else:
66             print '\t\t',k,v
67
68 def show_test_spec (message,all_plc_specs):
69     now=time.strftime("%H:%M:%S", time.localtime())
70     print ">",now,"--",message
71     for plc_spec in all_plc_specs:
72         show_test_spec_pass (plc_spec,1)
73         show_test_spec_pass (plc_spec,2)
74
75 def show_test_spec_pass (plc_spec,passno):
76     for (key,val) in plc_spec.iteritems():
77         if passno == 2:
78             if key == 'sites':
79                 for site in val:
80                     show_site_spec(site)
81             elif key=='initscripts':
82                 for initscript in val:
83                     show_initscript_spec (initscript)
84             elif key=='slices':
85                 for slice in val:
86                     show_slice_spec (slice)
87             elif key=='keys':
88                 for key in val:
89                     show_key_spec (key)
90         elif passno == 1:
91             if key not in ['sites','initscripts','slices','keys']:
92                 print '\t',key,':',val
93
94 def system(command,background=False):
95     now=time.strftime("%H:%M:%S", time.localtime())
96     if background: command += " &"
97     print "+",now,':',command
98     return os.system("set -x; " + command)
99
100 def match (string, pattern):
101     # tmp - there's probably much simpler
102     # rewrite * into .*, ? into .
103     pattern=pattern.replace("*",".*")
104     pattern=pattern.replace("?",".")
105     return re.compile(pattern).match(string)
106