Repository navigation
Expand file tree
/
Copy path_content.py
More file actions
134 lines (97 loc) · 5.28 KB
/
Copy path_content.py
File metadata and controls
134 lines (97 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""The content provider interface, the context it reads and the contributions it returns."""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Callable
from pathlib import Path, PurePosixPath
from ._identity import ProjectIdentity
from ._inputs import Settings
from ._types import Component, Dependency, OrganizationalEntity, Property
class ContentProvider:
"""
Adds what a project knows beyond its metadata to its SBOM; subclass it and override what the project needs.
Name a subclass in the ``content-provider`` option, which the hook, ``python-sbom embed`` and ``python-sbom
archive`` load, or return an instance from :meth:`python_sbom.hatch.SbomBuildHook.content_provider`. When the class
lives in the project, the SBOM lists its module among the build tools.
"""
# ruff: disable[unused-method-argument, no-self-use] # the defaults add nothing, whatever they are given
def bundled_wheel(self, context: ProviderContext, wheel: PurePosixPath) -> BundledWheelInfo:
"""
Describe what the project does with a wheel it bundles, as a file or unpacked into a directory.
:param context: the project and the artifact being described
:param wheel: the wheel's path inside the artifact
:returns: its extra properties, and whether the project's supplier supplies it; neither by default
"""
return BundledWheelInfo()
def contributions(self, context: ProviderContext) -> Contributions:
"""
Add components, their dependencies, root properties and build tools, before references and the serial derive.
:param context: the project and the artifact being described
:returns: the contributions; none by default
"""
return Contributions()
# ruff: enable[unused-method-argument, no-self-use]
@dataclass(frozen=True, slots=True, kw_only=True)
class BundledWheelInfo:
"""
What a project adds to the component of one bundled wheel.
Both fields default to empty, so a provider names only what it adds.
:param properties: properties after ``<prefix>:bundled-wheel``, before those from the wheel's metadata
:param supplier: the wheel's supplier, or ``None`` to name none; only the project decides a bundled wheel's
supplier, never the wheel's own metadata
"""
properties: list[Property] = field(default_factory=list)
supplier: OrganizationalEntity | None = None
@dataclass(frozen=True, slots=True, kw_only=True)
class Contributions:
"""
What 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-ref`` or ``name``, a ``bom-ref`` repeats, the ``formula:`` and ``workflow:`` refs of the build's
formulation included, a dependency names one that does not exist, or a ``bom-ref`` gets a second dependency entry,
as the root does if a provider adds one, since python-sbom writes the root's.
:param components: extra top-level components, such as vendored C libraries; the root depends on each, and a
component's own parts nest under its ``components``
:param dependencies: extra dependency entries, such as the dependencies of those components
:param root_properties: extra properties of the root component; their names must not start with the project's
prefix, which python-sbom's own properties use
:param tools: extra build tools
"""
components: list[Component] = field(default_factory=list)
dependencies: list[Dependency] = field(default_factory=list)
root_properties: list[Property] = field(default_factory=list)
tools: list[Component] = field(default_factory=list)
class ProviderContext:
"""
What a content provider may use about the build it contributes to.
:param root: the project directory
:param settings: the project's options
:param identity: how the project's SBOMs name it, including the prefix of its properties
:param read: reads a member of the artifact being described, for :meth:`read`
:ivar previous_contributions: contributions from earlier providers during ``contributions``
:ivar previous_bundled_wheel: properties and supplier from earlier providers for the current ``bundled_wheel``
"""
def __init__(
self, *, root: Path, settings: Settings, identity: ProjectIdentity, read: Callable[[PurePosixPath], bytes]
) -> None:
self.root = root
self.settings = settings
self.identity = identity
self._read = read
self.previous_contributions = Contributions()
self.previous_bundled_wheel = BundledWheelInfo()
def read(self, path: PurePosixPath) -> bytes:
"""
Read a file of the artifact being described: a member of the built wheel or of the archive.
:param path: the file's path inside the artifact, as a bundled wheel's path is given
:returns: the file's bytes
:raises KeyError: when the artifact holds no such file
"""
return self._read(path)
__all__ = [
"BundledWheelInfo",
"ContentProvider",
"Contributions",
"ProviderContext",
]