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