annotate deseq/stderr_wrapper.py @ 3:a49aff09553e draft default tip

Uploaded
author nikhil-joshi
date Wed, 09 Jan 2013 18:39:12 -0500
parents d7f27b43b8ff
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
1 #!/usr/bin/python
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
2
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
3 """
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
4 Wrapper that executes a program with its arguments but reports standard error
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
5 messages only if the program exit status was not 0. This is useful to prevent
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
6 Galaxy to interpret that there was an error if something was printed on stderr,
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
7 e.g. if this was simply a warning.
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
8 Example: ./stderr_wrapper.py myprog arg1 -f arg2
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
9 Author: Florent Angly
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
10 """
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
11
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
12 import sys, subprocess
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
13
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
14 assert sys.version_info[:2] >= ( 2, 4 )
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
15
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
16 def stop_err( msg ):
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
17 sys.stderr.write( "%s\n" % msg )
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
18 sys.exit()
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
19
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
20 def __main__():
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
21 # Get command-line arguments
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
22 args = sys.argv
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
23 # Remove name of calling program, i.e. ./stderr_wrapper.py
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
24 args.pop(0)
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
25 # If there are no arguments left, we're done
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
26 if len(args) == 0:
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
27 return
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
28
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
29 # If one needs to silence stdout
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
30 args.append( ">" )
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
31 args.append( "/dev/null" )
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
32
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
33 #cmdline = " ".join(args)
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
34 #print cmdline
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
35 try:
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
36 # Run program
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
37 proc = subprocess.Popen( args=args, shell=False, stderr=subprocess.PIPE )
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
38 returncode = proc.wait()
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
39 # Capture stderr, allowing for case where it's very large
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
40 stderr = ''
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
41 buffsize = 1048576
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
42 try:
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
43 while True:
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
44 stderr += proc.stderr.read( buffsize )
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
45 if not stderr or len( stderr ) % buffsize != 0:
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
46 break
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
47 except OverflowError:
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
48 pass
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
49 # Running Grinder failed: write error message to stderr
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
50 if returncode != 0:
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
51 raise Exception, stderr
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
52 except Exception, e:
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
53 # Running Grinder failed: write error message to stderr
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
54 stop_err( 'Error: ' + str( e ) )
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
55
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
56
d7f27b43b8ff Uploaded
nikhil-joshi
parents:
diff changeset
57 if __name__ == "__main__": __main__()