Example #1
0
def simulate(source, filename="<string>"):
    global options, snk
    getopts(["--simul", filename])
    node = parse(source, filename=filename)
    snk = snakes.plugins.load(options.plugins, "snakes.nets", "snk")
    build = Builder(snk)
    net = build.build(node)
    net.label(srcfile=filename, snakes=snk)
    return ABCDSimulator(node, net, draw(net, None))
Example #2
0
def simulate (source, filename="<string>") :
    global options, snk
    getopts(["--simul", filename])
    node = parse(source, filename=filename)
    snk = snakes.plugins.load(options.plugins, "snakes.nets", "snk")
    build = Builder(snk)
    net = build.build(node)
    net.label(srcfile=filename, snakes=snk)
    return ABCDSimulator(node, net, draw(net, None))
Example #3
0
def main(args=sys.argv[1:], src=None):
    global options, snk
    # get options
    try:
        if src is None:
            getopts(args)
        else:
            getopts(list(args) + ["<string>"])
    except SystemExit:
        raise
    except:
        die(ERR_OPT, str(sys.exc_info()[1]))
    # read source
    if src is not None:
        source = src
    else:
        try:
            source = open(abcd).read()
        except:
            die(ERR_IO, "could not read input file %r" % abcd)
    # parse
    try:
        node = parse(source, filename=abcd)
    except ParseError:
        die(ERR_PARSE, str(sys.exc_info()[1]))
    except:
        bug()
    # compile
    dirname = os.path.dirname(abcd)
    if dirname and dirname not in sys.path:
        sys.path.append(dirname)
    elif "." not in sys.path:
        sys.path.append(".")
    try:
        snk = snakes.plugins.load(options.plugins, "snakes.nets", "snk")
    except:
        die(ERR_PLUGIN, str(sys.exc_info()[1]))
    build = Builder(snk)
    try:
        net = build.build(node)
        net.label(srcfile=abcd, snakes=snk)
    except (CompilationError, DeclarationError):
        die(ERR_COMPILE, str(sys.exc_info()[1]))
    except:
        bug()
    # output
    if options.pnml:
        save_pnml(net, options.pnml)
    for engine in gv_engines:
        target = getattr(options, "gv%s" % engine)
        if target:
            draw(net, target, engine)
    if options.html:
        try:
            html(abcd, node, net, draw(net, None), options.html)
        except:
            bug()
    trace, lineno = [], None
    if options.check:
        states, lineno, trace = Checker(net, options.progress).run()
        if options.progress:
            print("%s states explored" % states)
    if options.simul:
        engine = "dot"
        for eng in gv_engines:
            if getattr(options, "gv%s" % eng):
                engine = eng
                break
        try:
            simul = Simulator(node, net, draw(net, None, engine), options.port)
        except:
            bug()
        simul.start()
        if options.headless:
            with open(options.headless, "w") as out:
                out.write(
                    json({
                        "res": "%sr" % simul.url,
                        "url": simul.url,
                        "key": simul.server.httpd.key,
                        "host": "127.0.0.1",
                        "port": simul.port
                    }))
        else:
            webbrowser.open(simul.url)
        simul.wait()
    elif trace:
        if lineno is None:
            print("unsafe execution:")
        else:
            asserts = dict((a.lineno, a) for a in net.label("asserts"))
            print("line %s, %r failed:" %
                  (lineno, asserts[lineno].st.source()))
        for trans, mode in trace:
            print("  %s %s" % (trans, mode))
    return net
Example #4
0
def main (args=sys.argv[1:], src=None) :
    global options, snk
    # get options
    try:
        if src is None :
            getopts(args)
        else :
            getopts(list(args) + ["<string>"])
    except SystemExit :
        raise
    except :
        die(ERR_OPT, str(sys.exc_info()[1]))
    # read source
    if src is not None :
        source = src
    else :
        try :
            source = open(abcd).read()
        except :
            die(ERR_IO, "could not read input file %r" % abcd)
    # parse
    try :
        node = parse(source, filename=abcd)
    except ParseError :
        die(ERR_PARSE, str(sys.exc_info()[1]))
    except :
        bug()
    # compile
    dirname = os.path.dirname(abcd)
    if dirname and dirname not in sys.path :
        sys.path.append(dirname)
    elif "." not in sys.path :
        sys.path.append(".")
    try :
        snk = snakes.plugins.load(options.plugins, "snakes.nets", "snk")
    except :
        die(ERR_PLUGIN, str(sys.exc_info()[1]))
    build = Builder(snk)
    try :
        net = build.build(node)
        net.label(srcfile=abcd, snakes=snk)
    except (CompilationError, DeclarationError) :
        die(ERR_COMPILE, str(sys.exc_info()[1]))
    except :
        bug()
    # output
    if options.pnml :
        save_pnml(net, options.pnml)
    for engine in gv_engines :
        target = getattr(options, "gv%s" % engine)
        if target :
            draw(net, target, engine)
    if options.html :
        try :
            html(abcd, node, net, draw(net, None), options.html)
        except :
            bug()
    trace, lineno = [], None
    if options.check :
        lineno, trace = Checker(net).run()
    if options.simul :
        try :
            simul = Simulator(node, net, draw(net, None), options.port)
        except :
            bug()
        simul.start()
        if options.headless :
            with open(options.headless, "w") as out :
                out.write(json({"res" : "%sr" % simul.url,
                                "url" : simul.url,
                                "key" : simul.server.httpd.key,
                                "host" : "127.0.0.1",
                                "port" : simul.port}))
        else :
            webbrowser.open(simul.url)
        simul.wait()
    elif trace :
        if lineno is None :
            print("unsafe execution:")
        else :
            asserts = dict((a.lineno, a) for a in net.label("asserts"))
            print("line %s, %r failed:"
                  % (lineno, asserts[lineno].st.source()))
        for trans, mode in trace :
            print("  %s %s" % (trans, mode))
    return net