<commit_before>#! /usr/bin/env nix-shell
#! nix-shell -i python -p python rustUnstable.cargo pythonPackages.configobj
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/125ffff089b6bd360c82cf986d8cc9b17fc2e8ac.tar.gz

# this script sets up a new rustfbp component
# by reading the utils/component.ini which you have configured
# then:
# * writing the cargo.toml file
# * writing the default.nix file
# * writing the lib.rs file
# * then runs `cargo generate-lockfile` on the component to create the lockfile
# * inserting the component into components/default.nix

import os.path
import sys
import shlex
import subprocess
from configobj import ConfigObj
config = ConfigObj("component.ini")

def write_cargo_deps(deps):
    toml = "rustfbp = \"*\"\ncapnp = \"*\"\n"
    for crate in deps:
        toml += crate + " = \"" + deps[crate] + "\"\n"
    return toml

def create_cargo_toml (component_name, cargo_deps):
    cargo_toml_template = """
[package]
name = \"""" + component_name + """\"
version = "0.1.0"
authors = ["test <<EMAIL>>"]

[lib]
name = \"""" + component_name + """\"
crate-type = ["dylib"]

[dependencies]
""" + write_cargo_deps(cargo_deps)
    return cargo_toml_template

def write_contracts(ports, include_type):
    contracts = ""
    contract_set = set()
    for port_type in ports:
        for port in ports[port_type]:
            contract_set.add(ports[port_type][port])
    if include_type == "nix_contracts":
        contracts += ' '.join(map("{0}".format, contract_set))
    elif include_type == "rust_contracts":
        contracts += ", ".join(contract_set)
    elif include_type == "nix_header":
        if len(contract_set) > 0:
            contracts += "# contracts:\n, "
        contracts += ', '.join(map("{0}".format, contract_set))
    return contracts

def write_external_dependencies(external_dependencies):
    externs = ""
    externs_set = set()
    for deps in external_dependencies:
        for extern in external_dependencies[deps]:
            externs_set.add(extern)
    if len(externs_set) > 0:
        externs = "# external dependencies:\n, "
        externs += ', '.join(map("{0}".format, externs_set))
    return externs

def write_builtins(external_dependencies):
    externs = ""
    externs_set = set()
    for deps in external_dependencies:
        for extern in external_dependencies[deps]:
            externs_set.add(extern)
    if len(externs_set) > 0:
        externs = "buildInputs = [ "
        externs += ' '.join(map("{0}".format, externs_set))
        externs += " ];\n"
    return externs

def create_default_nix (component_description, ports, external_dependencies):
    default_nix = """
{ stdenv, buildFractalideComponent, genName, upkeepers
""" + write_contracts(ports, "nix_header")  + """
""" + write_external_dependencies(external_dependencies) + """
, ...}:

buildFractalideComponent rec {
  name = genName ./.;
  src = ./.;
  contracts = [""" + write_contracts(ports, "nix_contracts") + """];
  depsSha256 = "2m6n74fm7k99pp13j5d5yyp4j0znc0s10958hhyyh3shq9rj8862";
  """ + write_builtins(external_dependencies) + """
  meta = with stdenv.lib; {
    description = "Component: """ + component_description + """";
    homepage = https://gitlab.com/fractalide/fractalide/tree/master/components/maths/boolean/nand;
    license = with licenses; [ mpl20 ];
    maintainers = with upkeepers; [ dmichiels sjmackenzie];
  };
}
    """
    return default_nix

def write_externs(deps):
    externs = "#![feature(question_mark)]\n#[macro_use]\nextern crate rustfbp;\nextern crate capnp;\n"
    for crate in deps:
        externs += "extern crate " + crate + ";\n"
    return externs

def write_input_output_array_ports(ports):
    formatted_port_types = []
    for port_type in ports:
        port_list = []
        start = port_type + "("
        for port in ports[port_type]:
            port_list.append(port + ": " + ports[port_type][port])
        formatted_port_types.append(start + ", ".join(map("{0}".format, port_list)) + "),")
    return "\n  ".join(formatted_port_types)

def write_extra_ports(extra_ports):
    e_ports = "  option(" + extra_ports["option"] + "),\n  acc(" + extra_ports["acc"] + "), "
    if extra_ports["portal"] == "true":
        e_ports += "portal()"
    return e_ports

