# HG changeset patch
# User galaxy-dev@invbfotv02.agresearch.co.nz
# Date 1380067994 0
#      Wed Sep 25 00:13:14 2013 +0000
# Branch agr003
# Node ID e130088e73282b3de82bd9f601b1e37508ea6a3a
# Parent  6822f41bc9bb2a2bf4673d6dcdeb1939730d970f
Fixed handling of unicode in tool stdout and stderr.

diff -r 6822f41bc9bb -r e130088e7328 lib/galaxy/jobs/__init__.py
--- a/lib/galaxy/jobs/__init__.py	Mon Aug 19 13:06:17 2013 -0400
+++ b/lib/galaxy/jobs/__init__.py	Wed Sep 25 00:13:14 2013 +0000
@@ -21,6 +21,7 @@
 from galaxy.jobs.actions.post import ActionBox
 from galaxy.jobs.mapper import JobRunnerMapper
 from galaxy.jobs.runners import BaseJobRunner
+from galaxy.util import asciify
 from galaxy.util.bunch import Bunch
 from galaxy.util.expressions import ExpressionContext
 from galaxy.util.json import from_json_string
@@ -871,6 +872,16 @@
         self.sa_session.expunge_all()
         job = self.get_job()
 
+        # Deal with unicode in stdout and/or stderr.
+        stdout_a = asciify( stdout )
+        stderr_a = asciify( stderr )
+        if stdout_a != stdout:
+            log.debug( "JobWrapper removed unicode characters from stdout for job %d" % ( job.id ) )
+            stdout = stdout_a
+        if stderr_a != stderr:
+            log.debug( "JobWrapper removed unicode characters from stderr for job %d" % ( job.id ) )
+            stderr = stderr_a
+
         # TODO: After failing here, consider returning from the function.
         try:
             self.reclaim_ownership()
@@ -1667,6 +1678,16 @@
             self.fail( task.info )
             return
 
+        # Deal with unicode in stdout and/or stderr.
+        stdout_a = asciify( stdout )
+        stderr_a = asciify( stderr )
+        if stdout_a != stdout:
+            log.debug( "TaskWrapper removed unicode characters from stdout for job %d" % ( self.job_id ) )
+            stdout = stdout_a
+        if stderr_a != stderr:
+            log.debug( "TaskWrapper removed unicode characters from stderr for job %d" % ( self.job_id ) )
+            stderr = stderr_a
+
         # Check what the tool returned. If the stdout or stderr matched
         # regular expressions that indicate errors, then set an error.
         # The same goes if the tool's exit code was in a given range.
diff -r 6822f41bc9bb -r e130088e7328 lib/galaxy/util/__init__.py
--- a/lib/galaxy/util/__init__.py	Mon Aug 19 13:06:17 2013 -0400
+++ b/lib/galaxy/util/__init__.py	Wed Sep 25 00:13:14 2013 +0000
@@ -539,6 +539,10 @@
     except:
         return default
 
+def asciify(value):
+    """Convert to ASCII string, with unicode characters removed."""
+    return value.decode(DEFAULT_ENCODING).encode('ascii', 'ignore')
+
 def object_to_string( obj ):
     return binascii.hexlify( pickle.dumps( obj, 2 ) )
     
