Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ repos:
priority: 3
args: ["--fix", "--unsafe-fixes", "--exit-non-zero-on-fix"]
- repo: https://github.com/gaborbernat/simplint-pre-commit
rev: "5f12b15e1199c42bdeb3e13ea1c8b45952fbd802" # frozen: v0.11.0
rev: "a12d8eaac290be24041a3e2778c636c56453722e" # frozen: v0.12.2
hooks:
- id: simplint
priority: 4
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ lint.ignore = [
"undocumented-public-init", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-method", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-nested-class", # undocumented-public-api (simplint SMP903) covers the public API instead
"unnecessary-regular-expression-compile", # conflicts with simplint inline-regex
]
lint.per-file-ignores."bench/**/*.py" = [
"import-private-name", # Benchmarks time the private entry points the public API wraps
Expand Down
5 changes: 3 additions & 2 deletions src/python_sbom/_formats/_spdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import re
from datetime import UTC, datetime
from types import MappingProxyType
from typing import TYPE_CHECKING, Final, NotRequired, TypedDict

from python_sbom._model._graph import walk_components # simplint: ignore[private-import-path] # avoid root export
Expand All @@ -29,7 +30,7 @@
)

# SPDX 2.3 JSON names every CycloneDX 1.6 hash algorithm, but by names of its own
_HASH_ALGORITHMS: Final[dict[str, str]] = {
_HASH_ALGORITHMS: Final[MappingProxyType[str, str]] = MappingProxyType({
"MD5": "MD5",
"SHA-1": "SHA1",
"SHA-256": "SHA256",
Expand All @@ -42,7 +43,7 @@
"BLAKE2b-384": "BLAKE2b-384",
"BLAKE2b-512": "BLAKE2b-512",
"BLAKE3": "BLAKE3",
}
})
_LICENSE_REF: Final[re.Pattern[str]] = re.compile(r"LicenseRef-[A-Za-z0-9.-]+")
_SPDX_ID_PATTERN: Final[re.Pattern[str]] = re.compile(r"[^A-Za-z0-9.]+")

Expand Down
6 changes: 5 additions & 1 deletion src/python_sbom/_model/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dataclasses import dataclass
from email.parser import HeaderParser
from email.policy import compat32
from types import MappingProxyType
from typing import TYPE_CHECKING, Final, Literal, TypeAlias

from packaging.metadata import parse_email
Expand Down Expand Up @@ -35,7 +36,10 @@
# the empty line that ends the headers, as the email parser ends them; a line of whitespace continues a header
_BODY: Final[re.Pattern[str]] = re.compile(r"^\r?\n", re.MULTILINE)
# unparsed keys a field by its header name, lowercased, which is the RawMetadata key with dashes except for these
_HEADERS: Final[dict[str, str]] = {"classifiers": "classifier", "license_files": "license-file"}
_HEADERS: Final[MappingProxyType[str, str]] = MappingProxyType({
"classifiers": "classifier",
"license_files": "license-file",
})


def parse_metadata(text: str) -> CoreMetadata:
Expand Down
5 changes: 3 additions & 2 deletions src/python_sbom/_model/_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import re
from types import MappingProxyType
from typing import Final

from packageurl import PackageURL
Expand All @@ -15,7 +16,7 @@
# the label normalization and mapping cyclonedx-py applies to Project-URL entries, so the same metadata yields the same
# reference types whichever generator produced the document, plus the well-known labels of PEP 753 it lacks; funding
# labels have no CycloneDX type and fall to "other"
_URL_LABEL_TO_REFERENCE_TYPE: Final[dict[str, str]] = {
_URL_LABEL_TO_REFERENCE_TYPE: Final[MappingProxyType[str, str]] = MappingProxyType({
"bugtracker": "issue-tracker",
"issuetracker": "issue-tracker",
"issues": "issue-tracker",
Expand All @@ -38,7 +39,7 @@
"repository": "vcs",
"github": "vcs",
"chat": "chat",
}
})