def write_simple_input_extractor(port, contract):
    ip = "ip_" + port
    reader = port + "_reader"
    return """
    let """ + port + """ = {
        let mut """ + ip + """ = self.ports.recv(\"""" + port + """\")?;
        let """ + reader + """: """ + contract + """::Reader = """ + ip + """.get_root()?;
        """ + reader + """.get_XXX() // read contract: """ + contract + """ to replace XXX
    };"""

def write_simple_input_extractors(simple_inputs):
    simple_input_extractors = ""
    for port in simple_inputs:
        simple_input_extractors += write_simple_input_extractor(port, simple_inputs[port])
    return simple_input_extractors

def write_inputs_array_extractor(port, contract):
    ip = "ip_" + port
    reader = port + "_reader"
    return """
    let """ + port + """ = {
        let mut """ + ip + """ = self.ports.recv(\"""" + port + """\")?;
        let """ + reader + """: """ + contract + """::Reader = """ + ip + """.get_root()?;
        """ + reader + """.get_XXX() // read contract: """ + contract + """ to replace XXX
    };"""

def write_inputs_array_extractors(input_arrays):
    input_array_extractors = ""
    for port in input_arrays:
        input_array_extractors += write_inputs_array_extractor(port, input_arrays[port])
    return input_array_extractors

def write_simple_outputs_extractor(port, contract):
    out_ip = "out_ip_" + port
    return """
    let mut """ + out_ip + """ = IP::new();
    {
      let mut variable = """ + out_ip + """.init_root::<""" + contract + """::Builder>();
      variable.set_XXX(YYY); // read contract: """ + contract + """ to replace XXX
    }"""

def write_simple_outputs_extractors(simple_outputs):
    simple_outputs_extractors = ""
    for port in simple_outputs:
        simple_outputs_extractors += write_simple_outputs_extractor(port, simple_outputs[port])
    return simple_outputs_extractors

def write_outputs_array_extractor(port, contract):
    out_ip = "out_ip_" + port
    return """<commit_msg>Problem: created component doesn't work.<commit_after>#! /usr/bin/env nix-shell
#! nix-shell -i python -p python rustUnstable.cargo pythonPackages.configobj
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/125ffff089b6bd360c82cf986d8cc9b17fc2e8ac.tar.gz

# this script sets up a new rustfbp component
# by reading the utils/component.ini which you have configured
# then:
# * writing the cargo.toml file
# * writing the default.nix file
# * writing the lib.rs file
# * then runs `cargo generate-lockfile` on the component to create the lockfile
# * inserting the component into components/default.nix

import os.path
import sys
import shlex
import subprocess
from configobj import ConfigObj
config = ConfigObj("component.ini")

def write_cargo_deps(deps):
    toml = "rustfbp = \"*\"\ncapnp = \"*\"\n"
    for crate in deps:
        toml += crate + " = \"" + deps[crate] + "\"\n"
    return toml

def create_cargo_toml (component_name, cargo_deps):
    cargo_toml_template = """
[package]
name = \"""" + component_name + """\"
version = "0.1.0"
authors = ["test <<EMAIL>>"]

[lib]
name = \"""" + component_name + """\"
crate-type = ["dylib"]

[dependencies]
""" + write_cargo_deps(cargo_deps)
    return cargo_toml_template

def write_contracts(ports, include_type):
    contracts = ""
    contract_set = set()
    for port_type in ports:
        for port in ports[port_type]:
            contract_set.add(ports[port_type][port])
    if include_type == "nix_contracts":
        contracts += ' '.join(map("{0}".format, contract_set))
    elif include_type == "rust_contracts":
        contracts += ", ".join(contract_set)
    elif include_type == "nix_header":
        if len(contract_set) > 0:
            contracts += "# contracts:\n, "
        contracts += ', '.join(map("{0}".format, contract_set))
    return contracts

def write_external_dependencies(external_dependencies):
    externs = ""
    externs_set = set()
    for deps in external_dependencies:
        for extern in external_dependencies[deps]:
            externs_set.add(extern)
    if len(externs_set) > 0:
        externs = "# external dependencies:\n, "
        externs += ', '.join(map("{0}".format, externs_set))
    return externs

def write_builtins(external_dependencies):
    externs = ""
    externs_set = set()
    for deps in external_dependencies:
        for extern in external_dependencies[deps]:
            externs_set.add(extern)
    if len(externs_set) > 0:
        externs = "buildInputs = [ "
        externs += ' '.join(map("{0}".format, externs_set))
        externs += " ];\n"
    return externs

def create_default_nix (component_description, ports, external_dependencies):
    default_nix = """
{ stdenv, buildFractalideComponent, genName, upkeepers
""" + write_contracts(ports, "nix_header")  + """
""" + write_external_dependencies(external_dependencies) + """
, ...}:

