Repository navigation
Expand file tree
/
Copy path_tools.py
More file actions
72 lines (59 loc) · 2.51 KB
/
Copy path_tools.py
File metadata and controls
72 lines (59 loc) · 2.51 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
"""Describe the build tools: the interpreter, the installed distributions and the build scripts."""
from __future__ import annotations
__lazy_modules__ = [
"packageurl",
f"{__spec__.parent}._components", # ty: ignore[missing-union-attribute] # imported modules have a spec
f"{__spec__.parent}._names", # ty: ignore[missing-union-attribute] # imported modules have a spec
]
from typing import TYPE_CHECKING, Final
from packageurl import PackageURL
from ._components import component_from_metadata, file_component
from ._names import purl, tool_ref
if TYPE_CHECKING:
from pathlib import PurePosixPath
from ._inputs import BuildFacts
from ._types import Component, Dependency
def tool_components(facts: BuildFacts) -> tuple[list[Component], list[Dependency]]:
interpreter = facts.interpreter
package = PackageURL(type="generic", name=interpreter.name, version=interpreter.version).to_string()
tools: Final[list[Component]] = [
{
"type": "platform",
"bom-ref": tool_ref(package),
"name": interpreter.name,
"version": interpreter.version,
"purl": package,
"properties": [{"name": "python:implementation", "value": interpreter.implementation}],
}
]
refs: Final[dict[str, str]] = {
distribution.name: tool_ref(purl(distribution.name, distribution.metadata.version))
for distribution in facts.distributions
}
dependencies: Final[list[Dependency]] = []
for distribution in sorted(facts.distributions, key=lambda entry: entry.name):
component = component_from_metadata(distribution.metadata)
component["bom-ref"] = refs[distribution.name]
if distribution.files:
component["components"] = [file_component(component["bom-ref"], row) for row in distribution.files]
tools.append(component)
dependencies.append({
"ref": component["bom-ref"],
"dependsOn": sorted(refs[name] for name in distribution.depends_on),
})
return tools, dependencies
def script_tool(path: PurePosixPath, sha256: str, version: str, link: str | None) -> Component:
tool: Final[Component] = {
"type": "application",
"bom-ref": tool_ref(str(path)),
"name": path.name,
"version": version,
"hashes": [{"alg": "SHA-256", "content": sha256}],
}
if link is not None:
tool["externalReferences"] = [{"type": "vcs", "url": link}]
return tool
__all__ = [
"script_tool",
"tool_components",
]