Logo Search packages:      
Sourcecode: hellanzb version File versions  Download package

def Hellanzb::NZBLeecher::ArticleDecoder::assembleNZBFile (   nzbFile,
  autoFinish = True 
)

Assemble the final file from all the NZBFile's decoded segments 

Definition at line 608 of file ArticleDecoder.py.

00608                                                :
    """ Assemble the final file from all the NZBFile's decoded segments """
    # FIXME: does someone has to pad the file if we have broken pieces?

    # don't overwrite existing files -- instead rename them to 'file_dupeX' if they exist
    handleDupeNZBFile(nzbFile)
    if handleCanceledFile(nzbFile):
        return

    nzbFile.nzb.assembleLock.acquire()
    file = open(nzbFile.getDestination(), 'wb')
    write = file.write

    # Sort the segments incase they were out of order in the NZB file
    toAssembleSegments = nzbFile.nzbSegments[:]
    toAssembleSegments.sort(lambda x, y : cmp(x.number, y.number))
    
    for nzbSegment in toAssembleSegments:
        decodedSegmentFile = open(nzbSegment.getDestination(), 'rb')
        read = decodedSegmentFile.read
        try:
            while True:
                buf = read(BUF_SIZE)
                if not buf:
                    break
                write(buf)

        except IOError, ioe:
            nzbFile.nzb.assembleLock.release()
            file.close()
            decodedSegmentFile.close()
            handleIOError(ioe) # will re-raise
                    
        decodedSegmentFile.close()

        # Avoid delaying CTRL-C during this possibly lengthy file assembly loop
        try:
            checkShutdown()

        except SystemExit, se:
            # We were interrupted. Instead of waiting to finish, just delete the file. It
            # will be automatically assembled upon restart
            debug('(CTRL-C) Removing unfinished file: ' + nzbFile.getDestination())
            file.close()
            try:
                os.remove(nzbFile.getDestination())
            except OSError, ose:
                # postponement might have moved the file we just wrote to:
                # exceptions.OSError: [Errno 2] No such file or directory: 
                if ose.errno != 2:
                    debug('Unexpected ERROR while removing nzbFile: ' + nzbFile.getDestination())
            nzbFile.nzb.assembleLock.release()
            raise

    file.close()
    # Finally, delete all the segment files when finished
    for nzbSegment in toAssembleSegments:
        try:
            os.remove(nzbSegment.getDestination())
        except OSError, ose:
            # postponement might have moved the file we just wrote to:
            # exceptions.OSError: [Errno 2] No such file or directory: 
            if ose.errno != 2:
                debug('Unexpected ERROR while removing segmentFile: ' + segmentFile)

    Hellanzb.queue.fileDone(nzbFile)
    nzbFile.nzb.assembleLock.release()
    reactor.callFromThread(fileDone)
    
    debug('Assembled file: ' + nzbFile.getDestination() + ' from segment files: ' + \
          str([nzbSegment.getDestination() for nzbSegment in toAssembleSegments]))

    # nudge gc
    for nzbSegment in nzbFile.nzbSegments:
        del nzbSegment.nzbFile
        del nzbSegment
    del nzbFile.nzbSegments
    
    if autoFinish and not handleCanceledFile(nzbFile):
        # After assembling a file, check the contents of the filesystem to determine if we're done 
        tryFinishNZB(nzbFile.nzb)

def fileDone():


Generated by  Doxygen 1.6.0   Back to index