Repository navigation
Expand file tree
/
Copy path_document.py
More file actions
82 lines (69 loc) · 2.84 KB
/
Copy path_document.py
File metadata and controls
82 lines (69 loc) · 2.84 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
from __future__ import annotations
import json
import uuid
from collections import Counter
from datetime import UTC, datetime
from typing import TYPE_CHECKING, Final
from python_sbom._errors import SbomError
if TYPE_CHECKING:
from ._inputs import BuildFacts
from ._types import Body, Component, Document, Formula, Property
def with_header(body: Body, namespace: uuid.UUID) -> Document:
# a provider contributes refs of its own, so the document is checked before it is written rather than by validate
refs: Final[Counter[str]] = Counter()
pending: Final[list[Component]] = [
body["metadata"]["component"],
*body["components"],
*body["metadata"]["tools"]["components"],
]
while pending:
component = pending.pop()
refs[component["bom-ref"]] += 1
pending.extend(component.get("components", []))
if repeated := sorted(ref for ref, count in refs.items() if count > 1):
msg = f"bom-ref values repeat: {', '.join(repeated)}"
raise SbomError(msg)
named = {ref for entry in body["dependencies"] for ref in (entry["ref"], *entry.get("dependsOn", []))}
if unknown := sorted(named - refs.keys()):
msg = f"dependencies name bom-refs no component has: {', '.join(unknown)}"
raise SbomError(msg)
return {
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.6",
"serialNumber": f"urn:uuid:{uuid.uuid5(namespace, json.dumps(body, sort_keys=True))}",
"version": 1,
**body,
}
def contributed_properties(properties: list[Property], prefix: str) -> list[Property]:
if claimed := sorted(prop["name"] for prop in properties if prop["name"].startswith(f"{prefix}:")):
msg = f"contributed root properties {', '.join(claimed)} use the {prefix}: prefix python-sbom writes"
raise SbomError(msg)
return properties
def formulation(uid: str, name: str, tools: list[Component], output_ref: str, facts: BuildFacts) -> Formula:
formula: Formula = {
"bom-ref": f"formula:{uid}",
"workflows": [
{
"bom-ref": f"workflow:{uid}",
"uid": uid,
"name": name,
"taskTypes": ["build"],
"resourceReferences": [{"ref": tool["bom-ref"]} for tool in tools],
"outputs": [{"type": "artifact", "resource": {"ref": output_ref}}],
}
],
}
if facts.epoch_from_environment:
formula["workflows"][0]["inputs"] = [
{"environmentVars": [{"name": "SOURCE_DATE_EPOCH", "value": str(facts.epoch)}]}
]
return formula
def timestamp(facts: BuildFacts) -> str:
return datetime.fromtimestamp(facts.epoch, tz=UTC).isoformat()
__all__ = [
"contributed_properties",
"formulation",
"timestamp",
"with_header",
]