예제 #1
0
    def cretrieve(self, containerName, containerId=None, targetDir='.'):
        """
        Sends a CRETRIEVE command to NG/AMS to retrieve the full contents of a
        container and dumps them into the file system.
        """
        if (not containerId and not containerName):
            msg = "Must specify parameter -containerId or -containerName for " +\
                  "a CRETRIEVE Command"
            raise Exception(msg)
        if not targetDir:
            targetDir = '.'

        pars = []
        if containerId:
            pars.append(("container_id", containerId))
        if containerName:
            pars.append(("container_name", containerName))

        resp, host, port = self._get('CRETRIEVE', pars=pars)
        host_id = "%s:%d" % (host, port)
        with contextlib.closing(resp):
            if resp.status != NGAMS_HTTP_SUCCESS:
                return ngamsStatus.ngamsStatus().unpackXmlDoc(resp.read(), 1)

            size = int(resp.getheader('Content-Length'))
            handler = ngamsMIMEMultipart.FilesystemWriterHandler(
                1024, basePath=targetDir)
            parser = ngamsMIMEMultipart.MIMEMultipartParser(
                handler, resp, size, 65536)
            parser.parse()
            return ngamsStatus.dummy_success_stat(host_id)
예제 #2
0
def saveFromHttpToFile(ngamsCfgObj,
                       reqPropsObj,
                       httpRef,
                       trgFilename,
                       blockSize,
                       mutexDiskAccess=1,
                       diskInfoObj=None):
    """
    Save the data available on an HTTP channel into the given file.

    ngamsCfgObj:     NG/AMS Configuration object (ngamsConfig).

    reqPropsObj:     NG/AMS Request Properties object (ngamsReqProps).

    trgFilename:     Target name for file where data will be
                     written (string).

    blockSize:       Block size (bytes) to apply when reading the data
                     from the HTTP channel (integer).

    mutexDiskAccess: Require mutual exclusion for disk access (integer).

    diskInfoObj:     Disk info object. Only needed if mutual exclusion
                     is required for disk access (ngamsDiskInfo).

    Returns:         Tuple. Element 0: Time in took to write
                     file (s) (tuple).
    """
    T = TRACE()

    checkCreatePath(os.path.dirname(trgFilename))

    start = time.time()
    try:
        # Make mutual exclusion on disk access (if requested).
        if (mutexDiskAccess):
            ngamsHighLevelLib.acquireDiskResource(ngamsCfgObj,
                                                  diskInfoObj.getSlotId())

        # Distinguish between Archive Pull and Push Request. By Archive
        # Pull we may simply read the file descriptor until it returns "".
        remSize = reqPropsObj.getSize()
        logger.debug("Archive Push/Pull Request - Data size: %d", remSize)

        handler = ngamsMIMEMultipart.FilesystemWriterHandler(
            blockSize, True, trgFilename)
        parser = ngamsMIMEMultipart.MIMEMultipartParser(
            handler, httpRef.rfile, remSize, blockSize)
        parser.parse()
        deltaTime = time.time() - start

        fileDataList = handler.getFileDataList()
        crcTime = handler.getCrcTime()
        writingTime = handler.getWritingTime()
        rootContainer = handler.getRoot()
        readingTime = parser.getReadingTime()
        bytesRead = parser.getBytesRead()
        ingestRate = (float(bytesRead) / deltaTime)
        reqPropsObj.setBytesReceived(bytesRead)

        logger.debug(
            "Transfer time: %.3f s; CRC time: %.3f s; write time %.3f s",
            readingTime, crcTime, writingTime)

        return [deltaTime, rootContainer, fileDataList, ingestRate]

    finally:
        # Release disk resouce.
        if (mutexDiskAccess):
            ngamsHighLevelLib.releaseDiskResource(ngamsCfgObj,
                                                  diskInfoObj.getSlotId())