annotate hide_stderr.py @ 2:ab1a8640f817 draft

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