buildFractalideComponent rec {
  name = genName ./.;
  src = ./.;
  contracts = [""" + write_contracts(ports, "nix_contracts") + """];
  depsSha256 = "2m6n74fm7k99pp13j5d5yyp4j0znc0s10958hhyyh3shq9rj8862";
  """ + write_builtins(external_dependencies) + """
  meta = with stdenv.lib; {
    description = "Component: """ + component_description + """";
    homepage = https://gitlab.com/fractalide/fractalide/tree/master/components/maths/boolean/nand;
    license = with licenses; [ mpl20 ];
    maintainers = with upkeepers; [ dmichiels sjmackenzie];
  };
}
    """
    return default_nix

def write_externs(deps):
    externs = "#![feature(question_mark)]\n#[macro_use]\nextern crate rustfbp;\nextern crate capnp;\n"
    for crate in deps:
        externs += "extern crate " + crate + ";\n"
    return externs

def write_input_output_array_ports(ports):
    formatted_port_types = []
    for port_type in ports:
        port_list = []
        start = port_type + "("
        for port in ports[port_type]:
            port_list.append(port + ": " + ports[port_type][port])
        formatted_port_types.append(start + ", ".join(map("{0}".format, port_list)) + "),")
    return "\n  ".join(formatted_port_types)

def write_extra_ports(extra_ports):
    e_ports = "  option(" + extra_ports["option"] + "),\n  acc(" + extra_ports["acc"] + "), "
    if extra_ports["portal"] == "true":
        e_ports += "portal()"
    return e_ports

def write_simple_input_extractor(port, contract):
    ip = "ip_" + port
    reader = port + "_reader"
    return """
    let mut """ + ip + """ = self.ports.recv(\"""" + port + """\")?;
    let """ + port + """ = {
        let """ + reader + """: """ + contract + """::Reader = """ + ip + """.get_root()?;
        """ + reader + """.get_XXX() // read contract: """ + contract + """ to replace XXX
    };"""

def write_simple_input_extractors(simple_inputs):
    simple_input_extractors = ""
    for port in simple_inputs:
        simple_input_extractors += write_simple_input_extractor(port, simple_inputs[port])
    return simple_input_extractors

def write_inputs_array_extractor(port, contract):
    ip = "ip_" + port
    reader = port + "_reader"
    return """
    let mut """ + ip + """ = self.ports.recv(\"""" + port + """\")?;
    let """ + port + """ = {
        let """ + reader + """: """ + contract + """::Reader = """ + ip + """.get_root()?;
        """ + reader + """.get_XXX() // read contract: """ + contract + """ to replace XXX
    };"""

def write_inputs_array_extractors(input_arrays):
    input_array_extractors = ""
    for port in input_arrays:
        input_array_extractors += write_inputs_array_extractor(port, input_arrays[port])
    return input_array_extractors

def write_simple_outputs_extractor(port, contract):
    out_ip = "out_ip_" + port
    return """
    let mut """ + out_ip + """ = IP::new();
    {
      let mut variable = """ + out_ip + """.init_root::<""" + contract + """::Builder>();
      variable.set_XXX(YYY); // read contract: """ + contract + """ to replace XXX
    }"""

def write_simple_outputs_extractors(simple_outputs):
    simple_outputs_extractors = ""
    for port in simple_outputs:
        simple_outputs_extractors += write_simple_outputs_extractor(port, simple_outputs[port])
    return simple_outputs_extractors

def write_outputs_array_extractor(port, contract):
    out_ip = "out_ip_" + port
    return """
{'fraction_non_alphanumeric': 0.15286066682581365, 'fraction_numerical': 0.009867112278188907, 'mean_word_length': 3.4949928469241773, 'pattern_counts': {'":': 20, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2738368', 'n_tokens_mistral': 4211, 'n_tokens_neox': 4108, 'n_words': 862}