Пример #1
0
def main(args):

    #Load or generate user preferences
    prefs = boxchannel.initUserPreferences()
    
    #Load all index files, see if we can find the mentioned file
    indexFileDirectory = prefs['indexDirectory']
    requestDirectory = prefs['requestDirectory']
    stageDirectory = prefs['stageDirectory']

    if not os.path.exists(prefs['requestDirectory']):
        os.makedirs(prefs['requestDirectory'])

    if len(args) < 1:
        print "No file requested, listing all files indexed"
        for pathAndHashes in boxchannel.indexedFiles(indexFileDirectory):
            fullPath = pathAndHashes.pop(0)
            print os.path.basename(fullPath)
        return
    requestedFile = args.pop()
    
    #Clean up routine
    requests = boxchannel.filesInDirectory(requestDirectory)
    if len(requests) > boxchannel.maximumNumRequests:
        print "More then", boxchannel.maximumNumRequests, "requests, cleaning up"
        requests.sort(key=lambda x: os.path.getctime(x))
        while len(requests) > boxchannel.maximumNumRequests:
            target = requests.pop(0)
            print "Removing", target
            os.unlink(target)
            
    
    print "Looking for:", requestedFile  
    requestMade = False  
    for pathAndHashes in boxchannel.indexedFiles(indexFileDirectory):
        fullPath = pathAndHashes.pop(0)
        hashes = pathAndHashes
        if os.path.basename(fullPath) == requestedFile:
            #Found the file
            requestMade = True
            print "Requesting:", fullPath
            requestStageDirectory = os.path.join(stageDirectory, requestedFile)
            
            if not os.path.exists(requestStageDirectory):
                os.mkdir(requestStageDirectory)
            
            for (blockIndex, h) in enumerate(hashes):
                h = h.strip() #Remove newline if it is on the line
                stageName = os.path.join(requestStageDirectory, "%s.%s" %(h, blockIndex))
                requestName = os.path.join(requestDirectory, h)
                print "Creating file:", requestName
                file(requestName, 'w').close()
                file(stageName, 'w').close()
    if not requestMade:
        print "Could not find requested file"
        return 1
    return 0
Пример #2
0
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)