0
|
1 #!/usr/bin/env python
|
|
2
|
|
3
|
|
4 # borrowed from: http://wiki.g2.bx.psu.edu/Future/Job%20Failure%20When%20stderr and modified for use with Trinity tools.
|
|
5
|
|
6 """
|
|
7 Wrapper that execute a program and its arguments but reports standard error
|
|
8 messages only if the program exit status was not 0
|
|
9 Example: ./stderr_wrapper.py myprog arg1 -f arg2
|
|
10 """
|
|
11
|
|
12 import sys, subprocess, os
|
|
13
|
|
14 assert sys.version_info[:2] >= ( 2, 4 )
|
|
15
|
|
16 TRINITY_BASE_DIR = ""
|
|
17 if os.environ.has_key('TRINITY_HOME'):
|
|
18 TRINITY_BASE_DIR = os.environ['TRINITY_HOME'];
|
|
19 else:
|
|
20 # Cicada Dennis added looking for the location of the Trinity program using the unix "which" utility.
|
|
21 # I tried using "command -v Trinity" but for some reason, I was getting a OS permission error with that.
|
|
22 try:
|
|
23 pipe1 = subprocess.Popen(["which", "Trinity"], stdout=subprocess.PIPE)
|
|
24 except:
|
|
25 t, v, tb = sys.exc_info()
|
|
26 sys.stderr.write("You must set the environmental variable TRINITY_HOME to the base installation directory of Trinity before running this");
|
|
27 raise t, v, tb
|
|
28 else:
|
|
29 output1, err1 = pipe1.communicate()
|
|
30 # Take off the last part of the path (which is the Trinity command)
|
|
31 TRINITY_BASE_DIR = "/".join(output1.split("/")[0:-1])
|
|
32
|
|
33 # get bindir
|
|
34 bindir = sys.argv[0]
|
|
35 bindir = bindir.split("/")
|
|
36 if len(bindir) > 1:
|
|
37 bindir.pop()
|
|
38 bindir = "/".join(bindir)
|
|
39 else:
|
|
40 bindir = "."
|
|
41
|
|
42
|
|
43 ## add locations of tools to path setting.
|
|
44 TOOL_PATHS_FILE = bindir + "/__add_to_PATH_setting.txt";
|
|
45 for line in open(TOOL_PATHS_FILE):
|
|
46 line = line.rstrip()
|
|
47 os.environ['PATH'] += ":" + line
|
|
48
|
|
49
|
|
50 def stop_err( msg ):
|
|
51 sys.stderr.write( "%s\n" % msg )
|
|
52 sys.exit()
|
|
53
|
|
54 def __main__():
|
|
55 # Get command-line arguments
|
|
56 args = sys.argv
|
|
57 # Remove name of calling program, i.e. ./stderr_wrapper.py
|
|
58 args.pop(0)
|
|
59 # If there are no arguments left, we're done
|
|
60 if len(args) == 0:
|
|
61 return
|
|
62
|
|
63 # If one needs to silence stdout
|
|
64 #args.append( ">" )
|
|
65 #args.append( "/dev/null" )
|
|
66
|
|
67 args[0] = "".join([TRINITY_BASE_DIR, '/', args[0]]);
|
|
68
|
|
69 cmdline = " ".join(args)
|
|
70
|
|
71
|
|
72
|
|
73 try:
|
|
74 # Run program
|
|
75 err_capture = open("stderr.txt", 'w')
|
|
76 proc = subprocess.Popen( args=cmdline, shell=True, stderr=err_capture, stdout=sys.stdout )
|
|
77 returncode = proc.wait()
|
|
78 err_capture.close()
|
|
79
|
|
80
|
|
81 if returncode != 0:
|
|
82 raise Exception
|
|
83
|
|
84 except Exception:
|
|
85 # Running Grinder failed: write error message to stderr
|
|
86 err_text = open("stderr.txt").readlines()
|
|
87 stop_err( "ERROR:\n" + "\n".join(err_text))
|
|
88
|
|
89
|
|
90 if __name__ == "__main__": __main__()
|