Ejemplo n.º 1
0
    def render_template(self, template_path, **data):
        namespace = self.get_template_namespace()
        if self.session.messages:
            messages = self.session.messages
            self.session.messages = []
        else:
            messages = []
        server = '%s://%s/' % (self.request.protocol, self.request.host)
        namespace.update({'controller': get_controller(),
                          'version': __version__,
                          'b64encode': b64encode,
                          'dumps': json.dumps,
                          'session': self.session, 'messages': messages,
                          'SERVER': server})

        # Stats endpoints
        controller = get_controller()
        if self.session.endpoints and controller:
            endpoints = self.session.endpoints
            stats_endpoints = []
            for endpoint in endpoints:
                client = controller.get_client(endpoint)
                if client and client.stats_endpoint:
                    stats_endpoints.append(client.stats_endpoint)
            namespace.update({'stats_endpoints': stats_endpoints,
                              'endpoints': self.session.endpoints})

        namespace.update(data)

        try:
            template = app.loader.load(template_path)
            return template.generate(**namespace)
        except Exception:
            print exceptions.text_error_template().render()
Ejemplo n.º 2
0
 def renderTemplate(self, tmpl, outPath, **kwargs):
     try:
         content = tmpl.render(**kwargs)
         open(outPath, "w").write(content)
     except:
         print exceptions.text_error_template().render()
         sys.exit(1)
Ejemplo n.º 3
0
def serveTemplate(templateName, templateLoc, **kwargs):
    mylookup = TemplateLookup(directories=templateLoc)
    try:
        template = mylookup.get_template(templateName)
        return template.render(**kwargs)
    except:
        print exceptions.text_error_template().render()
        return None
Ejemplo n.º 4
0
def writeFile(name, data):
  try:
    template = Template(filename='templates/server/files/%s.txt' % name)
    output = file('output/server/%s' % name, 'w')
    output.write(template.render(**data))
    output.close()
  except:
    print exceptions.text_error_template().render()
Ejemplo n.º 5
0
def render_one(outfile, transform, args):
    try:
        result = transform.render(**args)
    except:
        print exceptions.text_error_template().render()
    log.debug("writing %s", outfile)
    with open(outfile, 'w') as fd:
        fd.write(result)
Ejemplo n.º 6
0
def writeModel(data):
  try:
    template = Template(filename='templates/CS/model.txt')
    output = file('output/CS/%s.CS' % data['model'].name, 'w')
    output.write(template.render(**data))
    output.close()
  except:
    print exceptions.text_error_template().render()
Ejemplo n.º 7
0
def generate(infile, outfile, context):
    print outfile
    mytemplate = Template(filename=infile, lookup = lookup)
    try:
        open(os.path.join(deploy_dir, outfile), 'w').write(mytemplate.render(**context.vars()))
    except:
        print 'error'
        print exceptions.text_error_template().render()
Ejemplo n.º 8
0
def writeModel(data):
    try:
        template = Template(filename="templates/java/model.txt")
        output = file("output/java/%s.java" % data["model"].name, "w")
        output.write(template.render(**data))
        output.close()
    except:
        print exceptions.text_error_template().render()
Ejemplo n.º 9
0
def _render(template, context, app):
    """Renders the template and fires the signal"""
    try:
        rv = template.render(**context)
        template_rendered.send(app, template=template, context=context)
        return rv
    except:
        print exceptions.text_error_template().render()
        raise
Ejemplo n.º 10
0
 def report(self):
     """ render a pass/fail report
     """
     r = None
     try:
         r = self.report_template.render(host="http://" + Test.make_hostname(), test_suites=self.test_suites, test_errors=self.has_errors())
     except:
         print exceptions.text_error_template().render()
     return r
Ejemplo n.º 11
0
def writeFile(name, data):
    try:

        template = Template(filename="templates/java/files/%s.txt" % name)
        output = file("output/java/%s" % name, "w")
        output.write(template.render(**data))
        output.close()
    except:
        print exceptions.text_error_template().render()
Ejemplo n.º 12
0
def save_graph_html(stat, path) :
    print 'making {0} chart'.format(stat.coder_handle_name)
    html = ''
    try:
        template = TEMPLATE_LOOKUP.get_template('graph.tmpl')
        html = template.render(plot_data = stat.rating_plot , stat = stat, current_rank = stat.temp_rank)
    except Exception , e:
        print exceptions.text_error_template().render()
        raise e
Ejemplo n.º 13
0
 def serve_template(self, template_name, **kwargs):
     from mako.template import Template
     from mako.lookup import TemplateLookup
     from mako import exceptions
     try:
         render_template = Template(filename=os.path.join(os.path.join(os.path.expanduser('~'), 'versions/canvas/release-tools/trunk/src/fsa/releasetools/classes'), 'templates', template_name))
         return render_template.render(**kwargs)
     except:
         print exceptions.text_error_template().render()
Ejemplo n.º 14
0
 def __init__(self, template_dir=None):
     self.tmpdir = None
     #if template_dir == None:
     template_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
     #    print template_dir
     try:    
         self.template_lookup = TemplateLookup(directories=[template_dir])
     except:
         print exceptions.text_error_template().render()
Ejemplo n.º 15
0
 def update(self, data, **kwargs):
     try:
         self.data['html'] = self.template.render(data=data, **kwargs).encode('utf-8')
     except Exception:
         log.err("Error rendering template")
         # twisted.log.msg munges the formatting; use print instead
         print text_error_template().render()
         raise
     #self.data['json'] = json.dumps(data, indent=4, sort_keys=True)
     self.data['json'] = json.dumps(data)
Ejemplo n.º 16
0
def serve_template(uri, **kwargs):
    _t = mylookup().get_template(str(uri))
    if 'self' in kwargs:
        kwargs.pop('self')
    try:
        return _t.render(c=c._real, **kwargs)
    except Exception, err:
        print "Mako error in %s: %s" % (uri, err)
        print exceptions.text_error_template().render()
        return exceptions.html_error_template().render(full=True)
Ejemplo n.º 17
0
def _run(argv):
    parser, args, other = argparser(argv)
    if not len(sys.argv) > 1:
        print parser.print_help()
        sys.exit(0)
    # defaults that can be overwritten by arguments
    tmpl_stored_args = {
        'http_server': 'waitress',
        'lang': 'en',
        'database_engine': 'sqlite',
        'host': '127.0.0.1',
        'port': 5000,
        'error_aggregation_service': None,
    }
    if other:
        # parse arguments, we assume only first is correct
        kwargs = {}
        for el in _escape_split(other[0], ','):
            kv = _escape_split(el, '=')
            if len(kv) == 2:
                k, v = kv
                kwargs[k] = v
        # update our template stored args
        tmpl_stored_args.update(kwargs)

    # use default that cannot be replaced
    tmpl_stored_args.update({
        'uuid': lambda: uuid.uuid4().hex,
        'here': os.path.abspath(os.curdir),
    })
    if args.show_defaults:
        for k,v in tmpl_stored_args.iteritems():
            print '%s=%s' % (k, v)
        sys.exit(0)
    try:
        # built in template
        tmpl_file = os.path.join(here, TMPL)
        if args.template:
            tmpl_file = args.template

        with open(tmpl_file, 'rb') as f:
            tmpl_data = f.read()
            if args.raw:
                tmpl = tmpl_data
            else:
                tmpl = Template(tmpl_data).render(**tmpl_stored_args)
        with open(args.filename, 'wb') as f:
            f.write(tmpl)
        print 'Wrote new config file in %s' % (os.path.abspath(args.filename))

    except Exception:
        from mako import exceptions
        print exceptions.text_error_template().render()
