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