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