Ichunjo/hatch-sbom

GitHub: Ichunjo/hatch-sbom

Hatchling 构建钩子插件,在 Python wheel 打包过程中自动生成 CycloneDX 格式的软件物料清单(SBOM)。

Stars: 0 | Forks: 0

# Hatch SBOM
CI/CD CI - Test Coverage Status CI - Lint CD - Publish
Package PyPI - Version PyPI - Python Version
Meta Hatch project linting - Ruff types - Mypy License - MIT
一个 Hatchling build hook 插件,用于在 wheel 创建过程中自动生成软件物料清单 (SBOM)。 ## 用法 要使用此插件,请在 `pyproject.toml` 的 `build-system` 中配置同时依赖 `hatchling` (>=1.28.0) 和 `hatch-sbom`。 对于 `requirements.txt` SBOM: ``` [build-system] requires = ["hatchling>=1.28.0", "hatch-sbom[cdx]"] build-backend = "hatchling.build" ``` 基础安装非常精简。请仅根据所选 source 使用的后端来安装对应的 extras: - `requirements`、`poetry`、`pipenv` 和 `environment` 使用 `cyclonedx-py`,需要 `hatch-sbom[cdx]`。 - `uv` 直接使用 `uv export`,需要 `hatch-sbom[uv]`。 - `pdm` 同时使用 `pdm export` 和 `cyclonedx-py`,因此需要 `hatch-sbom[pdm,cdx]`。 接下来,专门为 `wheel` 目标配置 build hook: ``` [tool.hatch.build.targets.wheel.hooks.sbom] source = "requirements" path = "requirements.txt" format = "json" # Optional, defaults to "json" spec-version = "1.6" # Optional, defaults to "1.6" ``` ### 支持的 Sources `source` 字段决定了 SBOM 的构建方式。 | Source | Requires | Backend | Path behavior | | -------------- | --------------------- | ---------------------------------------------- | ----------------------------------------------------------------- | | `requirements` | `hatch-sbom[cdx]` | `cyclonedx-py requirements` | 可选;存在时默认为 `requirements.txt`。 | | `poetry` | `hatch-sbom[cdx]` | `cyclonedx-py poetry` | 可选;默认为当前目录。 | | `pipenv` | `hatch-sbom[cdx]` | `cyclonedx-py pipenv` | 可选;默认为当前目录。 | | `environment` | `hatch-sbom[cdx]` | `cyclonedx-py environment` | 可选;默认为当前目录。 | | `uv` | `hatch-sbom[uv]` | `uv export` | 可选;默认为当前目录。需要 `uv.lock`。 | | `pdm` | `hatch-sbom[pdm,cdx]` | `pdm export`, 然后 `cyclonedx-py requirements` | 可选;默认为当前目录。需要 `pdm.lock`。 | `uv` source 仅支持 `json` 格式和 CycloneDX `1.5`。 ### 特定于 Source 的参数 通过创建一个以 source 名称命名的嵌套表,您可以将额外的参数传递给底层工具(例如 `uv export`、`pdm export` 或 `cyclonedx-py `)。 这对于传递像 `--without`、`--no-dev` 等标志非常有用。 例如,要在使用 Poetry 时忽略 `dev` 和 `test` 组: ``` [tool.hatch.build.targets.wheel.hooks.sbom.poetry] without = ["dev", "test"] # Appends `--without dev --without test` ``` 要在使用 uv 时包含所有 extras: ``` [tool.hatch.build.targets.wheel.hooks.sbom.uv] all-extras = true # Appends `--all-extras` ``` 您可以使用 `extra-args` 键来传递任意原始参数列表: ``` [tool.hatch.build.targets.wheel.hooks.sbom.pipenv] extra-args = ["--mc-type", "firmware"] ``` 生成的 SBOM 文件(例如 `sbom.cdx.json`)将自动放置在生成的 wheel 的 `.dist-info/sboms/` 目录中。
标签:Hatch插件, Python, SBOM生成, 供应链合规, 无后门, 跌倒检测, 软件物料清单, 逆向工具