From 9c2ff801f5c94bf077e915c24856435dee5259a6 Mon Sep 17 00:00:00 2001 From: Jean-Francois Panisset Date: Sat, 28 Nov 2026 00:16:43 -0800 Subject: [PATCH] Split release commands out of the CLI module, uv 0.13.7 python-sonar.yml's pytest --pylint run fails on any pylint message, so cli/aswfdocker.py exceeding max-module-lines (1016/1000) failed on main while the pre-commit pylint check, which only enforced fail-under=8, passed on the PR. Move the release, publishreleases and exportsboms commands into cli/release.py, and the shared click options and helpers into cli/options.py. pylintrc now fails on any message (fail-on), so pull requests are checked the same way. uv 0.13.5 to 0.13.7, as required by dependabot. Assisted-by: Claude/Opus 5.5 Signed-off-by: Jean-Francois Panisset --- CLAUDE.md | 5 +- pylintrc | 5 +- pyproject.toml | 2 +- python/aswfdocker/cli/aswfdocker.py | 394 +--------------------------- python/aswfdocker/cli/options.py | 88 +++++++ python/aswfdocker/cli/release.py | 326 +++++++++++++++++++++++ 6 files changed, 434 insertions(+), 386 deletions(-) create mode 100644 python/aswfdocker/cli/options.py create mode 100644 python/aswfdocker/cli/release.py diff --git a/CLAUDE.md b/CLAUDE.md index 7941d8ef..bc355fc7 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -76,7 +76,7 @@ uv run pytest python/aswfdocker # Type checking uv run mypy python/aswfdocker -# Linting (score must be >= 8.0) +# Linting (any pylint message fails, as in python-sonar.yml's pytest --pylint) uv run pylint python/aswfdocker # Formatting (Black, targets Python 3.10) @@ -137,7 +137,8 @@ uv run aswfdocker dockergen --check - **Black** formatting (target: py310). Vendored Conan recipes in `packages/conan/recipes/` are excluded from Black. - **MyPy** strict type checking (Python 3.11 target) -- **Pylint** with fail-under score of 8.0 +- **Pylint**: any message fails (`fail-on` in `pylintrc`), matching `pytest --pylint` + in `python-sonar.yml`; disable a check inline with a reason if it really doesn't apply - **Naming**: PascalCase classes, snake_case functions/variables, UPPER_CASE constants ### Docker Images diff --git a/pylintrc b/pylintrc index 2929d653..4ffd3512 100644 --- a/pylintrc +++ b/pylintrc @@ -14,7 +14,10 @@ extension-pkg-whitelist= # Return non-zero exit code if any of these messages/categories are detected, # even if score is above --fail-under value. Syntax same as enable. Messages # specified are enabled, while categories only check already-enabled messages. -fail-on= +# Any message fails, as with the pytest --pylint run of python-sonar.yml +# (pytest-pylint ignores fail-under), so the pre-commit check of pull requests +# catches what would otherwise only fail on main. +fail-on=C,R,W,E,F # Specify a score threshold to be exceeded before program exits with error. fail-under=8 diff --git a/pyproject.toml b/pyproject.toml index e5d56288..16f13810 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -78,7 +78,7 @@ packages = { find = { where = ["python"] } } aswfdocker = ["data/*.yaml", "data/*.jinja2"] [tool.uv] -required-version = "0.13.5" +required-version = "0.13.7" [tool.pytest.ini_options] testpaths = [ diff --git a/python/aswfdocker/cli/aswfdocker.py b/python/aswfdocker/cli/aswfdocker.py index f8d9c049..211600f8 100644 --- a/python/aswfdocker/cli/aswfdocker.py +++ b/python/aswfdocker/cli/aswfdocker.py @@ -5,9 +5,7 @@ """ import os -import sys import logging -import warnings import click @@ -20,10 +18,15 @@ dockergen as aswf_dockergen, index, utils, - releaser, settings as aswf_settings, ) from aswfdocker import conandiff as conandiff_mod +from aswfdocker.cli import release as release_commands +from aswfdocker.cli.options import ( + common_image_options, + get_group_info, + pass_build_info, +) # Avoid pylint giving us grief for complex command lines # pylint: disable=too-many-arguments @@ -33,9 +36,6 @@ logger = logging.getLogger("build-images") -pass_build_info = click.make_pass_decorator(aswfinfo.ASWFInfo) - - @click.group() @click.option( "--repo-root", @@ -62,83 +62,6 @@ def cli(ctx, repo_root, repo_uri, source_branch, verbose): ) -def validate_image_name(_, __, value): - if value is None: - return None - try: - return utils.get_image_spec(value) - except RuntimeError as e: - raise click.BadParameter(e.args[0]) - - -def common_image_options(function): - function = click.option( - "--ci-image-type", - "-t", - required=False, - help="Builds a Conan package or a container image.", - type=click.Choice(constants.ImageType.__members__.keys(), case_sensitive=True), - )(function) - function = click.option( - "--group", - "-g", - required=False, - multiple=True, - help='The name of the group of images to build, e.g. "base" or "vfx",' - " can be specified multiple times.", - )(function) - function = click.option( - "--version", - "-v", - required=False, - multiple=True, - help='The major version number to build, e.g. "2027", can be specified multiple times.' - ' Can also be "all".', - )(function) - function = click.option( - "--full-name", - "-n", - callback=validate_image_name, - required=False, - help="The full image name, e.g. aswftesting/ci-common:2027-clang22 or aswf/ci-package-openexr:2027", - )(function) - function = click.option( - "--target", - "-tg", - required=False, - multiple=True, - help='An optional package or image name to build, e.g. "usd", can be specified multiple times.', - )(function) - return function - - -def get_group_info(build_info, ci_image_type, groups, versions, full_name, targets): - idx = index.Index() - if full_name: - org, image_type, target, version = full_name - versions = [version] - targets = [target] - try: - groups = [idx.get_group_from_image(image_type, target)] - except RuntimeError as e: - raise click.BadOptionUsage(option_name="--full-name", message=e.args[0]) - build_info.set_org(org) - else: - if ci_image_type is None: - image_type = constants.ImageType.IMAGE - else: - image_type = constants.ImageType[ci_image_type] - if not groups and targets: - groups = [idx.get_group_from_image(image_type, targets[0])] - group_info = groupinfo.GroupInfo( - type_=image_type, - names=groups, - versions=versions, - targets=targets, - ) - return group_info - - @cli.command() @common_image_options @click.option( @@ -442,305 +365,6 @@ def settings(settings_path, github_access_token): s.save() -@cli.command() -@common_image_options -@click.option( - "--sha", - "-s", - help="The sha to create the release tag on, defaults to current sha.", -) -@click.option( - "--github-org", - "-o", - default=constants.MAIN_GITHUB_ASWF_ORG, - help="The GitHub organization/username to create the release on," - f" defaults to {constants.MAIN_GITHUB_ASWF_ORG}.", -) -@click.option( - "--docker-org", - "-do", - default=constants.TESTING_DOCKER_ORG, - help="The Docker organization/username to upload the Docker image to," - f" defaults to {constants.TESTING_DOCKER_ORG}.", -) -@click.option( - "--message", - "-m", - help="The release message of each image's GitHub release (images only).", -) -@click.option( - "--force-rebuild", - is_flag=True, - help="Conan packages only: rebuild and upload the selected packages even if " - "Artifactory already has an up-to-date binary.", -) -@click.option( - "--workflow-ref", - default="", - help="The branch to run the release workflow definition from, defaults to the " - "repository's default branch. Releases are always built from --sha.", -) -@click.option( - "--full-release", - is_flag=True, - help="Release every Conan package and image of the given years (-v), in " - "dependency order.", -) -@click.option("--dry-run", "-d", is_flag=True, help="Just logs what would happen.") -@pass_build_info -def release( # pylint: disable=too-many-arguments,too-many-locals - build_info, - ci_image_type, - group, - version, - full_name, - target, - sha, - github_org, - docker_org, - message, - force_rebuild, - workflow_ref, - full_release, - dry_run, -): - """Releases ci-image Docker images and/or Conan packages. - - Starts a GitHub Actions workflow building them from the commit, the only - place holding the Docker Hub and Artifactory credentials: images are - pushed to Docker Hub and each recorded as a GitHub release (and tag) on - the commit, Conan packages are uploaded to Artifactory. With - --full-release, every package and image of the given years is released, - in dependency order. - """ - - # Disable SSL unclosed ResourceWarning coming from GitHub - warnings.filterwarnings( - action="ignore", message="unclosed", category=ResourceWarning - ) - - if not sha: - if utils.get_current_branch() != "main": - click.secho( - "Cannot release from non-main branch! Specify --sha to create a release on a given commit.", - fg="red", - ) - sys.exit(1) - sha = utils.get_current_sha() - if docker_org: - build_info.set_org(docker_org) - - workflow_releaser: releaser.WorkflowReleaser - if full_release: - if ci_image_type or group or full_name or target: - raise click.BadOptionUsage( - "--full-release", - "--full-release releases every group: it can't be combined with " - "-t, -g, -n or --target", - ) - years = sorted(vi.version for vi in index.Index().iter_years()) - if list(version) == [constants.ALL]: - version = years - if not version or set(version) - set(years): - raise click.BadOptionUsage( - "--version", - f"--full-release needs years to release (-v), from: {', '.join(years)}", - ) - full_releaser = releaser.FullReleaser( - build_info=build_info, - versions=sorted(version), - sha=sha, - github_org=github_org, - workflow_ref=workflow_ref, - force_rebuild=force_rebuild, - message=message, - ) - full_releaser.gather() - workflow_releaser = full_releaser - prompt = ( - f"Are you sure you want GitHub Actions to release every Conan package " - f"({'forcing a rebuild' if force_rebuild else 'unless up to date'}) of " - f"{' '.join(sorted(version))} to {build_info.docker_org} from sha={sha}, " - f"and these {len(full_releaser.releases)} images (as draft GitHub " - "releases until they're pushed)?\n" - + "\n".join(r.name for r in full_releaser.releases) - + "\n" - ) - else: - group_info = get_group_info( - build_info, ci_image_type, group, version, full_name, target - ) - if group_info.type == constants.ImageType.PACKAGE: - package_releaser = releaser.PackageReleaser( - build_info=build_info, - group_info=group_info, - sha=sha, - github_org=github_org, - workflow_ref=workflow_ref, - force_rebuild=force_rebuild, - ) - package_releaser.gather() - workflow_releaser = package_releaser - prompt = ( - "Are you sure you want GitHub Actions to build and upload these {} " - "Conan packages from sha={} ({})?\n{}\n".format( - len(package_releaser.release_list), - sha, - "forcing a rebuild" if force_rebuild else "unless up to date", - "\n".join(package_releaser.release_list), - ) - ) - else: - if force_rebuild: - raise click.BadOptionUsage( - "--force-rebuild", - "--force-rebuild only applies to Conan package releases", - ) - image_releaser = releaser.ImageReleaser( - build_info=build_info, - group_info=group_info, - sha=sha, - github_org=github_org, - workflow_ref=workflow_ref, - message=message, - ) - image_releaser.gather() - workflow_releaser = image_releaser - prompt = ( - "Are you sure you want GitHub Actions to build and push these {} " - "images from sha={} (as draft GitHub releases until they're " - "pushed)?\n{}\n".format( - len(image_releaser.releases), - sha, - "\n".join(r.name for r in image_releaser.releases), - ) - ) - if build_info.docker_org not in ( - constants.PUBLISH_DOCKER_ORG, - constants.TESTING_DOCKER_ORG, - ): - raise click.BadOptionUsage( - "--docker-org", - f"Releases go to {constants.PUBLISH_DOCKER_ORG} or " - f"{constants.TESTING_DOCKER_ORG}, not {build_info.docker_org}", - ) - if not click.confirm(prompt): - click.echo("Release cancelled.") - return - try: - workflow_releaser.release(dry_run=dry_run) - except RuntimeError as e: - raise click.ClickException(e.args[0]) - click.echo("Dry run: no release started." if dry_run else "Release started.") - - -@cli.command() -@common_image_options -@click.option( - "--sha", - "-s", - required=True, - help="The sha the releases are on.", -) -@click.option( - "--docker-org", - "-do", - required=True, - help="The Docker organization the images were pushed to.", -) -@click.option( - "--message", - "-m", - default="", - help="The release message, for releases without a draft.", -) -@click.option( - "--sbom-dir", - default="", - help="The directory holding the image SBOMs (from exportsboms) to attach " - "to the releases.", -) -@pass_build_info -def publishreleases( - build_info, - ci_image_type, - group, - version, - full_name, - target, - sha, - docker_org, - message, - sbom_dir, -): - """Publishes the draft GitHub release (creating its tag) of each released - image (GitHub Actions only), with the workflow's GITHUB_TOKEN, with the - image's SBOM attached.""" - if not utils.on_github_actions(): - raise click.UsageError( - "GitHub releases of images can only be published from GitHub Actions." - ) - build_info.set_org(docker_org) - group_info = get_group_info( - build_info, ci_image_type, group, version, full_name, target - ) - if group_info.type != constants.ImageType.IMAGE: - raise click.BadOptionUsage("--ci-image-type", "Only images get GitHub releases") - publisher = releaser.ReleasePublisher( - build_info=build_info, - group_info=group_info, - sha=sha, - message=message, - token=os.environ["GITHUB_TOKEN"], - repository=os.environ["GITHUB_REPOSITORY"], - sbom_dir=sbom_dir, - ) - try: - publisher.publish() - except FileNotFoundError as e: - raise click.ClickException(e.args[0]) - - -@cli.command() -@common_image_options -@click.option( - "--output-dir", - "-o", - required=True, - help="The directory to export the SBOMs to.", -) -@click.option( - "--docker-org", - "-do", - default="", - help="The Docker organization of the images, instead of the one derived " - "from the repository URI and branch.", -) -@pass_build_info -def exportsboms( - build_info, - ci_image_type, - group, - version, - full_name, - target, - output_dir, - docker_org, -): - """Exports the SBOM of each image, as built in the local Docker image - store, for publishreleases to attach to its GitHub release.""" - if docker_org: - build_info.set_org(docker_org) - group_info = get_group_info( - build_info, ci_image_type, group, version, full_name, target - ) - if group_info.type != constants.ImageType.IMAGE: - raise click.BadOptionUsage("--ci-image-type", "Only images have SBOMs") - releaser.export_sboms( - releaser.image_releases(build_info, group_info, message=""), output_dir - ) - - @cli.command() @click.pass_context @click.option( @@ -1014,3 +638,9 @@ def conandiff( # pylint: disable=too-many-statements if not found_outdated: click.secho("\nAll recipes are up to date!", fg="green") + + +# The release commands, in their own module (cli/release.py) +cli.add_command(release_commands.release) +cli.add_command(release_commands.publishreleases) +cli.add_command(release_commands.exportsboms) diff --git a/python/aswfdocker/cli/options.py b/python/aswfdocker/cli/options.py new file mode 100644 index 00000000..7e0b6915 --- /dev/null +++ b/python/aswfdocker/cli/options.py @@ -0,0 +1,88 @@ +# Copyright (c) Contributors to the aswf-docker Project. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +""" +Click options and helpers shared by the aswfdocker commands +""" + +import click + +from aswfdocker import aswfinfo, constants, groupinfo, index, utils + +pass_build_info = click.make_pass_decorator(aswfinfo.ASWFInfo) + + +def validate_image_name(_, __, value): + if value is None: + return None + try: + return utils.get_image_spec(value) + except RuntimeError as e: + raise click.BadParameter(e.args[0]) + + +def common_image_options(function): + function = click.option( + "--ci-image-type", + "-t", + required=False, + help="Builds a Conan package or a container image.", + type=click.Choice(constants.ImageType.__members__.keys(), case_sensitive=True), + )(function) + function = click.option( + "--group", + "-g", + required=False, + multiple=True, + help='The name of the group of images to build, e.g. "base" or "vfx",' + " can be specified multiple times.", + )(function) + function = click.option( + "--version", + "-v", + required=False, + multiple=True, + help='The major version number to build, e.g. "2027", can be specified multiple times.' + ' Can also be "all".', + )(function) + function = click.option( + "--full-name", + "-n", + callback=validate_image_name, + required=False, + help="The full image name, e.g. aswftesting/ci-common:2027-clang22 or aswf/ci-package-openexr:2027", + )(function) + function = click.option( + "--target", + "-tg", + required=False, + multiple=True, + help='An optional package or image name to build, e.g. "usd", can be specified multiple times.', + )(function) + return function + + +def get_group_info(build_info, ci_image_type, groups, versions, full_name, targets): + idx = index.Index() + if full_name: + org, image_type, target, version = full_name + versions = [version] + targets = [target] + try: + groups = [idx.get_group_from_image(image_type, target)] + except RuntimeError as e: + raise click.BadOptionUsage(option_name="--full-name", message=e.args[0]) + build_info.set_org(org) + else: + if ci_image_type is None: + image_type = constants.ImageType.IMAGE + else: + image_type = constants.ImageType[ci_image_type] + if not groups and targets: + groups = [idx.get_group_from_image(image_type, targets[0])] + group_info = groupinfo.GroupInfo( + type_=image_type, + names=groups, + versions=versions, + targets=targets, + ) + return group_info diff --git a/python/aswfdocker/cli/release.py b/python/aswfdocker/cli/release.py new file mode 100644 index 00000000..78576d84 --- /dev/null +++ b/python/aswfdocker/cli/release.py @@ -0,0 +1,326 @@ +# Copyright (c) Contributors to the aswf-docker Project. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +""" +aswfdocker release commands: starting the release workflows, and the +GitHub Actions-only steps they run +""" + +import os +import sys +import logging +import warnings + +import click + +from aswfdocker import constants, index, releaser, utils +from aswfdocker.cli.options import ( + common_image_options, + get_group_info, + pass_build_info, +) + +# Avoid pylint giving us grief for complex command lines +# pylint: disable=too-many-arguments +# pylint: disable=too-many-locals +# pylint: disable=too-many-branches + +logger = logging.getLogger("build-images") + + +@click.command() +@common_image_options +@click.option( + "--sha", + "-s", + help="The sha to create the release tag on, defaults to current sha.", +) +@click.option( + "--github-org", + "-o", + default=constants.MAIN_GITHUB_ASWF_ORG, + help="The GitHub organization/username to create the release on," + f" defaults to {constants.MAIN_GITHUB_ASWF_ORG}.", +) +@click.option( + "--docker-org", + "-do", + default=constants.TESTING_DOCKER_ORG, + help="The Docker organization/username to upload the Docker image to," + f" defaults to {constants.TESTING_DOCKER_ORG}.", +) +@click.option( + "--message", + "-m", + help="The release message of each image's GitHub release (images only).", +) +@click.option( + "--force-rebuild", + is_flag=True, + help="Conan packages only: rebuild and upload the selected packages even if " + "Artifactory already has an up-to-date binary.", +) +@click.option( + "--workflow-ref", + default="", + help="The branch to run the release workflow definition from, defaults to the " + "repository's default branch. Releases are always built from --sha.", +) +@click.option( + "--full-release", + is_flag=True, + help="Release every Conan package and image of the given years (-v), in " + "dependency order.", +) +@click.option("--dry-run", "-d", is_flag=True, help="Just logs what would happen.") +@pass_build_info +def release( # pylint: disable=too-many-arguments,too-many-locals + build_info, + ci_image_type, + group, + version, + full_name, + target, + sha, + github_org, + docker_org, + message, + force_rebuild, + workflow_ref, + full_release, + dry_run, +): + """Releases ci-image Docker images and/or Conan packages. + + Starts a GitHub Actions workflow building them from the commit, the only + place holding the Docker Hub and Artifactory credentials: images are + pushed to Docker Hub and each recorded as a GitHub release (and tag) on + the commit, Conan packages are uploaded to Artifactory. With + --full-release, every package and image of the given years is released, + in dependency order. + """ + + # Disable SSL unclosed ResourceWarning coming from GitHub + warnings.filterwarnings( + action="ignore", message="unclosed", category=ResourceWarning + ) + + if not sha: + if utils.get_current_branch() != "main": + click.secho( + "Cannot release from non-main branch! Specify --sha to create a release on a given commit.", + fg="red", + ) + sys.exit(1) + sha = utils.get_current_sha() + if docker_org: + build_info.set_org(docker_org) + + workflow_releaser: releaser.WorkflowReleaser + if full_release: + if ci_image_type or group or full_name or target: + raise click.BadOptionUsage( + "--full-release", + "--full-release releases every group: it can't be combined with " + "-t, -g, -n or --target", + ) + years = sorted(vi.version for vi in index.Index().iter_years()) + if list(version) == [constants.ALL]: + version = years + if not version or set(version) - set(years): + raise click.BadOptionUsage( + "--version", + f"--full-release needs years to release (-v), from: {', '.join(years)}", + ) + full_releaser = releaser.FullReleaser( + build_info=build_info, + versions=sorted(version), + sha=sha, + github_org=github_org, + workflow_ref=workflow_ref, + force_rebuild=force_rebuild, + message=message, + ) + full_releaser.gather() + workflow_releaser = full_releaser + prompt = ( + f"Are you sure you want GitHub Actions to release every Conan package " + f"({'forcing a rebuild' if force_rebuild else 'unless up to date'}) of " + f"{' '.join(sorted(version))} to {build_info.docker_org} from sha={sha}, " + f"and these {len(full_releaser.releases)} images (as draft GitHub " + "releases until they're pushed)?\n" + + "\n".join(r.name for r in full_releaser.releases) + + "\n" + ) + else: + group_info = get_group_info( + build_info, ci_image_type, group, version, full_name, target + ) + if group_info.type == constants.ImageType.PACKAGE: + package_releaser = releaser.PackageReleaser( + build_info=build_info, + group_info=group_info, + sha=sha, + github_org=github_org, + workflow_ref=workflow_ref, + force_rebuild=force_rebuild, + ) + package_releaser.gather() + workflow_releaser = package_releaser + prompt = ( + "Are you sure you want GitHub Actions to build and upload these {} " + "Conan packages from sha={} ({})?\n{}\n".format( + len(package_releaser.release_list), + sha, + "forcing a rebuild" if force_rebuild else "unless up to date", + "\n".join(package_releaser.release_list), + ) + ) + else: + if force_rebuild: + raise click.BadOptionUsage( + "--force-rebuild", + "--force-rebuild only applies to Conan package releases", + ) + image_releaser = releaser.ImageReleaser( + build_info=build_info, + group_info=group_info, + sha=sha, + github_org=github_org, + workflow_ref=workflow_ref, + message=message, + ) + image_releaser.gather() + workflow_releaser = image_releaser + prompt = ( + "Are you sure you want GitHub Actions to build and push these {} " + "images from sha={} (as draft GitHub releases until they're " + "pushed)?\n{}\n".format( + len(image_releaser.releases), + sha, + "\n".join(r.name for r in image_releaser.releases), + ) + ) + if build_info.docker_org not in ( + constants.PUBLISH_DOCKER_ORG, + constants.TESTING_DOCKER_ORG, + ): + raise click.BadOptionUsage( + "--docker-org", + f"Releases go to {constants.PUBLISH_DOCKER_ORG} or " + f"{constants.TESTING_DOCKER_ORG}, not {build_info.docker_org}", + ) + if not click.confirm(prompt): + click.echo("Release cancelled.") + return + try: + workflow_releaser.release(dry_run=dry_run) + except RuntimeError as e: + raise click.ClickException(e.args[0]) + click.echo("Dry run: no release started." if dry_run else "Release started.") + + +@click.command() +@common_image_options +@click.option( + "--sha", + "-s", + required=True, + help="The sha the releases are on.", +) +@click.option( + "--docker-org", + "-do", + required=True, + help="The Docker organization the images were pushed to.", +) +@click.option( + "--message", + "-m", + default="", + help="The release message, for releases without a draft.", +) +@click.option( + "--sbom-dir", + default="", + help="The directory holding the image SBOMs (from exportsboms) to attach " + "to the releases.", +) +@pass_build_info +def publishreleases( + build_info, + ci_image_type, + group, + version, + full_name, + target, + sha, + docker_org, + message, + sbom_dir, +): + """Publishes the draft GitHub release (creating its tag) of each released + image (GitHub Actions only), with the workflow's GITHUB_TOKEN, with the + image's SBOM attached.""" + if not utils.on_github_actions(): + raise click.UsageError( + "GitHub releases of images can only be published from GitHub Actions." + ) + build_info.set_org(docker_org) + group_info = get_group_info( + build_info, ci_image_type, group, version, full_name, target + ) + if group_info.type != constants.ImageType.IMAGE: + raise click.BadOptionUsage("--ci-image-type", "Only images get GitHub releases") + publisher = releaser.ReleasePublisher( + build_info=build_info, + group_info=group_info, + sha=sha, + message=message, + token=os.environ["GITHUB_TOKEN"], + repository=os.environ["GITHUB_REPOSITORY"], + sbom_dir=sbom_dir, + ) + try: + publisher.publish() + except FileNotFoundError as e: + raise click.ClickException(e.args[0]) + + +@click.command() +@common_image_options +@click.option( + "--output-dir", + "-o", + required=True, + help="The directory to export the SBOMs to.", +) +@click.option( + "--docker-org", + "-do", + default="", + help="The Docker organization of the images, instead of the one derived " + "from the repository URI and branch.", +) +@pass_build_info +def exportsboms( + build_info, + ci_image_type, + group, + version, + full_name, + target, + output_dir, + docker_org, +): + """Exports the SBOM of each image, as built in the local Docker image + store, for publishreleases to attach to its GitHub release.""" + if docker_org: + build_info.set_org(docker_org) + group_info = get_group_info( + build_info, ci_image_type, group, version, full_name, target + ) + if group_info.type != constants.ImageType.IMAGE: + raise click.BadOptionUsage("--ci-image-type", "Only images have SBOMs") + releaser.export_sboms( + releaser.image_releases(build_info, group_info, message=""), output_dir + )