annotate bcftools_wrapper.py @ 2:14567aa2be12 draft default tip

Added wrapper script for correct stderr handling
author geert-vandeweyer
date Thu, 10 Apr 2014 09:44:09 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
1 #!/usr/bin/env python
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
2
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
3 """
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
4 Wrapper that execute a program and its arguments but reports standard error
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
5 messages only if the program exit status was not 0
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
6 Example: ./stderr_wrapper.py myprog arg1 -f arg2
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
7
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
8 Taken from bcftools_view package of gregory-minevich
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
9 """
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
10
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
11 import sys, subprocess
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
12
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
13 assert sys.version_info[:2] >= ( 2, 4 )
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
14
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
15 def stop_err( msg ):
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
16 sys.stderr.write( "%s\n" % msg )
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
17 sys.exit()
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
18
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
19 def __main__():
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
20 # Get command-line arguments
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
21 args = sys.argv
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
22 # Remove name of calling program, i.e. ./stderr_wrapper.py
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
23 args.pop(0)
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
24 # If there are no arguments left, we're done
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
25 if len(args) == 0:
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
26 return
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
27
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
28 # If one needs to silence stdout
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
29 #args.append( ">" )
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
30 #args.append( "/dev/null" )
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
31
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
32 cmdline = " ".join(args)
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
33 try:
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
34 # Run program
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
35 proc = subprocess.Popen( args=cmdline, shell=True, stderr=subprocess.PIPE )
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
36 returncode = proc.wait()
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
37 # Capture stderr, allowing for case where it's very large
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
38 stderr = ''
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
39 buffsize = 1048576
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
40 try:
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
41 while True:
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
42 stderr += proc.stderr.read( buffsize )
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
43 if not stderr or len( stderr ) % buffsize != 0:
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
44 break
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
45 except OverflowError:
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
46 pass
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
47 # Running BCFtools failed: write error message to stderr
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
48 if returncode != 0:
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
49 raise Exception, stderr
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
50 except Exception, e:
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
51 # Running BCFtools failed: write error message to stderr
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
52 stop_err( 'Error:\n' + str( e ) )
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
53
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
54
14567aa2be12 Added wrapper script for correct stderr handling
geert-vandeweyer
parents:
diff changeset
55 if __name__ == "__main__": __main__()