From a361e7d9cf7d571bfe19144a011f39bb02bdad63 Mon Sep 17 00:00:00 2001 From: Peter Cock Date: Wed, 18 Apr 2012 10:36:23 +0100 Subject: [PATCH] Handle merging bad BLAST XML files better --- lib/galaxy/datatypes/xml.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/galaxy/datatypes/xml.py b/lib/galaxy/datatypes/xml.py index d9e659bd70c..ef1f1623e28 100644 --- a/lib/galaxy/datatypes/xml.py +++ b/lib/galaxy/datatypes/xml.py @@ -104,13 +104,41 @@ class BlastXml( GenericXml ): for f in split_files: h = open(f) body = False - header = [] + header = h.readline() + if not header: + out.close() + h.close() + raise ValueError("BLAST XML file %s was empty" % f) + if header.strip() != '': + out.write(header) #for diagnosis + out.close() + h.close() + raise ValueError("%s is not an XML file!" % f) + line = h.readline() + header += line + if line.strip() not in ['', + '']: + out.write(header) #for diagnosis + out.close() + h.close() + raise ValueError("%s is not a BLAST XML file!" % f) while True: line = h.readline() - header.append(line) + if not line: + out.write(header) #for diagnosis + out.close() + h.close() + raise ValueError("BLAST XML file %s ended prematurely" % f) + header += line if "" in line: break - header = "".join(header) + if len(header) > 10000: + #Something has gone wrong, don't load too much into memory! + #Write what we have to the merged file for diagnostics + out.write(header) + out.close() + h.close() + raise ValueError("BLAST XML file %s has too long a header!" % f) if "" not in header: out.close() h.close()