annotate ctat_trinity_tool_wrapper.py @ 0:5eca0c75b178 draft default tip

Upload ctat tools.
author trinity_ctat
date Tue, 17 Jul 2018 11:47:32 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
1 #!/usr/bin/env python
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
2
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
3
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
4 # borrowed from: http://wiki.g2.bx.psu.edu/Future/Job%20Failure%20When%20stderr and modified for use with Trinity tools.
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
5
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
6 """
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
7 Wrapper that execute a program and its arguments but reports standard error
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
8 messages only if the program exit status was not 0
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
9 Example: ./stderr_wrapper.py myprog arg1 -f arg2
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
10 """
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
11
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
12 import sys, subprocess, os
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
13 print sys.version
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
14
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
15 assert sys.version_info[:2] >= ( 2, 4 )
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
16
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
17 TRINITY_BASE_DIR = ""
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
18 if os.environ.has_key('TRINITY_HOME'):
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
19 TRINITY_BASE_DIR = os.environ['TRINITY_HOME'];
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
20 elif hasattr(os, 'symlink'): # symlink was implemented to always return false when it was not implemented in earlier versions.
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
21 # 2017-09-26
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
22 # Cicada Dennis added looking for the location of the Trinity program using the Unix "which" utility.
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
23 # I tried using "command -v Trinity" but for some reason, I was getting a OS permission error with that.
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
24 # I just found distutils.spawn.find_executable() which might work, but already implemented the below.
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
25 try:
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
26 pipe1 = subprocess.Popen(["which", "Trinity"], stdout=subprocess.PIPE)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
27 except:
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
28 msg = "You must set the environmental variable TRINITY_HOME to the base installation directory of Trinity before running {:s}.\n".format(sys.argv[0])
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
29 sys.stderr.write(msg)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
30 # t, v, tb = sys.exc_info()
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
31 # raise t, v, tb
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
32 # For some reason the above was giving a syntax error.
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
33 # A simple raise should reraise the existing exception.
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
34 raise
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
35 else:
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
36 TrinityPath, err_info = pipe1.communicate()
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
37 # FIX - probably should be checking err_info for errors...
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
38 # Determine the TRINITY_BASE_DIR from output1.
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
39 # If TrinityPath is a link, we need to dereference the link.
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
40 TrinityPath = TrinityPath.rstrip() # Need to strip off a newline.
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
41 # print "Trinity that was found is: {:s}".format(repr(TrinityPath))
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
42 # print os.path.islink(TrinityPath)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
43 TrinityPath = os.path.abspath(TrinityPath)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
44 # msg = "The Absolute Trinity path that was found is: {:s}".format(TrinityPath)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
45 # print msg
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
46 # print os.path.islink(TrinityPath)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
47 while os.path.islink(TrinityPath):
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
48 # print "That path is a link."
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
49 TrinityPath = os.path.join(os.path.dirname(TrinityPath),os.readlink(TrinityPath))
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
50 # print "The new path is: {:s}".format(TrinityPath)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
51 # Take off the last part of the path (which is the Trinity command)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
52 TRINITY_BASE_DIR = "/".join(TrinityPath.split("/")[0:-1])
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
53 else:
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
54 sys.stderr.write("Either set TRINITY_HOME to the trinity base directory, or ensure that directory is in the PATH before running.")
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
55 sys.exit(1)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
56
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
57
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
58 # get bindir
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
59 bindir = sys.argv[0]
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
60 bindir = bindir.split("/")
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
61 if len(bindir) > 1:
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
62 bindir.pop()
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
63 bindir = "/".join(bindir)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
64 else:
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
65 bindir = "."
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
66
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
67
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
68 ## add locations of tools to path setting.
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
69 #TOOL_PATHS_FILE = bindir + "/__add_to_PATH_setting.txt";
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
70 #for line in open(TOOL_PATHS_FILE):
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
71 # line = line.rstrip()
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
72 # os.environ['PATH'] += ":" + line
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
73
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
74 # Add TrinityPath and its utils to the PATH environment variable.
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
75 # print "Initially the PATH env variable is:\n\t{:s}".format(os.environ['PATH'])
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
76 os.environ['PATH'] = os.environ['PATH'] + ":{:s}:{:s}".format(TRINITY_BASE_DIR,TRINITY_BASE_DIR+"/util")
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
77 # print "Now the PATH env variable is:\n\t{:s}".format(os.environ['PATH'])
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
78
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
79
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
80 def stop_err( msg ):
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
81 sys.stderr.write( "%s\n" % msg )
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
82 sys.exit()
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
83
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
84 def __main__():
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
85 # Get command-line arguments
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
86 args = sys.argv
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
87 # Remove name of calling program, i.e. ./stderr_wrapper.py
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
88 args.pop(0)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
89 # If there are no arguments left, we're done
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
90 if len(args) == 0:
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
91 return
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
92
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
93 # If one needs to silence stdout
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
94 #args.append( ">" )
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
95 #args.append( "/dev/null" )
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
96 print "The TRINITY_BASE_DIR is:\n\t{:s}".format(TRINITY_BASE_DIR)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
97 print "The PATH env variable is:\n\t{:s}".format(os.environ['PATH'])
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
98
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
99 args[0] = "".join([TRINITY_BASE_DIR, '/', args[0]]);
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
100
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
101 cmdline = " ".join(args)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
102
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
103 print "The command being invoked is:\n\t{:s}".format(cmdline)
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
104
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
105
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
106 try:
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
107 # Run program
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
108 err_capture = open("stderr.txt", 'w')
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
109 proc = subprocess.Popen( args=cmdline, shell=True, stderr=err_capture, stdout=sys.stdout )
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
110 returncode = proc.wait()
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
111 err_capture.close()
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
112
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
113
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
114 if returncode != 0:
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
115 raise Exception
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
116
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
117 except Exception:
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
118 # Running Grinder failed: write error message to stderr
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
119 err_text = open("stderr.txt").readlines()
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
120 stop_err( "ERROR:\n" + "\n".join(err_text))
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
121
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
122
5eca0c75b178 Upload ctat tools.
trinity_ctat
parents:
diff changeset
123 if __name__ == "__main__": __main__()