def sbom_property(project: str, key: str) -> str:
Expand Down
5 changes: 3 additions & 2 deletions src/python_sbom/_model/_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import base64
import binascii
from itertools import starmap
from types import MappingProxyType
from typing import TYPE_CHECKING, Final

from installer.records import RecordEntry
Expand All @@ -17,13 +18,13 @@
from collections.abc import Iterable

# the algorithms a wheel RECORD may name, as CycloneDX spells them
_ALGORITHMS: Final[dict[str, str]] = {
_ALGORITHMS: Final[MappingProxyType[str, str]] = MappingProxyType({
"md5": "MD5",
"sha1": "SHA-1",
"sha256": "SHA-256",
"sha384": "SHA-384",
"sha512": "SHA-512",
}
})


def record_rows(rows: Iterable[tuple[str, str, str]], where: str) -> list[RecordRow]:
Expand Down
4 changes: 2 additions & 2 deletions src/python_sbom/_validate/_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ def _license_problems(
# references serves the newest; Apache-2.0, see the LICENSE beside them. Format assertion is on, as in CycloneDX's own
# Draft 7 validation, and offline, so a reference missing from the registry fails instead of reaching the network
_SCHEMAS: Final[Traversable] = files(__package__) / "schemas"
_REFERENCED: Final[list[dict[str, Json]]] = [
_REFERENCED: Final[tuple[dict[str, Json], ...]] = tuple(
json.loads((_SCHEMAS / name).read_text(encoding="utf-8")) for name in ("spdx.schema.json", "jsf-0.82.schema.json")
]
)
_VALIDATOR: Final[Draft7Validator] = Draft7Validator(
json.loads((_SCHEMAS / "bom-1.6.schema.json").read_text(encoding="utf-8")),
validate_formats=True,
Expand Down
36 changes: 18 additions & 18 deletions src/python_sbom/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ def _wrapped() -> _Backend:
return _load(Path.cwd())


@cache
def _load(root: Path) -> _Backend:
# PEP 517 runs every hook from the project directory, which holds the pyproject.toml naming the backend
if (reference := settings_from_pyproject(root).build_backend) is None:
msg = (
f"{root / 'pyproject.toml'} sets build-backend = 'python_sbom.backend' but names no backend to wrap; set"
" build-backend in [tool.python-sbom], such as 'mesonpy'"
)
raise SbomError(msg)
module, _, attribute = reference.partition(":")
if module == __name__:
msg = f"[tool.python-sbom] build-backend names {reference}, which would wrap itself"
raise SbomError(msg)
loaded = importlib.import_module(module)
# PEP 517 requires every backend to define the two mandatory hooks
return cast("_Backend", reduce(getattr, attribute.split("."), loaded) if attribute else loaded)


def build_sdist(sdist_directory: str, config_settings: ConfigSettings = None) -> str:
"""
Build an sdist with the wrapped backend, then record the source revision in it.
Expand All @@ -91,24 +109,6 @@ def __getattr__(name: str) -> _Hook:
raise AttributeError(msg)


@cache
def _load(root: Path) -> _Backend:
# PEP 517 runs every hook from the project directory, which holds the pyproject.toml naming the backend
if (reference := settings_from_pyproject(root).build_backend) is None:
msg = (
f"{root / 'pyproject.toml'} sets build-backend = 'python_sbom.backend' but names no backend to wrap; set"
" build-backend in [tool.python-sbom], such as 'mesonpy'"
)
raise SbomError(msg)
module, _, attribute = reference.partition(":")
if module == __name__:
msg = f"[tool.python-sbom] build-backend names {reference}, which would wrap itself"
raise SbomError(msg)
loaded = importlib.import_module(module)
# PEP 517 requires every backend to define the two mandatory hooks
return cast("_Backend", reduce(getattr, attribute.split("."), loaded) if attribute else loaded)


__all__ = [
"ConfigSettings",
"build_sdist",
Expand Down
2 changes: 1 addition & 1 deletion tests/_seeder/src/seeder/embed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# the layout of virtualenv's seed/wheels/embed/__init__.py: another assignment comes before the table
BUNDLE_FOLDER = Path(__file__).absolute().parent # simplint: ignore[missing-final] # virtualenv fixture
BUNDLE_SUPPORT = { # simplint: ignore[missing-final] # virtualenv fixture
BUNDLE_SUPPORT = { # simplint: ignore[missing-final, mutable-module-constant] # read by ast as a dict literal
"3.14": {"pip": "pip-1.0-py3-none-any.whl"},
"3.15": {"pip": "pip-1.0-py3-none-any.whl"},
}
54 changes: 28 additions & 26 deletions tests/_validate/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,33 +180,35 @@ def test_component_without_bom_ref(describe: Describe) -> None:
assert validate_document(json.dumps(loaded).encode(), None) == ["components lack a bom-ref: ['nameless']"]


@pytest.mark.parametrize(
"mutate",
[
pytest.param(
lambda loaded: loaded["formulation"][0]["workflows"][0].pop("resourceReferences"),
id="workflow-without-resources",
),
pytest.param(
lambda loaded: loaded["formulation"][0]["workflows"][0]["resourceReferences"].append({
"externalReference": {"type": "other", "url": "https://example.org"}
}),
id="workflow-external-resource",
),
pytest.param(
lambda loaded: loaded["metadata"]["properties"].append({"name": "example:extra"}),
id="property-without-value",
),
pytest.param(
lambda loaded: loaded["metadata"]["component"]["properties"].append({"name": "example:extra"}),
id="root-property-without-value",
),
pytest.param(
lambda loaded: loaded["metadata"].update(supplier={"url": ["https://example.org"]}),
id="supplier-without-name",
),
],
_WORKFLOW_MUTATIONS: Final[tuple[pytest.ParameterSet, ...]] = (
pytest.param(
lambda loaded: loaded["formulation"][0]["workflows"][0].pop("resourceReferences"),
id="workflow-without-resources",
),
pytest.param(
lambda loaded: loaded["formulation"][0]["workflows"][0]["resourceReferences"].append({
"externalReference": {"type": "other", "url": "https://example.org"}
}),
id="workflow-external-resource",
),
)
_METADATA_MUTATIONS: Final[tuple[pytest.ParameterSet, ...]] = (
pytest.param(
lambda loaded: loaded["metadata"]["properties"].append({"name": "example:extra"}),
id="property-without-value",
),
pytest.param(
lambda loaded: loaded["metadata"]["component"]["properties"].append({"name": "example:extra"}),
id="root-property-without-value",
),
pytest.param(
lambda loaded: loaded["metadata"].update(supplier={"url": ["https://example.org"]}),
id="supplier-without-name",
),
)


@pytest.mark.parametrize("mutate", [*_WORKFLOW_MUTATIONS, *_METADATA_MUTATIONS])
def test_schema_valid_variations_pass(describe: Describe, mutate: Callable[[dict[str, Json]], None]) -> None:
loaded = json.loads(dumps(describe()))
mutate(loaded)
Expand Down
8 changes: 4 additions & 4 deletions tests/_wheel/test_bundled.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def test_bundled_wheel(tmp_path: Path, write_wheel: WriteWheel) -> None:
]


def _bundled(data: bytes, path: str) -> BundledWheel:
return read_bundled_wheels([path], lambda _: BytesIO(data))[0]


@pytest.mark.parametrize(
("files", "message"),
[
Expand Down Expand Up @@ -264,10 +268,6 @@ def test_vendor_manifest_needs_exact_pins(
_bundled(wheel.read_bytes(), "pip-1.0-py3-none-any.whl")


def _bundled(data: bytes, path: str) -> BundledWheel:
return read_bundled_wheels([path], lambda _: BytesIO(data))[0]


def test_bundled_wheels_come_in_the_order_given(tmp_path: Path, write_wheel: WriteWheel) -> None:
# read side by side, yet reported in order, the first broken wheel's error included
good = write_wheel(tmp_path / "pip-1.0-py3-none-any.whl", _PIP).read_bytes()
Expand Down
18 changes: 9 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,19 @@ def describe( # ruff: ignore[too-many-arguments] # each a part of the wheel a t
return describe


@pytest.fixture
def context_for(settings: Settings) -> ContextFor:
def context(root: Path, artifact: Path) -> ProviderContext:
identity = project_identity(parse_metadata("Name: Example\nVersion: 1.0\n"), settings)
return ProviderContext(root=root, settings=settings, identity=identity, read=artifact_read(artifact))

return context


def artifact_read(artifact: Path) -> Callable[[PurePosixPath], bytes]:
# opens the artifact for each read, as a test context outlives no build that would hold it open
def read(path: PurePosixPath) -> bytes:
with open_artifact(artifact) as opened:
return opened.provider_read(path)

return read


@pytest.fixture
def context_for(settings: Settings) -> ContextFor:
def context(root: Path, artifact: Path) -> ProviderContext:
identity = project_identity(parse_metadata("Name: Example\nVersion: 1.0\n"), settings)
return ProviderContext(root=root, settings=settings, identity=identity, read=artifact_read(artifact))

return context
23 changes: 12 additions & 11 deletions tests/examples/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import zipfile
from dataclasses import dataclass
from pathlib import Path
from types import MappingProxyType
from typing import TYPE_CHECKING, Final, Protocol

import pytest
Expand All @@ -23,18 +24,9 @@
_PROMPT: Final[re.Pattern[str]] = re.compile(r"^\$ ", re.MULTILINE)
# a frontend's isolated environment would install python-sbom from PyPI; the environment the test runs in holds the
# checkout's
_ADDED: Final[dict[tuple[str, ...], list[str]]] = {
_ADDED: Final[MappingProxyType[tuple[str, ...], list[str]]] = MappingProxyType({
("python", "-m", "build"): ["--no-isolation", "--skip-dependency-check"]
}


@dataclass(frozen=True, slots=True, kw_only=True)
class Played:
# an example project of the docs, copied and taken through one of its documented sessions: each command with the
# lines the docs show under it, and with the lines it printed
directory: Path
expected: list[tuple[str, list[str]]]
output: list[tuple[str, list[str]]]
})


class ReadSbom(Protocol):
Expand Down Expand Up @@ -72,6 +64,15 @@ def _run(command: str, directory: Path) -> list[str]:
return result.stdout.splitlines() if name == "python-sbom" else []


@dataclass(frozen=True, slots=True, kw_only=True)
class Played:
# an example project of the docs, copied and taken through one of its documented sessions: each command with the
# lines the docs show under it, and with the lines it printed
directory: Path
expected: list[tuple[str, list[str]]]
output: list[tuple[str, list[str]]]


@pytest.fixture
def read_sbom() -> ReadSbom:
def read(wheel: Path) -> Document:
Expand Down
8 changes: 4 additions & 4 deletions tests/hatch/test_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ def initialize(self, version, build_data):
"""


def sbom_of(wheel: Path, name: str = _SBOM) -> Document:
return json.loads(zipfile.ZipFile(wheel).read(name))


def test_wheel_carries_valid_sbom(project: Path, build_wheel: BuildWheel) -> None:
assert validate(build_wheel(project), frozenset({"MIT"})) == []

Expand All @@ -71,6 +67,10 @@ def test_sbom_lists_dependencies_a_later_hook_adds(retagged_wheel: Path) -> None
]


def sbom_of(wheel: Path, name: str = _SBOM) -> Document:
return json.loads(zipfile.ZipFile(wheel).read(name))


def test_sbom_names_the_retagged_wheel(retagged_wheel: Path) -> None:
references = sbom_of(retagged_wheel)["metadata"]["component"]["externalReferences"]
assert {reference["type"]: reference["url"] for reference in references}["attestation"] == (
Expand Down
Loading
Loading