<commit_before>"""Generated an open-api spec for a grpc api spec.

Reads the the api spec in protobuf format and generate an open-api spec.
Optionally applies settings from the grpc-service configuration.
"""

def _collect_includes(gen_dir, srcs):
    """Build an include path mapping.

    It is important to not just collect unique dirnames, to also support
    proto files of the same name from different packages.

    The implementation below is similar to what bazel does in its
    ProtoCompileActionBuilder.java
    """
    includes = []
    for src in srcs:
        ref_path = src.path

        if ref_path.startswith(gen_dir):
            ref_path = ref_path[len(gen_dir):].lstrip("/")

        if src.owner.workspace_root:
            workspace_root = src.owner.workspace_root
            ref_path = ref_path[len(workspace_root):].lstrip("/")

        include = ref_path + "=" + src.path
        if include not in includes:
            includes.append(include)

    return includes

def _run_proto_gen_swagger(ctx, direct_proto_srcs, transitive_proto_srcs, actions, protoc, protoc_gen_swagger, grpc_api_configuration):
    swagger_files = []
    for proto in direct_proto_srcs:
        swagger_file = actions.declare_file(
            "%s.swagger.json" % proto.basename[:-len(".proto")],
            sibling = proto,
        )

        inputs = direct_proto_srcs + transitive_proto_srcs
        tools = [protoc_gen_swagger]

        options = ["logtostderr=true", "allow_repeated_fields_in_body=true"]
        if grpc_api_configuration:
            options.append("grpc_api_configuration=%s" % grpc_api_configuration.path)
            inputs.append(grpc_api_configuration)

        includes = _collect_includes(ctx.genfiles_dir.path, direct_proto_srcs + transitive_proto_srcs)

        args = actions.args()
        args.add("--plugin=%s" % protoc_gen_swagger.path)
        args.add("--swagger_out=%s:%s" % (",".join(options), ctx.bin_dir.path))
        args.add_all(["-I%s" % include for include in includes])
        args.add(proto.path)

        actions.run(
            executable = protoc,
            inputs = inputs,
            tools = tools,
            outputs = [swagger_file],
            arguments = [args],
        )

        swagger_files.append(swagger_file)

    return swagger_files

def _proto_gen_swagger_impl(ctx):
    proto = ctx.attr.proto[ProtoInfo]
    grpc_api_configuration = ctx.file.grpc_api_configuration
<commit_msg>Run proto gen swagger with protos from external repository<commit_after>"""Generated an open-api spec for a grpc api spec.

Reads the the api spec in protobuf format and generate an open-api spec.
Optionally applies settings from the grpc-service configuration.
"""

def _collect_includes(gen_dir, srcs):
    """Build an include path mapping.

    It is important to not just collect unique dirnames, to also support
    proto files of the same name from different packages.

    The implementation below is similar to what bazel does in its
    ProtoCompileActionBuilder.java
    """
    includes = []
    for src in srcs:
        ref_path = src.path

        if ref_path.startswith(gen_dir):
            ref_path = ref_path[len(gen_dir):].lstrip("/")

        if src.owner.workspace_root:
            workspace_root = src.owner.workspace_root
            ref_path = ref_path[len(workspace_root):].lstrip("/")

        include = ref_path + "=" + src.path
        if include not in includes:
            includes.append(include)

    return includes

def _run_proto_gen_swagger(ctx, direct_proto_srcs, transitive_proto_srcs, actions, protoc, protoc_gen_swagger, grpc_api_configuration):
    swagger_files = []
    for proto in direct_proto_srcs:
        swagger_file = actions.declare_file(
            "%s.swagger.json" % proto.basename[:-len(".proto")],
            sibling = proto,
        )

        inputs = direct_proto_srcs + transitive_proto_srcs
        tools = [protoc_gen_swagger]

        options = ["logtostderr=true", "allow_repeated_fields_in_body=true"]
        if grpc_api_configuration:
            options.append("grpc_api_configuration=%s" % grpc_api_configuration.path)
            inputs.append(grpc_api_configuration)

        output_dir = ctx.bin_dir.path
        if proto.owner.workspace_root:
            output_dir = "/".join([output_dir, proto.owner.workspace_root])

        includes = _collect_includes(ctx.genfiles_dir.path, direct_proto_srcs + transitive_proto_srcs)

        args = actions.args()
        args.add("--plugin=%s" % protoc_gen_swagger.path)
        args.add("--swagger_out=%s:%s" % (",".join(options), output_dir))
        args.add_all(["-I%s" % include for include in includes])
        args.add(proto.path)

        actions.run(
            executable = protoc,
            inputs = inputs,
            tools = tools,
            outputs = [swagger_file],
            arguments = [args],
        )

        swagger_files.append(swagger_file)

    return swagger_files

def _proto_gen_swagger_impl(ctx):
    proto = ctx.attr.proto[ProtoInfo]
    grpc_api_configuration = ctx.file.grpc_api_configuration

{'fraction_non_alphanumeric': 0.09473478175768252, 'fraction_numerical': 0.0, 'mean_word_length': 2.7936154417223458, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '5366762', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1530, 'n_words': 351}