Ejemplo n.º 18
0
def save_standing_html(round_id , stat_list , title, path) :

    html = ''
    try:
        template = TEMPLATE_LOOKUP.get_template(TEMPLATE_FILE)
        html = template.render(rows = stat_list ,
                                 url = WebScraping.get_active_standings_url(round_id) ,
                                 title = title, 
                                 last_updated = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S"))
    except Exception , e:
        print exceptions.text_error_template().render()
        raise e
Ejemplo n.º 19
0
 def render_template(self, template, *args, **kwargs):
     try:
         return template.render(
             encoding = 'utf-8',
             helpers = helpers,
             options = self.options,
             *args,
             **kwargs
         )
     except:
         print exceptions.text_error_template().render()
         sys.exit(1)
Ejemplo n.º 20
0
def _run(args):
    if args.config_file is None:
        if not args.show_defaults:
            raise ValueError("Missing argument: config_file")
    else:
        if args.show_defaults:
            raise ValueError("Can't specify both config_file and --show_defaults")

    # defaults that can be overwritten by arguments
    tmpl_stored_args = {
        'http_server': 'waitress',
        'lang': 'en',
        'database_engine': 'sqlite',
        'host': '127.0.0.1',
        'port': 5000,
        'error_aggregation_service': None,
    }
    for custom in args.custom:
        # parse arguments
        kwargs = {}
        for el in _escape_split(custom, ','):
            kv = _escape_split(el, '=')
            if len(kv) == 2:
                k, v = kv
                kwargs[k] = v
        # update our template stored args
        tmpl_stored_args.update(kwargs)

    if args.show_defaults:
        for k,v in tmpl_stored_args.iteritems():
            print '%s=%s' % (k, v)
        sys.exit(0)

    # use default that cannot be replaced
    tmpl_stored_args.update({
        'uuid': lambda: uuid.uuid4().hex,
    })
    try:
        # built in template
        tmpl_file = os.path.join(here, TMPL)

        with open(tmpl_file, 'rb') as f:
            tmpl_data = f.read().decode('utf-8')
            tmpl = Template(tmpl_data).render(**tmpl_stored_args)
        with open(args.config_file, 'wb') as f:
            f.write(tmpl.encode('utf-8'))
        print 'Wrote new config file in %s' % (os.path.abspath(args.config_file))

    except Exception:
        from mako import exceptions
        print exceptions.text_error_template().render()
Ejemplo n.º 21
0
def render_to(template_name, data=None, **kwargs):
    from settings import TEMPLATE_DIRS
    from mako.lookup import TemplateLookup

    data = data or {}
    data.update(kwargs)
    try:
        lookup   = TemplateLookup(directories=TEMPLATE_DIRS)
        template = lookup.get_template(template_name)
        rendered = template.render_unicode(**data)
    except Exception as e:
        from mako import exceptions
        print exceptions.text_error_template().render()
    return rendered
Ejemplo n.º 22
0
def serve(environ, start_response):
    """serves requests using the WSGI callable interface."""
    fieldstorage = cgi.FieldStorage(
            fp = environ['wsgi.input'],
            environ = environ,
            keep_blank_values = True
    )
    d = dict([(k, getfield(fieldstorage[k])) for k in fieldstorage])

    uri = environ.get('PATH_INFO', '/')
    if not uri:
        uri = '/index.html'
    else:
        uri = re.sub(r'^/$', '/index.html', uri)

    if re.match(r'.*\.html$', uri):
        try:
            template = lookup.get_template(uri)
            start_response("200 OK", [('Content-type','text/html')])
            return [template.render(**d)]
        except exceptions.TopLevelLookupException:
            start_response("404 Not Found", [])
            return ["Cant find template '%s'" % uri]
        except:
            if error_style == 'text':
                start_response("200 OK", [('Content-type','text/plain')])
                return [exceptions.text_error_template().render()]
            else:
                start_response("200 OK", [('Content-type','text/html')])
                return [exceptions.html_error_template().render()]
    else:
        u = re.sub(r'^\/+', '', uri)
        filename = os.path.join(root, u)
        start_response("200 OK", [('Content-type',guess_type(uri))])
        return [file(filename).read()]
Ejemplo n.º 23
0
    def __call__(self, value, system):
        context = system.pop('context', None)
        if context is not None:
            system['_context'] = context
        def_name = None
        if isinstance(value, tuple):
            def_name, value = value
        try:
            system.update(value)
        except (TypeError, ValueError):
            raise ValueError('renderer was passed non-dictionary as value')
        template = self.implementation()
        if def_name is not None:
            template = template.get_def(def_name)
        try:
            result = template.render_unicode(**system)
        except:
            try:
                exc_info = sys.exc_info()
                errtext = exceptions.text_error_template().render(
                    error=exc_info[1],
                    traceback=exc_info[2]
                    )
                reraise(MakoRenderingException(errtext), None, exc_info[2])
            finally:
                del exc_info

        return result
Ejemplo n.º 24
0
 def __init__(self, template):
     super(TemplateError, self).__init__()
     self.einfo = sys.exc_info()
     self.text = text_error_template().render()
     msg = "Error occurred while rendering template '{0}'"
     msg = msg.format(template.uri)
     super(TemplateError, self).__init__(msg)
Ejemplo n.º 25
0
def main():
    """Main function."""
    parser = argparse.ArgumentParser()
    parser.add_argument('--csv', action='store', help='The CSV file to parse.')
    parser.add_argument(
        '--out',
        action='store',
        help='The location to put the generated C file.')
    args = parser.parse_args()

    # This generator opens and writes the file itself, and it does so in bytes
    # mode. This solves both python 2 vs 3 problems and solves the locale
    # problem: Unicode can be rendered even if the shell calling this script
    # doesn't.
    with open(args.out, 'wb') as f:
        formats = [Format(l) for l in reader(args.csv)]
        try:
            # This basically does lazy evaluation and initialization, which
            # saves on memory and startup overhead.
            f.write(TEMPLATE.render(
                formats             = formats,
                srgb_to_linear_map  = list(get_srgb_to_linear_map(formats)),
            ))
        except Exception:
            # In the even there's an error this imports some helpers from mako
            # to print a useful stack trace and prints it, then exits with
            # status 1, if python is run with debug; otherwise it just raises
            # the exception
            if __debug__:
                import sys
                from mako import exceptions
                print(exceptions.text_error_template().render(),
                      file=sys.stderr)
                sys.exit(1)
            raise
Ejemplo n.º 26
0
    def __call__(self, value, system):
        # Update the system dictionary with the values from the user
        try:
            system.update(value)
        except (TypeError, ValueError):
            raise ValueError('renderer was passed non-dictionary as value')

        # Check if 'context' in the dictionary
        context = system.pop('context', None)

        # Rename 'context' to '_context' because Mako internally already has a
        # variable named 'context'
        if context is not None:
            system['_context'] = context

        template = self.template
        if self.defname is not None:
            template = template.get_def(self.defname)
        try:
            result = template.render_unicode(**system)
        except:
            try:
                exc_info = sys.exc_info()
                errtext = text_error_template().render(
                    error=exc_info[1],
                    traceback=exc_info[2]
                    )
                reraise(MakoRenderingException(errtext), None, exc_info[2])
            finally:
                del exc_info

        return result
Ejemplo n.º 27
0
def render_mail(data):
    '''
    Will throw exception if data does not match expected structure (that is,
    if the template rendering fails).
    '''

    template_filename = 'psi_mail_stats.mako'
    template_lookup = TemplateLookup(directories=[os.path.dirname(os.path.abspath(__file__))])

    # SECURITY IMPORTANT: `'h'` in the `default_filters` list causes HTML
    # escaping to be applied to all expression tags (${...}) in this
    # template. Because we're output untrusted user-supplied data, this is
    # essential.
    template = Template(filename=template_filename,
                        default_filters=['unicode', 'h'],
                        lookup=template_lookup)

    try:
        rendered = template.render(data=data)
    except:
        raise Exception(exceptions.text_error_template().render())

    # CSS in email HTML must be inline
    rendered = pynliner.fromString(rendered)

    return rendered
Ejemplo n.º 28
0
    def display(self, tplname, **args):
        try:
            if not tplname.startswith(self.loc):
                tplname = os.path.join(self.loc, tplname)

            if self.cache is False or tplname not in self.cache:
                mylookup = TemplateLookup(directories=[self.loc], 
                                          filesystem_checks=True, 
                                          module_directory=self.tmpdir, 
                                          output_encoding=self.charset, 
                                          encoding_errors='replace', 
                                          default_filters=['unicode'])
                c = Template(filename=tplname, lookup=mylookup, 
                             output_encoding=self.charset, 
                             encoding_errors='ignore')

                if self.cache is not False:
                    self.cache[tplname] = c
                        
            s = c.render(**args)

            if self.cache:
                c = self.cache[tplname]

            return s
        except:
            log.error('\n=== template error ===' + exceptions.text_error_template().render() + '\n=== template error end ===')
            return 'template error!'
Ejemplo n.º 29
0
	def create_single_pdf(self, cr, uid, ids, data, report_xml, context=None):
		"""generate the PDF"""
		if context is None:
			context={}
		htmls = []
		self.parser_instance = self.parser(cr, uid, self.name2, context=context)

		self.pool = pooler.get_pool(cr.dbname)
		objs = self.getObjects(cr, uid, ids, context)
		self.parser_instance.set_context(objs, data, ids, report_xml.report_type)

		template =  False
		if report_xml.report_file :
			path = addons.get_module_resource(report_xml.report_file)
			if os.path.exists(path) :
				template = file(path).read()
		if not template and report_xml.m2pdf_data:
			template =  report_xml.m2pdf_data
		if not template :
			raise except_osv(_('Error!'), _('Report template not found!'))

		body_mako_tpl = mako_template(template)
		helper = WebXHtmlHelper(cr, uid, report_xml.id, context)

		css = ''
		try :
			html = body_mako_tpl.render(helper=helper, css=css, _=self.translate_call, **self.parser_instance.localcontext)
			htmls.append(html)
		except Exception, e:
			msg = exceptions.text_error_template().render()
			raise except_osv(_('XHtml2Pdf render'), msg)
Ejemplo n.º 30
0
    def render_global_template(self, models, templ_name):

        # remove '.mako', then use extension from the template name
        src_file_name = templ_name.replace('.mako', '')

        lang_conventions = self.template_manager.get_conventions(self.lang)

        if lang_conventions.use_prefix:
            src_file_name = self.prefix + src_file_name[0].upper() + src_file_name[1:]

        outfile_name = os.path.join(self.outdir, src_file_name)

        decl_template = self.makolookup.get_template(templ_name)

        with open(outfile_name, 'w') as f:
            try:
                self.verbose_output("Writing %s" % outfile_name)
                f.write(decl_template.render(models=models,
                                             timestamp=str(datetime.date.today()),
                                             year=int(datetime.date.today().year),
                                             namespace=self.namespace,
                                             file_name=src_file_name))
            except:
                print(exceptions.text_error_template().render())
                sys.exit(-1)
Ejemplo n.º 31
0
    async def render(self, path):
        try:
            # Mako is not asyncio friendly so run it within a thread
            def do():
                # Split the path into template name and directory
                name = os.path.basename(path)
                dir = os.path.dirname(path)

                # This will be where we search for templates
                lookup = TemplateLookup(directories=[dir],
                                        module_directory="/tmp/mako/%s" % dir)

                # Get the template by its relative path
                tmpl = lookup.get_template(name)

                # Render the template
                return tmpl.render(middleware=self.service.middleware)

            return await self.service.middleware.run_in_thread(do)
        except Exception:
            self.service.logger.debug(
                'Failed to render mako template: {0}'.format(
                    exceptions.text_error_template().render()))
            raise
Ejemplo n.º 32
0
    def render_to_xml(self, context_dict):
        """
        Render the template using the `context_dict` dict.
        Returns an `etree` XML element.
        """
        # add dummy STATIC_URL to template context
        context_dict.setdefault("STATIC_URL", "/dummy-static/")
        try:
            xml_str = capa_render_template(self.TEMPLATE_NAME, context_dict)
        except:
            raise TemplateError(exceptions.text_error_template().render())

        # Attempt to construct an XML tree from the template
        # This makes it easy to use XPath to make assertions, rather
        # than dealing with a string.
        # We modify the string slightly by wrapping it in <test>
        # tags, to ensure it has one root element.
        try:
            xml = etree.fromstring("<test>" + xml_str + "</test>")
        except Exception as exc:
            raise TemplateError("Could not parse XML from '{0}': {1}".format(
                xml_str, str(exc)))
        else:
            return xml
Ejemplo n.º 33
0
                    log.warning("Unable to render %s:"
                                " %s not set" % (node, error))
                    from mako import exceptions
                    log.debug(exceptions.text_error_template().render())
                except AttributeError, error:
                    log.warning("Unable to render %s: %s " % (node, error))
                    from mako import exceptions
                    log.warning(exceptions.text_error_template().render())
                except NameError, error:
                    log.warning("Unable to render %s: %s. "
                                "Check all variables used are defined" %
                                (node, error))
                except TypeError, error:
                    log.warning("Unable to render %s: %s." % (node, error))
                    from mako import exceptions
                    log.warning(exceptions.text_error_template().render())

        if to_memory:
            # Render directly to NIDB
            render_output = render_template.render(
                node=node,
                version_banner=version_banner,
                date=date,
            )

            return render_output


#TODO: Add support for both src template and src folder (eg for quagga, servers)
def render_node(node):
    if not node.do_render:
Ejemplo n.º 34
0
def generate_rstmgr(topcfg, out_path):
    log.info("Generating rstmgr")

    # Define target path
    rtl_path = out_path / 'ip/rstmgr/rtl/autogen'
    rtl_path.mkdir(parents=True, exist_ok=True)
    doc_path = out_path / 'ip/rstmgr/data/autogen'
    doc_path.mkdir(parents=True, exist_ok=True)
    tpl_path = Path(__file__).resolve().parent / '../hw/ip/rstmgr/data'

    # Read template files from ip directory.
    tpls = []
    outputs = []
    names = ['rstmgr.hjson', 'rstmgr.sv', 'rstmgr_pkg.sv']

    for x in names:
        tpls.append(tpl_path / Path(x + ".tpl"))
        if "hjson" in x:
            outputs.append(doc_path / Path(x))
        else:
            outputs.append(rtl_path / Path(x))

    # Parameters needed for generation
    reset_obj = topcfg['resets']

    # unique clocks
    clks = reset_obj.get_clocks()

    # resets sent to reset struct
    output_rsts = reset_obj.get_top_resets()

    # sw controlled resets
    sw_rsts = reset_obj.get_sw_resets()

    # leaf resets
    leaf_rsts = reset_obj.get_generated_resets()

    # Number of reset requests
    n_rstreqs = len(topcfg["reset_requests"])

    # Generate templated files
    for idx, t in enumerate(tpls):
        out = StringIO()
        with t.open(mode='r', encoding='UTF-8') as fin:
            tpl = Template(fin.read())
            try:
                out = tpl.render(clks=clks,
                                 power_domains=topcfg['power']['domains'],
                                 num_rstreqs=n_rstreqs,
                                 sw_rsts=sw_rsts,
                                 output_rsts=output_rsts,
                                 leaf_rsts=leaf_rsts,
                                 export_rsts=topcfg['exported_rsts'],
                                 reset_obj=topcfg['resets'])

            except:  # noqa: E722
                log.error(exceptions.text_error_template().render())

        if out == "":
            log.error("Cannot generate {}".format(names[idx]))
            return

        with outputs[idx].open(mode='w', encoding='UTF-8') as fout:
            fout.write(genhdr + out)

    # Generate reg files
    hjson_path = outputs[0]
    gen_rtl.gen_rtl(IpBlock.from_path(str(hjson_path), []), str(rtl_path))
Ejemplo n.º 35
0
def generate_clkmgr(top, cfg_path, out_path):

    # Target paths
    rtl_path = out_path / 'ip/clkmgr/rtl/autogen'
    rtl_path.mkdir(parents=True, exist_ok=True)
    data_path = out_path / 'ip/clkmgr/data/autogen'
    data_path.mkdir(parents=True, exist_ok=True)

    # Template paths
    hjson_tpl = cfg_path / '../ip/clkmgr/data/clkmgr.hjson.tpl'
    rtl_tpl = cfg_path / '../ip/clkmgr/data/clkmgr.sv.tpl'
    pkg_tpl = cfg_path / '../ip/clkmgr/data/clkmgr_pkg.sv.tpl'

    hjson_out = data_path / 'clkmgr.hjson'
    rtl_out = rtl_path / 'clkmgr.sv'
    pkg_out = rtl_path / 'clkmgr_pkg.sv'

    tpls = [hjson_tpl, rtl_tpl, pkg_tpl]
    outputs = [hjson_out, rtl_out, pkg_out]
    names = ['clkmgr.hjson', 'clkmgr.sv', 'clkmgr_pkg.sv']

    # A dictionary of the aon attribute for easier lookup. src_aon_attr[C] is
    # True if clock C is always-on and False otherwise.
    src_aon_attr = {
        src['name']: (src['aon'] == 'yes')
        for src in (top['clocks']['srcs'] + top['clocks']['derived_srcs'])
    }

    # Classify the various clock signals. Here, we build the following
    # dictionaries, each mapping the derived clock name to its source.
    #
    # ft_clks:  Clocks fed through clkmgr but are not disturbed in any way.
    #           This maintains the clocking structure consistency.
    #           This includes two groups of clocks:
    #             - Clocks fed from the always-on source
    #             - Clocks fed to the powerup group
    #
    # rg_clks: Non-feedthrough clocks that have no software control. These
    #          clocks are root-gated and the root-gated clock is then exposed
    #          directly in clocks_o.
    #
    # sw_clks: Non-feedthrough clocks that have direct software control. These
    #          are root-gated, but (unlike rg_clks) then go through a second
    #          clock gate which is controlled by software.
    #
    # hints: Non-feedthrough clocks that have "hint" software control (with a
    #        feedback mechanism to allow blocks to avoid being suspended when
    #        they are not idle).
    ft_clks = {}
    rg_clks = {}
    sw_clks = {}
    hints = {}

    # We also build rg_srcs_set, which is the set of non-always-on clock sources
    # that are exposed without division. This doesn't include clock sources
    # that are only used to derive divided clocks (we might gate the divided
    # clocks, but don't bother gating the upstream source).
    rg_srcs_set = set()

    for grp in top['clocks']['groups']:
        if grp['name'] == 'powerup':
            # All clocks in the "powerup" group are considered feed-throughs.
            ft_clks.update(grp['clocks'])
            continue

        for clk, src in grp['clocks'].items():
            if src_aon_attr[src]:
                # Any always-on clock is a feedthrough
                ft_clks[clk] = src
                continue

            rg_srcs_set.add(src)

            if grp['sw_cg'] == 'no':
                # A non-feedthrough clock with no software control
                rg_clks[clk] = src
                continue

            if grp['sw_cg'] == 'yes':
                # A non-feedthrough clock with direct software control
                sw_clks[clk] = src
                continue

            # The only other valid value for the sw_cg field is "hint", which
            # means a non-feedthrough clock with "hint" software control.
            assert grp['sw_cg'] == 'hint'
            hints[clk] = src
            continue

    # hint clocks dict.
    #
    # The clock is constructed as clk_{src_name}_{module_name}. So to get the
    # module name we split from the right and pick the last entry
    hint_clks = {
        clk: {
            'name': clk.rsplit('_', 1)[-1],
            'src': src
        }
        for clk, src in hints.items()
    }

    # The names of blocks that use one or more sw hint clocks (clkmgr has an
    # "idle" feedback signal from each), in ascending order.
    hint_blocks = sorted(set([v['name'] for v in hint_clks.values()]))

    # Define a canonical ordering for rg_srcs
    rg_srcs = sorted(rg_srcs_set)

    for idx, tpl in enumerate(tpls):
        out = ""
        with tpl.open(mode='r', encoding='UTF-8') as fin:
            tpl = Template(fin.read())
            try:
                out = tpl.render(cfg=top,
                                 div_srcs=top['clocks']['derived_srcs'],
                                 rg_srcs=rg_srcs,
                                 ft_clks=ft_clks,
                                 rg_clks=rg_clks,
                                 sw_clks=sw_clks,
                                 export_clks=top['exported_clks'],
                                 hint_clks=hint_clks,
                                 hint_blocks=hint_blocks)
            except:  # noqa: E722
                log.error(exceptions.text_error_template().render())

        if out == "":
            log.error("Cannot generate {}".format(names[idx]))
            return

        with outputs[idx].open(mode='w', encoding='UTF-8') as fout:
            fout.write(genhdr + out)

    # Generate reg files
    gen_rtl.gen_rtl(IpBlock.from_path(str(hjson_out), []), str(rtl_path))
Ejemplo n.º 36
0
    def create_single_pdf(self,
                          cursor,
                          uid,
                          ids,
                          data,
                          report_xml,
                          context=None):
        """generate the PDF"""

        if context is None:
            context = {}
        htmls = []
        if report_xml.report_type != 'webkit':
            return super(HeaderFooterTextWebKitParser,
                         self).create_single_pdf(cursor,
                                                 uid,
                                                 ids,
                                                 data,
                                                 report_xml,
                                                 context=context)

        parser_instance = self.parser(cursor, uid, self.name2, context=context)

        self.pool = pooler.get_pool(cursor.dbname)
        objs = self.getObjects(cursor, uid, ids, context)
        parser_instance.set_context(objs, data, ids, report_xml.report_type)

        template = False

        if report_xml.report_file:
            path = addons.get_module_resource(
                *report_xml.report_file.split(os.path.sep))
            if os.path.exists(path):
                template = file(path).read()
        if not template and report_xml.report_webkit_data:
            template = report_xml.report_webkit_data
        if not template:
            raise except_osv(_('Error!'),
                             _('Webkit Report template not found !'))
        header = report_xml.webkit_header.html

        if not header and report_xml.header:
            raise except_osv(_('No header defined for this Webkit report!'),
                             _('Please set a header in company settings.'))

        css = report_xml.webkit_header.css
        if not css:
            css = ''

        translate_call = partial(self.translate_call, parser_instance)
        #default_filters=['unicode', 'entity'] can be used to set global filter
        body_mako_tpl = mako_template(template)
        helper = WebKitHelper(cursor, uid, report_xml.id, context)
        if report_xml.precise_mode:
            for obj in objs:
                parser_instance.localcontext['objects'] = [obj]
                try:
                    html = body_mako_tpl.render(helper=helper,
                                                css=css,
                                                _=translate_call,
                                                **parser_instance.localcontext)
                    htmls.append(html)
                except Exception:
                    msg = exceptions.text_error_template().render()
                    _logger.error(msg)
                    raise except_osv(_('Webkit render'), msg)
        else:
            try:
                html = body_mako_tpl.render(helper=helper,
                                            css=css,
                                            _=translate_call,
                                            **parser_instance.localcontext)
                htmls.append(html)
            except Exception:
                msg = exceptions.text_error_template().render()
                _logger.error(msg)
                raise except_osv(_('Webkit render'), msg)

        # NO html footer and header because we write them as text with
        # wkhtmltopdf
        head = foot = False

        if report_xml.webkit_debug:
            try:
                deb = body_mako_tpl.render(helper=helper,
                                           css=css,
                                           _debug=tools.ustr("\n".join(htmls)),
                                           _=translate_call,
                                           **parser_instance.localcontext)
            except Exception:
                msg = exceptions.text_error_template().render()
                _logger.error(msg)
                raise except_osv(_('Webkit render'), msg)
            return (deb, 'html')
        bin = self.get_lib(cursor, uid)
        pdf = self.generate_pdf(bin,
                                report_xml,
                                head,
                                foot,
                                htmls,
                                parser_instance=parser_instance)
        return (pdf, 'pdf')
Ejemplo n.º 37
0
def generate_plic(top, out_path):
    topname = top["name"]
    # Count number of interrupts
    # Interrupt source 0 is tied to 0 to conform RISC-V PLIC spec.
    # So, total number of interrupts are the number of entries in the list + 1
    src = sum([x["width"] if "width" in x else 1
               for x in top["interrupt"]]) + 1

    # Target and priority: Currently fixed
    target = int(top["num_cores"], 0) if "num_cores" in top else 1
    prio = 3

    # Define target path
    #   rtl: rv_plic.sv & rv_plic_reg_pkg.sv & rv_plic_reg_top.sv
    #   data: rv_plic.hjson
    rtl_path = out_path / 'ip/rv_plic/rtl/autogen'
    rtl_path.mkdir(parents=True, exist_ok=True)
    doc_path = out_path / 'ip/rv_plic/data/autogen'
    doc_path.mkdir(parents=True, exist_ok=True)
    hjson_path = out_path / 'ip/rv_plic/data/autogen'
    hjson_path.mkdir(parents=True, exist_ok=True)

    # Generating IP top module script is not generalized yet.
    # So, topgen reads template files from rv_plic directory directly.
    # Next, if the ip top gen tool is placed in util/ we can import the library.
    tpl_path = out_path / '../ip/rv_plic/data'
    hjson_tpl_path = tpl_path / 'rv_plic.hjson.tpl'
    rtl_tpl_path = tpl_path / 'rv_plic.sv.tpl'

    # Generate Register Package and RTLs
    out = StringIO()
    with hjson_tpl_path.open(mode='r', encoding='UTF-8') as fin:
        hjson_tpl = Template(fin.read())
        try:
            out = hjson_tpl.render(src=src, target=target, prio=prio)
        except:  # noqa: E722
            log.error(exceptions.text_error_template().render())
        log.info("RV_PLIC hjson: %s" % out)

    if out == "":
        log.error("Cannot generate interrupt controller config file")
        return

    hjson_gen_path = hjson_path / "rv_plic.hjson"
    gencmd = (
        "// util/topgen.py -t hw/top_{topname}/data/top_{topname}.hjson --plic-only "
        "-o hw/top_{topname}/\n\n".format(topname=topname))
    with hjson_gen_path.open(mode='w', encoding='UTF-8') as fout:
        fout.write(genhdr + gencmd + out)

    # Generate register RTLs (currently using shell execute)
    # TODO: More secure way to generate RTL
    hjson_obj = hjson.loads(out,
                            use_decimal=True,
                            object_pairs_hook=OrderedDict)
    validate.validate(hjson_obj)
    gen_rtl.gen_rtl(hjson_obj, str(rtl_path))

    # Generate RV_PLIC Top Module
    with rtl_tpl_path.open(mode='r', encoding='UTF-8') as fin:
        rtl_tpl = Template(fin.read())
        try:
            out = rtl_tpl.render(src=src, target=target, prio=prio)
        except:  # noqa: E722
            log.error(exceptions.text_error_template().render())
        log.info("RV_PLIC RTL: %s" % out)

    if out == "":
        log.error("Cannot generate interrupt controller RTL")
        return

    rtl_gen_path = rtl_path / "rv_plic.sv"
    with rtl_gen_path.open(mode='w', encoding='UTF-8') as fout:
        fout.write(genhdr + gencmd + out)
Ejemplo n.º 38
0
        .round_mode = ${hasmod(x, 'round_mode')},
        .condition = ${hasmod(x, 'condition')},
        .result_type = ${hasmod(x, 'result_type')},
        .vecsize = ${hasmod(x, 'vector_size')},
        .register_format = ${hasmod(x, 'register_format')},
        .slot = ${hasmod(x, 'slot')},
        .sr_count = ${hasmod(x, 'staging_register_count')},
        .sr_write_count = ${hasmod(x, 'staging_register_write_count')},
        .sr_control = ${sr_control},
    },
