Repository navigation
Expand file tree
/
Copy path_source.py
More file actions
63 lines (47 loc) · 2.36 KB
/
Copy path_source.py
File metadata and controls
63 lines (47 loc) · 2.36 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
"""Read what the project tree adds: the copyright notice, the bundled wheels it selects and its build scripts."""
from __future__ import annotations
__lazy_modules__ = [
"pathlib",
"python_sbom",
"python_sbom._model._inputs",
"python_sbom._wheel._zip",
]
from pathlib import PurePath
from typing import TYPE_CHECKING, Final
from python_sbom import SbomError
from python_sbom._model._inputs import BuildScript # simplint: ignore[private-import-path] # avoid root export
from python_sbom._wheel._zip import sha256_hex # simplint: ignore[private-import-path] # avoid root export
if TYPE_CHECKING:
from collections.abc import Iterable
from pathlib import Path, PurePosixPath
from python_sbom._model._inputs import Settings # simplint: ignore[private-import-path] # avoid root export
def copyright_notice(root: Path, license_files: list[str]) -> str | None:
# the first Copyright line of the license files the project names, as they sit in the project
for license_file in license_files:
if (path := root / license_file).is_file():
for line in path.read_text(encoding="utf-8").splitlines():
if line.startswith("Copyright"):
return line
return None
def selected_wheels(root: Path, settings: Settings) -> list[str]:
wheels: Final[set[str]] = set()
for pattern in settings.bundled_wheels:
# a wheel outside the project is none the project ships, and pathlib refuses an absolute pattern outright
if (path := PurePath(pattern)).anchor or ".." in path.parts:
msg = f"`bundled-wheels` pattern {pattern!r} must be relative to the project, without `..`"
raise SbomError(msg)
wheels.update(wheel.relative_to(root).as_posix() for wheel in root.glob(pattern))
return sorted(wheels)
def hash_build_scripts(root: Path, paths: Iterable[PurePosixPath]) -> list[BuildScript]:
# str, so a Windows drive in an absolute path survives the join with the native root; a script named twice, by the
# caller and by the provider, is listed once
return [BuildScript(path=path, sha256=file_sha256(root / str(path))) for path in dict.fromkeys(paths)]
def file_sha256(path: Path) -> str:
with path.open("rb") as stream:
return sha256_hex(stream)
__all__ = [
"copyright_notice",
"file_sha256",
"hash_build_scripts",
"selected_wheels",
]