annotate tools/ncbi_blast_plus/hide_stderr.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 """A simple script to redirect stderr to stdout when the return code is zero.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 See https://bitbucket.org/galaxy/galaxy-central/issue/325/
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 Currently Galaxy ignores the return code from command line tools (even if it
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 is non-zero which by convention indicates an error) and treats any output on
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 stderr as an error (even though by convention stderr is used for errors or
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 warnings).
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 This script runs the given command line, capturing all stdout and stderr in
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 memory, and gets the return code. For a zero return code, any stderr (which
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 should be warnings only) is added to the stdout. That way Galaxy believes
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 everything is fine. For a non-zero return code, we output stdout as is, and
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 any stderr, plus the return code to ensure there is some output on stderr.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 That way Galaxy treats this as an error.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 Once issue 325 is fixed, this script will not be needed.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 import subprocess
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 #Avoid using shell=True when we call subprocess to ensure if the Python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 #script is killed, so too is the BLAST process.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 words = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 for w in sys.argv[1:]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 if " " in w:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 words.append('"%s"' % w)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 words.append(w)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 cmd = " ".join(words)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 child = subprocess.Popen(sys.argv[1:],
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 except Exception, err:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 #Use .communicate as can get deadlocks with .wait(),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 stdout, stderr = child.communicate()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 return_code = child.returncode
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 if return_code:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 sys.stdout.write(stdout)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 sys.stderr.write(stderr)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 sys.stderr.write("Return error code %i from command:\n" % return_code)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 sys.stderr.write("%s\n" % cmd)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 sys.stdout.write(stdout)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 sys.stdout.write(stderr)