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