% endif
% endfor
};
"""


# Exact value to be ORed in to every opcode
def exact_op(op):
    return (op.opcode << 48) | (op.opcode2 << op.secondary_shift)


try:
    print(
        Template(template).render(immediates=immediates,
                                  instructions=instructions,
                                  skip=SKIP,
                                  exact=exact_op,
                                  typesize=typesize))
except:
    print(exceptions.text_error_template().render())
Ejemplo n.º 39
0
def do_generate(
    task: GenTask,
    nodes: list,
    project: DFProject,
    tmpl_dirs: list,
    templates: dict,
    target: str,
    defines: dict,
    other_paths: list,
):
    """
    Drive Mako templating engine to generate a file described by a generation
    task. Errors are written back to the task so that it can run under a thread
    pool executor.

    Args:
        task       : The generation task to perform
        nodes      : List of the DesignFormat nodes to process
        project    : DesignFormat DFProject containing this node
        tmpl_dirs  : All of the directories to add to the template lookup
        templates  : Map of all templates that are registered
        target     : Path to the output directory
        defines    : Extra values defined on the command line
        other_paths: Additional paths to files required by templates
    """
    # Get the full path to the template, node source, and output file
    template = task.template.strip()
    node_path = nodes[0].getAttribute('source_path')

    # Check if template exists
    if not task.template_path:
        task.add_error(f"Could not find template {task.template}")
        return

    # Build the context for the template file
    source_time = time.ctime(os.path.getmtime(
        node_path)) if node_path and os.path.exists(node_path) else None
    blade_ctx = {
        # Expose the externally provided defines
        "ext_defines": defines,
        # Fields relating to the files being used to generate output
        "template_path": task.template_path,
        "template_time": time.ctime(os.path.getmtime(task.template_path)),
        "generator_path": sys.argv[0],
        "generator_time": time.ctime(os.path.getmtime(sys.argv[0])),
        "source_path": node_path,
        "source_time": source_time,
        "output_path": task.output_path,
        # Expose the DFProject
        "project": project,
        # Add 'None' for each principal node type by default
        "block": None,
        "interconnect": None,
        "reg_groups": [x for x in nodes if isinstance(x, DFRegisterGroup)],
        "commands": [x for x in nodes if isinstance(x, DFCommand)],
        "defines": [x for x in nodes if isinstance(x, DFDefine)],
        # Provide 'other' paths that templates may require
        "other": other_paths,
    }

    # Select a block to add to the context
    blocks = [x for x in nodes if isinstance(x, DFBlock)]
    if len(blocks) > 0:
        if len(blocks) > 1:
            log_warning(
                f"Multiple DFBlocks ({len(blocks)}) present, only using the first."
            )
        blade_ctx['block'] = blocks[0]

    # Select an interconnect to add to the context
    interconnects = [x for x in nodes if isinstance(x, DFInterconnect)]
    if len(interconnects) > 0:
        if len(interconnects) > 1:
            print(
                f"WARNING: Multiple DFInterconnect ({len(interconnects)}) present, "
                "only generating the first.")
        blade_ctx['interconnect'] = interconnects[0]

    # Merge in the command line defines (don't allow it to override scope vars)
    for key in defines:
        if not key in blade_ctx:
            blade_ctx[key] = defines[key]

    # Declare a function to check if a value has been defined
    def ifdef(key):
        return (key in defines)

    blade_ctx['ifdef'] = ifdef

    # Import all templates into the lookup
    # NOTE: We always drag in the BLADE Core and Templating so that we have
    #       access to common functions and templates (like the copyright)
    all_dirs = [
        *tmpl_dirs,
        os.path.abspath(os.path.dirname(__file__)),
        os.path.abspath(os.path.dirname(task.template_path)),
    ]

    # Add the template directories onto the path to support imports
    for t_dir in all_dirs:
        sys.path.append(t_dir)

    # Build the lookup
    lookup = TemplateLookup(
        directories=all_dirs,
        imports=[
            "from datetime import datetime",
            "import math",
            "import os",
            "import re",
            "from designformat import DFBlock, DFCommand, DFConstants, DFDefine,"
            "DFInterconnect, DFPort, DFConstantTie, DFPort, DFConnection",
        ])

    # Render the template
    rendered = None
    try:
        rendered = lookup.get_template(os.path.basename(
            task.template)).render(**blade_ctx)
    except Exception:
        task.add_error(
            "An exception was raised while rendering the template: " +
            exceptions.text_error_template().render())
        return

    # Ensure output directory tree exists
    log_debug(f"Ensuring directory {os.path.dirname(task.output_path)} exists")
    os.makedirs(os.path.dirname(task.output_path), exist_ok=True)

    # Write the rendered output to file
    log_debug(f"Writing out {task.output_path}")
    with open(task.output_path, "w") as fh:
        fh.write('\n'.join([x.rstrip() for x in rendered.split('\n')]))
Ejemplo n.º 40
0
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import sys

from mako import exceptions
from mako.template import Template

try:
    style_template = Template(filename=os.environ['STYLE_TEMPLATE'],
                              input_encoding='utf8')
    style_template.render(PRODUCT='gecko')

    geckolib_template = Template(filename=os.environ['GECKOLIB_TEMPLATE'],
                                 input_encoding='utf8')
    output = geckolib_template.render(
        STYLE_STRUCTS=style_template.module.STYLE_STRUCTS,
        to_rust_ident=style_template.module.to_rust_ident)
    print(output.encode('utf8'))
except:
    sys.stderr.write(exceptions.text_error_template().render().encode('utf8'))
    sys.exit(1)
Ejemplo n.º 41
0
def generate(filename, info):

    functions = []
    function_length = 0
    for cn, cat in info.items():
        for fn, func in cat.items():
            functions.append(func['snake_name'])
            if function_length < len(fn):
                function_length = len(fn)
    function_length += 2

    unimplemented = []
    with open(
            abspath(
                join(dirname(abspath(__file__)), '..', '..', '..', 'src',
                     'functions', 'implements', 'unimplemented.c'))) as f:
        for l in f.readlines():
            l = l.rstrip()
            m = re.search('exec_([0-9a-z_]+)', l)
            if m:
                unimplemented.append(m.groups(0)[0])

    implemented = {}
    implement_types = ['float', 'generic']
    impl_length = 14
    for root, dirs, files in walk(
            abspath(join(dirname(abspath(__file__)), '..', '..', '..',
                         'src'))):
        for fn in files:
            if splitext(fn)[1] == '.c':
                with open(join(root, fn), encoding='utf-8') as f:
                    code = f.read()
                    m = re.findall('allocate_([0-9a-z_]+)_local_context', code)
                    if m:
                        func_name = m[0]
                        if func_name not in implemented:
                            implemented[func_name] = []
                        func_f = re.findall('exec_{}'.format(func_name), code)
                        if func_f:
                            implemented[func_name].append('float')
                        func_g = re.findall('([0-9a-z_]+)_generic', code)
                        if func_g:
                            implemented[func_name].append('generic')

    count = 0
    count_impl = 0
    counts = {}
    counts_impl = {}
    for cn, cat in info.items():
        counts[cn] = 0
        counts_impl[cn] = 0
        for fn, func in cat.items():
            count += 1
            counts[cn] += 1
            if func['snake_name'] not in unimplemented:
                count_impl += 1
                counts_impl[cn] += 1

    table = ''
    table += '# Implement status\n'
    table += '\n'
    table += 'Total {}/{}\n'.format(count_impl, count)
    table += '\n'
    implement_types = sorted(implement_types)
    header = '|{{:^{}}}|{{:^{}}}|'.format(function_length, impl_length).format(
        'Function', 'Available')
    for impl in implement_types:
        header += '{{:^{}}}|'.format(impl_length).format(impl)
    header += '\n'
    header += '|{{:^{}}}|{{:^{}}}|'.format(function_length,
                                           impl_length).format(
                                               '-' * function_length,
                                               '-' * impl_length)
    for impl in implement_types:
        header += '{{:^{}}}|'.format(impl_length).format('-' * impl_length)
    header += '\n'

    for cn, cat in info.items():
        table += '\n'
        table += '## {}\n'.format(cn)
        table += 'Count {}/{}\n'.format(counts_impl[cn], counts[cn])
        table += '\n'
        table += header
        for fn, func in cat.items():
            name = fn
            if func['snake_name'] in unimplemented:
                line = '|{{:^{}}}|{{:^{}}}|'.format(function_length,
                                                    impl_length).format(
                                                        fn, 'no')
                for impl in implement_types:
                    line += '{{:^{}}}|'.format(impl_length).format('-')
            else:
                line = '|{{:^{}}}|{{:^{}}}|'.format(function_length,
                                                    impl_length).format(
                                                        fn, 'yes')
                for impl in implement_types:
                    if func['snake_name'] in implemented:
                        if impl in implemented[func['snake_name']]:
                            line += '{{:^{}}}|'.format(impl_length).format(
                                'yes')
                        else:
                            line += '{{:^{}}}|'.format(impl_length).format('-')
                    else:
                        line += '{{:^{}}}|'.format(impl_length).format('?')
            table += line + '\n'

    from mako.template import Template
    from mako import exceptions
    try:
        tmpl = Template(filename=filename)
        output = tmpl.render(table=table)
        return output
    except:
        print(exceptions.text_error_template().render())
    return None
Ejemplo n.º 42
0
    def create_single_pdf(self,
                          cursor,
                          uid,
                          ids,
                          data,
                          report_xml,
                          context=None):
        """generate the PDF"""

        if context is None:
            context = {}
        htmls = []
        if report_xml.report_type != 'webkit':
            return super(WebKitParser, self).create_single_pdf(cursor,
                                                               uid,
                                                               ids,
                                                               data,
                                                               report_xml,
                                                               context=context)

        self.parser_instance = self.parser(cursor,
                                           uid,
                                           self.name2,
                                           context=context)

        self.pool = pooler.get_pool(cursor.dbname)
        objs = self.getObjects(cursor, uid, ids, context)
        self.parser_instance.set_context(objs, data, ids,
                                         report_xml.report_type)

        template = False

        if report_xml.report_file:
            # backward-compatible if path in Windows format
            report_path = report_xml.report_file.replace("\\", "/")
            path = addons.get_module_resource(*report_path.split('/'))
            if path and os.path.exists(path):
                template = file(path).read()
        if not template and report_xml.report_webkit_data:
            template = report_xml.report_webkit_data
        if not template:
            raise except_osv(_('Error!'),
                             _('Webkit report template not found!'))
        header = report_xml.webkit_header.html
        footer = report_xml.webkit_header.footer_html
        if not header and report_xml.header:
            raise except_osv(_('No header defined for this Webkit report!'),
                             _('Please set a header in company settings.'))
        if not report_xml.header:
            header = ''
            default_head = addons.get_module_resource('report_webkit',
                                                      'default_header.html')
            with open(default_head, 'r') as f:
                header = f.read()
        css = report_xml.webkit_header.css
        if not css:
            css = ''

        #default_filters=['unicode', 'entity'] can be used to set global filter
        body_mako_tpl = mako_template(template)
        helper = WebKitHelper(cursor, uid, report_xml.id, context)
        if report_xml.precise_mode:
            for obj in objs:
                self.parser_instance.localcontext['objects'] = [obj]
                try:
                    html = body_mako_tpl.render(
                        helper=helper,
                        css=css,
                        _=self.translate_call,
                        **self.parser_instance.localcontext)
                    htmls.append(html)
                except Exception:
                    msg = exceptions.text_error_template().render()
                    _logger.error(msg)
                    raise except_osv(_('Webkit render!'), msg)
        else:
            try:
                html = body_mako_tpl.render(
                    helper=helper,
                    css=css,
                    _=self.translate_call,
                    **self.parser_instance.localcontext)
                htmls.append(html)
            except Exception:
                msg = exceptions.text_error_template().render()
                _logger.error(msg)
                raise except_osv(_('Webkit render!'), msg)
        head_mako_tpl = mako_template(header)
        try:
            head = head_mako_tpl.render(helper=helper,
                                        css=css,
                                        _=self.translate_call,
                                        _debug=False,
                                        **self.parser_instance.localcontext)
        except Exception:
            raise except_osv(_('Webkit render!'),
                             exceptions.text_error_template().render())
        foot = False
        if footer:
            foot_mako_tpl = mako_template(footer)
            try:
                foot = foot_mako_tpl.render(
                    helper=helper,
                    css=css,
                    _=self.translate_call,
                    **self.parser_instance.localcontext)
            except:
                msg = exceptions.text_error_template().render()
                _logger.error(msg)
                raise except_osv(_('Webkit render!'), msg)
        if report_xml.webkit_debug:
            try:
                deb = head_mako_tpl.render(helper=helper,
                                           css=css,
                                           _debug=tools.ustr("\n".join(htmls)),
                                           _=self.translate_call,
                                           **self.parser_instance.localcontext)
            except Exception:
                msg = exceptions.text_error_template().render()
                _logger.error(msg)
                raise except_osv(_('Webkit render!'), msg)
            return (deb, 'html')
        bin = self.get_lib(cursor, uid)
        pdf = self.generate_pdf(bin, report_xml, head, foot, htmls)
        return (pdf, 'pdf')
Ejemplo n.º 43
0
def generate_packet_queues_config(jsonDir, outDir):
    '''Generate the C files for the PUS packet queues'''

    # File paths
    scriptDir = os.path.dirname(os.path.realpath(__file__))
    schemaFile = os.path.join(scriptDir, 'schemas',
                              'packet_queues_schema.json')
    headerTemplate = os.path.join(scriptDir, 'templates',
                                  'pus_packet_queues_config.h.mako')
    srcTemplate = os.path.join(scriptDir, 'templates',
                               'pus_packet_queues_config.c.mako')
    missionFile = os.path.join(jsonDir, 'packet_queues_config.json')

    outHeaderFile = os.path.join(outDir, 'include',
                                 'pus_packet_queues_config.h')
    outSrcFile = os.path.join(outDir, 'src', 'pus_packet_queues_config.c')

    if not os.path.isfile(missionFile):
        raise Exception(
            'packet_queues] configuration file not found at {}'.format(
                missionFile))

    # Load and validate configuration
    print(' - generate configuration for packet_queues')
    schema = loadJson(schemaFile)
    configData = loadJson(missionFile)
    try:
        jsonschema.validate(configData, schema)
    except Exception as err:
        perror('Error in packet_queues configuration {}:\n{}'.format(
            missionFile, err))
        raise

    # Render templates
    try:
        template = Template(filename=headerTemplate)
        outHeader = template.render(config=configData, tempvars=dict())
    except:
        perror('Error generating {}:'.format(headerTemplate))
        perror(text_error_template().render())
        raise

    try:
        template = Template(filename=srcTemplate)
        outSrc = template.render(config=configData, tempvars=dict())
    except:
        perror('Error generating {}:'.format(outSrcFile))
        perror(text_error_template().render())
        raise

    # Write file
    try:
        if not os.path.exists(os.path.dirname(outHeaderFile)):
            os.makedirs(os.path.dirname(outHeaderFile))
        with open(outHeaderFile, 'w') as fd:
            fd.write(outHeader)
    except Exception as err:
        perror('Error writing packet_queues generated code to {}:\n{}'.format(
            outHeaderFile, err))
        raise

    try:
        if not os.path.exists(os.path.dirname(outSrcFile)):
            os.makedirs(os.path.dirname(outSrcFile))
        with open(outSrcFile, 'w') as fd:
            fd.write(outSrc)
    except Exception as err:
        perror('Error writing packet_queues generated code to {}:\n{}'.format(
            outSrcFile, err))
        raise
Ejemplo n.º 44
0
    def render(self, **kwargs):
        """Doc..."""

        # ADD KWARGS TO TEMPLATE RENDER PROPERTIES
        if kwargs:
            data = DictUtils.merge(self._data, kwargs)
        else:
            data = self._data

        td = [self._rootDir] if StringUtils.isStringType(self._rootDir) else self._rootDir

        lookup = TemplateLookup(
            directories=td,
            input_encoding='utf-8',
            output_encoding='utf-8',
            encoding_errors='replace')

        template = self._template
        if template:
            if not template.startswith('/'):
                template = '/' + template

            try:
                target = lookup.get_template(template)
            except Exception as err:
                self._result   = None
                self._error    = err
                self._errorMsg = 'Failed to get template (%s):\n%s' % (
                    template,
                    exceptions.text_error_template().render().replace('%','%%') )
                self._log.writeError(self._errorMsg, self._error)
                return self.dom
        else:
            target = Template(self._source if self._source else '', lookup=lookup)

        mr = MakoDataTransporter(data=data, logger=self._log)
        try:
            self._result = target.render_unicode(mr=mr).replace('\r', '')
        except Exception:
            d = []
            if data:
                for n,v in data.items():
                    d.append(StringUtils.toUnicode(n) + ': ' + StringUtils.toUnicode(v))

            try:
                stack = exceptions.text_error_template().render().replace('%','%%')
            except Exception as err2:
                stack = ''
                self._log.writeError('Unable to build mako exception stack', err2)

            traces = mr.getTraces()
            self._errorMsg = 'Failed to render (%s):\n%s\n%sDATA:\n\t%s' % (
                str(template),
                str(stack),
                ('TRACES:\n\t' + '\n\t'.join(traces) if traces else ''),
                '\n\t'.join(d) if d else '')

            self._log.write(self._errorMsg)

        if self._minify:
            return self.minifyResult()

        return self.dom
Ejemplo n.º 45
0
def gen_nav(url):
    global test_url
    test_url = url

    h = helper_struct(url_for=get_test_url)
    c = context_struct(subsubmenu={})
    buf = StringIO()
    ctx = Context(buf, h=h, c=c)

    try:
        t.render_context(ctx)
        full_html = buf.getvalue()
    except:
        print exceptions.text_error_template().render()
        return

    full_html = buf.getvalue()
    sub_html_pt = full_html.find("<!-- Secondary navigation")
    pri_html = full_html[0:sub_html_pt]
    sec_html = full_html[sub_html_pt:-1]

    pri_text = [
        y for y in (x.strip()
                    for x in re.sub(r'<.*?>', '', pri_html).splitlines()) if y
    ]
    sec_text = [
        y for y in (x.strip()
                    for x in re.sub(r'<.*?>', '', sec_html).splitlines()) if y
    ]

    # put a flag "LINK" in each line with a url, strip it out later
    mangled_pri_links = re.sub(r'.*?href\s?=\s?[\'"](.*?)[\'"].*', r'LINK \1',
                               pri_html)
    pri_links = [
        x[5:].strip() for x in mangled_pri_links.splitlines()
        if x[0:5] == "LINK "
    ]
    mangled_sec_links = re.sub(r'.*?href\s?=\s?[\'"](.*?)[\'"].*', r'LINK \1',
                               sec_html)
    sec_links = [
        x[5:].strip() for x in mangled_sec_links.splitlines()
        if x[0:5] == "LINK "
    ]

    # Find the selected link, if there is one
    pri_sel = [
        re.sub(r'.*?href\s?=\s?[\'"](.*?)[\'"].*', r'\1', x)
        for x in pri_html.splitlines() if "selected" in x
    ]
    sec_sel = [
        re.sub(r'.*?href\s?=\s?[\'"](.*?)[\'"].*', r'\1', x)
        for x in sec_html.splitlines() if "selected" in x
    ]
    sec_sel_text = [
        re.sub(r'<.*?>', '', x).strip() for x in sec_html.splitlines()
        if "selected" in x
    ]

    return {
        'full_html': full_html,
        'pri_text': pri_text,
        'pri_links': pri_links,
        'sec_text': sec_text,
        'sec_links': sec_links,
        'pri_sel': pri_sel[0] if pri_sel else None,
        'sec_sel': sec_sel[0] if sec_sel else None,
        'sec_sel_text': sec_sel_text[0] if sec_sel_text else None
    }
Ejemplo n.º 46
0
def generate_alert_handler(top, out_path):
    # default values
    esc_cnt_dw = 32
    accu_cnt_dw = 16
    async_on = "'0"
    # leave this constant
    n_classes = 4

    topname = top["name"]

    if esc_cnt_dw < 1:
        log.error("EscCntDw must be larger than 0")
    if accu_cnt_dw < 1:
        log.error("AccuCntDw must be larger than 0")

    # Count number of alerts
    n_alerts = sum([x["width"] if "width" in x else 1 for x in top["alert"]])

    if n_alerts < 1:
        # set number of alerts to 1 such that the config is still valid
        # that input will be tied off
        n_alerts = 1
        log.warning("no alerts are defined in the system")
    else:
        async_on = ""
        for alert in top['alert']:
            for k in range(alert['width']):
                async_on = str(alert['async']) + async_on
        # convert to hexstring to shorten line length
        async_on = ("%d'h" % n_alerts) + hex(int(async_on, 2))[2:]

    log.info("alert handler parameterization:")
    log.info("NAlerts   = %d" % n_alerts)
    log.info("EscCntDw  = %d" % esc_cnt_dw)
    log.info("AccuCntDw = %d" % accu_cnt_dw)
    log.info("AsyncOn   = %s" % async_on)

    # Define target path
    rtl_path = out_path / 'ip/alert_handler/rtl/autogen'
    rtl_path.mkdir(parents=True, exist_ok=True)
    doc_path = out_path / 'ip/alert_handler/data/autogen'
    doc_path.mkdir(parents=True, exist_ok=True)

    # Generating IP top module script is not generalized yet.
    # So, topgen reads template files from alert_handler directory directly.
    tpl_path = Path(__file__).resolve().parent / '../hw/ip/alert_handler/data'
    hjson_tpl_path = tpl_path / 'alert_handler.hjson.tpl'

    # Generate Register Package and RTLs
    out = StringIO()
    with hjson_tpl_path.open(mode='r', encoding='UTF-8') as fin:
        hjson_tpl = Template(fin.read())
        try:
            out = hjson_tpl.render(n_alerts=n_alerts,
                                   esc_cnt_dw=esc_cnt_dw,
                                   accu_cnt_dw=accu_cnt_dw,
                                   async_on=async_on,
                                   n_classes=n_classes)
        except:  # noqa: E722
            log.error(exceptions.text_error_template().render())
        log.info("alert_handler hjson: %s" % out)

    if out == "":
        log.error("Cannot generate alert_handler config file")
        return

    hjson_gen_path = doc_path / "alert_handler.hjson"
    gencmd = (
        "// util/topgen.py -t hw/top_{topname}/data/top_{topname}.hjson --alert-handler-only "
        "-o hw/top_{topname}/\n\n".format(topname=topname))
    with hjson_gen_path.open(mode='w', encoding='UTF-8') as fout:
        fout.write(genhdr + gencmd + out)

    # Generate register RTLs (currently using shell execute)
    # TODO: More secure way to gneerate RTL
    gen_rtl.gen_rtl(IpBlock.from_text(out, [], str(hjson_gen_path)),
                    str(rtl_path))
Ejemplo n.º 47
0
    def create_single_pdf(self, cursor, uid, ids, data, report_xml, context=None):
        """generate the PDF"""

        if context is None:
            context={}
        htmls = []
        if report_xml.report_type != 'webkit':
            return super(WebKitParser,self).create_single_pdf(cursor, uid, ids, data, report_xml, context=context)

        self.parser_instance = self.parser(cursor,
                                           uid,
                                           self.name2,
                                           context=context)

        self.pool = pooler.get_pool(cursor.dbname)
        objs = self.getObjects(cursor, uid, ids, context)
        self.parser_instance.set_context(objs, data, ids, report_xml.report_type)

        template =  False

        if report_xml.report_file :
            path = addons.get_module_resource(report_xml.report_file)
            if os.path.exists(path) :
                template = file(path).read()
        if not template and report_xml.report_webkit_data :
            template =  report_xml.report_webkit_data
        if not template :
            raise except_osv(_('Error!'), _('Webkit Report template not found !'))
        header = report_xml.webkit_header.html
        footer = report_xml.webkit_header.footer_html
        if not header and report_xml.header:
            raise except_osv(
                  _('No header defined for this Webkit report!'),
                  _('Please set a header in company settings')
              )
        if not report_xml.header :
            header = ''
            default_head = addons.get_module_resource('report_webkit', 'default_header.html')
            with open(default_head,'r') as f:
                header = f.read()
        css = report_xml.webkit_header.css
        if not css :
            css = ''
        user = self.pool.get('res.users').browse(cursor, uid, uid)
        company= user.company_id

        #default_filters=['unicode', 'entity'] can be used to set global filter
        body_mako_tpl = mako_template(template)
        helper = WebKitHelper(cursor, uid, report_xml.id, context)
        if report_xml.precise_mode:
            for obj in objs:
                self.parser_instance.localcontext['objects'] = [obj]
                try :
                    html = body_mako_tpl.render(helper=helper,
                                                css=css,
                                                _=self.translate_call,
                                                **self.parser_instance.localcontext)
                    htmls.append(html)
                except Exception, e:
                    msg = exceptions.text_error_template().render()
                    _logger.error(msg)
                    raise except_osv(_('Webkit render'), msg)
Ejemplo n.º 48
0
def generate_clkmgr(top, cfg_path, out_path):

    # Target paths
    rtl_path = out_path / 'ip/clkmgr/rtl/autogen'
    rtl_path.mkdir(parents=True, exist_ok=True)
    data_path = out_path / 'ip/clkmgr/data/autogen'
    data_path.mkdir(parents=True, exist_ok=True)

    # Template paths
    hjson_tpl = cfg_path / '../ip/clkmgr/data/clkmgr.hjson.tpl'
    rtl_tpl = cfg_path / '../ip/clkmgr/data/clkmgr.sv.tpl'
    pkg_tpl = cfg_path / '../ip/clkmgr/data/clkmgr_pkg.sv.tpl'

    hjson_out = data_path / 'clkmgr.hjson'
    rtl_out = rtl_path / 'clkmgr.sv'
    pkg_out = rtl_path / 'clkmgr_pkg.sv'

    tpls = [hjson_tpl, rtl_tpl, pkg_tpl]
    outputs = [hjson_out, rtl_out, pkg_out]
    names = ['clkmgr.hjson', 'clkmgr.sv', 'clkmgr_pkg.sv']

    # clock classification
    grps = top['clocks']['groups']

    src_aon_attr = OrderedDict()
    ft_clks = OrderedDict()
    rg_clks = OrderedDict()
    sw_clks = OrderedDict()
    hint_clks = OrderedDict()

    # construct a dictionary of the aon attribute for easier lookup
    # ie, src_name_A: True, src_name_B: False
    for src in top['clocks']['srcs'] + top['clocks']['derived_srcs']:
        if src['aon'] == 'yes':
            src_aon_attr[src['name']] = True
        else:
            src_aon_attr[src['name']] = False

    rg_srcs = [src for (src, attr) in src_aon_attr.items() if not attr]

    # clocks fed through clkmgr but are not disturbed in any way
    # This maintains the clocking structure consistency
    # This includes two groups of clocks
    # Clocks fed from the always-on source
    # Clocks fed to the powerup group
    ft_clks = OrderedDict([(clk, src) for grp in grps
                           for (clk, src) in grp['clocks'].items()
                           if src_aon_attr[src] or grp['name'] == 'powerup'])

    # root-gate clocks
    rg_clks = OrderedDict([(clk, src) for grp in grps
                           for (clk, src) in grp['clocks'].items()
                           if grp['name'] != 'powerup' and grp['sw_cg'] == 'no'
                           and not src_aon_attr[src]])

    # direct sw control clocks
    sw_clks = OrderedDict([(clk, src) for grp in grps
                           for (clk, src) in grp['clocks'].items()
                           if grp['sw_cg'] == 'yes' and not src_aon_attr[src]])

    # sw hint clocks
    hint_clks = OrderedDict([
        (clk, src) for grp in grps for (clk, src) in grp['clocks'].items()
        if grp['sw_cg'] == 'hint' and not src_aon_attr[src]
    ])

    for idx, tpl in enumerate(tpls):
        out = ""
        with tpl.open(mode='r', encoding='UTF-8') as fin:
            tpl = Template(fin.read())
            try:
                out = tpl.render(cfg=top,
                                 div_srcs=top['clocks']['derived_srcs'],
                                 rg_srcs=rg_srcs,
                                 ft_clks=ft_clks,
                                 rg_clks=rg_clks,
                                 sw_clks=sw_clks,
                                 hint_clks=hint_clks)
            except:  # noqa: E722
                log.error(exceptions.text_error_template().render())

        if out == "":
            log.error("Cannot generate {}".format(names[idx]))
            return

        with outputs[idx].open(mode='w', encoding='UTF-8') as fout:
            fout.write(genhdr + out)

    # Generate reg files
    with open(str(hjson_out), 'r') as out:
        hjson_obj = hjson.load(out,
                               use_decimal=True,
                               object_pairs_hook=OrderedDict)
    validate.validate(hjson_obj)
    gen_rtl.gen_rtl(hjson_obj, str(rtl_path))
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--out-c', help='Output C file.')
    parser.add_argument('--out-h', help='Output H file.')
    parser.add_argument('--xml',
                        help='Vulkan API XML file.',
                        required=True,
                        action='append',
                        dest='xml_files')
    args = parser.parse_args()

    entrypoints = get_entrypoints_from_xml(args.xml_files)

    device_entrypoints = []
    physical_device_entrypoints = []
    instance_entrypoints = []
    for e in entrypoints:
        if e.is_device_entrypoint():
            device_entrypoints.append(e)
        elif e.is_physical_device_entrypoint():
            physical_device_entrypoints.append(e)
        else:
            instance_entrypoints.append(e)

    for i, e in enumerate(e for e in device_entrypoints if not e.alias):
        e.disp_table_index = i

    device_strmap = StringIntMap()
    for i, e in enumerate(device_entrypoints):
        e.entry_table_index = i
        device_strmap.add_string("vk" + e.name, e.entry_table_index)
    device_strmap.bake()

    for i, e in enumerate(e for e in physical_device_entrypoints
                          if not e.alias):
        e.disp_table_index = i

    physical_device_strmap = StringIntMap()
    for i, e in enumerate(physical_device_entrypoints):
        e.entry_table_index = i
        physical_device_strmap.add_string("vk" + e.name, e.entry_table_index)
    physical_device_strmap.bake()

    for i, e in enumerate(e for e in instance_entrypoints if not e.alias):
        e.disp_table_index = i

    instance_strmap = StringIntMap()
    for i, e in enumerate(instance_entrypoints):
        e.entry_table_index = i
        instance_strmap.add_string("vk" + e.name, e.entry_table_index)
    instance_strmap.bake()

    # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
    # per entry point.
    try:
        if args.out_h:
            with open(args.out_h, 'w') as f:
                f.write(
                    TEMPLATE_H.render(
                        instance_entrypoints=instance_entrypoints,
                        physical_device_entrypoints=physical_device_entrypoints,
                        device_entrypoints=device_entrypoints,
                        filename=os.path.basename(__file__)))
        if args.out_c:
            with open(args.out_c, 'w') as f:
                f.write(
                    TEMPLATE_C.render(
                        instance_entrypoints=instance_entrypoints,
                        physical_device_entrypoints=physical_device_entrypoints,
                        device_entrypoints=device_entrypoints,
                        instance_strmap=instance_strmap,
                        physical_device_strmap=physical_device_strmap,
                        device_strmap=device_strmap,
                        filename=os.path.basename(__file__)))
    except Exception:
        # In the event there's an error, this imports some helpers from mako
        # to print a useful stack trace and prints it, then exits with
        # status 1, if python is run with debug; otherwise it just raises
        # the exception
        if __debug__:
            import sys
            from mako import exceptions
            sys.stderr.write(exceptions.text_error_template().render() + '\n')
            sys.exit(1)
        raise
Ejemplo n.º 50
0
def make_appliance(disk_img,
                   name,
                   template,
                   outfile,
                   networks=None,
                   ram=1024,
                   vcpus=1,
                   arch=None,
                   title="Linux",
                   project="Linux",
                   releasever="29"):
    """
    Generate an appliance description file

    :param str disk_img: Full path of the disk image
    :param str name: Name of the appliance, passed to the template
    :param str template: Full path of Mako template
    :param str outfile: Full path of file to write, using template
    :param list networks: List of networks(str) from the kickstart
    :param int ram: Ram, in MiB, passed to template. Default is 1024
    :param int vcpus: CPUs, passed to template. Default is 1
    :param str arch: CPU architecture. Default is 'x86_64'
    :param str title: Title, passed to template. Default is 'Linux'
    :param str project: Project, passed to template. Default is 'Linux'
    :param str releasever: Release version, passed to template. Default is 29
    """
    if not (disk_img and template and outfile):
        return None

    log.info("Creating appliance definition using %s", template)

    if not arch:
        arch = "x86_64"

    log.info("Calculating SHA256 checksum of %s", disk_img)
    sha256 = hashlib.sha256()
    with open(disk_img, "rb") as f:
        while True:
            data = f.read(1024**2)
            if not data:
                break
            sha256.update(data)
    log.info("SHA256 of %s is %s", disk_img, sha256.hexdigest())
    disk_info = DataHolder(name=os.path.basename(disk_img),
                           format="raw",
                           checksum_type="sha256",
                           checksum=sha256.hexdigest())
    try:
        result = Template(filename=template).render(disks=[disk_info],
                                                    name=name,
                                                    arch=arch,
                                                    memory=ram,
                                                    vcpus=vcpus,
                                                    networks=networks,
                                                    title=title,
                                                    project=project,
                                                    releasever=releasever)
    except Exception:
        log.error(text_error_template().render())
        raise

    with open(outfile, "w") as f:
        f.write(result)
Ejemplo n.º 51
0
def _exit():
    sys.stderr.write(exceptions.text_error_template().render())
    sys.exit(1)
Ejemplo n.º 52
0
    def send(self, event_entry, data, recipient=None, eventid=None):
        """Send an alert.
            Arguments:
                key:    The key to look up.
                data:   A Dictionary with the event information.
        """
        # pylint: disable=too-many-branches
        # pylint: disable=too-many-locals
        # pylint: disable=too-many-statements
        # pylint: disable=too-many-return-statements

        subject = event_entry.email_subject
        if subject == None:
            subject = event_entry.subject

        if subject.find("%") != -1:
            # Use the data dict for template substitution.
            try:
                subject = subject % data
            except (ValueError, KeyError) as ex:
                subject = "Email Template subject conversion failure: " + \
                    str(ex) + \
                    "subject: " + subject + \
                    ", data: " + str(data)

        message = event_entry.email_message
        if message:
            try:
                mako_template = Template(message)
                message = mako_template.render(**data)
            except StandardError:
                message = "Email mako template message conversion failure: " + \
                    exceptions.text_error_template().render() + \
                    "\ntemplate: " + message + \
                        "\ndata: " + str(data)
        else:
            message = self.make_default_message(event_entry, subject, data)

        if not message:
            # message is empty, set it to be the subject
            message = subject

        if recipient:
            # Force to only one test recipient.
            # It is sent even if alerts are disabled.
            to_emails = [recipient]
        else:
            if not self.system[SystemKeys.ALERTS_ENABLED]:
                logger.info("Alerts disabled.  Not sending: Subject: %s, %s",
                            subject, message)
                return

            to_emails = []

            if event_entry.publisher_visibility:
                to_emails = self.publisher_email(data)
            if event_entry.admin_visibility:
                to_emails += self.admin_emails(event_entry)

        # Remove any duplicates
        to_emails = list(set(to_emails))

        bcc = None
        if self.system[SystemKeys.ALERTS_ADMIN_ENABLED] and not recipient:
            # Get the diagnostics email and bcc it there if it exists.
            entry = UserProfile.get(self.envid, 0)
            if entry and entry.email != None and entry.email != "":
                bcc = [entry.email]

        if not to_emails and not bcc:
            logger.debug(
                "No admin users exist with enabled email addresses. "
                "Not sending: Subject: %s, Message: %s", subject, message)
            return

        # Send only PHONE-HOME related events if their palette license
        # has expired.
        if event_entry.key not in [
                EventControl.PHONE_HOME_FAILED, EventControl.PHONE_HOME_OK,
                EventControl.EMAIL_TEST
        ]:
            entry = Domain.getone()
            if entry.expiration_time and \
                            datetime.utcnow() > entry.expiration_time:
                logger.debug(
                    "License expired. " +
                    "Not sending: Subject: %s, Message: %s", subject, message)
                return

            if entry.contact_time:
                silence_time = (datetime.utcnow() - \
                                        entry.contact_time).total_seconds()
                max_silence_time = self.system[SystemKeys.MAX_SILENCE_TIME]
                if silence_time > max_silence_time and max_silence_time != -1:
                    logger.debug(
                        "Phonehome contact time is %d > %d. " +
                        "Not sending: Subject: %s, Message: %s", silence_time,
                        max_silence_time, subject, message)
                    return

        if self.email_limit_manager.email_limit_reached(event_entry, eventid):
            return

        sendit = True
        #        print '\n------------event key is', event_entry.key
        if self.system[SystemKeys.EMAIL_MUTE_RECONNECT_SECONDS]:
            # Potentially mute the connect or reconnect emails.
            if event_entry.key == EventControl.AGENT_DISCONNECT:
                self._mute_dis_check(data, to_emails, bcc, subject, message)
                # If the event is emailed, it is done there,
                # after a delay
                return

            elif event_entry.key in [
                    EventControl.AGENT_COMMUNICATION,
                    EventControl.INIT_STATE_STARTED,
                    EventControl.INIT_STATE_STOPPED,
                    EventControl.INIT_STATE_DEGRADED
            ]:
                sendit = self._mute_reconn_check(data)

        if sendit:
            self._do_send(to_emails, bcc, subject, message)
def generate(filename, info):
    defines = []
    for cn, cat in info.items():
        defines.append(
            '////////////////////////////////////////////////////////////////////////////////'
        )
        defines.append('/// @defgroup {} {}'.format(
            cn.split(',').pop().replace(' ', ''),
            cn.split(',').pop()))
        defines.append('/// @{')
        for fn, func in cat.items():
            defines.append('')
            defines.append('/// @defgroup {0} {0}'.format(fn))
            defines.append('/// @{')
            defines.append('')
            name = func['snake_name']
            if 'arguments' in func and len(func['arguments']) > 0:

                for an, arg in func['arguments'].items():
                    if 'available_values' in arg:
                        defines.append('/// Named values for {}.{}'.format(
                            fn, an))
                        defines.append('typedef enum {')
                        for t in arg['available_values']:
                            defines.append('    {}_{}_{},'.format(
                                name.upper(), an.upper(), t.upper()))
                        defines.append('    END_OF_{}_{}'.format(
                            name.upper(), an.upper()))
                        defines.append('}} {}_{}_value_t;'.format(name, an))
                        defines.append('')
                defines.append('/// Local context for {}'.format(fn))
                defines.append('typedef struct {')

                for an, arg in func['arguments'].items():

                    if arg['type'] == 'bool':
                        defines.append('  uint8_t {}; ///< {}'.format(
                            an, arg['type']))

                    elif arg['type'] == 'double' or arg['type'] == 'float':
                        defines.append('  float {}; ///< {}'.format(
                            an, arg['type']))

                    elif arg['type'] == 'int64':
                        defines.append('  int32_t {}; ///< {}'.format(
                            an, arg['type']))

                    elif arg['type'] == 'repeated int64' or arg[
                            'type'] == 'Shape':
                        defines.append(
                            '  rt_list_t {}; ///< Original type is [{}]'.
                            format(an, arg['type']))

                    elif arg['type'] == 'string':
                        defines.append('{0}_{1}_value_t {1}; ///< {2}'.format(
                            name, an, arg['type']))

                defines.append('  void* data; ///< General purpose data area')
                defines.append('}} {}_local_context_t;'.format(name))
            defines.append('')
            defines.append('/// Allocate {} local context'.format(fn))
            defines.append(
                'rt_function_error_t allocate_{}_local_context(rt_function_t* f);'
                .format(name))
            defines.append('')
            defines.append('/// Free {} local context '.format(fn))
            defines.append(
                'rt_function_error_t free_{}_local_context(rt_function_t* f);'.
                format(name))
            defines.append('')
            defines.append('/// Exec {}'.format(fn))
            defines.append(
                'rt_function_error_t exec_{}(rt_function_t* f);'.format(name))
            defines.append('/// @}')
            defines.append('')
        defines.append('/// @}')
        defines.append('')

    from mako.template import Template
    from mako import exceptions
    try:
        tmpl = Template(filename=filename)
        output = tmpl.render(FUNCTION_DEFINES='\n'.join(defines))
        return output
    except:
        print(exceptions.text_error_template().render())
    return None
Ejemplo n.º 54
0
def gen_fpv(block: IpBlock, outdir):
    # Read Register templates
    fpv_csr_tpl = Template(
        filename=resource_filename('reggen', 'fpv_csr.sv.tpl'))

    # Generate a module with CSR assertions for each device interface. For a
    # device interface with no name, we generate <block>_csr_assert_fpv. For a
    # named interface, we generate <block>_<ifname>_csr_assert_fpv.
    lblock = block.name.lower()
    generated = []
    for if_name, rb in block.reg_blocks.items():
        if not rb.flat_regs:
            # No registers to check!
            continue

        if if_name is None:
            mod_base = lblock
        else:
            mod_base = lblock + '_' + if_name.lower()

        mod_name = mod_base + '_csr_assert_fpv'
        filename = mod_name + '.sv'
        generated.append(filename)
        reg_top_path = os.path.join(outdir, filename)
        with open(reg_top_path, 'w', encoding='UTF-8') as fout:
            try:
                fout.write(
                    fpv_csr_tpl.render(block=block,
                                       mod_base=mod_base,
                                       if_name=if_name,
                                       rb=rb))
            except:  # noqa F722 for template Exception handling
                log.error(exceptions.text_error_template().render())
                return 1

    # Generate a fusesoc core file that points at the files we've just
    # generated.
    core_data = {
        'name': "lowrisc:fpv:{}_csr_assert".format(lblock),
        'filesets': {
            'files_dv': {
                'depend': [
                    "lowrisc:ip:{}".format(lblock),
                    "lowrisc:tlul:headers",
                    "lowrisc:prim:assert",
                ],
                'files':
                generated,
                'file_type':
                'systemVerilogSource'
            },
        },
        'targets': {
            'default': {
                'filesets': [
                    'files_dv',
                ],
            },
        },
    }
    core_file_path = os.path.join(outdir, lblock + '_csr_assert_fpv.core')
    with open(core_file_path, 'w') as core_file:
        core_file.write('CAPI=2:\n')
        yaml.dump(core_data, core_file, encoding='utf-8')

    return 0
Ejemplo n.º 55
0
def generate_alert_handler(top, out_path):
    # default values
    esc_cnt_dw = 32
    accu_cnt_dw = 16
    lfsr_seed = 2**31 - 1
    async_on = "'0"
    # leave this constant
    n_classes = 4

    topname = top["name"]

    # check if there are any params to be passed through reggen and placed into
    # the generated package
    ip_list_in_top = [x["name"].lower() for x in top["module"]]
    ah_idx = ip_list_in_top.index("alert_handler")
    if 'localparam' in top['module'][ah_idx]:
        if 'EscCntDw' in top['module'][ah_idx]['localparam']:
            esc_cnt_dw = int(top['module'][ah_idx]['localparam']['EscCntDw'])
        if 'AccuCntDw' in top['module'][ah_idx]['localparam']:
            accu_cnt_dw = int(top['module'][ah_idx]['localparam']['AccuCntDw'])
        if 'LfsrSeed' in top['module'][ah_idx]['localparam']:
            lfsr_seed = int(top['module'][ah_idx]['localparam']['LfsrSeed'], 0)

    if esc_cnt_dw < 1:
        log.error("EscCntDw must be larger than 0")
    if accu_cnt_dw < 1:
        log.error("AccuCntDw must be larger than 0")
    if (lfsr_seed & 0xFFFFFFFF) == 0 or lfsr_seed > 2**32:
        log.error("LFSR seed out of range or zero")

    # Count number of interrupts
    n_alerts = sum([x["width"] if "width" in x else 1 for x in top["alert"]])

    if n_alerts < 1:
        # set number of alerts to 1 such that the config is still valid
        # that input will be tied off
        n_alerts = 1
        log.warning("no alerts are defined in the system")
    else:
        async_on = ""
        for alert in top['alert']:
            for k in range(alert['width']):
                async_on = str(alert['async']) + async_on
        async_on = ("%d'b" % n_alerts) + async_on

    log.info("alert handler parameterization:")
    log.info("NAlerts   = %d" % n_alerts)
    log.info("EscCntDw  = %d" % esc_cnt_dw)
    log.info("AccuCntDw = %d" % accu_cnt_dw)
    log.info("LfsrSeed  = %d" % lfsr_seed)
    log.info("AsyncOn   = %s" % async_on)

    # Define target path
    rtl_path = out_path / 'ip/alert_handler/rtl/autogen'
    rtl_path.mkdir(parents=True, exist_ok=True)
    doc_path = out_path / 'ip/alert_handler/data/autogen'
    doc_path.mkdir(parents=True, exist_ok=True)
    dv_path = out_path / 'ip/alert_handler/dv'
    dv_path.mkdir(parents=True, exist_ok=True)

    # Generating IP top module script is not generalized yet.
    # So, topgen reads template files from alert_handler directory directly.
    tpl_path = out_path / '../ip/alert_handler/data'
    hjson_tpl_path = tpl_path / 'alert_handler.hjson.tpl'
    dv_tpl_path = tpl_path / 'alert_handler_env_pkg__params.sv.tpl'

    # Generate Register Package and RTLs
    out = StringIO()
    with hjson_tpl_path.open(mode='r', encoding='UTF-8') as fin:
        hjson_tpl = Template(fin.read())
        try:
            out = hjson_tpl.render(n_alerts=n_alerts,
                                   esc_cnt_dw=esc_cnt_dw,
                                   accu_cnt_dw=accu_cnt_dw,
                                   lfsr_seed=lfsr_seed,
                                   async_on=async_on,
                                   n_classes=n_classes)
        except:  # noqa: E722
            log.error(exceptions.text_error_template().render())
        log.info("alert_handler hjson: %s" % out)

    if out == "":
        log.error("Cannot generate alert_handler config file")
        return

    hjson_gen_path = doc_path / "alert_handler.hjson"
    gencmd = (
        "// util/topgen.py -t hw/top_{topname}/data/top_{topname}.hjson --alert-handler-only "
        "-o hw/top_{topname}/\n\n".format(topname=topname))
    with hjson_gen_path.open(mode='w', encoding='UTF-8') as fout:
        fout.write(genhdr + gencmd + out)

    # Generate register RTLs (currently using shell execute)
    # TODO: More secure way to gneerate RTL
    hjson_obj = hjson.loads(out,
                            use_decimal=True,
                            object_pairs_hook=validate.checking_dict)
    validate.validate(hjson_obj)
    gen_rtl.gen_rtl(hjson_obj, str(rtl_path))

    # generate testbench for alert_handler
    with dv_tpl_path.open(mode='r', encoding='UTF-8') as fin:
        dv_tpl = Template(fin.read())
        try:
            out = dv_tpl.render(n_alerts=n_alerts, async_on=async_on)
        except:  # noqa : E722
            log.error(exceptions.text_error_template().render())
        log.info("ALERT_HANDLER DV: %s" % out)
        if out == "":
            log.error("Cannot generate dv alert_handler parameter file")
            return

        dv_gen_path = dv_path / 'alert_handler_env_pkg__params.sv'
        with dv_gen_path.open(mode='w', encoding='UTF-8') as fout:
            fout.write(genhdr + gencmd + out)
                                                _=self.translate_call,
                                                **self.parser_instance.localcontext)
                    htmls.append(html)
                except Exception, e:
                    msg = exceptions.text_error_template().render()
                    _logger.error(msg)
                    raise except_osv(_('Webkit render'), msg)
        else:
            try:
                html = body_mako_tpl.render(helper=helper,
                                            css=css,
                                            _=self.translate_call,
                                            **self.parser_instance.localcontext)
                htmls.append(html)
            except Exception, e:
                msg = exceptions.text_error_template().render()
                _logger.error(msg)
                raise except_osv(_('Webkit render'), msg)

        # NO html footer and header because we write them as text with wkhtmltopdf
        head = foot = False

        if report_xml.webkit_debug:
            try:
                deb = body_mako_tpl.render(helper=helper,
                                           css=css,
                                           _debug=tools.ustr("\n".join(htmls)),
                                           _=self.translate_call,
                                           **self.parser_instance.localcontext)
            except Exception, e:
                msg = exceptions.text_error_template().render()
Ejemplo n.º 57
0
def generate_pinmux_and_padctrl(top, out_path):
    topname = top["name"]
    # MIO Pads
    n_mio_pads = top["pinmux"]["num_mio"]
    if n_mio_pads <= 0:
        # TODO: add support for no MIO case
        log.error("Topgen does currently not support generation of a top " +
                  "without a pinmux.")
        return

    if "padctrl" not in top:
        # TODO: add support for no MIO case
        log.error("Topgen does currently not support generation of a top " +
                  "without a padctrl instance.")
        return

    # Get number of wakeup detectors
    if "num_wkup_detect" in top["pinmux"]:
        num_wkup_detect = top["pinmux"]["num_wkup_detect"]
    else:
        num_wkup_detect = 1

    if num_wkup_detect <= 0:
        # TODO: add support for no wakeup counter case
        log.error("Topgen does currently not support generation of a top " +
                  "without DIOs.")
        return

    if "wkup_cnt_width" in top["pinmux"]:
        wkup_cnt_width = top["pinmux"]["wkup_cnt_width"]
    else:
        wkup_cnt_width = 8

    if wkup_cnt_width <= 1:
        log.error("Wakeup counter width must be greater equal 2.")
        return

    # Total inputs/outputs
    # Validation ensures that the width field is present.
    num_mio_inputs = sum([x["width"] for x in top["pinmux"]["inputs"]])
    num_mio_outputs = sum([x["width"] for x in top["pinmux"]["outputs"]])
    num_mio_inouts = sum([x["width"] for x in top["pinmux"]["inouts"]])

    num_dio_inputs = sum([
        x["width"] if x["type"] == "input" else 0 for x in top["pinmux"]["dio"]
    ])
    num_dio_outputs = sum([
        x["width"] if x["type"] == "output" else 0
        for x in top["pinmux"]["dio"]
    ])
    num_dio_inouts = sum([
        x["width"] if x["type"] == "inout" else 0 for x in top["pinmux"]["dio"]
    ])

    n_mio_periph_in = num_mio_inouts + num_mio_inputs
    n_mio_periph_out = num_mio_inouts + num_mio_outputs
    n_dio_periph_in = num_dio_inouts + num_dio_inputs
    n_dio_periph_out = num_dio_inouts + num_dio_outputs
    n_dio_pads = num_dio_inouts + num_dio_inputs + num_dio_outputs

    if n_dio_pads <= 0:
        # TODO: add support for no DIO case
        log.error("Topgen does currently not support generation of a top " +
                  "without DIOs.")
        return

    log.info("Generating pinmux with following info from hjson:")
    log.info("num_mio_inputs:  %d" % num_mio_inputs)
    log.info("num_mio_outputs: %d" % num_mio_outputs)
    log.info("num_mio_inouts:  %d" % num_mio_inouts)
    log.info("num_dio_inputs:  %d" % num_dio_inputs)
    log.info("num_dio_outputs: %d" % num_dio_outputs)
    log.info("num_dio_inouts:  %d" % num_dio_inouts)
    log.info("num_wkup_detect: %d" % num_wkup_detect)
    log.info("wkup_cnt_width:  %d" % wkup_cnt_width)
    log.info("This translates to:")
    log.info("n_mio_periph_in:  %d" % n_mio_periph_in)
    log.info("n_mio_periph_out: %d" % n_mio_periph_out)
    log.info("n_dio_periph_in:  %d" % n_dio_periph_in)
    log.info("n_dio_periph_out: %d" % n_dio_periph_out)
    log.info("n_dio_pads:       %d" % n_dio_pads)

    # Target path
    #   rtl: pinmux_reg_pkg.sv & pinmux_reg_top.sv
    #   data: pinmux.hjson
    rtl_path = out_path / 'ip/pinmux/rtl/autogen'
    rtl_path.mkdir(parents=True, exist_ok=True)
    data_path = out_path / 'ip/pinmux/data/autogen'
    data_path.mkdir(parents=True, exist_ok=True)

    # Template path
    tpl_path = out_path / '../ip/pinmux/data/pinmux.hjson.tpl'

    # Generate register package and RTLs
    gencmd = ("// util/topgen.py -t hw/top_{topname}/data/top_{topname}.hjson "
              "-o hw/top_{topname}/\n\n".format(topname=topname))

    hjson_gen_path = data_path / "pinmux.hjson"

    out = StringIO()
    with tpl_path.open(mode='r', encoding='UTF-8') as fin:
        hjson_tpl = Template(fin.read())
        try:
            # TODO: pass in information about always-on peripherals
            # TODO: pass in information on which DIOs can be selected
            # as wakeup signals
            # TODO: pass in signal names such that we can introduce
            # named enums for select signals
            out = hjson_tpl.render(
                n_mio_periph_in=n_mio_periph_in,
                n_mio_periph_out=n_mio_periph_out,
                n_mio_pads=n_mio_pads,
                # each DIO has in, out and oe wires
                # some of these have to be tied off in the
                # top, depending on the type.
                n_dio_periph_in=n_dio_pads,
                n_dio_periph_out=n_dio_pads,
                n_dio_pads=n_dio_pads,
                n_wkup_detect=num_wkup_detect,
                wkup_cnt_width=wkup_cnt_width)
        except:  # noqa: E722
            log.error(exceptions.text_error_template().render())
        log.info("PINMUX HJSON: %s" % out)

    if out == "":
        log.error("Cannot generate pinmux HJSON")
        return

    with hjson_gen_path.open(mode='w', encoding='UTF-8') as fout:
        fout.write(genhdr + gencmd + out)

    hjson_obj = hjson.loads(out,
                            use_decimal=True,
                            object_pairs_hook=validate.checking_dict)
    validate.validate(hjson_obj)
    gen_rtl.gen_rtl(hjson_obj, str(rtl_path))

    # Target path
    #   rtl: padctrl_reg_pkg.sv & padctrl_reg_top.sv
    #   data: padctrl.hjson
    rtl_path = out_path / 'ip/padctrl/rtl/autogen'
    rtl_path.mkdir(parents=True, exist_ok=True)
    data_path = out_path / 'ip/padctrl/data/autogen'
    data_path.mkdir(parents=True, exist_ok=True)

    # Template path
    tpl_path = out_path / '../ip/padctrl/data/padctrl.hjson.tpl'

    # Generate register package and RTLs
    gencmd = ("// util/topgen.py -t hw/top_{topname}/data/top_{topname}.hjson "
              "-o hw/top_{topname}/\n\n".format(topname=topname))

    hjson_gen_path = data_path / "padctrl.hjson"

    out = StringIO()
    with tpl_path.open(mode='r', encoding='UTF-8') as fin:
        hjson_tpl = Template(fin.read())
        try:
            out = hjson_tpl.render(n_mio_pads=n_mio_pads,
                                   n_dio_pads=n_dio_pads,
                                   attr_dw=10)
        except:  # noqa: E722
            log.error(exceptions.text_error_template().render())
        log.info("PADCTRL HJSON: %s" % out)

    if out == "":
        log.error("Cannot generate padctrl HJSON")
        return

    with hjson_gen_path.open(mode='w', encoding='UTF-8') as fout:
        fout.write(genhdr + gencmd + out)

    hjson_obj = hjson.loads(out,
                            use_decimal=True,
                            object_pairs_hook=validate.checking_dict)
    validate.validate(hjson_obj)
    gen_rtl.gen_rtl(hjson_obj, str(rtl_path))
Ejemplo n.º 58
0
def generate_pinmux(top, out_path):

    topname = top['name']
    pinmux = top['pinmux']

    # Generation without pinmux and pinout configuration is not supported.
    assert 'pinmux' in top
    assert 'pinout' in top

    # Get number of wakeup detectors
    if 'num_wkup_detect' in pinmux:
        num_wkup_detect = pinmux['num_wkup_detect']
    else:
        num_wkup_detect = 1

    if num_wkup_detect <= 0:
        # TODO: add support for no wakeup counter case
        log.error('Topgen does currently not support generation of a top ' +
                  'without DIOs.')
        return

    if 'wkup_cnt_width' in pinmux:
        wkup_cnt_width = pinmux['wkup_cnt_width']
    else:
        wkup_cnt_width = 8

    if wkup_cnt_width <= 1:
        log.error('Wakeup counter width must be greater equal 2.')
        return

    # MIO Pads
    n_mio_pads = pinmux['io_counts']['muxed']['pads']

    # Total inputs/outputs
    # Reuse the counts from the merge phase
    n_mio_periph_in = (pinmux['io_counts']['muxed']['inouts'] +
                       pinmux['io_counts']['muxed']['inputs'])
    n_mio_periph_out = (pinmux['io_counts']['muxed']['inouts'] +
                        pinmux['io_counts']['muxed']['outputs'])
    n_dio_periph_in = (pinmux['io_counts']['dedicated']['inouts'] +
                       pinmux['io_counts']['dedicated']['inputs'])
    n_dio_periph_out = (pinmux['io_counts']['dedicated']['inouts'] +
                        pinmux['io_counts']['dedicated']['outputs'])
    n_dio_pads = (pinmux['io_counts']['dedicated']['inouts'] +
                  pinmux['io_counts']['dedicated']['inputs'] +
                  pinmux['io_counts']['dedicated']['outputs'])

    # TODO: derive this value
    attr_dw = 13

    # Generation with zero MIO/DIO pads is currently not supported.
    assert (n_mio_pads > 0)
    assert (n_dio_pads > 0)

    log.info('Generating pinmux with following info from hjson:')
    log.info('attr_dw:         %d' % attr_dw)
    log.info('num_wkup_detect: %d' % num_wkup_detect)
    log.info('wkup_cnt_width:  %d' % wkup_cnt_width)
    log.info('n_mio_periph_in:  %d' % n_mio_periph_in)
    log.info('n_mio_periph_out: %d' % n_mio_periph_out)
    log.info('n_dio_periph_in:  %d' % n_dio_periph_in)
    log.info('n_dio_periph_out: %d' % n_dio_periph_out)
    log.info('n_dio_pads:       %d' % n_dio_pads)

    # Target path
    #   rtl: pinmux_reg_pkg.sv & pinmux_reg_top.sv
    #   data: pinmux.hjson
    rtl_path = out_path / 'ip/pinmux/rtl/autogen'
    rtl_path.mkdir(parents=True, exist_ok=True)
    data_path = out_path / 'ip/pinmux/data/autogen'
    data_path.mkdir(parents=True, exist_ok=True)

    # Template path
    tpl_path = Path(
        __file__).resolve().parent / '../hw/ip/pinmux/data/pinmux.hjson.tpl'

    # Generate register package and RTLs
    gencmd = ("// util/topgen.py -t hw/top_{topname}/data/top_{topname}.hjson "
              "-o hw/top_{topname}/\n\n".format(topname=topname))

    hjson_gen_path = data_path / "pinmux.hjson"

    out = StringIO()
    with tpl_path.open(mode='r', encoding='UTF-8') as fin:
        hjson_tpl = Template(fin.read())
        try:
            out = hjson_tpl.render(
                n_mio_periph_in=n_mio_periph_in,
                n_mio_periph_out=n_mio_periph_out,
                n_mio_pads=n_mio_pads,
                # each DIO has in, out and oe wires
                # some of these have to be tied off in the
                # top, depending on the type.
                n_dio_periph_in=n_dio_pads,
                n_dio_periph_out=n_dio_pads,
                n_dio_pads=n_dio_pads,
                attr_dw=attr_dw,
                n_wkup_detect=num_wkup_detect,
                wkup_cnt_width=wkup_cnt_width)
        except:  # noqa: E722
            log.error(exceptions.text_error_template().render())
        log.info("PINMUX HJSON: %s" % out)

    if out == "":
        log.error("Cannot generate pinmux HJSON")
        return

    with hjson_gen_path.open(mode='w', encoding='UTF-8') as fout:
        fout.write(genhdr + gencmd + out)

    gen_rtl.gen_rtl(IpBlock.from_text(out, [], str(hjson_gen_path)),
                    str(rtl_path))
Ejemplo n.º 59
0
def generate_rstmgr(topcfg, out_path):
    log.info("Generating rstmgr")

    # Define target path
    rtl_path = out_path / 'ip/rstmgr/rtl/autogen'
    rtl_path.mkdir(parents=True, exist_ok=True)
    doc_path = out_path / 'ip/rstmgr/data/autogen'
    doc_path.mkdir(parents=True, exist_ok=True)
    tpl_path = out_path / '../ip/rstmgr/data'

    # Read template files from ip directory.
    tpls = []
    outputs = []
    names = ['rstmgr.hjson', 'rstmgr.sv', 'rstmgr_pkg.sv']

    for x in names:
        tpls.append(tpl_path / Path(x + ".tpl"))
        if "hjson" in x:
            outputs.append(doc_path / Path(x))
        else:
            outputs.append(rtl_path / Path(x))

    # Parameters needed for generation
    clks = []
    output_rsts = OrderedDict()
    sw_rsts = OrderedDict()
    leaf_rsts = OrderedDict()

    # unique clocks
    for rst in topcfg["resets"]["nodes"]:
        if rst['type'] != "ext" and rst['clk'] not in clks:
            clks.append(rst['clk'])

    # resets sent to reset struct
    output_rsts = [
        rst for rst in topcfg["resets"]["nodes"] if rst['type'] == "top"
    ]

    # sw controlled resets
    sw_rsts = [
        rst for rst in topcfg["resets"]["nodes"]
        if 'sw' in rst and rst['sw'] == 1
    ]

    # leaf resets
    leaf_rsts = [rst for rst in topcfg["resets"]["nodes"] if rst['gen']]

    log.info("output resets {}".format(output_rsts))
    log.info("software resets {}".format(sw_rsts))
    log.info("leaf resets {}".format(leaf_rsts))

    # Generate templated files
    for idx, t in enumerate(tpls):
        out = StringIO()
        with t.open(mode='r', encoding='UTF-8') as fin:
            tpl = Template(fin.read())
            try:
                out = tpl.render(clks=clks,
                                 sw_rsts=sw_rsts,
                                 output_rsts=output_rsts,
                                 leaf_rsts=leaf_rsts)

            except:  # noqa: E722
                log.error(exceptions.text_error_template().render())

        if out == "":
            log.error("Cannot generate {}".format(names[idx]))
            return

        with outputs[idx].open(mode='w', encoding='UTF-8') as fout:
            fout.write(genhdr + out)

    # Generate reg files
    hjson_path = outputs[0]
    with open(str(hjson_path), 'r') as out:
        hjson_obj = hjson.load(out,
                               use_decimal=True,
                               object_pairs_hook=OrderedDict)
    validate.validate(hjson_obj)
    gen_rtl.gen_rtl(hjson_obj, str(rtl_path))
Ejemplo n.º 60
0
    def get_results(my):
        code = my.kwargs.get("code")
        script_path = my.kwargs.get("script_path")
        file_path = my.kwargs.get("file_path")

        # if a script path is specified, then get it from the custom_script
        # table
        if script_path:

            folder = os.path.dirname(script_path)
            title = os.path.basename(script_path)

            search = Search("config/custom_script")
            search.add_filter("folder", folder)
            search.add_filter("title", title)
            custom_script = search.get_sobject()
            if not custom_script:
                raise TacticException(
                    "Custom script with path [%s/%s] does not exist" %
                    (folder, title))
            code = custom_script.get_value("script")

        elif file_path:
            f = open(file_path)
            code = f.read()
            f.close()

        server = TacticServerStub.get(protocol='local')
        server.login = Environment.get_user_name()
        spt_mako_results = {}

        #kwargs = {
        #    'server': server,
        #    'spt_mako_results': spt_mako_results
        #}

        code = '''
<%%def name='spt_run_code()'>
<%%
%s
%%>
</%%def>

<%%
spt_mako_results['spt_ret_val'] = spt_run_code()
%%>
''' % code

        #template = Template(code, output_encoding='utf-8', input_encoding='utf-8')
        try:
            template = Template(code)
            template.render(server=server,
                            spt_mako_results=spt_mako_results,
                            kwargs=my.kwargs,
                            **my.kwargs)
        except Exception, e:
            print "Error in Mako code: "
            print exceptions.text_error_template().render()
            print "---"
            print "Code:"
            print code
            print "---"
            raise
            raise CommandExitException(e)