def handleDupeNZBFileNeedsDownload(nzbFile, workingDirDupeMap): """ Determine whether or not this NZBFile is a known duplicate. If so, also determine if this NZBFile needs to be downloaded """ isDupe = False # Search the dupes on disk for a match for file in workingDirDupeMap.iterkeys(): if nzbFile.subject.find(file) > -1: isDupe = True debug('handleDupeNeedsDownload: handling dupe: %s' % file) # *sigh* we're a dupe. Find the first unidentified index in the dupeEntry # (dupeEntry[1] is None) for dupeEntry in workingDirDupeMap[file]: origin = None if dupeEntry[1] is None: # Found an entry -- this is for our specified nzbFile dupeEntry[1] = nzbFile # Set our filename now, since we know it, for sanity sake dupeFilename = nextDupeName(os.path.join( Hellanzb.WORKING_DIR, file), checkOnDisk=False, minIteration=dupeEntry[0] + 1) nzbFile.filename = os.path.basename(dupeFilename) debug('handleDupeNeedsDownload: marking fileNum: %i as dupeFilename' \ ' %s (dupeEntry index: %i)' % (nzbFile.number, nzbFile.filename, dupeEntry[0])) # Now that we have the correct filename we can determine if this dupe # needs to be downloaded if os.path.isfile(dupeFilename): debug('handleDupeNeedsDownload: dupeName: %s needsDownload: False' \ % nzbFile.filename) return isDupe, False debug('handleDupeNeedsDownload: dupeName: %s needsDownload: True' \ % nzbFile.filename) return isDupe, True # Keep track of the origin -- we need to handle it specially (rename it to # an actual dupeName ASAP) if there are more duplicates in the NZB than # there are currently on disk elif dupeEntry[0] == -1: origin = dupeEntry[1] # Didn't find a match on disk. Needs to be downloaded return isDupe, True return isDupe, None
def handleDupeNZBFileNeedsDownload(nzbFile, workingDirDupeMap): """ Determine whether or not this NZBFile is a known duplicate. If so, also determine if this NZBFile needs to be downloaded """ isDupe = False # Search the dupes on disk for a match for file in workingDirDupeMap.iterkeys(): if nzbFile.subject.find(file) > -1: isDupe = True debug('handleDupeNeedsDownload: handling dupe: %s' % file) # *sigh* we're a dupe. Find the first unidentified index in the dupeEntry # (dupeEntry[1] is None) for dupeEntry in workingDirDupeMap[file]: origin = None if dupeEntry[1] is None: # Found an entry -- this is for our specified nzbFile dupeEntry[1] = nzbFile # Set our filename now, since we know it, for sanity sake dupeFilename = nextDupeName(os.path.join(Hellanzb.WORKING_DIR, file), checkOnDisk = False, minIteration = dupeEntry[0] + 1) nzbFile.filename = os.path.basename(dupeFilename) debug('handleDupeNeedsDownload: marking fileNum: %i as dupeFilename' \ ' %s (dupeEntry index: %i)' % (nzbFile.number, nzbFile.filename, dupeEntry[0])) # Now that we have the correct filename we can determine if this dupe # needs to be downloaded if os.path.isfile(dupeFilename): debug('handleDupeNeedsDownload: dupeName: %s needsDownload: False' \ % nzbFile.filename) return isDupe, False debug('handleDupeNeedsDownload: dupeName: %s needsDownload: True' \ % nzbFile.filename) return isDupe, True # Keep track of the origin -- we need to handle it specially (rename it to # an actual dupeName ASAP) if there are more duplicates in the NZB than # there are currently on disk elif dupeEntry[0] == -1: origin = dupeEntry[1] # Didn't find a match on disk. Needs to be downloaded return isDupe, True return isDupe, None
def handleDupeNZBSegment(nzbSegment): """ Handle a duplicate NZBSegment file on disk (prior to writing a new one), if one exists """ dest = nzbSegment.getDestination() if validWorkingFile(dest, overwriteZeroByteFiles = \ nzbSegment.nzbFile.nzb.overwriteZeroByteFiles): # We have lazily found a duplicate segment (a .segmentXXXX already on disk that we # were about to write to). Determine the new, duplicate filename, that either the # on disk file or the segment ABOUT to be written to disk will be renamed to. We # must avoid renaming it a filename already on disk (nextDupeName will check on # disk for us) OR to an already reserved filename that may not already be on disk # (represented by eschewNames) parentFilename = dest[:-12] # remove .segmentXXXX segmentNumStr = dest[-12:] # just .segmentXXXX dupeNZBFileName = nextDupeName(parentFilename, eschewNames=knownRealNZBFilenames()) beingDownloadedNZBSegment = Hellanzb.queue.isBeingDownloadedFile(dest) info('Duplicate segment (%s), renaming parent file: %s to %s' % \ (segmentNumStr, os.path.basename(parentFilename), os.path.basename(dupeNZBFileName))) if beingDownloadedNZBSegment is not None: debug('handleDupeNZBSegment: handling dupe: %s renaming to: %s' % \ (os.path.basename(dest), os.path.basename(dupeNZBFileName))) # FIXME: should probably assert beingDownloadedNZBSegment.nzbFile.number != # nzbSegment.nzbFile.number here # Maintain the correct order when renaming -- the earliest (as they appear in # the NZB) clashing NZBFile gets renamed if beingDownloadedNZBSegment.nzbFile.number < nzbSegment.nzbFile.number: renameFile = beingDownloadedNZBSegment.nzbFile else: renameFile = nzbSegment.nzbFile ArticleDecoder.setRealFileName(renameFile, os.path.basename(dupeNZBFileName), forceChange=True) else: # NOTE: Probably nothing should trigger this, except maybe .par .segment0001 # files (when smartpar is added). CAUTION: Other cases that might trigger this # block should no longer happen! debug('handleDupeNZBSegment: handling dupe (not ' + \ 'beingDownloadedNZBSegment!?): %s renaming to: %s' % \ (os.path.basename(dest), os.path.basename(dupeNZBFileName))) os.rename(dest, dupeNZBFileName + segmentNumStr)
def handleDupeNZBSegment(nzbSegment): """ Handle a duplicate NZBSegment file on disk (prior to writing a new one), if one exists """ dest = nzbSegment.getDestination() if validWorkingFile(dest, overwriteZeroByteFiles = \ nzbSegment.nzbFile.nzb.overwriteZeroByteFiles): # We have lazily found a duplicate segment (a .segmentXXXX already on disk that we # were about to write to). Determine the new, duplicate filename, that either the # on disk file or the segment ABOUT to be written to disk will be renamed to. We # must avoid renaming it a filename already on disk (nextDupeName will check on # disk for us) OR to an already reserved filename that may not already be on disk # (represented by eschewNames) parentFilename = dest[:-12] # remove .segmentXXXX segmentNumStr = dest[-12:] # just .segmentXXXX dupeNZBFileName = nextDupeName(parentFilename, eschewNames = knownRealNZBFilenames()) beingDownloadedNZBSegment = Hellanzb.queue.isBeingDownloadedFile(dest) info('Duplicate segment (%s), renaming parent file: %s to %s' % \ (segmentNumStr, os.path.basename(parentFilename), os.path.basename(dupeNZBFileName))) if beingDownloadedNZBSegment is not None: debug('handleDupeNZBSegment: handling dupe: %s renaming to: %s' % \ (os.path.basename(dest), os.path.basename(dupeNZBFileName))) # FIXME: should probably assert beingDownloadedNZBSegment.nzbFile.number != # nzbSegment.nzbFile.number here # Maintain the correct order when renaming -- the earliest (as they appear in # the NZB) clashing NZBFile gets renamed if beingDownloadedNZBSegment.nzbFile.number < nzbSegment.nzbFile.number: renameFile = beingDownloadedNZBSegment.nzbFile else: renameFile = nzbSegment.nzbFile ArticleDecoder.setRealFileName(renameFile, os.path.basename(dupeNZBFileName), forceChange = True) else: # NOTE: Probably nothing should trigger this, except maybe .par .segment0001 # files (when smartpar is added). CAUTION: Other cases that might trigger this # block should no longer happen! debug('handleDupeNZBSegment: handling dupe (not ' + \ 'beingDownloadedNZBSegment!?): %s renaming to: %s' % \ (os.path.basename(dest), os.path.basename(dupeNZBFileName))) os.rename(dest, dupeNZBFileName + segmentNumStr)
def testNextDupeName(self): """ Test the nextDupeName functionality. """ + nextDupeName.__doc__ testFile = os.path.join(self.tempDir, 'file') testFile0 = os.path.join(self.tempDir, 'file_hellanzb_dupe0') testFile1 = os.path.join(self.tempDir, 'file_hellanzb_dupe1') testFile2 = os.path.join(self.tempDir, 'file_hellanzb_dupe2') self.assertEqual(nextDupeName(testFile), testFile0) self.assertEqual(nextDupeName(testFile, eschewNames = (testFile0)), testFile1) touch(testFile) touch(testFile0) self.assertEqual(nextDupeName(testFile), testFile1) self.assertEqual(nextDupeName(testFile, checkOnDisk = False), testFile0) self.assertEqual(nextDupeName(testFile, checkOnDisk = False, eschewNames = (testFile0)), testFile1) # checkOnDisk = False, minIteration = 0 should do nothing self.assertEqual(nextDupeName(testFile, checkOnDisk = False, minIteration = 0), testFile)
def testNextDupeName(self): """ Test the nextDupeName functionality. """ + nextDupeName.__doc__ testFile = os.path.join(self.tempDir, 'file') testFile0 = os.path.join(self.tempDir, 'file_hellanzb_dupe0') testFile1 = os.path.join(self.tempDir, 'file_hellanzb_dupe1') testFile2 = os.path.join(self.tempDir, 'file_hellanzb_dupe2') self.assertEqual(nextDupeName(testFile), testFile0) self.assertEqual(nextDupeName(testFile, eschewNames=(testFile0)), testFile1) touch(testFile) touch(testFile0) self.assertEqual(nextDupeName(testFile), testFile1) self.assertEqual(nextDupeName(testFile, checkOnDisk=False), testFile0) self.assertEqual( nextDupeName(testFile, checkOnDisk=False, eschewNames=(testFile0)), testFile1) # checkOnDisk = False, minIteration = 0 should do nothing self.assertEqual( nextDupeName(testFile, checkOnDisk=False, minIteration=0), testFile)