701ad3ffc4a05435677ee3e61c52650cd7c25122
[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, dry_run=None):
26     dry_run = dry_run if dry_run is not None else getattr(options, 'dry_run', False)
27     if dry_run:
28         print 'dry_run:',command
29         return 0
30     
31     if silent :    
32         if command.find(';')>=0: command = "(%s) 2> /dev/null" % command
33         else: command += " 2> /dev/null"
34     if background: command += " &"
35     if silent:
36         print '.',
37         sys.stdout.flush()
38     else:
39         now=time.strftime("%H:%M:%S", time.localtime())
40         # don't show in summary
41         print "->",now,'--',
42         sys.stdout.flush()
43     if not silent:
44         command = "set -x; " + command
45     return os.system(command)
46
47 ### WARNING : this ALWAYS does its job, even in dry_run mode
48 def output_of (command):
49     import commands
50     (code,string) = commands.getstatusoutput(command)
51     return (code,string)
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