Repository navigation
Expand file tree
/
Copy path_metadata.py
More file actions
112 lines (93 loc) · 3.88 KB
/
Copy path_metadata.py
File metadata and controls
112 lines (93 loc) · 3.88 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
"""Parse a distribution's core metadata, unvalidated, and read its fields as the distribution wrote them."""
from __future__ import annotations
__lazy_modules__ = [
"email.parser",
"email.policy",
"packaging.metadata",
]
import re
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
if TYPE_CHECKING:
from packaging.metadata import RawMetadata
# the fields python-sbom reads, by their key in packaging's RawMetadata
Field: TypeAlias = Literal[
"author",
"author_email",
"classifiers",
"home_page",
"license",
"license_expression",
"license_files",
"maintainer",
"maintainer_email",
"name",
"provides_extra",
"requires_dist",
"requires_python",
"summary",
"version",
]
# 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[MappingProxyType[str, str]] = MappingProxyType({
"classifiers": "classifier",
"license_files": "license-file",
})
def parse_metadata(text: str) -> CoreMetadata:
# not validated: the SBOM describes a distribution whose metadata breaks the specification as well, as written; the
# body holds the long description, which nothing reads, and parsing it took most of the time
headers: Final[str] = text[: match.start()] if (match := _BODY.search(text)) else text
raw, unparsed = parse_email(headers)
return CoreMetadata(raw=raw, unparsed=unparsed, headers=headers)
@dataclass(frozen=True, slots=True, kw_only=True)
class CoreMetadata:
raw: RawMetadata
unparsed: dict[str, list[str]]
# the header block as written, for the values packaging reshapes
headers: str = ""
@property
def name(self) -> str:
return self.first("name") or ""
def first(self, field: Field) -> str | None:
return next(iter(self.values(field)), None)
def values(self, field: Field) -> list[str]:
# packaging keeps a field it cannot take as declared, such as a single-use field given twice, out of raw and its
# values as written in unparsed, from where they are still described
match self.raw.get(field):
case str() as value:
return [value]
case list() as values:
return values
return self.unparsed.get(_HEADERS.get(field, field.replace("_", "-")), [])
@property
def version(self) -> str:
return self.first("version") or ""
def keywords(self) -> list[str]:
# packaging splits Keywords at commas and strips each keyword, so the header is read again, as the distribution
# wrote it, only when one is there; packaging parses the block with the same policy
if "keywords" in self.raw:
return HeaderParser(policy=compat32).parsestr(self.headers).get_all("keywords", [])
return self.unparsed.get("keywords", [])
def project_urls(self) -> list[tuple[str, str]]:
# a label given twice sends every Project-URL to unparsed, where each splits at its first comma as packaging
# splits them; an entry without a URL names nothing to reference
pairs: Final[list[tuple[str, str]]] = (
list(self.raw["project_urls"].items())
if "project_urls" in self.raw
else [
(label.strip(), url.strip())
for label, _, url in (entry.partition(",") for entry in self.unparsed.get("project-url", []))
]
)
return [(label, url) for label, url in pairs if url]
__all__ = [
"CoreMetadata",
"Field",
"parse_metadata",
]