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