details: http://www.bx.psu.edu/hg/galaxy/rev/cf3c6da6f552 changeset: 1689:cf3c6da6f552 user: Greg Von Kuster <greg@bx.psu.edu> date: Tue Jan 06 11:35:49 2009 -0500 description: Better exception handling, messaging for upload utility. 3 file(s) affected in this change: lib/galaxy/datatypes/sniff.py lib/galaxy/tools/actions/upload.py test/functional/test_toolbox.py diffs (140 lines): diff -r e0162b7bf0ba -r cf3c6da6f552 lib/galaxy/datatypes/sniff.py --- a/lib/galaxy/datatypes/sniff.py Tue Jan 06 11:11:24 2009 -0500 +++ b/lib/galaxy/datatypes/sniff.py Tue Jan 06 11:35:49 2009 -0500 @@ -12,11 +12,11 @@ full_path = os.path.join(path, 'test', fname) return full_path -def stream_to_file(stream): +def stream_to_file( stream, suffix='', prefix='', dir=None, text=False ): """ Writes a stream to a temporary file, returns the temporary file's name """ - fd, temp_name = tempfile.mkstemp() + fd, temp_name = tempfile.mkstemp( suffix=suffix, prefix=prefix, dir=dir, text=text ) while 1: chunk = stream.read(1048576) if not chunk: diff -r e0162b7bf0ba -r cf3c6da6f552 lib/galaxy/tools/actions/upload.py --- a/lib/galaxy/tools/actions/upload.py Tue Jan 06 11:11:24 2009 -0500 +++ b/lib/galaxy/tools/actions/upload.py Tue Jan 06 11:35:49 2009 -0500 @@ -44,22 +44,33 @@ trans.log_event( 'created job id %d' % job.id, tool_id=tool.id ) if 'local_filename' in dir( data_file ): # Use the existing file + file_name = data_file.filename + file_name = file_name.split( '\\' )[-1] + file_name = file_name.split( '/' )[-1] try: - file_name = data_file.filename - file_name = file_name.split( '\\' )[-1] - file_name = file_name.split( '/' )[-1] data_list.append( self.add_file( trans, data_file.local_filename, file_name, file_type, dbkey, space_to_tab=space_to_tab ) ) except Exception, e: - return self.upload_empty( trans, job, "Error:", str( e ) ) + errmsg = 'exception in add_file using datafile.local_filename %s: %s' % ( data_file.local_filename, str( e ) ) + return self.upload_empty( trans, job, "Error:", errmsg ) elif 'filename' in dir( data_file ): + file_name = data_file.filename + file_name = file_name.split( '\\' )[-1] + file_name = file_name.split( '/' )[-1] try: - file_name = data_file.filename - file_name = file_name.split( '\\' )[-1] - file_name = file_name.split( '/' )[-1] - temp_name = sniff.stream_to_file( data_file.file ) + temp_name = sniff.stream_to_file( data_file.file, prefix='upload' ) + except Exception, e: + try: + # Attempt to remove temporary file + os.unlink( temp_name ) + except: + pass + errmsg = 'exception in sniff.stream_to_file using file %s: %s' % ( data_file.filename, str( e ) ) + return self.upload_empty( trans, job, "Error:", errmsg ) + try: data_list.append( self.add_file( trans, temp_name, file_name, file_type, dbkey, space_to_tab=space_to_tab ) ) except Exception, e: - return self.upload_empty( trans, job, "Error:", str( e ) ) + errmsg = 'exception in add_file using file temp_name %s: %s' % ( str( temp_name ), str( e ) ) + return self.upload_empty( trans, job, "Error:", errmsg ) if url_paste not in [ None, "" ]: if url_paste.lower().find( 'http://' ) >= 0 or url_paste.lower().find( 'ftp://' ) >= 0: url_paste = url_paste.replace( '\r', '' ).split( '\n' ) @@ -67,10 +78,20 @@ line = line.rstrip( '\r\n' ) if line: try: - temp_name = sniff.stream_to_file( urllib.urlopen( line ) ) + temp_name = sniff.stream_to_file( urllib.urlopen( line ), prefix='url_paste' ) + except Exception, e: + try: + # Attempt to remove temporary file + os.unlink( temp_name ) + except: + pass + errmsg = 'exception in sniff.stream_to_file using url_paste %s: %s' % ( url_paste, str( e ) ) + return self.upload_empty( trans, job, "Error:", errmsg ) + try: data_list.append( self.add_file( trans, temp_name, line, file_type, dbkey, info="uploaded url", space_to_tab=space_to_tab ) ) except Exception, e: - return self.upload_empty( trans, job, "Error:", str( e ) ) + errmsg = 'exception in add_file using url_paste temp_name %s: %s' % ( str( temp_name ), str( e ) ) + return self.upload_empty( trans, job, "Error:", errmsg ) else: is_valid = False for line in url_paste: @@ -80,10 +101,20 @@ break if is_valid: try: - temp_name = sniff.stream_to_file( StringIO.StringIO( url_paste ) ) + temp_name = sniff.stream_to_file( StringIO.StringIO( url_paste ), prefix='strio_url_paste' ) + except Exception, e: + try: + # Attempt to remove temporary file + os.unlink( temp_name ) + except: + pass + errmsg = 'exception in sniff.stream_to_file using StringIO.StringIO( url_paste ) %s: %s' % ( url_paste, str( e ) ) + return self.upload_empty( trans, job, "Error:", errmsg ) + try: data_list.append( self.add_file( trans, temp_name, 'Pasted Entry', file_type, dbkey, info="pasted entry", space_to_tab=space_to_tab ) ) except Exception, e: - return self.upload_empty( trans, job, "Error:", str( e ) ) + errmsg = 'exception in add_file using StringIO.StringIO( url_paste ) temp_name %s: %s' % ( str( temp_name ), str( e ) ) + return self.upload_empty( trans, job, "Error:", errmsg ) else: return self.upload_empty( trans, job, "No data error:", "you pasted no data." ) if self.empty: @@ -103,7 +134,7 @@ log.info("End of tool %s execution for job id %d, memory used increased by %s" % ( tool.id, job.id, m1 ) ) return dict( output=hda ) - def upload_empty(self, trans, job, err_code, err_msg): + def upload_empty( self, trans, job, err_code, err_msg ): data = trans.app.model.HistoryDatasetAssociation( create_dataset = True ) data.name = err_code data.extension = "txt" @@ -119,8 +150,8 @@ job.info = err_msg job.add_output_dataset( data.name, data ) job.flush() - log.info( 'job id %d ended with errors' % job.id ) - trans.log_event( 'job id %d ended with errors' % job.id, tool_id=job.tool_id ) + log.info( 'job id %d ended with errors, err_msg: %s' % ( job.id, err_msg ) ) + trans.log_event( 'job id %d ended with errors, err_msg: %s' % ( job.id, err_msg ), tool_id=job.tool_id ) return dict( output=data ) def add_file( self, trans, temp_name, file_name, file_type, dbkey, info=None, space_to_tab=False ): diff -r e0162b7bf0ba -r cf3c6da6f552 test/functional/test_toolbox.py --- a/test/functional/test_toolbox.py Tue Jan 06 11:11:24 2009 -0500 +++ b/test/functional/test_toolbox.py Tue Jan 06 11:35:49 2009 -0500 @@ -8,7 +8,6 @@ class ToolTestCase( TwillTestCase ): """Abstract test case that runs tests based on a `galaxy.tools.test.ToolTest`""" def do_it( self ): - print "###self.testdef: ", self.testdef # If the test generation had an error, raise if self.testdef.error: if self.testdef.exception: