Python API¶
The Python API is what a project needs to add its own knowledge to the SBOM: the hatchling build hook and the content
provider it calls, with the types those take and return. Everything else, from embedding to validation, runs through the
Command line interface. The core is importable from python_sbom without hatchling; the hook lives in python_sbom.hatch.
Hatchling build hook¶
- class python_sbom.hatch.SbomBuildHook(root, config, build_config, metadata, directory, target_name, app=None)¶
Bases:
BuildHookInterfaceHatchling build hook that writes a CycloneDX SBOM into the wheel’s
.dist-info/sboms/, as PEP 770 describes.It runs once hatchling has written the wheel, so the SBOM describes the wheel’s final metadata and file name whatever other hooks changed. Options come from
[tool.python-sbom], the one placepython-sbom embedandpython-sbom archiveread them too. For an sdist it records the source revision, so a wheel built from that sdist still names its commit.- Variables:
PLUGIN_NAME – the hook’s name in
[tool.hatch.build.hooks]- Parameters:
- content_provider()¶
Return what the project adds to its SBOM.
Override it in a hatchling custom build hook to build the provider with what the hook knows; the
content-provideroption is then ignored.- Return type:
- Returns:
the
content-provideroption’s provider, or one that adds nothing
Content provider¶
- class python_sbom.ContentProvider¶
Bases:
objectAdds what a project knows beyond its metadata to its SBOM; subclass it and override what the project needs.
Name a subclass in the
content-provideroption, which the hook,python-sbom embedandpython-sbom archiveload, or return an instance frompython_sbom.hatch.SbomBuildHook.content_provider(). When the class lives in the project, the SBOM lists its module among the build tools.- bundled_wheel(context, wheel)¶
Describe what the project does with a wheel it bundles, as a file or unpacked into a directory.
- Parameters:
context (
ProviderContext) – the project and the artifact being describedwheel (
PurePosixPath) – the wheel’s path inside the artifact
- Return type:
- Returns:
its extra properties, and whether the project’s supplier supplies it; neither by default
- contributions(context)¶
Add components, their dependencies, root properties and build tools, before references and the serial derive.
- Parameters:
context (
ProviderContext) – the project and the artifact being described- Return type:
- Returns:
the contributions; none by default
- class python_sbom.ProviderContext(*, root, settings, identity, read)¶
Bases:
objectWhat a content provider may use about the build it contributes to.
- Parameters:
root (
Path) – the project directorysettings (
Settings) – the project’s optionsidentity (
ProjectIdentity) – how the project’s SBOMs name it, including the prefix of its propertiesread (
Callable[[PurePosixPath],bytes]) – reads a member of the artifact being described, forread()
- read(path)¶
Read a file of the artifact being described: a member of the built wheel or of the archive.
- Parameters:
path (
PurePosixPath) – the file’s path inside the artifact, as a bundled wheel’s path is given- Return type:
- Returns:
the file’s bytes
- Raises:
KeyError – when the artifact holds no such file
- class python_sbom.BundledWheelInfo(*, properties=list(), supplier=None)¶
Bases:
objectWhat a project adds to the component of one bundled wheel.
Both fields default to empty, so a provider names only what it adds.
- Parameters:
properties (
list[Property], default:list()) – properties after<prefix>:bundled-wheel, before those from the wheel’s metadatasupplier (
OrganizationalEntity|None, default:None) – the wheel’s supplier, orNoneto name none; only the project decides a bundled wheel’s supplier, never the wheel’s own metadata
- class python_sbom.Contributions(*, components=list(), dependencies=list(), root_properties=list(), tools=list())¶
Bases:
objectWhat a project adds to the document beyond its bundled wheels, merged before references and the serial are derived.
Each field defaults to empty, so a provider names only what it adds. The build fails when a component or tool lacks
type,bom-reforname, abom-refrepeats, theformula:andworkflow:refs of the build’s formulation included, a dependency names one that does not exist, or abom-refgets a second dependency entry, as the root does if a provider adds one, since python-sbom writes the root’s.- Parameters:
components (
list[Component], default:list()) – extra top-level components, such as vendored C libraries; the root depends on each, and a component’s own parts nest under itscomponentsdependencies (
list[Dependency], default:list()) – extra dependency entries, such as the dependencies of those componentsroot_properties (
list[Property], default:list()) – extra properties of the root component; their names must not start with the project’s prefix, which python-sbom’s own properties usetools (
list[Component], default:list()) – extra build tools
- class python_sbom.Settings(*, supplier, repository, default_branch, bundled_wheels, content_provider)¶
Bases:
objectThe project’s options, from
[tool.python-sbom].- Parameters:
supplier (
OrganizationalEntity|None) – the organization that supplies the project, orNoneto name nonerepository (
str|None) – the repository URL, orNoneto take the firstSource,RepositoryorGitHubproject URLdefault_branch (
str) – the branch in links to files in the repository when the build’s commit is unknown or has uncommitted changesbundled_wheels (
tuple[str,...]) – glob patterns, relative to the project directory, of wheels the project ships as data filescontent_provider (
str|None) –module:attributeof the content provider, orNone
- class python_sbom.ProjectIdentity(*, property_prefix, repository, serial_namespace)¶
Bases:
objectHow every SBOM of a project names it.
- Parameters:
property_prefix (
str) – the project’s normalized name, which starts the names of the properties python-sbom writes, such asvirtualenvinvirtualenv:bundled-wheelrepository (
str|None) – the repository URL, from the options or the firstSource,RepositoryorGitHubproject URL, orNoneserial_namespace (
UUID) – the UUID namespace of the serial numbers of the project’s SBOMs
Component types¶
These TypedDict types describe the JSON a provider builds. Each follows the CycloneDX 1.6 object of the
same name; the CycloneDX 1.6 reference describes every key.
- class python_sbom.Component¶
A CycloneDX 1.6 component, a distribution, an application, the interpreter or a file, as a dictionary with the keys the CycloneDX component object defines. python-sbom uses
type,bom-ref,name,version,purl,supplier(anOrganizationalEntity),authors(Contact),description,licenses(License),copyright,hashes(Hash),externalReferences(ExternalReference),properties(Property),evidence(Evidence) and the nestedcomponents.
- class python_sbom.Property¶
Bases:
TypedDictA name and value pair attached to a component or the document.
- class python_sbom.Dependency¶
Bases:
TypedDictThe components one component depends on.
- Variables:
ref (
str) – thebom-refof the dependent componentdependsOn (
NotRequired[list[str]]) – thebom-refvalues of its dependencies
- class python_sbom.OrganizationalEntity¶
Bases:
TypedDictAn organization that supplies or makes a component.
- class python_sbom.Contact¶
Bases:
TypedDictA person who wrote or maintains a component.
- Variables:
name (
NotRequired[str]) – the person’s nameemail (
NotRequired[str]) – the person’s email address
- class python_sbom.ExternalReference¶
Bases:
TypedDictA link from a component to something about it.
- Variables:
type (
str) – the CycloneDX reference type, such asvcsorwebsiteurl (
str) – the linkcomment (
NotRequired[str]) – what the link is, such as the Project-URL label it came from
- class python_sbom.License¶
Bases:
TypedDictOne license entry of a component: a license or an SPDX expression.
- Variables:
license (
NotRequired[LicenseInfo]) – the license, by SPDX id or by nameexpression (
NotRequired[str]) – an SPDX license expressionacknowledgement (
NotRequired[str]) –declaredorconcluded, for an expression
- class python_sbom.LicenseInfo¶
Bases:
TypedDictOne license, by SPDX id or by name.
- Variables:
id (
NotRequired[str]) – the SPDX license idname (
NotRequired[str]) – the license’s name, when it has no SPDX idacknowledgement (
str) –declaredwhen the metadata names it,concludedwhen python-sbom resolved it
- class python_sbom.Identity¶
Bases:
TypedDictThe evidence for one field of a component’s identity.
- Variables:
field (
str) – the field it supports, such aspurlorhashconfidence (
int) – from 0 to 1methods (
list[IdentityMethod]) – how it was established