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