diff --git a/chunk.py b/chunk.py index d7505c6..19e72b4 100644 --- a/chunk.py +++ b/chunk.py @@ -60,17 +60,6 @@ def render_and_save(chunkfile, cave=False): # propagate the exception back to the parent process. raise Exception() -def valid_image(filename): - """Returns true if the file is valid, false if it can't be loaded (is - corrupt or something) - """ - try: - img = Image.open(filename) - img.load() - except Exception, e: - return False - return True - class ChunkRenderer(object): def __init__(self, chunkfile): if not os.path.exists(chunkfile): @@ -140,11 +129,7 @@ class ChunkRenderer(object): if os.path.getmtime(self.chunkfile) < os.path.getmtime(oldimg_path): # chunkfile is older than the image, don't even bother checking # the hash - if valid_image(oldimg_path): - return oldimg_path - else: - os.unlink(oldimg_path) - oldimg = None + return oldimg_path # Reasons for the code to get to this point: # 1) An old image doesn't exist @@ -163,7 +148,7 @@ class ChunkRenderer(object): dest_path = os.path.join(destdir, dest_filename) if oldimg: - if dest_filename == oldimg and valid_image(dest_path): + if dest_filename == oldimg: # There is an existing file, the chunk has a newer mtime, but the # hashes match. return dest_path @@ -175,7 +160,11 @@ class ChunkRenderer(object): # Render the chunk img = self.chunk_render(cave=cave) # Save it - img.save(dest_path) + try: + img.save(dest_path) + except: + os.unlink(dest_path) + raise # Return its location return dest_path diff --git a/gmap.py b/gmap.py index 69b25f3..20a7584 100755 --- a/gmap.py +++ b/gmap.py @@ -43,7 +43,9 @@ def main(): if i > 0: if 1000 % i == 0 or i % 1000 == 0: print "{0}/{1} chunks rendered".format(i, len(chunks)) - + + results['pool'].join() + print "Writing out html file" if not os.path.exists(destdir): os.mkdir(destdir) diff --git a/world.py b/world.py index 6fa893e..7e480e2 100644 --- a/world.py +++ b/world.py @@ -299,7 +299,22 @@ def render_worldtile(chunkmap, colstart, colend, rowstart, rowend, oldhash): # col colstart will get drawn on the image starting at x coordinates -(384/2) # row rowstart will get drawn on the image starting at y coordinates -(192/2) for col, row, chunkfile in tilelist: - chunkimg = Image.open(chunkfile) + try: + chunkimg = Image.open(chunkfile) + except IOError, e: + print "Error opening file", chunkfile + print "Attempting to re-generate it" + os.unlink(chunkfile) + # Do some string manipulation to determine what the chunk file is + # that goes with this image. Then call chunk.render_and_save + dirname, imagename = os.path.split(chunkfile) + parts = imagename.split(".") + datafile = "c.{0}.{1}.dat".format(parts[1],parts[2]) + print "Chunk came from data file", datafile + # XXX Don't forget to set cave mode here when it gets implemented! + chunk.render_and_save(os.path.join(dirname, datafile), False) + chunkimg = Image.open(chunkfile) + print "Success" xpos = -192 + (col-colstart)*192 ypos = -96 + (row-rowstart)*96 @@ -635,13 +650,21 @@ class ReturnableProcess(multiprocessing.Process): multiprocessing.Process.__init__(self, *args, **kwargs) def run(self): - results = self._target(*self._args, **self._kwargs) - self._respipe_in.send(results) - self.__sem.release() + try: + results = self._target(*self._args, **self._kwargs) + except BaseException, e: + self._respipe_in.send(e) + else: + self._respipe_in.send(results) + finally: + self.__sem.release() def get(self): self.join() - return self._respipe_out.recv() + ret = self._respipe_out.recv() + if isinstance(ret, BaseException): + raise ret + return ret def start(self): self._respipe_out, self._respipe_in = multiprocessing.Pipe()