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
|
"""Parse a pip-style requirements file for ``lpip install -r`` (ADR-0017)."""
from __future__ import annotations
from collections.abc import Iterator
from pathlib import Path
from packaging.requirements import InvalidRequirement, Requirement
from packaging.version import InvalidVersion, Version
from lpip.errors import (
CatalogExtrasNotSupportedError,
RequirementsFileError,
RequirementsParseError,
UnsupportedCatalogSpecifierError,
)
def iter_requirement_lines(text: str) -> Iterator[str]:
"""Yield non-empty requirement lines; drop comments and blanks."""
for raw in text.splitlines():
stripped = raw.strip()
if not stripped or stripped.startswith("#"):
continue
if " #" in stripped:
stripped = stripped.split(" #", 1)[0].strip()
if not stripped:
continue
yield stripped
def requirement_name(line: str) -> str:
"""Distribution name from a PEP 508 line. Reject pip option lines."""
if line.startswith("-"):
raise RequirementsFileError(
f"Unsupported requirements option: {line}",
hint="v1 install -r accepts PEP 508 requirement lines only "
"(no -r, -e, or other pip flags).",
)
try:
return Requirement(line).name
except InvalidRequirement as exc:
raise RequirementsParseError(line, detail=str(exc)) from exc
def parse_catalog_request(line: str) -> tuple[str, Version | None]:
"""Name plus optional == version for a catalog install (ADR-0018)."""
name = requirement_name(line)
try:
req = Requirement(line)
except InvalidRequirement as exc:
raise RequirementsParseError(line, detail=str(exc)) from exc
if req.extras:
raise CatalogExtrasNotSupportedError(name)
specs = list(req.specifier)
if not specs:
return name, None
if len(specs) == 1 and specs[0].operator in {"==", "==="}:
try:
return name, Version(specs[0].version)
except InvalidVersion as exc:
raise RequirementsParseError(line, detail=str(exc)) from exc
raise UnsupportedCatalogSpecifierError(line)
def load_requirement_lines(path: Path) -> tuple[str, ...]:
file = path.expanduser()
if not file.is_file():
raise RequirementsFileError(
f"Requirements file was not found: {file}",
hint="Pass -r lpip-requirements.txt (or another existing path).",
path=file,
)
return tuple(iter_requirement_lines(file.read_text(encoding="utf-8")))
|