4468b206a6b484ae2721c6ec59b7d527bd0e56c8
[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
39
40 # inserts a backslash before each occurence of the following chars
41 # \ " ' < > & | ; ( ) $ * ~ 
42 def backslash_shell_specials (command):
43     result=''
44     for char in command:
45         if char in "\\\"'<>&|;()$*~":
46             result +='\\'+char
47         else:
48             result +=char
49     return result
50