b246b1851c1c06fc42f42b9dac655a61127c55b3
[tests.git] / system / utils.py
1 # $Id$
2 import time
3 import os
4 import commands
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):
95     now=time.strftime("%H:%M:%S", time.localtime())
96     print "+",now,':',command
97     return os.system("set -x; " + command)
98
99 # checks whether a given hostname/ip responds to ping
100 ping_timeout_option = None
101 def check_ping (hostname):
102     # check OS (support for macos)
103     global ping_timeout_option
104     if not ping_timeout_option:
105         (status,osname) = commands.getstatusoutput("uname -s")
106         if status != 0:
107             raise Exception, "Cannot figure your OS name"
108         if osname == "Linux":
109             ping_timeout_option="-w"
110         elif osname == "Darwin":
111             ping_timeout_option="-t"
112
113     command="ping -c 1 %s 1 %s"%(ping_timeout_option,hostname)
114     (status,output) = commands.getstatusoutput(command)
115     return status == 0
116
117 # inserts a backslash before each occurence of the following chars
118 # \ " ' < > & | ; ( ) $ * ~ 
119 def backslash_shell_specials (command):
120     result=''
121     for char in command:
122         if char in "\\\"'<>&|;()$*~":
123             result +='\\'+char
124         else:
125             result +=char
126     return result
127
128 # check main IP address against the provided hostname
129 def is_local (hostname):
130     if hostname == "localhost":
131         return True
132     import socket
133     local_ip = socket.gethostbyname(socket.gethostname())
134     remote_ip = socket.gethostbyname(hostname)
135     return local_ip==remote_ip