-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdynamic_dependencies.py
81 lines (65 loc) · 2.69 KB
/
dynamic_dependencies.py
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
import inspect
import os
import re
from typing import Any
from hatchling.metadata.plugin.interface import MetadataHookInterface
from packaging.requirements import Requirement
from packaging.utils import canonicalize_name
# The helper functions in this file are heavily inspired on the job layed
# out by the hatch plugin `hatch-requirements-txt`
COMMENT_RE = re.compile(r"(^|\s+)#.*$")
PIP_COMMAND_RE = re.compile(r"\s+(-[A-Za-z]|--[A-Za-z]+)")
def find_config_settings_in_hatchling() -> dict[str, Any]:
# Terrible workaround (their words, not mine) given by @virtuald
# https://github.com/pypa/hatch/issues/1072#issuecomment-2448985229
# Hopefully this will be fixed in the future
for frame_info in inspect.stack():
frame = frame_info.frame
module = inspect.getmodule(frame)
if (
module
and module.__name__.startswith("hatchling.build")
and "config_settings" in frame.f_locals
):
return frame.f_locals["config_settings"] or {}
return {}
def parse_requirements(requirements: list[str]) -> tuple[list[Requirement], list[str]]:
comments = []
parsed_requirements: list[Requirement] = []
for line in requirements:
if line.lstrip().startswith("#"):
comments.append(line)
elif line.lstrip().startswith("-"):
# Likely an argument to pip from a requirements.txt file intended for pip
# (e.g. from pip-compile)
pass
elif line:
# Strip comments from end of line
line = COMMENT_RE.sub("", line)
if "-" in line:
line = PIP_COMMAND_RE.split(line)[0]
req = Requirement(line)
req.name = canonicalize_name(req.name)
parsed_requirements.append(req)
return parsed_requirements, comments
def load_requirements(filename: str) -> list[str]:
if not os.path.isfile(filename):
raise FileNotFoundError(filename)
with open(filename, encoding="UTF-8") as fp:
contents = fp.read()
# Unfold lines ending with \
contents = re.sub(r"\\\s*\n", " ", contents)
parsed_requirements, _ = parse_requirements(contents.splitlines())
return [str(r) for r in parsed_requirements]
class DynamicDependenciesMetaDataHook(MetadataHookInterface):
def update(self, metadata):
if find_config_settings_in_hatchling().get("LEGACY_DEPS", "False").lower() in (
"true",
"on",
"1",
"y",
"yes",
):
metadata["dependencies"] = load_requirements("requirements-legacydeps.txt")
else:
metadata["dependencies"] = load_requirements("requirements.txt")