# HG changeset patch # User geert-vandeweyer # Date 1397137449 14400 # Node ID 14567aa2be1282f750fa89319402c904fa72a034 # Parent 3182c7fac413c450f5740f1a7f5d5eab2e13d3fe Added wrapper script for correct stderr handling diff -r 3182c7fac413 -r 14567aa2be12 bcftools_cat.xml --- a/bcftools_cat.xml Thu Jul 11 14:27:32 2013 -0500 +++ b/bcftools_cat.xml Thu Apr 10 09:44:09 2014 -0400 @@ -5,7 +5,7 @@ samtools - bcftools cat $rinput > $output + bcftools_wrapper.py bcftools cat $rinput > $output @@ -39,4 +39,4 @@ - \ No newline at end of file + diff -r 3182c7fac413 -r 14567aa2be12 bcftools_index.xml --- a/bcftools_index.xml Thu Jul 11 14:27:32 2013 -0500 +++ b/bcftools_index.xml Thu Apr 10 09:44:09 2014 -0400 @@ -5,7 +5,7 @@ samtools - bcftools index $input > $output + bcftools_wrapper.py bcftools index $input > $output @@ -37,4 +37,4 @@ - \ No newline at end of file + diff -r 3182c7fac413 -r 14567aa2be12 bcftools_view.xml --- a/bcftools_view.xml Thu Jul 11 14:27:32 2013 -0500 +++ b/bcftools_view.xml Thu Apr 10 09:44:09 2014 -0400 @@ -3,8 +3,8 @@ samtools - - bcftools view + + bcftools_wrapper.py bcftools view #if str( $A ) == "true": -A #end if @@ -51,7 +51,7 @@ -v #end if $input - > $output + > $output diff -r 3182c7fac413 -r 14567aa2be12 bcftools_wrapper.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bcftools_wrapper.py Thu Apr 10 09:44:09 2014 -0400 @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +""" +Wrapper that execute a program and its arguments but reports standard error +messages only if the program exit status was not 0 +Example: ./stderr_wrapper.py myprog arg1 -f arg2 + +Taken from bcftools_view package of gregory-minevich +""" + +import sys, subprocess + +assert sys.version_info[:2] >= ( 2, 4 ) + +def stop_err( msg ): + sys.stderr.write( "%s\n" % msg ) + sys.exit() + +def __main__(): + # Get command-line arguments + args = sys.argv + # Remove name of calling program, i.e. ./stderr_wrapper.py + args.pop(0) + # If there are no arguments left, we're done + if len(args) == 0: + return + + # If one needs to silence stdout + #args.append( ">" ) + #args.append( "/dev/null" ) + + cmdline = " ".join(args) + try: + # Run program + proc = subprocess.Popen( args=cmdline, shell=True, stderr=subprocess.PIPE ) + returncode = proc.wait() + # Capture stderr, allowing for case where it's very large + stderr = '' + buffsize = 1048576 + try: + while True: + stderr += proc.stderr.read( buffsize ) + if not stderr or len( stderr ) % buffsize != 0: + break + except OverflowError: + pass + # Running BCFtools failed: write error message to stderr + if returncode != 0: + raise Exception, stderr + except Exception, e: + # Running BCFtools failed: write error message to stderr + stop_err( 'Error:\n' + str( e ) ) + + +if __name__ == "__main__": __main__()