def compile_fs():
    """Compile a flowsynth file into a downloadable PCAP."""

    check_pcap_path()

    # write flowsynth data to file
    fs_code = str(request.form.get("code"))
    hashobj = hashlib.md5()
    hashobj.update(f"{fs_code}{random.randint(1, 10000)}".encode("utf-8"))
    fname = hashobj.hexdigest()[0:15]

    inpath = tempfile.mkstemp()[1]
    outpath = get_pcap_file_path(fname)

    # write to temp input file
    with open(inpath, "w") as fptr:
        fptr.write(fs_code)

    # run the flowsynth command
    command = "%s %s -f pcap -w %s --display json --no-filecontent" % (
        BIN_PATH,
        inpath,
        outpath,
    )
    logger.debug(command)
    proc = subprocess.Popen(
        shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE
    )
    output, errors = proc.communicate()

    # parse flowsynth json
    try:
        synthstatus = json.loads(output)
    except ValueError:
        # there was a problem producing output.
        logger.error("Problem processing Flowsynth output: %s" % output)
        return render_template(
            "pcapwg/error.html", error_text=output + b"\n\n" + errors
        )

    # delete the tempfile
    os.unlink(inpath)

    # render the results page
    return render_template(
        "pcapwg/packet.html", buildstatus=synthstatus, filename=fname
    )
Compile a flowsynth file into a downloadable PCAP.
app/flowsynth.py