Repository navigation
Expand file tree
/
Copy path_embed.py
More file actions
112 lines (98 loc) · 4.74 KB
/
Copy path_embed.py
File metadata and controls
112 lines (98 loc) · 4.74 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
"""Build a wheel's SBOM and write it into the wheel."""
from __future__ import annotations
__lazy_modules__ = [
"pathlib",
"python_sbom._collect._content",
"python_sbom._collect._facts",
"python_sbom._collect._revision",
"python_sbom._collect._settings",
"python_sbom._collect._wheel",
"python_sbom._formats._json",
"python_sbom._model._bom",
"python_sbom._model._content",
"python_sbom._model._identity",
"python_sbom._model._wheel",
"python_sbom._wheel._reader",
"python_sbom._wheel._write",
]
from pathlib import PurePosixPath
from typing import TYPE_CHECKING
from python_sbom._collect._content import resolve_provider # simplint: ignore[private-import-path] # avoid root export
from python_sbom._collect._facts import collect_facts # simplint: ignore[private-import-path] # avoid root export
from python_sbom._collect._revision import revision_reader # simplint: ignore[private-import-path] # avoid root export
from python_sbom._collect._settings import ( # simplint: ignore[private-import-path] # avoid root export
settings_from_pyproject,
)
from python_sbom._collect._wheel import read_wheel # simplint: ignore[private-import-path] # avoid root export
from python_sbom._formats._json import dumps # simplint: ignore[private-import-path] # avoid root export
from python_sbom._model._bom import ( # simplint: ignore[private-import-path] # avoid root export
assemble,
checked_contributions,
)
from python_sbom._model._content import ProviderContext # simplint: ignore[private-import-path] # avoid root export
from python_sbom._model._identity import project_identity # simplint: ignore[private-import-path] # avoid root export
from python_sbom._model._wheel import wheel_artifact # simplint: ignore[private-import-path] # avoid root export
from python_sbom._wheel._reader import open_artifact # simplint: ignore[private-import-path] # avoid root export
from python_sbom._wheel._write import write_sbom # simplint: ignore[private-import-path] # avoid root export
if TYPE_CHECKING:
from collections.abc import Sequence
from pathlib import Path
from python_sbom._model._content import ( # simplint: ignore[private-import-path] # avoid root export
ContentProvider,
)
from python_sbom._model._inputs import ( # simplint: ignore[private-import-path] # avoid root export
BuildFacts,
Revision,
Settings,
)
def embed_sbom(
wheel: Path,
root: Path,
*,
provider: ContentProvider | str | None,
commit: str | None,
build_env: Sequence[str] | None,
) -> str:
settings = settings_from_pyproject(root)
content, scripts = resolve_provider(provider, settings, root)
facts = collect_facts(root, revision_reader(commit), path=build_env)
return embed_into(wheel, root, settings=settings, content=content, build_scripts=scripts, facts=facts)
def embed_into( # ruff: ignore[too-many-arguments] # what the hook and the command line each gather their own way
wheel: Path,
root: Path,
*,
settings: Settings,
content: ContentProvider,
build_scripts: Sequence[PurePosixPath],
facts: BuildFacts,
) -> str:
# the wheel stays open while its document is built, for its metadata, its bundled wheels and the provider to read,
# and is closed before it is rewritten
with open_artifact(wheel) as artifact:
tree = read_wheel(artifact, root, settings, build_scripts)
project = project_identity(tree.metadata, settings)
context = ProviderContext(root=root, settings=settings, identity=project, read=artifact.provider_read)
built = wheel_artifact(
tree,
project=project,
settings=settings,
bundled_info={
bundled.path: content.bundled_wheel(context, PurePosixPath(bundled.path)) for bundled in tree.bundled
},
)
contributions = checked_contributions(content.contributions(context), content)
return write_sbom(
wheel, built.sbom_file, dumps(assemble(built, project, settings, facts, contributions)), facts.epoch
)
def embed_built(wheel: Path, root: Path, revision: Revision | None, *, default_epoch: int | None = None) -> str:
# a backend plugin reads the revision before the build writes into the checkout, which could make it look dirty,
# and runs in the build's own environment, so the SBOM records the tools that built the wheel
settings = settings_from_pyproject(root)
content, scripts = resolve_provider(None, settings, root)
facts = collect_facts(root, lambda _root: revision, default_epoch=default_epoch)
return embed_into(wheel, root, settings=settings, content=content, build_scripts=scripts, facts=facts)
__all__ = [
"embed_built",
"embed_into",
"embed_sbom",
]