fix
[tests.git] / system / utils.py
1 # $Id$
2 import time, os, re, glob, sys
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
22
23 def system(command,background=False,silent=False):
24     if options.dry_run:
25         print 'dry_run:',command
26         return 0
27     
28     if silent :    
29         if command.find(';')>=0: command = "(%s) 2> /dev/null" % command
30         else: command += " 2> /dev/null"
31     if background: command += " &"
32     if silent:
33         print '.',
34         sys.stdout.flush()
35     else:
36         now=time.strftime("%H:%M:%S", time.localtime())
37         # don't show in summary
38         print "->",now,'--',
39         sys.stdout.flush()
40     return os.system("set -x; " + command)
41
42 ### WARNING : this ALWAYS does its job, even in dry_run mode
43 def output_of (command):
44     import commands
45 #    if options.dry_run:
46 #        print 'dry_run',command
47 #        return (0,'[[dry-run - fake output]]')
48 #    else:
49     (code,string) = commands.getstatusoutput(command)
50     return (code,string)
51
52
53
54 # convenience: translating shell-like pattern into regexp
55 def match (string, pattern):
56     # tmp - there's probably much simpler
57     # rewrite * into .*, ? into .
58     pattern=pattern.replace("*",".*")
59     pattern=pattern.replace("?",".")
60     return re.compile(pattern).match(string)
61     
62 def locate_hooks_scripts (message,path,extensions):
63     print message,'searching',path,'for extensions',extensions
64     scripts=[]
65     for ext in extensions:
66         # skip helper programs
67         scripts += glob.glob (path+'/[a-zA-Z]*.'+ext)
68     return scripts
69     
70 # quick & dirty - should probably use the parseroption object instead
71 # and move to TestMain as well
72 exclude_options_keys = [ 'ensure_value' , 'read_file', 'read_module' ]
73 def show_options (message,options):
74     now=time.strftime("%H:%M:%S", time.localtime())
75     print ">",now,"--",message
76     for k in dir(options):
77         if k.find("_")==0: continue
78         if k in exclude_options_keys: continue
79         print "    ",k,":",getattr(options,k)
80
81
82