comparison bcftools_view.py @ 0:80d72a1dbd13

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