Пример #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 retrieve(self,
                 fileId,
                 fileVersion=-1,
                 pars=[],
                 hdrs={},
                 targetFile=None,
                 processing=None,
                 processingPars=None):
        """
        Request file `fileId` from the NG/AMS Server, store it locally
        in `targetFile`, and return the result of the operation as an
        ngamsStatus object. If `targetFile` is a directory, the name of the
        retrieved file is appended to the directory name.

        If `file_version` is given then that specific of the version will be
        retrieved.
        If `processing` and `processingPars` are given, they are passed down
        as the processing plug-in name and parameters to be applied to the
        retrieved data *on the server side*, respectively.
        """

        pars = list(pars)
        pars.append(("file_id", fileId))
        if fileVersion != -1:
            pars.append(("file_version", str(fileVersion)))
        if processing:
            pars.append(("processing", processing))
            if processingPars:
                pars.append(("processingPars", processingPars))

        targetFile = targetFile or '.'

        resp, host, port = self._get('RETRIEVE', pars, hdrs)
        host_id = "%s:%d" % (host, port)
        with contextlib.closing(resp):

            if resp.status != NGAMS_HTTP_SUCCESS:
                return ngamsStatus.to_status(resp, host_id, 'RETRIEVE')

            # If the target path is a directory, take the filename
            # of the incoming data as the filename
            fname = targetFile
            if os.path.isdir(fname):
                cdisp = resp.getheader('Content-Disposition')
                parts = ngamsLib.parseHttpHdr(cdisp)
                if 'filename' not in parts:
                    msg = "Missing or invalid Content-Disposition header in HTTP response"
                    raise Exception(msg)
                fname = os.path.join(fname,
                                     os.path.basename(parts['filename']))

            # Dump the data into the target file
            readf = functools.partial(resp.read, 65536)
            with open(fname, 'wb') as f:
                for buf in iter(readf, ''):
                    f.write(buf)

            return ngamsStatus.dummy_success_stat(host_id)
Пример #3
0
    def status(self, pars=[], output=None):
        """
        Request a general status from the NG/AMS Server
        associated to the object.

        Returns:     NG/AMS Status object (ngamsStatus).
        """
        if 'file_list' in [p[0] for p in pars]:
            resp, host, port = self._get(NGAMS_STATUS_CMD, pars=pars)
            if resp.status != NGAMS_HTTP_SUCCESS:
                return ngamsStatus.to_status(resp, '%s:%d' % (host, port),
                                             'STATUS')

            output = output or 'file_list.xml.gz'
            with open(output, 'wb') as fout, contextlib.closing(resp):
                shutil.copyfileobj(resp, fout)
            return ngamsStatus.dummy_success_stat("%s:%d" % (host, port))

        return self.get_status(NGAMS_STATUS_CMD, pars=pars)