def render_cheetah(self, raw_data, search_table, subject=None):
        """
        Render data_input back into a file.
        data_input is either a string or a filename
        search_table is a hash of metadata keys and values
        (though results are always returned)
        subject is a profile or system object, if available (for snippet eval)
        """

        self.check_for_invalid_imports(raw_data)

        # backward support for Cobbler's legacy (and slightly more readable) 
        # template syntax.
        raw_data = raw_data.replace("TEMPLATE::","$")

        # HACK:  the ksmeta field may contain nfs://server:/mount in which
        # case this is likely WRONG for kickstart, which needs the NFS
        # directive instead.  Do this to make the templates work.
        newdata = ""
        if search_table.has_key("tree") and search_table["tree"].startswith("nfs://"): 
            for line in raw_data.split("\n"):
               if line.find("--url") != -1 and line.find("url ") != -1:
                   rest = search_table["tree"][6:] # strip off "nfs://" part
                   try:
                       (server, dir) = rest.split(":",2)
                   except:
                       raise CX("Invalid syntax for NFS path given during import: %s" % search_table["tree"])
                   line = "nfs --server %s --dir %s" % (server,dir)
                   # but put the URL part back in so koan can still see
                   # what the original value was
                   line = line + "\n" + "#url --url=%s" % search_table["tree"]
               newdata = newdata + line + "\n"
            raw_data = newdata 

        # tell Cheetah not to blow up if it can't find a symbol for something
        raw_data = "#errorCatcher ListErrors\n" + raw_data

        table_copy = search_table.copy()
 
        # for various reasons we may want to call a module inside a template and pass
        # it all of the template variables.  The variable "template_universe" serves
        # this purpose to make it easier to iterate through all of the variables without
        # using internal Cheetah variables

        search_table.update({
           "template_universe" : table_copy
        })

        # now do full templating scan, where we will also templatify the snippet insertions
        t = Template(source=raw_data, searchList=[search_table], compilerSettings={'useStackFrame':False})

        if fix_cheetah_class and functools is not None:
            t.SNIPPET = functools.partial(t.SNIPPET, t)
            t.read_snippet = functools.partial(t.read_snippet, t)

        try:
            data_out = t.respond()
            self.last_errors = t.errorCatcher().listErrors()
            if self.last_errors:
                self.logger.warning("errors were encountered rendering the template")
                self.logger.warning("\n" + pprint.pformat(self.last_errors))
        except Exception, e:
            self.logger.error(utils.cheetah_exc(e))
            raise CX("Error templating file, check cobbler.log for more details")
Exemple #2
0
    def render(self, data_input, search_table, out_path, subject=None):
        """
        Render data_input back into a file.
        data_input is either a string or a filename
        search_table is a hash of metadata keys and values
        out_path if not-none writes the results to a file
        (though results are always returned)
        subject is a profile or system object, if available (for snippet eval)
        """

        if type(data_input) != str:
           raw_data = data_input.read()
        else:
           raw_data = data_input

        self.check_for_invalid_imports(raw_data)

        # backward support for Cobbler's legacy (and slightly more readable) 
        # template syntax.
        raw_data = raw_data.replace("TEMPLATE::","$")

        # HACK:  the ksmeta field may contain nfs://server:/mount in which
        # case this is likely WRONG for kickstart, which needs the NFS
        # directive instead.  Do this to make the templates work.
        newdata = ""
        if search_table.has_key("tree") and search_table["tree"].startswith("nfs://"): 
            for line in raw_data.split("\n"):
               if line.find("--url") != -1 and line.find("url ") != -1:
                   rest = search_table["tree"][6:] # strip off "nfs://" part
                   try:
                       (server, dir) = rest.split(":",2)
                   except:
                       raise CX(_("Invalid syntax for NFS path given during import: %s" % search_table["tree"]))
                   line = "nfs --server %s --dir %s" % (server,dir)
                   # but put the URL part back in so koan can still see
                   # what the original value was
                   line = line + "\n" + "#url --url=%s" % search_table["tree"]
               newdata = newdata + line + "\n"
            raw_data = newdata 

        # tell Cheetah not to blow up if it can't find a symbol for something
        raw_data = "#errorCatcher Echo\n" + raw_data

        table_copy = search_table.copy()
 
        # for various reasons we may want to call a module inside a template and pass
        # it all of the template variables.  The variable "template_universe" serves
        # this purpose to make it easier to iterate through all of the variables without
        # using internal Cheetah variables

        search_table.update({
           "template_universe" : table_copy
        })

        # now do full templating scan, where we will also templatify the snippet insertions
        t = Template(source=raw_data, errorCatcher="Echo", searchList=[search_table])
        try:
            data_out = str(t)
        except Exception, e:
            if out_path is None:
               return utils.cheetah_exc(e)
            else:
               # FIXME: log this
               print utils.cheetah_exc(e)
               raise CX("Error templating file: %s" % out_path)