annotate bcftools_view.py @ 8:6572c40a8505 draft default tip

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