-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathnative.py
167 lines (144 loc) · 4.81 KB
/
native.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from __future__ import annotations
import contextlib
import re
from importlib import metadata
from pathlib import Path
from typing import Any
import tomli
from packaging.requirements import Requirement
from packaging.utils import canonicalize_name
from licensecheck.session import session
from licensecheck.types import PackageInfo, ucstr
def get_reqs(
skipDependencies: list[ucstr],
extras: list[str],
requirementsPaths: list[Path],
pyproject: dict[str, Any],
) -> set[PackageInfo]:
using = "[unknown]"
# determine using based on file type
for requirementsPath in requirementsPaths:
try:
tomli.loads(requirementsPath.read_text("utf-8"))
if pyproject.get("project", {}).get("dependencies") is not None:
using = "PEP631"
if pyproject.get("tool", {}).get("poetry", {}).get("dependencies") is not None:
using = "poetry"
except tomli.TOMLDecodeError:
using = "requirements"
return do_get_reqs(
using=using,
skipDependencies=skipDependencies,
extras=extras,
pyproject=pyproject,
requirementsPaths=requirementsPaths,
)
def do_get_reqs(
using: str,
skipDependencies: list[ucstr],
extras: list[str],
pyproject: dict[str, Any],
requirementsPaths: list[Path],
) -> set[PackageInfo]:
"""Underlying machineary to get requirements.
Args:
----
using (str): use requirements, poetry or PEP631.
skipDependencies (list[str]): list of dependencies to skip.
extras (str | None): to-do
pyproject (dict[str, Any]): to-do
requirementsPaths (list[Path]): to-do
Returns:
-------
set[str]: set of requirement packages
"""
reqs = set()
extrasReqs = {}
def resolveReq(req: str, *, extra: bool = True) -> ucstr:
requirement = Requirement(req)
extras = {ucstr(extra) for extra in requirement.extras}
name = ucstr(canonicalize_name(requirement.name))
canonicalName = name
if len(extras) > 0:
canonicalName = ucstr(f"{name}[{next(iter(extras))}]")
# Avoid overwriting the initial mapping in extrasReqs, only overwrite when extra is True
if extra:
extrasReqs[name] = extras
return canonicalName if extra else name
def resolveExtraReq(extraReq: str) -> ucstr | None:
match = re.search(r"extra\s*==\s*[\"'](.*?)[\"']", extraReq)
if match is None:
return None
return ucstr(match.group(1))
if using == "poetry":
try:
project = pyproject["tool"]["poetry"]
reqLists = [project["dependencies"]]
except KeyError as error:
msg = "Could not find specification of requirements (pyproject.toml)."
raise RuntimeError(msg) from error
for extra in extras:
reqLists.append(
project.get("group", {extra: {"dependencies": {}}})[extra]["dependencies"]
)
reqLists.append(project.get("dev-dependencies", {}))
for reqList in reqLists:
for req in reqList:
reqs.add(resolveReq(req))
# PEP631
if using == "PEP631":
try:
project = pyproject["project"]
reqLists = [project["dependencies"]]
except KeyError as error:
msg = "Could not find specification of requirements (pyproject.toml)."
raise RuntimeError(msg) from error
for extra in extras:
extra_info = project.get("optional-dependencies", {}).get(extra)
reqLists.append(extra_info) if extra_info else ""
for reqList in reqLists:
for req in reqList:
reqs.add(resolveReq(req))
# Requirements
if using == "requirements":
for reqPath in requirementsPaths:
if not reqPath.exists():
msg = f"Could not find specification of requirements ({reqPath})."
raise RuntimeError(msg)
for _line in reqPath.read_text(encoding="utf-8").splitlines():
line = _line.rstrip("\\").strip()
if not line or line[0] in {"#", "-"}:
continue
reqs.add(resolveReq(line))
# Remove PYTHON if define as requirement
with contextlib.suppress(KeyError):
reqs.remove("PYTHON")
# Remove skip dependencies
for skipDependency in skipDependencies:
with contextlib.suppress(KeyError):
reqs.remove(skipDependency)
# Get Dependencies, 1 deep
requirementsWithDeps = reqs.copy()
def update_dependencies(dependency: str) -> None:
dep = resolveReq(dependency, extra=False)
req = resolveReq(requirement, extra=False)
extra = resolveExtraReq(dependency)
if extra is not None:
if req in extrasReqs and extra in extrasReqs.get(req, []):
requirementsWithDeps.add(dep)
else:
requirementsWithDeps.add(dep)
for requirement in reqs:
try:
pkgMetadata = metadata.metadata(requirement)
for dependency in pkgMetadata.get_all("Requires-Dist") or []:
update_dependencies(dependency)
except metadata.PackageNotFoundError: # noqa: PERF203
request = session.get(
f"https://pypi.org/pypi/{requirement.split('[')[0]}/json", timeout=60
)
response: dict = request.json()
requires_dist: list = response.get("info", {}).get("requires_dist", []) or []
for dependency in requires_dist:
update_dependencies(dependency)
return {PackageInfo(name=r.split("[")[0]) for r in requirementsWithDeps}