comparison trinityrnaseq_protocol/trinityToolWrapper.py @ 3:87c03e2aeba3 draft default tip

Uploaded
author gkumar09
date Sun, 18 Oct 2015 10:40:27 -0400
parents 1c37a8003755
children
comparison
equal deleted inserted replaced
2:70e3c4c34a7c 3:87c03e2aeba3
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 sys.stderr.write("You must set the environmental variable TRINITY_BASE_DIR to the base installation directory of Trinity before running this");
21 sys.exit()
22
23
24
25 # get bindir
26 bindir = sys.argv[0]
27 bindir = bindir.split("/")
28 if len(bindir) > 1:
29 bindir.pop()
30 bindir = "/".join(bindir)
31 else:
32 bindir = "."
33
34
35 ## add locations of tools to path setting.
36 TOOL_PATHS_FILE = bindir + "/__add_to_PATH_setting.txt";
37 for line in open(TOOL_PATHS_FILE):
38 line = line.rstrip()
39 os.environ['PATH'] += ":" + line
40
41
42 def stop_err( msg ):
43 sys.stderr.write( "%s\n" % msg )
44 sys.exit()
45
46 def __main__():
47 # Get command-line arguments
48 args = sys.argv
49 # Remove name of calling program, i.e. ./stderr_wrapper.py
50 args.pop(0)
51 # If there are no arguments left, we're done
52 if len(args) == 0:
53 return
54
55 # If one needs to silence stdout
56 #args.append( ">" )
57 #args.append( "/dev/null" )
58
59 args[0] = "".join([TRINITY_BASE_DIR, '/', args[0]]);
60
61 cmdline = " ".join(args)
62
63
64
65 try:
66 # Run program
67 err_capture = open("stderr.txt", 'w')
68 proc = subprocess.Popen( args=cmdline, shell=True, stderr=err_capture, stdout=sys.stdout )
69 returncode = proc.wait()
70 err_capture.close()
71
72
73 if returncode != 0:
74 raise Exception
75
76 except Exception:
77 # Running Grinder failed: write error message to stderr
78 err_text = open("stderr.txt").readlines()
79 stop_err( "ERROR:\n" + "\n".join(err_text))
80
81
82 if __name__ == "__main__": __main__()