コード例 #1
0
ファイル: respond.py プロジェクト: bneijt/boxchannel
def main(args):
    prefs = boxchannel.loadUserPreferences()
    requestDirectory = prefs['requestDirectory']
    indexFileDirectory = prefs['indexDirectory']
    responseDirectory = prefs['responseDirectory']

    if not os.path.exists(prefs['responseDirectory']):
        os.makedirs(prefs['responseDirectory'])
    
    indexFileName = os.path.join(indexFileDirectory, "%s.index" % prefs['id'])
    indexFile = file(indexFileName, 'r')
    
    #Load all requests in memory
    requests = {}
    for request in glob.glob(os.path.join(requestDirectory, "*")):
        requestedHash = os.path.basename(request)
        requests[requestedHash] = True
    
    #See if we have that block in our index
    for pathAndHashes in boxchannel.indexedFiles(indexFile):
        fullPath = pathAndHashes.pop(0)
        hashes = pathAndHashes
        for (blockIdx, h) in enumerate(hashes):
            h = h.strip() #Remove newline if it is on the line
            if h in requests:
                print "Responding to", h
                #Respond with our file hash
                responseFile = os.path.join(responseDirectory, h)
                localFile = file(fullPath, 'r')
                IOS_BEG = 0
                localFile.seek(blockIdx * boxchannel.blockSize, IOS_BEG)
                block = localFile.read(boxchannel.blockSize)
                localFile.close()
                rf = file(responseFile, 'w')
                rf.write(block)
                rf.close()
                for requestName in glob.glob(os.path.join(requestDirectory, h)):
                    print "Removing", requestName
                    os.unlink(requestName)
コード例 #2
0
ファイル: stage.py プロジェクト: bneijt/boxchannel
def main(args):
    #Move all of our requests from the response directory into the stage directory
    prefs = boxchannel.loadUserPreferences()
    downloadDirectory = prefs['downloadDirectory']
    stageDirectory = prefs['stageDirectory']
    responseDirectory = prefs['responseDirectory']

    if not os.path.exists(prefs['stageDirectory']):
        os.makedirs(prefs['stageDirectory'])
    
    #For each of the responses, move it if we have an empty staged block for it
    for responseName in glob.glob(os.path.join(responseDirectory, "*")):
        blockHash = os.path.basename(responseName)
        stageFiles = glob.glob(os.path.join(stageDirectory, "*", blockHash + ".*")) #Find any staged file needing the block
        if len(stageFiles) > 0:
            removeResponse = False
            block = file(responseName, 'r').read(boxchannel.blockSize)
            for stageFile in stageFiles:
                if os.path.getsize(stageFile) == 0:
                    print "Realing in", blockHash
                    removeResponse = True
                    file(stageFile, 'w').write(block)
            if removeResponse:
                print "Removing", responseName
                #os.unlink(responseName)
    
    #See if we can patch up a file from the staged blocks, patch it up and clean the directory
    for stageFileDirectory in glob.glob(os.path.join(stageDirectory, "*")):
        complete = True
        blockFiles = glob.glob(os.path.join(stageFileDirectory, "*"))
        if len(blockFiles) > 0:
            for blockFile in blockFiles:
                if os.path.getsize(blockFile) == 0:
                    complete = False
                    break
        else:
            complete = False
        if complete:
            #Bundle into Downloads directory
            downloadName = os.path.basename(stageFileDirectory)
            print "Complete download:", downloadName
            blockFilesSorted = [None] * len(blockFiles)
            print "Size: %i times %i bytes" % (len(blockFiles), boxchannel.blockSize)
            for bfn in blockFiles:
                (h, idx) = os.path.basename(bfn).split(".", 1)
                blockFilesSorted[int(idx)] = bfn
            
            if None in blockFilesSorted:
                raise Exception("Could not find one of the file blocks")
            outputFileName = os.path.join(downloadDirectory, downloadName)
            outputFile = file(outputFileName, 'w')
            for blockFileName in blockFilesSorted:
                print "Writing:", blockFileName
                with open(blockFileName) as blockFile:
                    block = blockFile.read(boxchannel.blockSize)
                    outputFile.write(block)
                os.unlink(blockFileName)
            print "Written:", outputFileName

    #Clean up routine
    responses = boxchannel.filesInDirectory(responseDirectory)
    if len(responses) > boxchannel.maximumNumResponses:
        print "More then", boxchannel.maximumNumResponses, "requests, cleaning up"
        responses.sort(key=lambda x: os.path.getctime(x))
        while len(responses) > boxchannel.maximumNumResponses:
            target = responses.pop(0)
            print "Removing", target
            os.unlink(target)