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
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)