re-instate check-vsys-defaults as an ignored step
[tests.git] / system / utils.py
1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA 
3 #
4 import time, os, re, glob, sys
5 from pprint import PrettyPrinter
6
7 options={}
8
9 def init_options(options_arg):
10     global options
11     options=options_arg
12
13 # how could this accept a list again ?
14 def header(message):
15     now=time.strftime("%H:%M:%S", time.localtime())
16     print "*",now,'--',message
17
18 def pprint(message,spec,depth=2):
19     now=time.strftime("%H:%M:%S", time.localtime())
20     print ">",now,"--",message
21     PrettyPrinter(indent=8,depth=depth).pprint(spec)
22
23
24
25 def system(command,background=False,silent=False):
26     if getattr(options,'dry_run',None):
27         print 'dry_run:',command
28         return 0
29     
30     if silent :    
31         if command.find(';')>=0: command = "(%s) 2> /dev/null" % command
32         else: command += " 2> /dev/null"
33     if background: command += " &"
34     if silent:
35         print '.',
36         sys.stdout.flush()
37     else:
38         now=time.strftime("%H:%M:%S", time.localtime())
39         # don't show in summary
40         print "->",now,'--',
41         sys.stdout.flush()
42     if not silent:
43         command = "set -x; " + command
44     return os.system(command)
45
46 ### WARNING : this ALWAYS does its job, even in dry_run mode
47 def output_of (command):
48     import commands
49     (code,string) = commands.getstatusoutput(command)
50     return (code,string)
51
52
53 # convenience: translating shell-like pattern into regexp
54 def match (string, pattern):
55     # tmp - there's probably much simpler
56     # rewrite * into .*, ? into .
57     pattern=pattern.replace("*",".*")
58     pattern=pattern.replace("?",".")
59     return re.compile(pattern).match(string)
60     
61 def locate_hooks_scripts (message,path,extensions):
62     print message,'searching',path,'for extensions',extensions
63     scripts=[]
64     for ext in extensions:
65         # skip helper programs
66         scripts += glob.glob (path+'/[a-zA-Z]*.'+ext)
67     return scripts
68     
69 # quick & dirty - should probably use the parseroption object instead
70 # and move to TestMain as well
71 exclude_options_keys = [ 'ensure_value' , 'read_file', 'read_module' ]
72 def show_options (message,options):
73     now=time.strftime("%H:%M:%S", time.localtime())
74     print ">",now,"--",message
75     for k in dir(options):
76         if k.find("_")==0: continue
77         if k in exclude_options_keys: continue
78         print "    ",k,":",getattr(options,k)
79
80
81