Mercurial > repos > trinity_ctat > align_and_estimate_abundance
annotate trinityToolWrapper.py @ 6:12bc09b4a26d draft
Adding TrinityPath/util to the PATH so other utilities can be found.
| author | trinity_ctat |
|---|---|
| date | Tue, 26 Sep 2017 13:58:08 -0400 |
| parents | 98d99fed9d35 |
| children | 78d12a725443 |
| rev | line source |
|---|---|
| 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']; | |
|
5
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
19 elif hasattr(os, 'symlink'): # symlink was implemented to always return false when it was not implemented in earlier versions. |
|
2
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
20 # 2017-09-26 |
|
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
21 # Cicada Dennis added looking for the location of the Trinity program using the Unix "which" utility. |
| 0 | 22 # I tried using "command -v Trinity" but for some reason, I was getting a OS permission error with that. |
|
2
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
23 # I just found distutils.spawn.find_executable() which might work, but already implemented the below. |
| 0 | 24 try: |
| 25 pipe1 = subprocess.Popen(["which", "Trinity"], stdout=subprocess.PIPE) | |
| 26 except: | |
| 27 t, v, tb = sys.exc_info() | |
| 3 | 28 sys.stderr.write("You must set the environmental variable TRINITY_HOME to the base installation directory of Trinity before running {:s}.".format(sys.argv[0])) |
| 0 | 29 raise t, v, tb |
| 30 else: | |
|
2
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
31 TrinityPath, err_info = pipe1.communicate() |
|
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
32 # FIX - probably should be checking err_info for errors... |
|
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
33 # Determine the TRINITY_BASE_DIR from output1. |
|
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
34 # If TrinityPath is a link, we need to dereference the link. |
|
5
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
35 TrinityPath = TrinityPath.rstrip() # Need to strip off a newline. |
|
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
36 # print "Trinity that was found is: {:s}".format(repr(TrinityPath)) |
|
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
37 # print os.path.islink(TrinityPath) |
|
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
38 TrinityPath = os.path.abspath(TrinityPath) |
|
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
39 print "The Absolute Trinity path that was found is: {:s}".format(TrinityPath) |
|
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
40 # print os.path.islink(TrinityPath) |
| 4 | 41 while os.path.islink(TrinityPath): |
|
5
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
42 print "That path is a link." |
|
2
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
43 TrinityPath = os.path.join(os.path.dirname(TrinityPath),os.readlink(TrinityPath)) |
|
5
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
44 print "The new path is: {:s}".format(TrinityPath) |
| 0 | 45 # Take off the last part of the path (which is the Trinity command) |
|
2
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
46 TRINITY_BASE_DIR = "/".join(TrinityPath.split("/")[0:-1]) |
|
6
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
47 else: |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
48 print "Either set TRINITY_HOME to the trinity base directory, or ensure that directory is in the PATH." |
| 0 | 49 |
| 50 # get bindir | |
| 51 bindir = sys.argv[0] | |
| 52 bindir = bindir.split("/") | |
| 53 if len(bindir) > 1: | |
| 54 bindir.pop() | |
| 55 bindir = "/".join(bindir) | |
| 56 else: | |
| 57 bindir = "." | |
| 58 | |
| 59 | |
| 60 ## add locations of tools to path setting. | |
|
6
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
61 #TOOL_PATHS_FILE = bindir + "/__add_to_PATH_setting.txt"; |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
62 #for line in open(TOOL_PATHS_FILE): |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
63 # line = line.rstrip() |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
64 # os.environ['PATH'] += ":" + line |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
65 |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
66 # Add TrinityPath and its utils to the PATH environment variable. |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
67 # print "Initially the PATH env variable is:\n\t{:s}".format(os.environ['PATH']) |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
68 os.environ['PATH'] = os.environ['PATH'] + ":{:s}:{:s}".format(TRINITY_BASE_DIR,TRINITY_BASE_DIR+"/util") |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
69 # print "Now the PATH env variable is:\n\t{:s}".format(os.environ['PATH']) |
| 0 | 70 |
| 71 | |
| 72 def stop_err( msg ): | |
| 73 sys.stderr.write( "%s\n" % msg ) | |
| 74 sys.exit() | |
| 75 | |
| 76 def __main__(): | |
| 77 # Get command-line arguments | |
| 78 args = sys.argv | |
| 79 # Remove name of calling program, i.e. ./stderr_wrapper.py | |
| 80 args.pop(0) | |
| 81 # If there are no arguments left, we're done | |
| 82 if len(args) == 0: | |
| 83 return | |
| 84 | |
| 85 # If one needs to silence stdout | |
| 86 #args.append( ">" ) | |
| 87 #args.append( "/dev/null" ) | |
|
5
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
88 print "The TRINITY_BASE_DIR is:\n\t{:s}".format(TRINITY_BASE_DIR) |
|
6
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
89 print "The PATH env variable is:\n\t{:s}".format(os.environ['PATH']) |
| 0 | 90 |
| 91 args[0] = "".join([TRINITY_BASE_DIR, '/', args[0]]); | |
| 92 | |
| 93 cmdline = " ".join(args) | |
| 94 | |
|
5
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
95 print "The command being invoked is:\n\t{:s}".format(cmdline) |
| 0 | 96 |
| 97 | |
| 98 try: | |
| 99 # Run program | |
| 100 err_capture = open("stderr.txt", 'w') | |
| 101 proc = subprocess.Popen( args=cmdline, shell=True, stderr=err_capture, stdout=sys.stdout ) | |
| 102 returncode = proc.wait() | |
| 103 err_capture.close() | |
| 104 | |
| 105 | |
| 106 if returncode != 0: | |
| 107 raise Exception | |
| 108 | |
| 109 except Exception: | |
| 110 # Running Grinder failed: write error message to stderr | |
| 111 err_text = open("stderr.txt").readlines() | |
| 112 stop_err( "ERROR:\n" + "\n".join(err_text)) | |
| 113 | |
| 114 | |
| 115 if __name__ == "__main__": __main__() |
