Exemple #1
0
 def _copyFile(self, source, destination, register, isBundle):
     debug("copyFile: %s, %s" % (source, destination))
     if not os.path.isfile(source):
         error("file not found '%s'" % source)
         return 1
     debug("isBundle: %d" % isBundle)
     if not isBundle:
         if os.path.isdir(destination):
             destination = os.path.normpath(destination + "/" +
                                            os.path.basename(source))
         debug("destination: " + destination)
         if os.path.isfile(destination):
             log("warning: will overwrite %s" % destination)
     else:
         if not os.path.isdir(destination):
             error("destination is not a directory")
             return 1
     fs = FileSystem(self.config)
     fileinfo = fs.getFileInfo(source)
     debug("fileinfo: " + str(fileinfo))
     fileserver = fs.getFileServer(destination)
     debug("fileserver: " + fileserver)
     request = Message(Message.REGISTER_COPY, fileinfo + [fileserver])
     if not self.connection.sendMessage(request):
         error("cannot send message")
         return 1
     retval = 0
     while True:
         msg = self.connection.receiveMessage()
         if msg == None:
             error("cannot receive message")
             retval = 1
             break
         elif msg.type == Message.WAIT:
             time.sleep(int(msg.content[0]))
             if not self.connection.sendMessage(request):
                 error("cannot send message")
                 retval = 1
                 break
         elif msg.type == Message.FILE_OK:
             if not isBundle:
                 retval, reply = self._copySingleFile(
                     source, destination, register)
             else:
                 retval, reply = self._copyBundleFile(source, destination)
             if not self.connection.sendMessage(reply):
                 error("cannot send message")
                 retval = 1
             break
         else:
             error("unexpected message: %s" % str(msg))
             retval = 1
             break
     return retval
Exemple #2
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)
Exemple #3
0
    def _copyFile(self, source, destination, register, isBundle):
	debug("copyFile: %s, %s" % (source, destination))
	if not os.path.isfile(source):
	    error("file not found '%s'" % source)
	    return 1
	debug("isBundle: %d" % isBundle)
	if not isBundle:
	    if os.path.isdir(destination):
		destination = os.path.normpath(destination + "/" + os.path.basename(source))
	    debug("destination: " + destination)
	    if os.path.isfile(destination):
		log("warning: will overwrite %s" % destination)
	else:
	    if not os.path.isdir(destination):
		error("destination is not a directory")
		return 1
	fs = FileSystem(self.config)
	fileinfo = fs.getFileInfo(source)
	debug("fileinfo: " + str(fileinfo))
	fileserver = fs.getFileServer(destination)
	debug("fileserver: " + fileserver)
	request = Message(Message.REGISTER_COPY, fileinfo + [fileserver])
	if not self.connection.sendMessage(request):
	    error("cannot send message")
	    return 1
	retval = 0
	while True:
	    msg = self.connection.receiveMessage()
	    if msg == None:
		error("cannot receive message")
		retval = 1
		break
	    elif msg.type == Message.WAIT:
		time.sleep(int(msg.content[0]))
		if not self.connection.sendMessage(request):
		    error("cannot send message")
		    retval = 1
		    break
	    elif msg.type == Message.FILE_OK:
		if not isBundle:
		    retval, reply = self._copySingleFile(source, destination, register)
		else:
		    retval, reply = self._copyBundleFile(source, destination)
		if not self.connection.sendMessage(reply):
		    error("cannot send message")
		    retval = 1
		break
	    else:
		error("unexpected message: %s" % str(msg))
		retval = 1
		break
	return retval