fab68441ddeddb2558797848c455255c0d2c382b
[tests.git] / system / utils.py
1 # $Id$
2 import time
3 import os
4 import commands
5 import pprint
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 show_spec(message,spec,depth=2):
13     now=time.strftime("%H:%M:%S", time.localtime())
14     print ">",now,"--",message
15     pprint.PrettyPrinter(indent=6,depth=depth).pprint(spec)
16
17 def system(command):
18     now=time.strftime("%H:%M:%S", time.localtime())
19     print "+",now,':',command
20     return os.system("set -x; " + command)
21
22 # checks whether a given hostname/ip responds to ping
23 ping_timeout_option = None
24 def check_ping (hostname):
25     # check OS (support for macos)
26     global ping_timeout_option
27     if not ping_timeout_option:
28         (status,osname) = commands.getstatusoutput("uname -s")
29         if status != 0:
30             raise Exception, "Cannot figure your OS name"
31         if osname == "Linux":
32             ping_timeout_option="-w"
33         elif osname == "Darwin":
34             ping_timeout_option="-t"
35
36     command="ping -c 1 %s 1 %s"%(ping_timeout_option,hostname)
37     (status,output) = commands.getstatusoutput(command)
38     return status == 0