Beispiel #1
0
    def _fetchFile(self, filename, fileSystem=None, fetcher=None, closeOnError=True):
	if not fileSystem:
	    fileSystem = FileSystem(self.config)
	if not fetcher:
	    fetcher = CacheFetcher(self.config, fileSystem, self.connection)

	if not os.path.isfile(filename):
	    error("file not found '%s'" % filename)
	    if closeOnError and not self.single: fetcher.sendExit()
	    return (filename, 1)

	fileinfo = fileSystem.getFileInfo(filename)
	destination = self.getDestination(fileSystem, filename)
	if destination is None:
	    return (filename, 1)
	log("destination: " + str(destination))

	if os.path.isfile(destination):
	    wait = fetcher.isActive(destination)
	    while wait > 0:
		log("file transfer in progress. wait %ds" % wait)
		time.sleep(wait)
		wait = fetcher.isActive(destination)

	fileExists, canCopy, removed = fileSystem.destinationExists(fileinfo, destination)
	if fileExists:
	    if removed:
		log("removed " + destination)
		fetcher.sendFileRemoved(fileinfo, destination)
	    if not canCopy:
		error("cannot copy file to %s" % destination)
		return (filename, 1)
	    else:
		log("using existing file")
		fileSystem.setATime(destination)
		fetcher.sendFileLocation(fileinfo, destination)
		return (destination, 0)

	freeSpace, removed = fileSystem.checkFreeSpace(int(fileinfo[1]), destination)
	if not freeSpace:
	    log("not enough free space in %s" % fileSystem.cacheDir)
	    if closeOnError and self.single: fetcher.sendExit()
	    return (filename, 1)

	log("request: " + filename)
	fetcher.requestFile(fileinfo, destination)

	msg = self.connection.receiveMessage()
	resultFile, ok, term = fetcher.handleMessage(fileinfo, destination, msg)
	while not ok:
	    msg = self.connection.receiveMessage()
	    resultFile, ok, term = fetcher.handleMessage(fileinfo, destination, msg)
	return (resultFile, 0)