コード例 #1
0
ファイル: query_master.py プロジェクト: pietergvw/quilt
def get_client_proxy(clientRec):
    """
    return a pyro proxy object to the specified by the client record
    """
    pyroname = clientRec["clientName"]
    nshost = clientRec["registrarHost"]
    nsport = clientRec["registrarPort"]

    uri = quilt_core.get_uri(nshost, nsport, pyroname)

    return Pyro4.Proxy(uri)
コード例 #2
0
ファイル: quilt_smd.py プロジェクト: pietergvw/quilt
    def Query(self, queryId, sourceQuerySpec,
            queryClientName,
            queryClientNamseServerHost,
            queryClientNamseServerPort):
        """
        Query the source, currently hardcoded to behave as calling a cmdline
        tool that outputs source results on each output line
        """

        try:
            with self._lock:
                # set last query member to queryId
                self._lastQuery = queryId

            # _sourceName should not change, set at init, so ok to read
            # without lock
            logging.info("Source Manager: " + str(self._sourceName) +
                         " received query: " + str(queryId))

            # get the sourcePatternSpec from the pattern name in the
            #   sourceQuerySpec
            # pattern spec should be read only and safe to access without
            #   a lock
            srcPatSpecs = quilt_data.src_spec_get(self._sourceSpec,
                sourcePatterns=True)
            srcPatSpec = quilt_data.src_pat_specs_get(srcPatSpecs,
                quilt_data.src_query_spec_get(sourceQuerySpec,
                    srcPatternName=True))

            # get the variables in the query
            srcQueryVars = quilt_data.src_query_spec_get(sourceQuerySpec,
                variables=True)

            # iterate src query variables map, create a simple var name to
            #   var value map
            varNameValueDict = {}
            for srcVarName, srcVarSpec in srcQueryVars.items():
                varNameValueDict[srcVarName] = quilt_data.var_spec_get(
                    srcVarSpec, value=True)

            # create cmd line for the source
            # use the template in the sourcePatternSpec, and use the values
            #   provided for the variables, and environment variables
            replacements = {}
            # replacements = os.environ.copy()
            for k, v in varNameValueDict.items():
                replacements[k] = v

            # "template" was not added as official schema member because it is
            # specific to this command line case
            templateCmd = srcPatSpec['template']
            template = Template(templateCmd)
            logging.info("Ready to use : " + templateCmd +
                         " with replacements: " + str(replacements))
            cmdline = template.safe_substitute(replacements)

            # setup context for the cmdline stdout callback
            srcQueryId = quilt_data.src_query_spec_get(
                sourceQuerySpec, name=True)
            context = {'queryId': queryId, 'srcQueryId': srcQueryId}

            #           sei_core.run_process_lite(cmdline, shell=True,
            #               outFunc=self.OnGrepLine, outObj=context)

            #           I thought run_process apparently has problems
            sei_core.run_process(cmdline, shell=True,
                whichReturn=sei_core.EXITCODE,
                outFunc=self.OnGrepLine, outObj=context, logToPython=False)

            # Set query result events list in query master using query id
            results = []
            with self._lock:
                if queryId in self._sourceResults:
                    if srcQueryId in self._sourceResults[queryId]:
                        results = list(self._sourceResults[queryId][srcQueryId])

            uri = quilt_core.get_uri(queryClientNamseServerHost,
                queryClientNamseServerPort, queryClientName)

            with Pyro4.Proxy(uri) as query:
                query.AppendSourceQueryResults(srcQueryId, results)
                query.CompleteSrcQuery(srcQueryId)

                # catch exception!
        except Exception, error:
            try:

                # attempt to report error to the query client
                uri = quilt_core.get_uri(queryClientNamseServerHost,
                    queryClientNamseServerPort, queryClientName)
                srcQueryId = quilt_data.src_query_spec_get(
                    sourceQuerySpec, name=True)

                with Pyro4.Proxy(uri) as query:
                    query.OnSourceQueryError(srcQueryId, error)

            except Exception, error2:
                logging.error("Unable to send source query error to " +
                              "query master")
                logging.exception(error2)