<commit_before># Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""An implementation of container_push based on google/containerregistry.

This variant of container_push accepts a container_bundle target and publishes
the embedded image references.
"""

load("@io_bazel_rules_docker//container:providers.bzl", "BundleInfo")
load(
    "//container:layer_tools.bzl",
    _gen_img_args = "generate_args_for_image",
)
load(
    "//skylib:path.bzl",
    "runfile",
)

def _get_runfile_path(ctx, f):
    return "${RUNFILES}/%s" % runfile(ctx, f)

def _impl(ctx):
    """Core implementation of container_push."""
    stamp = ctx.attr.bundle[BundleInfo].stamp
    images = ctx.attr.bundle[BundleInfo].container_images

    stamp_inputs = []
    if stamp:
        stamp_inputs = [ctx.info_file, ctx.version_file]

    stamp_arg = " ".join(["--stamp-info-file=%s" % _get_runfile_path(ctx, f) for f in stamp_inputs])

    scripts = []
    runfiles = []
    for index, tag in enumerate(images.keys()):
        image = images[tag]

        pusher_args, pusher_inputs = _gen_img_args(ctx, image, _get_runfile_path)
        pusher_args += ["--stamp-info-file=%s" % _get_runfile_path(ctx, f) for f in stamp_inputs]
        pusher_args.append("--dst={}".format(tag))
        pusher_args.append("--format={}".format(ctx.attr.format))
        out = ctx.actions.declare_file("%s.%d.push" % (ctx.label.name, index))
        ctx.actions.expand_template(
            template = ctx.file._tag_tpl,
            substitutions = {
                "%{args}": " ".join(pusher_args),
                "%{container_pusher}": _get_runfile_path(ctx, ctx.executable._pusher),
            },
            output = out,
            is_executable = True,
        )

        scripts.append(out)
        runfiles.append(out)
        runfiles += pusher_inputs

    ctx.actions.expand_template(
        template = ctx.file._all_tpl,
        substitutions = {
            "%{async_push_statements}": "\n".join([
                "async \"%s\"" % _get_runfile_path(ctx, command)
                for command in scripts
            ]),
            "%{push_statements}": "\n".join([
                "\"%s\"" % _get_runfile_path(ctx, command)
                for command in scripts
            ]),
            "%{sequential}": "true" if ctx.attr.sequential else "",
        },
        output = ctx.outputs.executable,
        is_executable = True,
    )

    return [
        DefaultInfo(
            runfiles = ctx.runfiles(
                files = [ctx.executable._pusher] + stamp_inputs + runfiles,
                transitive_files = ctx.attr._pusher[DefaultInfo].default_runfiles.files,
            ),
        ),
    ]

container_push = rule(
    attrs = {
        "bundle": attr.label(
            mandatory = True,
            doc = "The bundle of tagged images to publish.",
        ),
        "format": attr.string(
            mandatory = True,
            values = [
                "OCI",
                "Docker",
            ],
            doc = "The form to push: Docker or OCI.",
        ),
        "sequential": attr.bool(
            default = False,
            doc = "If true, push images sequentially.",
        ),
        "_all_tpl": attr.label(
            default = Label("//contrib:push-all.sh.tpl"),
            allow_single_file = True,
        ),
        "_pusher": attr.label(<commit_msg>contrib/push-all: add option to skip unchanged digest (#1744)<commit_after># Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""An implementation of container_push based on google/containerregistry.

This variant of container_push accepts a container_bundle target and publishes
the embedded image references.
"""

load("@io_bazel_rules_docker//container:providers.bzl", "BundleInfo")
load(
    "//container:layer_tools.bzl",
    _gen_img_args = "generate_args_for_image",
)
load(
    "//skylib:path.bzl",
    "runfile",
)

def _get_runfile_path(ctx, f):
    return "${RUNFILES}/%s" % runfile(ctx, f)

def _impl(ctx):
    """Core implementation of container_push."""
    stamp = ctx.attr.bundle[BundleInfo].stamp
    images = ctx.attr.bundle[BundleInfo].container_images

    stamp_inputs = []
    if stamp:
        stamp_inputs = [ctx.info_file, ctx.version_file]

    stamp_arg = " ".join(["--stamp-info-file=%s" % _get_runfile_path(ctx, f) for f in stamp_inputs])

    scripts = []
    runfiles = []
    for index, tag in enumerate(images.keys()):
        image = images[tag]

        pusher_args, pusher_inputs = _gen_img_args(ctx, image, _get_runfile_path)
        pusher_args += ["--stamp-info-file=%s" % _get_runfile_path(ctx, f) for f in stamp_inputs]
        if ctx.attr.skip_unchanged_digest:
            pusher_args.append("--skip-unchanged-digest")
        pusher_args.append("--dst={}".format(tag))
        pusher_args.append("--format={}".format(ctx.attr.format))
        out = ctx.actions.declare_file("%s.%d.push" % (ctx.label.name, index))
        ctx.actions.expand_template(
            template = ctx.file._tag_tpl,
            substitutions = {
                "%{args}": " ".join(pusher_args),
                "%{container_pusher}": _get_runfile_path(ctx, ctx.executable._pusher),
            },
            output = out,
            is_executable = True,
        )

        scripts.append(out)
        runfiles.append(out)
        runfiles += pusher_inputs

    ctx.actions.expand_template(
        template = ctx.file._all_tpl,
        substitutions = {
            "%{async_push_statements}": "\n".join([
                "async \"%s\"" % _get_runfile_path(ctx, command)
                for command in scripts
            ]),
            "%{push_statements}": "\n".join([
                "\"%s\"" % _get_runfile_path(ctx, command)
                for command in scripts
            ]),
            "%{sequential}": "true" if ctx.attr.sequential else "",
        },
        output = ctx.outputs.executable,
        is_executable = True,
    )

    return [
        DefaultInfo(
            runfiles = ctx.runfiles(
                files = [ctx.executable._pusher] + stamp_inputs + runfiles,
                transitive_files = ctx.attr._pusher[DefaultInfo].default_runfiles.files,
            ),
        ),
    ]

container_push = rule(
    attrs = {
        "bundle": attr.label(
            mandatory = True,
            doc = "The bundle of tagged images to publish.",
        ),
        "format": attr.string(
            mandatory = True,
            values = [
                "OCI",
                "Docker",
            ],
            doc = "The form to push: Docker or OCI.",
        ),
        "sequential": attr.bool(
            default = False,
            doc = "If true, push images sequentially.",
        ),
        "skip_unchanged_digest": attr.bool(
            default = False,
            doc = "Only push images if the digest has changed, default to False",
        ),
        "_all_tpl": attr.label(
            default = Label("//contrib:push-all.sh.tpl"),
            allow_single_file = True,
        ),
        "_pusher": attr.label(
{'fraction_non_alphanumeric': 0.12142769532687354, 'fraction_numerical': 0.0024530847540782533, 'mean_word_length': 2.233148295003965, 'pattern_counts': {'":': 21, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2055927', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2414, 'n_words': 591}