forceupdate 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- forceupdate/__init__.py +9 -0
- forceupdate/__main__.py +5 -0
- forceupdate/advisories/__init__.py +30 -0
- forceupdate/advisories/cvss.py +95 -0
- forceupdate/advisories/osv.py +584 -0
- forceupdate/checks/__init__.py +49 -0
- forceupdate/checks/base.py +98 -0
- forceupdate/checks/deprecated.py +115 -0
- forceupdate/checks/outdated.py +183 -0
- forceupdate/checks/priority.py +82 -0
- forceupdate/checks/vulns.py +201 -0
- forceupdate/cli.py +309 -0
- forceupdate/detect/__init__.py +5 -0
- forceupdate/detect/languages.py +266 -0
- forceupdate/detect/manifests.py +478 -0
- forceupdate/engine.py +313 -0
- forceupdate/models.py +510 -0
- forceupdate/net/__init__.py +20 -0
- forceupdate/net/cache.py +217 -0
- forceupdate/net/http.py +270 -0
- forceupdate/parsers/__init__.py +43 -0
- forceupdate/parsers/base.py +22 -0
- forceupdate/parsers/json_spans.py +123 -0
- forceupdate/parsers/lockfiles.py +231 -0
- forceupdate/parsers/npm.py +1257 -0
- forceupdate/parsers/python.py +1421 -0
- forceupdate/parsers/toml_spans.py +297 -0
- forceupdate/registries/__init__.py +52 -0
- forceupdate/registries/base.py +294 -0
- forceupdate/registries/npm.py +153 -0
- forceupdate/registries/pypi.py +242 -0
- forceupdate/report/__init__.py +359 -0
- forceupdate/report/json.py +34 -0
- forceupdate/report/markdown.py +169 -0
- forceupdate/report/terminal.py +376 -0
- forceupdate/sources/__init__.py +1 -0
- forceupdate/sources/base.py +45 -0
- forceupdate/sources/local.py +183 -0
- forceupdate/update/__init__.py +8 -0
- forceupdate/update/constraints.py +317 -0
- forceupdate/update/planner.py +531 -0
- forceupdate/versions/__init__.py +40 -0
- forceupdate/versions/base.py +229 -0
- forceupdate/versions/npm.py +432 -0
- forceupdate/versions/pypi.py +220 -0
- forceupdate-0.1.0.dist-info/METADATA +79 -0
- forceupdate-0.1.0.dist-info/RECORD +50 -0
- forceupdate-0.1.0.dist-info/WHEEL +4 -0
- forceupdate-0.1.0.dist-info/entry_points.txt +2 -0
- forceupdate-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,1421 @@
|
|
|
1
|
+
"""Les parseurs Python : manifestes, lockfiles, et le lien entre les deux.
|
|
2
|
+
|
|
3
|
+
Manifestes lus :
|
|
4
|
+
|
|
5
|
+
- ``requirements*.txt`` et ``requirements*.in`` (pip, pip-tools), avec les
|
|
6
|
+
``-r autre.txt`` et ``-c contraintes.txt`` suivis ;
|
|
7
|
+
- ``pyproject.toml`` : PEP 621 (``[project]``), PEP 735 (``[dependency-groups]``),
|
|
8
|
+
Poetry, ``[build-system] requires``, et les dépendances de dev de uv et PDM ;
|
|
9
|
+
- ``Pipfile`` et ``setup.cfg``.
|
|
10
|
+
|
|
11
|
+
``setup.py`` n'est pas lu : il faudrait l'exécuter. Le projet reçoit une remarque.
|
|
12
|
+
|
|
13
|
+
Lockfiles lus : ``uv.lock``, ``poetry.lock``, ``pdm.lock``, ``Pipfile.lock``, et le
|
|
14
|
+
``requirements*.txt`` compilé par pip-tools à partir du ``.in`` du même nom.
|
|
15
|
+
|
|
16
|
+
Pour un projet (``parse_project``) :
|
|
17
|
+
|
|
18
|
+
- les dépendances directes sont celles des manifestes, avec leur contrainte telle
|
|
19
|
+
qu'écrite et son emplacement exact (``Location``) ;
|
|
20
|
+
- leur version installée vient du lockfile qui va avec leur manifeste ;
|
|
21
|
+
- le reste du lockfile donne les transitives (``direct=False``). Le graphe de
|
|
22
|
+
``uv.lock`` et de ``poetry.lock`` dit leur scope : une transitive tirée par une
|
|
23
|
+
dépendance de dev est en ``dev``.
|
|
24
|
+
|
|
25
|
+
Les noms sont normalisés selon PEP 503 (``Django_REST.framework`` →
|
|
26
|
+
``django-rest-framework``). Une dépendance git, locale, par URL, membre du
|
|
27
|
+
workspace ou tirée d'un autre index que PyPI est « non vérifiable », avec la raison.
|
|
28
|
+
|
|
29
|
+
Repris de ``reference/argos_sca_parsers.py`` (L389-645), puis adapté :
|
|
30
|
+
``Dependency`` au lieu de ``SCAComponent``, contraintes gardées telles qu'écrites au
|
|
31
|
+
lieu d'une version devinée, emplacements, lockfiles reliés aux manifestes, et un
|
|
32
|
+
fichier invalide signalé (``ParseError``) au lieu d'une liste vide.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
import dataclasses
|
|
36
|
+
import json
|
|
37
|
+
import logging
|
|
38
|
+
import posixpath
|
|
39
|
+
import re
|
|
40
|
+
import tomllib
|
|
41
|
+
from collections import defaultdict
|
|
42
|
+
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
|
|
43
|
+
from dataclasses import dataclass, field
|
|
44
|
+
from typing import Any
|
|
45
|
+
|
|
46
|
+
from packaging.requirements import InvalidRequirement, Requirement
|
|
47
|
+
from packaging.specifiers import InvalidSpecifier, SpecifierSet
|
|
48
|
+
from packaging.utils import canonicalize_name
|
|
49
|
+
|
|
50
|
+
from forceupdate.detect.manifests import match_dependency_file
|
|
51
|
+
from forceupdate.models import Dependency, Ecosystem, Location, Notice, Project, Scope
|
|
52
|
+
from forceupdate.parsers.base import (
|
|
53
|
+
REASON_GIT,
|
|
54
|
+
REASON_LOCAL,
|
|
55
|
+
REASON_URL,
|
|
56
|
+
REASON_WORKSPACE,
|
|
57
|
+
ParseError,
|
|
58
|
+
ReadText,
|
|
59
|
+
)
|
|
60
|
+
from forceupdate.parsers.lockfiles import (
|
|
61
|
+
Declared,
|
|
62
|
+
Edge,
|
|
63
|
+
LockedPackage,
|
|
64
|
+
Lockfile,
|
|
65
|
+
dirname,
|
|
66
|
+
graph_starts,
|
|
67
|
+
in_workspace,
|
|
68
|
+
join,
|
|
69
|
+
relative_to,
|
|
70
|
+
transitives,
|
|
71
|
+
with_installed,
|
|
72
|
+
)
|
|
73
|
+
from forceupdate.parsers.toml_spans import StringSpan, TomlPath, string_spans
|
|
74
|
+
from forceupdate.versions.pypi import PYPI
|
|
75
|
+
|
|
76
|
+
logger = logging.getLogger(__name__)
|
|
77
|
+
|
|
78
|
+
_REQUIREMENTS_PATTERNS = (
|
|
79
|
+
"requirements*.txt",
|
|
80
|
+
"requirements/*.txt",
|
|
81
|
+
"requirements*.in",
|
|
82
|
+
"requirements/*.in",
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
# ---------------------------------------------------------------------------
|
|
87
|
+
# Scopes et raisons
|
|
88
|
+
# ---------------------------------------------------------------------------
|
|
89
|
+
|
|
90
|
+
_DEV_WORDS = frozenset({"lint", "linting", "doc", "docs", "typing", "types", "ci", "qa", "check"})
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _looks_dev(name: str) -> bool:
|
|
94
|
+
"""Vrai si un nom de fichier, de groupe ou d'extra désigne des outils de développement.
|
|
95
|
+
|
|
96
|
+
``requirements-dev.txt``, ``test``, ``docs``, ``lint`` le sont ; ``docker`` non.
|
|
97
|
+
"""
|
|
98
|
+
words = re.split(r"[^a-z0-9]+", name.lower())
|
|
99
|
+
return any(word.startswith(("dev", "test")) or word in _DEV_WORDS for word in words)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _group_scope(name: str) -> Scope:
|
|
103
|
+
"""Le scope d'un extra ou d'un groupe : ``dev`` pour les outils, sinon ``optional``."""
|
|
104
|
+
return Scope.DEV if _looks_dev(name) else Scope.OPTIONAL
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _file_scope(path: str) -> Scope:
|
|
108
|
+
"""Le scope d'un fichier ``requirements`` d'après son nom (``requirements-dev.txt``…)."""
|
|
109
|
+
return Scope.DEV if _looks_dev(posixpath.basename(path)) else Scope.RUNTIME
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _url_reason(url: str) -> str:
|
|
113
|
+
"""Pourquoi une dépendance par URL ou par chemin n'est pas vérifiable.
|
|
114
|
+
|
|
115
|
+
La raison ne cite jamais l'URL : elle peut contenir un token.
|
|
116
|
+
"""
|
|
117
|
+
lowered = url.strip().lower()
|
|
118
|
+
for vcs in ("git", "hg", "svn", "bzr"):
|
|
119
|
+
if lowered.startswith(f"{vcs}+"):
|
|
120
|
+
return f"dépendance {vcs}"
|
|
121
|
+
if lowered.startswith("file:") or not re.match(r"[a-z][a-z0-9+.-]*://", lowered):
|
|
122
|
+
return REASON_LOCAL
|
|
123
|
+
if lowered.endswith(".git"):
|
|
124
|
+
return REASON_GIT
|
|
125
|
+
return REASON_URL
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _index_reason(index: str) -> str:
|
|
129
|
+
return f"vient de l'index « {index} », pas de PyPI"
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
# ---------------------------------------------------------------------------
|
|
133
|
+
# PEP 508
|
|
134
|
+
# ---------------------------------------------------------------------------
|
|
135
|
+
|
|
136
|
+
_NAME_AND_EXTRAS = re.compile(
|
|
137
|
+
r"\s*(?P<name>[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?)\s*(?:\[(?P<extras>[^\]]*)\]\s*)?"
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
Locate = Callable[[int, int], Location | None]
|
|
141
|
+
"""Donne l'emplacement dans le fichier des caractères ``start`` à ``end`` d'un texte lu."""
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@dataclass(frozen=True)
|
|
145
|
+
class _Pep508:
|
|
146
|
+
"""Une dépendance PEP 508 découpée (``httpx[http2] >=0.27 ; python_version >= "3.11"``)."""
|
|
147
|
+
|
|
148
|
+
name: str
|
|
149
|
+
extras: frozenset[str]
|
|
150
|
+
constraint: str
|
|
151
|
+
span: tuple[int, int] | None
|
|
152
|
+
"""Position de la contrainte dans le texte ; ``None`` si on ne l'a pas retrouvée."""
|
|
153
|
+
url: str | None
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _split_pep508(text: str) -> _Pep508 | None:
|
|
157
|
+
"""Découpe une dépendance PEP 508 ; ``None`` si elle n'est pas valide.
|
|
158
|
+
|
|
159
|
+
Sans contrainte, ``span`` est vide, juste après le nom et les extras : c'est là
|
|
160
|
+
qu'une contrainte s'ajouterait.
|
|
161
|
+
"""
|
|
162
|
+
try:
|
|
163
|
+
requirement = Requirement(text)
|
|
164
|
+
except InvalidRequirement:
|
|
165
|
+
return None
|
|
166
|
+
head = _NAME_AND_EXTRAS.match(text)
|
|
167
|
+
if head is None:
|
|
168
|
+
return None
|
|
169
|
+
name = canonicalize_name(requirement.name)
|
|
170
|
+
extras = frozenset(canonicalize_name(extra) for extra in requirement.extras)
|
|
171
|
+
if requirement.url is not None:
|
|
172
|
+
return _Pep508(name, extras, "", None, requirement.url)
|
|
173
|
+
start = head.end()
|
|
174
|
+
if text.startswith("(", start):
|
|
175
|
+
start += 1
|
|
176
|
+
end = text.index(")", start)
|
|
177
|
+
else:
|
|
178
|
+
semicolon = text.find(";", start)
|
|
179
|
+
end = len(text) if semicolon == -1 else semicolon
|
|
180
|
+
while start < end and text[start].isspace():
|
|
181
|
+
start += 1
|
|
182
|
+
while end > start and text[end - 1].isspace():
|
|
183
|
+
end -= 1
|
|
184
|
+
constraint = text[start:end]
|
|
185
|
+
try:
|
|
186
|
+
found = SpecifierSet(constraint) == requirement.specifier
|
|
187
|
+
except InvalidSpecifier:
|
|
188
|
+
found = False
|
|
189
|
+
if not found:
|
|
190
|
+
return _Pep508(name, extras, str(requirement.specifier), None, None)
|
|
191
|
+
return _Pep508(name, extras, constraint, (start, end), None)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _edge_from_requirement(text: str) -> Edge | None:
|
|
195
|
+
"""Le nom et les extras au début d'une dépendance écrite (``PySocks (>=1.5.6)``)."""
|
|
196
|
+
head = _NAME_AND_EXTRAS.match(text)
|
|
197
|
+
if head is None:
|
|
198
|
+
return None
|
|
199
|
+
extras = head.group("extras") or ""
|
|
200
|
+
return (
|
|
201
|
+
canonicalize_name(head.group("name")),
|
|
202
|
+
frozenset(canonicalize_name(e.strip()) for e in extras.split(",") if e.strip()),
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
# ---------------------------------------------------------------------------
|
|
207
|
+
# Dépendances déclarées
|
|
208
|
+
# ---------------------------------------------------------------------------
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _declare_pep508(text: str, *, file: str, scope: Scope, locate: Locate) -> Declared | None:
|
|
212
|
+
parsed = _split_pep508(text)
|
|
213
|
+
if parsed is None:
|
|
214
|
+
return None
|
|
215
|
+
dependency = Dependency(
|
|
216
|
+
ecosystem=Ecosystem.PYPI,
|
|
217
|
+
name=parsed.name,
|
|
218
|
+
constraint=parsed.constraint,
|
|
219
|
+
scope=scope,
|
|
220
|
+
file=file,
|
|
221
|
+
location=locate(*parsed.span) if parsed.span is not None else None,
|
|
222
|
+
unverifiable_reason=_url_reason(parsed.url) if parsed.url is not None else None,
|
|
223
|
+
)
|
|
224
|
+
return Declared(dependency, parsed.extras)
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _declare_constraint(
|
|
228
|
+
name: str,
|
|
229
|
+
value: str,
|
|
230
|
+
span: StringSpan | None,
|
|
231
|
+
*,
|
|
232
|
+
file: str,
|
|
233
|
+
scope: Scope,
|
|
234
|
+
reason: str | None = None,
|
|
235
|
+
extras: frozenset[str] = frozenset(),
|
|
236
|
+
) -> Declared:
|
|
237
|
+
"""Une dépendance dont la contrainte est une chaîne TOML (Poetry, Pipfile)."""
|
|
238
|
+
start = len(value) - len(value.lstrip())
|
|
239
|
+
end = len(value.rstrip())
|
|
240
|
+
dependency = Dependency(
|
|
241
|
+
ecosystem=Ecosystem.PYPI,
|
|
242
|
+
name=canonicalize_name(name),
|
|
243
|
+
constraint=value[start:end],
|
|
244
|
+
scope=scope,
|
|
245
|
+
file=file,
|
|
246
|
+
location=span.location(start, end) if span is not None else None,
|
|
247
|
+
unverifiable_reason=reason,
|
|
248
|
+
)
|
|
249
|
+
return Declared(dependency, extras)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _declare_unversioned(
|
|
253
|
+
name: str, *, file: str, scope: Scope, reason: str | None, extras: frozenset[str]
|
|
254
|
+
) -> Declared:
|
|
255
|
+
"""Une dépendance sans contrainte écrite (dépendance git, chemin local…)."""
|
|
256
|
+
dependency = Dependency(
|
|
257
|
+
ecosystem=Ecosystem.PYPI,
|
|
258
|
+
name=canonicalize_name(name),
|
|
259
|
+
scope=scope,
|
|
260
|
+
file=file,
|
|
261
|
+
unverifiable_reason=reason,
|
|
262
|
+
)
|
|
263
|
+
return Declared(dependency, extras)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def _span_locator(span: StringSpan | None) -> Locate:
|
|
267
|
+
if span is None:
|
|
268
|
+
return lambda start, end: None
|
|
269
|
+
return span.location
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def _line_locator(line: int, offset: int, limit: int) -> Locate:
|
|
273
|
+
"""Les caractères d'un texte qui commence à la colonne ``offset`` de la ligne ``line``.
|
|
274
|
+
|
|
275
|
+
Au-delà de ``limit``, le texte continue sur la ligne suivante (``\\`` en fin de
|
|
276
|
+
ligne) : on ne donne pas d'emplacement.
|
|
277
|
+
"""
|
|
278
|
+
|
|
279
|
+
def locate(start: int, end: int) -> Location | None:
|
|
280
|
+
if offset + end > limit:
|
|
281
|
+
return None
|
|
282
|
+
return Location(line=line, column=offset + start, end_column=offset + end)
|
|
283
|
+
|
|
284
|
+
return locate
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def _extras(value: Any) -> frozenset[str]:
|
|
288
|
+
if not isinstance(value, list):
|
|
289
|
+
return frozenset()
|
|
290
|
+
return frozenset(canonicalize_name(extra) for extra in value if isinstance(extra, str))
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
# ---------------------------------------------------------------------------
|
|
294
|
+
# requirements.txt
|
|
295
|
+
# ---------------------------------------------------------------------------
|
|
296
|
+
|
|
297
|
+
_COMMENT = re.compile(r"(?:^|\s)#")
|
|
298
|
+
_INLINE_OPTION = re.compile(r"\s--?[A-Za-z]")
|
|
299
|
+
_OPTION = re.compile(r"(?P<name>--[A-Za-z][\w-]*|-[A-Za-z])\s*=?\s*(?P<argument>.*)")
|
|
300
|
+
_EGG = re.compile(r"[#&]egg=(?P<name>[A-Za-z0-9][A-Za-z0-9._-]*)")
|
|
301
|
+
_ARCHIVE_SUFFIXES = (".whl", ".zip", ".tar.gz", ".tgz", ".tar.bz2", ".tar.xz")
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
@dataclass
|
|
305
|
+
class _RequirementsFile:
|
|
306
|
+
"""Un ``requirements*.txt`` lu, avec ses ``-r`` et ``-c``."""
|
|
307
|
+
|
|
308
|
+
path: str
|
|
309
|
+
declared: list[Declared] = field(default_factory=list)
|
|
310
|
+
includes: list[str] = field(default_factory=list)
|
|
311
|
+
"""Fichiers inclus avec ``-r``, en chemins relatifs à la source."""
|
|
312
|
+
constraints: list[str] = field(default_factory=list)
|
|
313
|
+
"""Fichiers de contraintes donnés avec ``-c``."""
|
|
314
|
+
notices: list[Notice] = field(default_factory=list)
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def parse_requirements_txt(path: str, content: str) -> list[Dependency]:
|
|
318
|
+
"""Les dépendances écrites dans un ``requirements*.txt`` ou un ``.in`` de pip-tools.
|
|
319
|
+
|
|
320
|
+
Les ``-r`` et ``-c`` ne sont pas suivis ici (``parse_project`` le fait), ni les
|
|
321
|
+
autres options (``--index-url``…). ``-e .`` et les chemins locaux sans nom de
|
|
322
|
+
paquet sont le code du projet : ils sont sautés. Le scope vient du nom du
|
|
323
|
+
fichier (``requirements-dev.txt`` → ``dev``).
|
|
324
|
+
"""
|
|
325
|
+
parsed = _parse_requirements(path, content, _file_scope(path))
|
|
326
|
+
return [declared.dependency for declared in parsed.declared]
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def _parse_requirements(path: str, content: str, scope: Scope) -> _RequirementsFile:
|
|
330
|
+
parsed = _RequirementsFile(path)
|
|
331
|
+
for number, text, first_length in _logical_lines(content):
|
|
332
|
+
comment = _COMMENT.search(text)
|
|
333
|
+
if comment is not None:
|
|
334
|
+
text = text[: comment.start()]
|
|
335
|
+
body = text.strip()
|
|
336
|
+
if not body:
|
|
337
|
+
continue
|
|
338
|
+
if body.startswith("-"):
|
|
339
|
+
_requirements_option(parsed, body, scope)
|
|
340
|
+
continue
|
|
341
|
+
indent = len(text) - len(text.lstrip())
|
|
342
|
+
option = _INLINE_OPTION.search(text, indent)
|
|
343
|
+
requirement = text[indent : option.start() if option else len(text)].rstrip()
|
|
344
|
+
declared = _declare_pep508(
|
|
345
|
+
requirement,
|
|
346
|
+
file=path,
|
|
347
|
+
scope=scope,
|
|
348
|
+
locate=_line_locator(number, indent, first_length),
|
|
349
|
+
) or _declare_egg(requirement, file=path, scope=scope)
|
|
350
|
+
if declared is not None:
|
|
351
|
+
parsed.declared.append(declared)
|
|
352
|
+
elif not _looks_like_path_or_url(requirement):
|
|
353
|
+
parsed.notices.append(
|
|
354
|
+
Notice(
|
|
355
|
+
file=path,
|
|
356
|
+
message=f"ligne {number} : « {requirement} » n'est pas une dépendance valide.",
|
|
357
|
+
)
|
|
358
|
+
)
|
|
359
|
+
return parsed
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def _logical_lines(content: str) -> Iterator[tuple[int, str, int]]:
|
|
363
|
+
"""Les lignes logiques d'un ``requirements.txt`` : ``(numéro, texte, longueur de la 1re)``.
|
|
364
|
+
|
|
365
|
+
Une ligne finie par ``\\`` continue sur la suivante, comme pour pip. Le texte
|
|
366
|
+
commence en colonne 0 de la ligne ``numéro`` ; au-delà de la longueur de la
|
|
367
|
+
première ligne, il vient des lignes suivantes.
|
|
368
|
+
"""
|
|
369
|
+
start = 0
|
|
370
|
+
parts: list[str] = []
|
|
371
|
+
first_length = 0
|
|
372
|
+
for number, raw in enumerate(content.split("\n"), start=1):
|
|
373
|
+
line = raw.rstrip("\r")
|
|
374
|
+
if line.endswith("\\") and not _COMMENT.match(line.lstrip()):
|
|
375
|
+
if not parts:
|
|
376
|
+
start, first_length = number, len(line) - 1
|
|
377
|
+
parts.append(line[:-1])
|
|
378
|
+
continue
|
|
379
|
+
if parts:
|
|
380
|
+
parts.append(line)
|
|
381
|
+
yield start, "".join(parts), first_length
|
|
382
|
+
parts = []
|
|
383
|
+
else:
|
|
384
|
+
yield number, line, len(line)
|
|
385
|
+
if parts:
|
|
386
|
+
yield start, "".join(parts), first_length
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def _requirements_option(parsed: _RequirementsFile, body: str, scope: Scope) -> None:
|
|
390
|
+
"""Traite une ligne d'option : ``-r``, ``-c`` et ``-e`` ; les autres sont ignorées."""
|
|
391
|
+
match = _OPTION.match(body)
|
|
392
|
+
if match is None:
|
|
393
|
+
return
|
|
394
|
+
name = match.group("name")
|
|
395
|
+
words = match.group("argument").split()
|
|
396
|
+
argument = words[0] if words else ""
|
|
397
|
+
if name in ("-e", "--editable"):
|
|
398
|
+
declared = _declare_egg(argument, file=parsed.path, scope=scope)
|
|
399
|
+
if declared is not None:
|
|
400
|
+
parsed.declared.append(declared)
|
|
401
|
+
return
|
|
402
|
+
if name not in ("-r", "--requirement", "-c", "--constraint") or not argument:
|
|
403
|
+
return
|
|
404
|
+
target = _include_path(parsed.path, argument)
|
|
405
|
+
if target is None:
|
|
406
|
+
parsed.notices.append(
|
|
407
|
+
Notice(
|
|
408
|
+
file=parsed.path,
|
|
409
|
+
message=f"« {name} {argument} » : hors du dossier analysé, pas suivi.",
|
|
410
|
+
)
|
|
411
|
+
)
|
|
412
|
+
elif name in ("-r", "--requirement"):
|
|
413
|
+
parsed.includes.append(target)
|
|
414
|
+
else:
|
|
415
|
+
parsed.constraints.append(target)
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
def _include_path(including: str, argument: str) -> str | None:
|
|
419
|
+
"""Le chemin, relatif à la source, d'un fichier donné à ``-r`` ou ``-c``.
|
|
420
|
+
|
|
421
|
+
Relatif au dossier du fichier qui l'inclut, comme pour pip. ``None`` pour une
|
|
422
|
+
URL, un chemin absolu, ou un chemin qui sort de la source.
|
|
423
|
+
"""
|
|
424
|
+
argument = argument.replace("\\", "/")
|
|
425
|
+
if "://" in argument or argument.startswith("/") or re.match(r"[A-Za-z]:/", argument):
|
|
426
|
+
return None
|
|
427
|
+
path = posixpath.normpath(posixpath.join(posixpath.dirname(including), argument))
|
|
428
|
+
if path == ".." or path.startswith("../"):
|
|
429
|
+
return None
|
|
430
|
+
return path
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
def _declare_egg(text: str, *, file: str, scope: Scope) -> Declared | None:
|
|
434
|
+
"""Une dépendance par URL ou chemin nommée par ``#egg=`` (``-e git+https://…#egg=nom``)."""
|
|
435
|
+
egg = _EGG.search(text)
|
|
436
|
+
if egg is None:
|
|
437
|
+
return None
|
|
438
|
+
return _declare_unversioned(
|
|
439
|
+
egg.group("name"), file=file, scope=scope, reason=_url_reason(text), extras=frozenset()
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
def _looks_like_path_or_url(text: str) -> bool:
|
|
444
|
+
"""Vrai pour un chemin local ou une URL sans nom de paquet (``./libs/x``, ``https://…``)."""
|
|
445
|
+
return (
|
|
446
|
+
"://" in text
|
|
447
|
+
or text.startswith((".", "/", "~", "\\"))
|
|
448
|
+
or re.match(r"[A-Za-z]:[\\/]", text) is not None
|
|
449
|
+
or text.lower().endswith(_ARCHIVE_SUFFIXES)
|
|
450
|
+
)
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
# ---------------------------------------------------------------------------
|
|
454
|
+
# pyproject.toml, Pipfile, setup.cfg
|
|
455
|
+
# ---------------------------------------------------------------------------
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def _load_toml(content: str) -> dict[str, Any]:
|
|
459
|
+
try:
|
|
460
|
+
return tomllib.loads(content)
|
|
461
|
+
except tomllib.TOMLDecodeError as err:
|
|
462
|
+
raise ParseError(f"TOML invalide ({err}).") from err
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
def _table(data: Any, *keys: str) -> dict[str, Any]:
|
|
466
|
+
"""La table ``data[k1][k2]…`` ; une table vide si elle n'existe pas ou n'en est pas une."""
|
|
467
|
+
for key in keys:
|
|
468
|
+
if not isinstance(data, dict):
|
|
469
|
+
return {}
|
|
470
|
+
data = data.get(key)
|
|
471
|
+
return data if isinstance(data, dict) else {}
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
@dataclass
|
|
475
|
+
class _Pyproject:
|
|
476
|
+
"""Un ``pyproject.toml`` lu."""
|
|
477
|
+
|
|
478
|
+
declared: list[Declared]
|
|
479
|
+
notices: list[Notice]
|
|
480
|
+
tools: frozenset[str]
|
|
481
|
+
"""Les outils qui ont une table ``[tool.x]`` : ``poetry``, ``pdm``, ``uv``…"""
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
def parse_pyproject_toml(path: str, content: str) -> list[Dependency]:
|
|
485
|
+
"""Les dépendances d'un ``pyproject.toml``.
|
|
486
|
+
|
|
487
|
+
Dans l'ordre : ``[project]`` (dépendances, puis extras), ``[dependency-groups]``,
|
|
488
|
+
les dépendances de dev de uv et PDM, Poetry (``[tool.poetry.dependencies]``,
|
|
489
|
+
``dev-dependencies``, groupes), et enfin ``[build-system] requires``. Le projet
|
|
490
|
+
qui se cite lui-même (``app[all]`` dans les extras de ``app``) est sauté.
|
|
491
|
+
|
|
492
|
+
Lève ``ParseError`` si le TOML est invalide.
|
|
493
|
+
"""
|
|
494
|
+
return [declared.dependency for declared in _parse_pyproject(path, content).declared]
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
def _parse_pyproject(
|
|
498
|
+
path: str, content: str, *, inherited_sources: Mapping[str, Any] | None = None
|
|
499
|
+
) -> _Pyproject:
|
|
500
|
+
data = _load_toml(content)
|
|
501
|
+
spans = string_spans(content)
|
|
502
|
+
declared: list[Declared] = []
|
|
503
|
+
notices: list[Notice] = []
|
|
504
|
+
|
|
505
|
+
def pep508_list(keys: TomlPath, values: Any, scope: Scope) -> None:
|
|
506
|
+
if not isinstance(values, list):
|
|
507
|
+
return
|
|
508
|
+
for index, value in enumerate(values):
|
|
509
|
+
if not isinstance(value, str):
|
|
510
|
+
continue # {include-group = "…"} : ses dépendances sont lues avec son groupe
|
|
511
|
+
item = _declare_pep508(
|
|
512
|
+
value, file=path, scope=scope, locate=_span_locator(spans.get((*keys, index)))
|
|
513
|
+
)
|
|
514
|
+
if item is None:
|
|
515
|
+
notices.append(
|
|
516
|
+
Notice(file=path, message=f"« {value} » n'est pas une dépendance valide.")
|
|
517
|
+
)
|
|
518
|
+
else:
|
|
519
|
+
declared.append(item)
|
|
520
|
+
|
|
521
|
+
project = _table(data, "project")
|
|
522
|
+
pep508_list(("project", "dependencies"), project.get("dependencies"), Scope.RUNTIME)
|
|
523
|
+
for extra, values in _table(project, "optional-dependencies").items():
|
|
524
|
+
pep508_list(("project", "optional-dependencies", extra), values, _group_scope(extra))
|
|
525
|
+
for group, values in _table(data, "dependency-groups").items():
|
|
526
|
+
pep508_list(("dependency-groups", group), values, _group_scope(group))
|
|
527
|
+
uv_dev = _table(data, "tool", "uv").get("dev-dependencies")
|
|
528
|
+
pep508_list(("tool", "uv", "dev-dependencies"), uv_dev, Scope.DEV)
|
|
529
|
+
for group, values in _table(data, "tool", "pdm", "dev-dependencies").items():
|
|
530
|
+
pep508_list(("tool", "pdm", "dev-dependencies", group), values, Scope.DEV)
|
|
531
|
+
|
|
532
|
+
poetry = _table(data, "tool", "poetry")
|
|
533
|
+
pep621_names = {item.dependency.name for item in declared}
|
|
534
|
+
sections: list[tuple[tuple[str, ...], Scope]] = [
|
|
535
|
+
(("tool", "poetry", "dependencies"), Scope.RUNTIME),
|
|
536
|
+
(("tool", "poetry", "dev-dependencies"), Scope.DEV),
|
|
537
|
+
]
|
|
538
|
+
for group in _table(poetry, "group"):
|
|
539
|
+
scope = Scope.RUNTIME if group == "main" else _group_scope(group)
|
|
540
|
+
sections.append((("tool", "poetry", "group", group, "dependencies"), scope))
|
|
541
|
+
poetry_reasons: dict[str, str] = {}
|
|
542
|
+
for keys, scope in sections:
|
|
543
|
+
for raw_name, spec in _table(data, *keys).items():
|
|
544
|
+
name = canonicalize_name(raw_name)
|
|
545
|
+
if name == "python":
|
|
546
|
+
continue
|
|
547
|
+
items = _poetry_dependency(raw_name, spec, keys, spans, file=path, scope=scope)
|
|
548
|
+
if scope is Scope.RUNTIME and name in pep621_names:
|
|
549
|
+
# Poetry 2 : l'entrée complète une dépendance de [project] (source…).
|
|
550
|
+
for item in items:
|
|
551
|
+
if item.dependency.unverifiable_reason is not None:
|
|
552
|
+
poetry_reasons[name] = item.dependency.unverifiable_reason
|
|
553
|
+
continue
|
|
554
|
+
declared.extend(items)
|
|
555
|
+
|
|
556
|
+
build = _table(data, "build-system").get("requires")
|
|
557
|
+
pep508_list(("build-system", "requires"), build, Scope.BUILD)
|
|
558
|
+
|
|
559
|
+
names = (project.get("name"), poetry.get("name"))
|
|
560
|
+
own = {canonicalize_name(name) for name in names if isinstance(name, str)}
|
|
561
|
+
sources = {canonicalize_name(key): value for key, value in (inherited_sources or {}).items()}
|
|
562
|
+
sources.update(
|
|
563
|
+
(canonicalize_name(key), value)
|
|
564
|
+
for key, value in _table(data, "tool", "uv", "sources").items()
|
|
565
|
+
)
|
|
566
|
+
result = []
|
|
567
|
+
for item in declared:
|
|
568
|
+
if item.dependency.name in own:
|
|
569
|
+
continue
|
|
570
|
+
reason = poetry_reasons.get(item.dependency.name) or _uv_source_reason(
|
|
571
|
+
sources.get(item.dependency.name)
|
|
572
|
+
)
|
|
573
|
+
if reason is not None and item.dependency.scope is not Scope.BUILD:
|
|
574
|
+
item = Declared(
|
|
575
|
+
dataclasses.replace(item.dependency, unverifiable_reason=reason), item.extras
|
|
576
|
+
)
|
|
577
|
+
result.append(item)
|
|
578
|
+
return _Pyproject(
|
|
579
|
+
declared=result,
|
|
580
|
+
notices=notices,
|
|
581
|
+
tools=frozenset(_table(data, "tool")),
|
|
582
|
+
)
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
def _poetry_dependency(
|
|
586
|
+
raw_name: str,
|
|
587
|
+
spec: Any,
|
|
588
|
+
keys: TomlPath,
|
|
589
|
+
spans: Mapping[TomlPath, StringSpan],
|
|
590
|
+
*,
|
|
591
|
+
file: str,
|
|
592
|
+
scope: Scope,
|
|
593
|
+
) -> list[Declared]:
|
|
594
|
+
"""Une dépendance Poetry : ``"^2.31"``, ``{version = "^8.1", optional = true}``, ou une
|
|
595
|
+
liste de ces tables (une contrainte par version de Python)."""
|
|
596
|
+
if isinstance(spec, str):
|
|
597
|
+
return [
|
|
598
|
+
_declare_constraint(
|
|
599
|
+
raw_name, spec, spans.get((*keys, raw_name)), file=file, scope=scope
|
|
600
|
+
)
|
|
601
|
+
]
|
|
602
|
+
if isinstance(spec, dict):
|
|
603
|
+
tables = [(spec, (*keys, raw_name))]
|
|
604
|
+
elif isinstance(spec, list):
|
|
605
|
+
tables = [
|
|
606
|
+
(item, (*keys, raw_name, i)) for i, item in enumerate(spec) if isinstance(item, dict)
|
|
607
|
+
]
|
|
608
|
+
else:
|
|
609
|
+
return []
|
|
610
|
+
items = []
|
|
611
|
+
for table, table_keys in tables:
|
|
612
|
+
item_scope = (
|
|
613
|
+
Scope.OPTIONAL if table.get("optional") is True and scope is Scope.RUNTIME else scope
|
|
614
|
+
)
|
|
615
|
+
reason = _poetry_reason(table)
|
|
616
|
+
extras = _extras(table.get("extras"))
|
|
617
|
+
version = table.get("version")
|
|
618
|
+
if isinstance(version, str):
|
|
619
|
+
span = spans.get((*table_keys, "version"))
|
|
620
|
+
items.append(
|
|
621
|
+
_declare_constraint(
|
|
622
|
+
raw_name,
|
|
623
|
+
version,
|
|
624
|
+
span,
|
|
625
|
+
file=file,
|
|
626
|
+
scope=item_scope,
|
|
627
|
+
reason=reason,
|
|
628
|
+
extras=extras,
|
|
629
|
+
)
|
|
630
|
+
)
|
|
631
|
+
else:
|
|
632
|
+
items.append(
|
|
633
|
+
_declare_unversioned(
|
|
634
|
+
raw_name, file=file, scope=item_scope, reason=reason, extras=extras
|
|
635
|
+
)
|
|
636
|
+
)
|
|
637
|
+
return items
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
def _poetry_reason(table: Mapping[str, Any]) -> str | None:
|
|
641
|
+
if "git" in table:
|
|
642
|
+
return REASON_GIT
|
|
643
|
+
if "path" in table:
|
|
644
|
+
return REASON_LOCAL
|
|
645
|
+
if isinstance(table.get("url"), str):
|
|
646
|
+
return _url_reason(table["url"])
|
|
647
|
+
source = table.get("source")
|
|
648
|
+
if isinstance(source, str) and source.lower() != "pypi":
|
|
649
|
+
return _index_reason(source)
|
|
650
|
+
return None
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
def _uv_source_reason(source: Any) -> str | None:
|
|
654
|
+
"""Pourquoi une source de ``[tool.uv.sources]`` rend la dépendance non vérifiable."""
|
|
655
|
+
entries = source if isinstance(source, list) else [source]
|
|
656
|
+
for entry in entries:
|
|
657
|
+
if not isinstance(entry, dict):
|
|
658
|
+
continue
|
|
659
|
+
if entry.get("workspace") is True:
|
|
660
|
+
return REASON_WORKSPACE
|
|
661
|
+
if "git" in entry:
|
|
662
|
+
return REASON_GIT
|
|
663
|
+
if "path" in entry:
|
|
664
|
+
return REASON_LOCAL
|
|
665
|
+
if isinstance(entry.get("url"), str):
|
|
666
|
+
return _url_reason(entry["url"])
|
|
667
|
+
if isinstance(entry.get("index"), str):
|
|
668
|
+
return _index_reason(entry["index"])
|
|
669
|
+
return None
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
_PIPFILE_RESERVED = frozenset({"source", "requires", "pipenv", "scripts"})
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
def parse_pipfile(path: str, content: str) -> list[Dependency]:
|
|
676
|
+
"""Les dépendances d'un ``Pipfile`` : ``[packages]``, ``[dev-packages]``, et les
|
|
677
|
+
autres catégories de pipenv (``[docs]``…).
|
|
678
|
+
|
|
679
|
+
Lève ``ParseError`` si le TOML est invalide.
|
|
680
|
+
"""
|
|
681
|
+
return [declared.dependency for declared in _parse_pipfile(path, content)]
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
def _parse_pipfile(path: str, content: str) -> list[Declared]:
|
|
685
|
+
data = _load_toml(content)
|
|
686
|
+
spans = string_spans(content)
|
|
687
|
+
declared = []
|
|
688
|
+
for section, packages in data.items():
|
|
689
|
+
if section in _PIPFILE_RESERVED or not isinstance(packages, dict):
|
|
690
|
+
continue
|
|
691
|
+
if section == "packages":
|
|
692
|
+
scope = Scope.RUNTIME
|
|
693
|
+
elif section == "dev-packages":
|
|
694
|
+
scope = Scope.DEV
|
|
695
|
+
else:
|
|
696
|
+
scope = _group_scope(section)
|
|
697
|
+
for raw_name, spec in packages.items():
|
|
698
|
+
if isinstance(spec, str):
|
|
699
|
+
span = spans.get((section, raw_name))
|
|
700
|
+
declared.append(_declare_constraint(raw_name, spec, span, file=path, scope=scope))
|
|
701
|
+
continue
|
|
702
|
+
if not isinstance(spec, dict):
|
|
703
|
+
continue
|
|
704
|
+
reason = _pipfile_reason(spec)
|
|
705
|
+
extras = _extras(spec.get("extras"))
|
|
706
|
+
version = spec.get("version")
|
|
707
|
+
if isinstance(version, str):
|
|
708
|
+
span = spans.get((section, raw_name, "version"))
|
|
709
|
+
declared.append(
|
|
710
|
+
_declare_constraint(
|
|
711
|
+
raw_name,
|
|
712
|
+
version,
|
|
713
|
+
span,
|
|
714
|
+
file=path,
|
|
715
|
+
scope=scope,
|
|
716
|
+
reason=reason,
|
|
717
|
+
extras=extras,
|
|
718
|
+
)
|
|
719
|
+
)
|
|
720
|
+
else:
|
|
721
|
+
declared.append(
|
|
722
|
+
_declare_unversioned(
|
|
723
|
+
raw_name, file=path, scope=scope, reason=reason, extras=extras
|
|
724
|
+
)
|
|
725
|
+
)
|
|
726
|
+
return declared
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
def _pipfile_reason(spec: Mapping[str, Any]) -> str | None:
|
|
730
|
+
for vcs in ("git", "hg", "svn", "bzr"):
|
|
731
|
+
if vcs in spec:
|
|
732
|
+
return f"dépendance {vcs}"
|
|
733
|
+
if "path" in spec:
|
|
734
|
+
return REASON_LOCAL
|
|
735
|
+
if isinstance(spec.get("file"), str):
|
|
736
|
+
return _url_reason(spec["file"])
|
|
737
|
+
index = spec.get("index")
|
|
738
|
+
if isinstance(index, str) and index.lower() != "pypi":
|
|
739
|
+
return _index_reason(index)
|
|
740
|
+
return None
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
_CFG_SEPARATOR = re.compile(r"[=:]")
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
def parse_setup_cfg(path: str, content: str) -> list[Dependency]:
|
|
747
|
+
"""Les dépendances d'un ``setup.cfg`` : ``install_requires``, ``extras_require``,
|
|
748
|
+
``tests_require`` (dev) et ``setup_requires`` (build).
|
|
749
|
+
|
|
750
|
+
Comme setuptools : une dépendance par ligne quand la valeur est sur plusieurs
|
|
751
|
+
lignes, sinon séparées par des ``;``.
|
|
752
|
+
"""
|
|
753
|
+
return [declared.dependency for declared in _parse_setup_cfg(path, content)[0]]
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
def _parse_setup_cfg(path: str, content: str) -> tuple[list[Declared], list[Notice]]:
|
|
757
|
+
declared: list[Declared] = []
|
|
758
|
+
notices: list[Notice] = []
|
|
759
|
+
lines = [line.rstrip("\r") for line in content.split("\n")]
|
|
760
|
+
section = ""
|
|
761
|
+
index = 0
|
|
762
|
+
while index < len(lines):
|
|
763
|
+
line = lines[index]
|
|
764
|
+
index += 1
|
|
765
|
+
stripped = line.strip()
|
|
766
|
+
if not stripped or stripped.startswith(("#", ";")) or line[0] in " \t":
|
|
767
|
+
continue
|
|
768
|
+
if stripped.startswith("[") and stripped.endswith("]"):
|
|
769
|
+
section = stripped[1:-1].strip().lower()
|
|
770
|
+
continue
|
|
771
|
+
separator = _CFG_SEPARATOR.search(line)
|
|
772
|
+
if separator is None:
|
|
773
|
+
continue
|
|
774
|
+
key = line[: separator.start()].strip().lower().replace("-", "_")
|
|
775
|
+
values = [(index, separator.end())] # (numéro de ligne, colonne de début)
|
|
776
|
+
while index < len(lines):
|
|
777
|
+
following = lines[index]
|
|
778
|
+
if (
|
|
779
|
+
following.strip()
|
|
780
|
+
and following[0] not in " \t"
|
|
781
|
+
and not following.lstrip().startswith(("#", ";"))
|
|
782
|
+
):
|
|
783
|
+
break
|
|
784
|
+
values.append((index + 1, 0))
|
|
785
|
+
index += 1
|
|
786
|
+
scope = _setup_cfg_scope(section, key)
|
|
787
|
+
if scope is None:
|
|
788
|
+
continue
|
|
789
|
+
for number, column, text in _setup_cfg_items(lines, values):
|
|
790
|
+
item = _declare_pep508(
|
|
791
|
+
text,
|
|
792
|
+
file=path,
|
|
793
|
+
scope=scope,
|
|
794
|
+
locate=_line_locator(number, column, len(lines[number - 1])),
|
|
795
|
+
)
|
|
796
|
+
if item is None:
|
|
797
|
+
notices.append(
|
|
798
|
+
Notice(
|
|
799
|
+
file=path,
|
|
800
|
+
message=f"ligne {number} : « {text} » n'est pas une dépendance valide.",
|
|
801
|
+
)
|
|
802
|
+
)
|
|
803
|
+
else:
|
|
804
|
+
declared.append(item)
|
|
805
|
+
return declared, notices
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
def _setup_cfg_scope(section: str, key: str) -> Scope | None:
|
|
809
|
+
if section == "options":
|
|
810
|
+
return {
|
|
811
|
+
"install_requires": Scope.RUNTIME,
|
|
812
|
+
"tests_require": Scope.DEV,
|
|
813
|
+
"setup_requires": Scope.BUILD,
|
|
814
|
+
}.get(key)
|
|
815
|
+
if section == "options.extras_require":
|
|
816
|
+
return _group_scope(key)
|
|
817
|
+
return None
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
def _setup_cfg_items(
|
|
821
|
+
lines: Sequence[str], values: Sequence[tuple[int, int]]
|
|
822
|
+
) -> Iterator[tuple[int, int, str]]:
|
|
823
|
+
"""Les dépendances d'une valeur de ``setup.cfg`` : ``(ligne, colonne, texte)``."""
|
|
824
|
+
texts = [(number, column, lines[number - 1][column:]) for number, column in values]
|
|
825
|
+
filled = [text for _, _, text in texts if text.strip() and not text.strip().startswith("#")]
|
|
826
|
+
if len(filled) > 1:
|
|
827
|
+
pieces = texts # une dépendance par ligne
|
|
828
|
+
else:
|
|
829
|
+
number, column, text = next(
|
|
830
|
+
((n, c, t) for n, c, t in texts if t.strip() and not t.strip().startswith("#")),
|
|
831
|
+
(0, 0, ""),
|
|
832
|
+
)
|
|
833
|
+
pieces = []
|
|
834
|
+
offset = 0
|
|
835
|
+
for piece in text.split(";"):
|
|
836
|
+
pieces.append((number, column + offset, piece))
|
|
837
|
+
offset += len(piece) + 1
|
|
838
|
+
for number, column, text in pieces:
|
|
839
|
+
stripped = text.strip()
|
|
840
|
+
if not stripped or stripped.startswith("#"):
|
|
841
|
+
continue
|
|
842
|
+
yield number, column + len(text) - len(text.lstrip()), stripped
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
# ---------------------------------------------------------------------------
|
|
846
|
+
# Lockfiles
|
|
847
|
+
# ---------------------------------------------------------------------------
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
def installed_version(lock: Lockfile, name: str) -> str | None:
|
|
851
|
+
"""La version figée de ``name`` dans ``lock`` ; la plus haute selon PEP 440 s'il y en a
|
|
852
|
+
plusieurs (fourches par marqueurs de ``uv.lock``), la première si aucune n'est lisible."""
|
|
853
|
+
versions = [p.version for p in lock.packages if p.name == name and p.version]
|
|
854
|
+
return PYPI.highest(versions) or next(iter(versions), None)
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
def parse_uv_lock(path: str, content: str) -> Lockfile:
|
|
858
|
+
"""Un ``uv.lock``. Il couvre tout le workspace ; ses membres sont ``local``."""
|
|
859
|
+
data = _load_toml(content)
|
|
860
|
+
packages = []
|
|
861
|
+
for entry in _entries(data.get("package")):
|
|
862
|
+
source = _table(entry, "source")
|
|
863
|
+
packages.append(
|
|
864
|
+
LockedPackage(
|
|
865
|
+
name=canonicalize_name(entry["name"]),
|
|
866
|
+
version=str(entry.get("version", "")),
|
|
867
|
+
dependencies=_uv_edges(entry.get("dependencies")),
|
|
868
|
+
extras={
|
|
869
|
+
canonicalize_name(extra): _uv_edges(items)
|
|
870
|
+
for extra, items in _table(entry, "optional-dependencies").items()
|
|
871
|
+
},
|
|
872
|
+
local="editable" in source or "virtual" in source,
|
|
873
|
+
unverifiable_reason=_uv_lock_reason(source),
|
|
874
|
+
)
|
|
875
|
+
)
|
|
876
|
+
return Lockfile(
|
|
877
|
+
path=path, ecosystem=Ecosystem.PYPI, packages=tuple(packages), graph=True, shared=True
|
|
878
|
+
)
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
def _entries(value: Any) -> list[dict[str, Any]]:
|
|
882
|
+
"""Les ``[[package]]`` qui ont un nom."""
|
|
883
|
+
if not isinstance(value, list):
|
|
884
|
+
return []
|
|
885
|
+
return [
|
|
886
|
+
entry for entry in value if isinstance(entry, dict) and isinstance(entry.get("name"), str)
|
|
887
|
+
]
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
def _uv_edges(items: Any) -> tuple[Edge, ...]:
|
|
891
|
+
if not isinstance(items, list):
|
|
892
|
+
return ()
|
|
893
|
+
return tuple(
|
|
894
|
+
(canonicalize_name(item["name"]), _extras(item.get("extra")))
|
|
895
|
+
for item in items
|
|
896
|
+
if isinstance(item, dict) and isinstance(item.get("name"), str)
|
|
897
|
+
)
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
def _uv_lock_reason(source: Mapping[str, Any]) -> str | None:
|
|
901
|
+
if "git" in source:
|
|
902
|
+
return REASON_GIT
|
|
903
|
+
if source.keys() & {"path", "directory", "editable", "virtual"}:
|
|
904
|
+
return REASON_LOCAL
|
|
905
|
+
if "url" in source:
|
|
906
|
+
return REASON_URL
|
|
907
|
+
return None
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
def parse_poetry_lock(path: str, content: str) -> Lockfile:
|
|
911
|
+
"""Un ``poetry.lock``.
|
|
912
|
+
|
|
913
|
+
Le scope de chaque paquet vient de ``category`` (Poetry ≤ 1.4) ou de ``groups``
|
|
914
|
+
(Poetry 2) ; entre les deux, Poetry n'écrit ni l'un ni l'autre, et c'est le
|
|
915
|
+
graphe, suivi depuis les groupes du ``pyproject.toml``, qui le donne.
|
|
916
|
+
"""
|
|
917
|
+
data = _load_toml(content)
|
|
918
|
+
packages = []
|
|
919
|
+
for entry in _entries(data.get("package")):
|
|
920
|
+
dependencies = []
|
|
921
|
+
for raw_name, spec in _table(entry, "dependencies").items():
|
|
922
|
+
specs = spec if isinstance(spec, list) else [spec]
|
|
923
|
+
tables = [item for item in specs if isinstance(item, dict)]
|
|
924
|
+
if (
|
|
925
|
+
tables
|
|
926
|
+
and len(tables) == len(specs)
|
|
927
|
+
and all(t.get("optional") is True for t in tables)
|
|
928
|
+
):
|
|
929
|
+
continue # tirée seulement par un extra
|
|
930
|
+
extras = frozenset().union(*(_extras(t.get("extras")) for t in tables))
|
|
931
|
+
dependencies.append((canonicalize_name(raw_name), extras))
|
|
932
|
+
extras_edges = {
|
|
933
|
+
canonicalize_name(extra): tuple(
|
|
934
|
+
edge
|
|
935
|
+
for requirement in requirements
|
|
936
|
+
if isinstance(requirement, str) and (edge := _edge_from_requirement(requirement))
|
|
937
|
+
)
|
|
938
|
+
for extra, requirements in _table(entry, "extras").items()
|
|
939
|
+
if isinstance(requirements, list)
|
|
940
|
+
}
|
|
941
|
+
packages.append(
|
|
942
|
+
LockedPackage(
|
|
943
|
+
name=canonicalize_name(entry["name"]),
|
|
944
|
+
version=str(entry.get("version", "")),
|
|
945
|
+
dependencies=tuple(dependencies),
|
|
946
|
+
extras=extras_edges,
|
|
947
|
+
scope=_poetry_lock_scope(entry),
|
|
948
|
+
unverifiable_reason=_poetry_lock_reason(_table(entry, "source")),
|
|
949
|
+
)
|
|
950
|
+
)
|
|
951
|
+
return Lockfile(path=path, ecosystem=Ecosystem.PYPI, packages=tuple(packages), graph=True)
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
def _poetry_lock_scope(entry: Mapping[str, Any]) -> Scope | None:
|
|
955
|
+
category = entry.get("category")
|
|
956
|
+
if category in ("dev", "test"):
|
|
957
|
+
return Scope.DEV
|
|
958
|
+
if category == "main":
|
|
959
|
+
return Scope.RUNTIME
|
|
960
|
+
groups = entry.get("groups")
|
|
961
|
+
if isinstance(groups, list) and groups:
|
|
962
|
+
return Scope.RUNTIME if "main" in groups else _group_scope(str(groups[0]))
|
|
963
|
+
return None
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
def _poetry_lock_reason(source: Mapping[str, Any]) -> str | None:
|
|
967
|
+
kind = source.get("type")
|
|
968
|
+
if kind == "git":
|
|
969
|
+
return REASON_GIT
|
|
970
|
+
if kind in ("directory", "file"):
|
|
971
|
+
return REASON_LOCAL
|
|
972
|
+
if kind == "url":
|
|
973
|
+
return REASON_URL
|
|
974
|
+
return None # « legacy » : un autre index, mais un index quand même
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
def parse_pdm_lock(path: str, content: str) -> Lockfile:
|
|
978
|
+
"""Un ``pdm.lock``. Le scope vient de ``groups`` (``default`` → ``runtime``)."""
|
|
979
|
+
data = _load_toml(content)
|
|
980
|
+
packages = []
|
|
981
|
+
for entry in _entries(data.get("package")):
|
|
982
|
+
groups = entry.get("groups")
|
|
983
|
+
scope = None
|
|
984
|
+
if isinstance(groups, list) and groups:
|
|
985
|
+
scope = Scope.RUNTIME if "default" in groups else _group_scope(str(groups[0]))
|
|
986
|
+
reason = None
|
|
987
|
+
if "git" in entry:
|
|
988
|
+
reason = REASON_GIT
|
|
989
|
+
elif "path" in entry:
|
|
990
|
+
reason = REASON_LOCAL
|
|
991
|
+
elif "url" in entry:
|
|
992
|
+
reason = REASON_URL
|
|
993
|
+
packages.append(
|
|
994
|
+
LockedPackage(
|
|
995
|
+
name=canonicalize_name(entry["name"]),
|
|
996
|
+
version=str(entry.get("version", "")),
|
|
997
|
+
scope=scope,
|
|
998
|
+
unverifiable_reason=reason,
|
|
999
|
+
)
|
|
1000
|
+
)
|
|
1001
|
+
return Lockfile(path=path, ecosystem=Ecosystem.PYPI, packages=tuple(packages))
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
def parse_pipfile_lock(path: str, content: str) -> Lockfile:
|
|
1005
|
+
"""Un ``Pipfile.lock`` : ``default`` (``runtime``), ``develop`` (``dev``), et les autres
|
|
1006
|
+
catégories de pipenv.
|
|
1007
|
+
|
|
1008
|
+
Lève ``ParseError`` si le JSON est invalide.
|
|
1009
|
+
"""
|
|
1010
|
+
try:
|
|
1011
|
+
data = json.loads(content)
|
|
1012
|
+
except ValueError as err:
|
|
1013
|
+
raise ParseError(f"JSON invalide ({err}).") from err
|
|
1014
|
+
if not isinstance(data, dict):
|
|
1015
|
+
raise ParseError("JSON inattendu : un objet est attendu.")
|
|
1016
|
+
sections = sorted(
|
|
1017
|
+
(key for key in data if key != "_meta"),
|
|
1018
|
+
key=lambda key: (key != "default", key != "develop", key),
|
|
1019
|
+
)
|
|
1020
|
+
packages = []
|
|
1021
|
+
for section in sections:
|
|
1022
|
+
bucket = data[section]
|
|
1023
|
+
if not isinstance(bucket, dict):
|
|
1024
|
+
continue
|
|
1025
|
+
if section == "default":
|
|
1026
|
+
scope = Scope.RUNTIME
|
|
1027
|
+
elif section == "develop":
|
|
1028
|
+
scope = Scope.DEV
|
|
1029
|
+
else:
|
|
1030
|
+
scope = _group_scope(section)
|
|
1031
|
+
for raw_name, info in bucket.items():
|
|
1032
|
+
if not isinstance(info, dict):
|
|
1033
|
+
continue
|
|
1034
|
+
packages.append(
|
|
1035
|
+
LockedPackage(
|
|
1036
|
+
name=canonicalize_name(raw_name),
|
|
1037
|
+
version=str(info.get("version", "")).lstrip("="),
|
|
1038
|
+
scope=scope,
|
|
1039
|
+
unverifiable_reason=_pipfile_reason(info),
|
|
1040
|
+
)
|
|
1041
|
+
)
|
|
1042
|
+
return Lockfile(path=path, ecosystem=Ecosystem.PYPI, packages=tuple(packages))
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
def _requirements_lock(parsed: _RequirementsFile, scope: Scope) -> Lockfile:
|
|
1046
|
+
"""Un ``.txt`` compilé par pip-tools, ou un fichier de contraintes (``-c``), vu comme
|
|
1047
|
+
un lockfile : la version figée est celle d'un ``==``."""
|
|
1048
|
+
packages = []
|
|
1049
|
+
for declared in parsed.declared:
|
|
1050
|
+
dependency = declared.dependency
|
|
1051
|
+
packages.append(
|
|
1052
|
+
LockedPackage(
|
|
1053
|
+
name=dependency.name,
|
|
1054
|
+
version=_pinned_version(dependency.constraint) or "",
|
|
1055
|
+
scope=scope,
|
|
1056
|
+
unverifiable_reason=dependency.unverifiable_reason,
|
|
1057
|
+
constraint=dependency.constraint,
|
|
1058
|
+
location=dependency.location,
|
|
1059
|
+
)
|
|
1060
|
+
)
|
|
1061
|
+
return Lockfile(path=parsed.path, ecosystem=Ecosystem.PYPI, packages=tuple(packages))
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
def _pinned_version(constraint: str) -> str | None:
|
|
1065
|
+
"""La version d'une contrainte exacte (``==2.0.1``) ; ``None`` sinon."""
|
|
1066
|
+
try:
|
|
1067
|
+
specifiers = list(SpecifierSet(constraint))
|
|
1068
|
+
except InvalidSpecifier:
|
|
1069
|
+
return None
|
|
1070
|
+
if len(specifiers) != 1:
|
|
1071
|
+
return None
|
|
1072
|
+
specifier = specifiers[0]
|
|
1073
|
+
if specifier.operator in ("==", "===") and "*" not in specifier.version:
|
|
1074
|
+
return specifier.version
|
|
1075
|
+
return None
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
# ---------------------------------------------------------------------------
|
|
1079
|
+
# Lien manifeste ↔ lockfile
|
|
1080
|
+
# ---------------------------------------------------------------------------
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
def _with_installed(declared: Iterable[Declared], locks: Sequence[Lockfile]) -> list[Dependency]:
|
|
1084
|
+
"""Les dépendances directes, avec la version du premier lockfile qui les connaît."""
|
|
1085
|
+
|
|
1086
|
+
def installed(item: Declared) -> tuple[str, str] | None:
|
|
1087
|
+
name = item.dependency.name
|
|
1088
|
+
return next(
|
|
1089
|
+
((v, lock.path) for lock in locks if (v := installed_version(lock, name))), None
|
|
1090
|
+
)
|
|
1091
|
+
|
|
1092
|
+
return with_installed(declared, installed)
|
|
1093
|
+
|
|
1094
|
+
|
|
1095
|
+
def _transitives(lock: Lockfile, declared: Sequence[Declared], names: set[str]) -> list[Dependency]:
|
|
1096
|
+
"""Les paquets du lockfile qui ne sont pas des directes.
|
|
1097
|
+
|
|
1098
|
+
Une autre version d'une directe (fourche par marqueurs de ``uv.lock`` : numpy 1.26 pour
|
|
1099
|
+
Python < 3.10, 2.x au-dessus) est rendue comme une transitive, comme une autre version
|
|
1100
|
+
d'une directe imbriquée dans ``node_modules`` : la directe garde la plus haute
|
|
1101
|
+
(``installed_version``), mais les failles de l'autre doivent être cherchées aussi (E08).
|
|
1102
|
+
"""
|
|
1103
|
+
installed = {name: installed_version(lock, name) for name in names}
|
|
1104
|
+
|
|
1105
|
+
def is_direct(package: LockedPackage) -> bool:
|
|
1106
|
+
if package.name not in names:
|
|
1107
|
+
return False
|
|
1108
|
+
return not package.version or package.version == installed[package.name]
|
|
1109
|
+
|
|
1110
|
+
return transitives(lock, graph_starts(declared), is_direct)
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
# ---------------------------------------------------------------------------
|
|
1114
|
+
# Un projet
|
|
1115
|
+
# ---------------------------------------------------------------------------
|
|
1116
|
+
|
|
1117
|
+
_LOCK_PARSERS: dict[str, Callable[[str, str], Lockfile]] = {
|
|
1118
|
+
"uv.lock": parse_uv_lock,
|
|
1119
|
+
"poetry.lock": parse_poetry_lock,
|
|
1120
|
+
"pdm.lock": parse_pdm_lock,
|
|
1121
|
+
"Pipfile.lock": parse_pipfile_lock,
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
_PYPROJECT_LOCKS = ("uv.lock", "poetry.lock", "pdm.lock")
|
|
1125
|
+
"""Les lockfiles d'un ``pyproject.toml``, par ordre de préférence quand l'outil n'est pas
|
|
1126
|
+
connu. Avec ``[tool.poetry]`` ou ``[tool.pdm]``, le lockfile de cet outil passe devant."""
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
def parse_project(project: Project, read_text: ReadText) -> None:
|
|
1130
|
+
"""Lit les fichiers Python de ``project`` et remplit ses dépendances.
|
|
1131
|
+
|
|
1132
|
+
Ajoute à ``project.dependencies`` les directes (dans l'ordre des manifestes)
|
|
1133
|
+
puis les transitives, et à ``project.notices`` ce qui n'a pas pu être lu. Le
|
|
1134
|
+
``uv.lock`` d'un workspace dont le projet n'est pas membre est retiré de
|
|
1135
|
+
``project.lockfiles``.
|
|
1136
|
+
"""
|
|
1137
|
+
_ProjectParser(project, read_text).run()
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
class _ProjectParser:
|
|
1141
|
+
"""Lit les fichiers Python d'un projet et relie manifestes et lockfiles."""
|
|
1142
|
+
|
|
1143
|
+
def __init__(self, project: Project, read_text: ReadText) -> None:
|
|
1144
|
+
self.project = project
|
|
1145
|
+
self.read_text = read_text
|
|
1146
|
+
self.notices: list[Notice] = []
|
|
1147
|
+
self.directs: list[Dependency] = []
|
|
1148
|
+
self.transitives: list[Dependency] = []
|
|
1149
|
+
self.used_locks: set[str] = set()
|
|
1150
|
+
self.pinned_files: dict[str, Lockfile | None] = {}
|
|
1151
|
+
"""Les ``.txt`` compilés et fichiers de contraintes déjà lus (voir ``_pinned_file``)."""
|
|
1152
|
+
self.manifests = self._python_files(project.manifests)
|
|
1153
|
+
self.locks = self._python_files(project.lockfiles)
|
|
1154
|
+
|
|
1155
|
+
def run(self) -> None:
|
|
1156
|
+
if "pyproject.toml" in self.manifests:
|
|
1157
|
+
self._pyproject(self.manifests["pyproject.toml"][0])
|
|
1158
|
+
if "Pipfile" in self.manifests:
|
|
1159
|
+
self._pipfile(self.manifests["Pipfile"][0])
|
|
1160
|
+
if "setup.cfg" in self.manifests:
|
|
1161
|
+
self._setup_cfg(self.manifests["setup.cfg"][0])
|
|
1162
|
+
for path in self.manifests.get("setup.py", []):
|
|
1163
|
+
self.notices.append(
|
|
1164
|
+
Notice(
|
|
1165
|
+
file=path,
|
|
1166
|
+
message="pas lu : il faudrait l'exécuter pour connaître ses dépendances.",
|
|
1167
|
+
)
|
|
1168
|
+
)
|
|
1169
|
+
self._requirements()
|
|
1170
|
+
self._orphan_locks()
|
|
1171
|
+
|
|
1172
|
+
order = {path: index for index, path in enumerate(self.project.manifests)}
|
|
1173
|
+
self.directs.sort(key=lambda d: order.get(d.file, len(order)))
|
|
1174
|
+
self.transitives.sort(key=lambda d: (d.file, d.name, PYPI.sort_key(d.installed or "")))
|
|
1175
|
+
self.project.dependencies.extend(self.directs + self.transitives)
|
|
1176
|
+
self.project.notices.extend(self.notices)
|
|
1177
|
+
|
|
1178
|
+
# -- lecture ------------------------------------------------------------
|
|
1179
|
+
|
|
1180
|
+
@staticmethod
|
|
1181
|
+
def _python_files(paths: Iterable[str]) -> dict[str, list[str]]:
|
|
1182
|
+
"""Les fichiers Python de ``paths``, par motif reconnu (``pyproject.toml``…)."""
|
|
1183
|
+
found: dict[str, list[str]] = defaultdict(list)
|
|
1184
|
+
for path in paths:
|
|
1185
|
+
match = match_dependency_file(path)
|
|
1186
|
+
if match is not None and match.ecosystem is Ecosystem.PYPI:
|
|
1187
|
+
found[match.pattern].append(path)
|
|
1188
|
+
return found
|
|
1189
|
+
|
|
1190
|
+
def _read(self, path: str) -> str | None:
|
|
1191
|
+
try:
|
|
1192
|
+
content = self.read_text(path)
|
|
1193
|
+
except ValueError:
|
|
1194
|
+
content = None
|
|
1195
|
+
if content is None:
|
|
1196
|
+
self.notices.append(
|
|
1197
|
+
Notice(file=path, message="introuvable, illisible ou trop gros : pas lu.")
|
|
1198
|
+
)
|
|
1199
|
+
return content
|
|
1200
|
+
|
|
1201
|
+
def _lock(self, path: str, parser: Callable[[str, str], Lockfile]) -> Lockfile | None:
|
|
1202
|
+
self.used_locks.add(path)
|
|
1203
|
+
content = self._read(path)
|
|
1204
|
+
if content is None:
|
|
1205
|
+
return None
|
|
1206
|
+
try:
|
|
1207
|
+
return parser(path, content)
|
|
1208
|
+
except ParseError as err:
|
|
1209
|
+
self.notices.append(Notice(file=path, message=str(err)))
|
|
1210
|
+
return None
|
|
1211
|
+
|
|
1212
|
+
def _lock_named(self, name: str) -> str | None:
|
|
1213
|
+
paths = self.locks.get(name)
|
|
1214
|
+
return paths[0] if paths else None
|
|
1215
|
+
|
|
1216
|
+
# -- manifestes ---------------------------------------------------------
|
|
1217
|
+
|
|
1218
|
+
def _pyproject(self, path: str) -> None:
|
|
1219
|
+
inherited: dict[str, Any] = {}
|
|
1220
|
+
uv_lock = self._lock_named("uv.lock")
|
|
1221
|
+
if uv_lock is not None and dirname(uv_lock) != self.project.path:
|
|
1222
|
+
member, inherited = self._uv_membership(uv_lock)
|
|
1223
|
+
if not member:
|
|
1224
|
+
logger.debug("%s n'est pas membre du workspace de %s", self.project.path, uv_lock)
|
|
1225
|
+
self.project.lockfiles.remove(uv_lock)
|
|
1226
|
+
del self.locks["uv.lock"]
|
|
1227
|
+
content = self._read(path)
|
|
1228
|
+
if content is None:
|
|
1229
|
+
return
|
|
1230
|
+
try:
|
|
1231
|
+
parsed = _parse_pyproject(path, content, inherited_sources=inherited)
|
|
1232
|
+
except ParseError as err:
|
|
1233
|
+
self.notices.append(Notice(file=path, message=str(err)))
|
|
1234
|
+
return
|
|
1235
|
+
self.notices.extend(parsed.notices)
|
|
1236
|
+
|
|
1237
|
+
preferred = [name for name in ("poetry", "pdm") if name in parsed.tools]
|
|
1238
|
+
candidates = [f"{name}.lock" for name in preferred] + list(_PYPROJECT_LOCKS)
|
|
1239
|
+
lock_path = next((p for name in candidates if (p := self._lock_named(name))), None)
|
|
1240
|
+
lock = None
|
|
1241
|
+
if lock_path is not None:
|
|
1242
|
+
lock = self._lock(lock_path, _LOCK_PARSERS[posixpath.basename(lock_path)])
|
|
1243
|
+
for name in _PYPROJECT_LOCKS:
|
|
1244
|
+
other = self._lock_named(name)
|
|
1245
|
+
if other is not None and other != lock_path:
|
|
1246
|
+
self.used_locks.add(other)
|
|
1247
|
+
self.notices.append(
|
|
1248
|
+
Notice(
|
|
1249
|
+
file=other,
|
|
1250
|
+
message=f"pas lu : {lock_path} est le lockfile pris pour {path}.",
|
|
1251
|
+
)
|
|
1252
|
+
)
|
|
1253
|
+
self._link(parsed.declared, lock)
|
|
1254
|
+
|
|
1255
|
+
def _uv_membership(self, lock: str) -> tuple[bool, dict[str, Any]]:
|
|
1256
|
+
"""Vrai si le projet est membre du workspace uv de ``lock`` ; et les sources de la
|
|
1257
|
+
racine, dont les membres héritent."""
|
|
1258
|
+
root = dirname(lock)
|
|
1259
|
+
content = self._read(join(root, "pyproject.toml"))
|
|
1260
|
+
if content is None:
|
|
1261
|
+
return False, {}
|
|
1262
|
+
try:
|
|
1263
|
+
data = _load_toml(content)
|
|
1264
|
+
except ParseError:
|
|
1265
|
+
return False, {} # la remarque vient avec le projet racine
|
|
1266
|
+
workspace = _table(data, "tool", "uv", "workspace")
|
|
1267
|
+
members = [m for m in workspace.get("members", []) if isinstance(m, str)]
|
|
1268
|
+
excluded = [m for m in workspace.get("exclude", []) if isinstance(m, str)]
|
|
1269
|
+
member = in_workspace(relative_to(self.project.path, root), members, excluded)
|
|
1270
|
+
return member, _table(data, "tool", "uv", "sources")
|
|
1271
|
+
|
|
1272
|
+
def _pipfile(self, path: str) -> None:
|
|
1273
|
+
content = self._read(path)
|
|
1274
|
+
if content is None:
|
|
1275
|
+
return
|
|
1276
|
+
try:
|
|
1277
|
+
declared = _parse_pipfile(path, content)
|
|
1278
|
+
except ParseError as err:
|
|
1279
|
+
self.notices.append(Notice(file=path, message=str(err)))
|
|
1280
|
+
return
|
|
1281
|
+
lock_path = self._lock_named("Pipfile.lock")
|
|
1282
|
+
lock = self._lock(lock_path, parse_pipfile_lock) if lock_path is not None else None
|
|
1283
|
+
self._link(declared, lock)
|
|
1284
|
+
|
|
1285
|
+
def _setup_cfg(self, path: str) -> None:
|
|
1286
|
+
content = self._read(path)
|
|
1287
|
+
if content is None:
|
|
1288
|
+
return
|
|
1289
|
+
declared, notices = _parse_setup_cfg(path, content)
|
|
1290
|
+
self.notices.extend(notices)
|
|
1291
|
+
self._link(declared, None)
|
|
1292
|
+
|
|
1293
|
+
def _link(self, declared: list[Declared], lock: Lockfile | None) -> None:
|
|
1294
|
+
locks = [lock] if lock is not None else []
|
|
1295
|
+
self.directs.extend(_with_installed(declared, locks))
|
|
1296
|
+
if lock is not None:
|
|
1297
|
+
# Une dépendance de build (hatchling…) n'est pas installée avec le projet :
|
|
1298
|
+
# le même paquet, tiré à l'exécution, reste une transitive.
|
|
1299
|
+
names = {d.dependency.name for d in declared if d.dependency.scope is not Scope.BUILD}
|
|
1300
|
+
self.transitives.extend(_transitives(lock, declared, names))
|
|
1301
|
+
|
|
1302
|
+
# -- pip et pip-tools ---------------------------------------------------
|
|
1303
|
+
|
|
1304
|
+
def _requirements(self) -> None:
|
|
1305
|
+
"""Les ``requirements*.txt`` et ``.in``, avec leurs ``-r`` et ``-c``.
|
|
1306
|
+
|
|
1307
|
+
Un fichier tiré par ``-r`` depuis un fichier ``runtime`` est ``runtime`` ;
|
|
1308
|
+
sinon ``dev``. La version installée vient du ``.txt`` compilé par pip-tools
|
|
1309
|
+
(pour un ``.in``), puis des fichiers de contraintes.
|
|
1310
|
+
"""
|
|
1311
|
+
roots = [p for pattern in _REQUIREMENTS_PATTERNS for p in self.manifests.get(pattern, [])]
|
|
1312
|
+
roots.sort(key=self.project.manifests.index)
|
|
1313
|
+
if not roots:
|
|
1314
|
+
return
|
|
1315
|
+
files: dict[str, _RequirementsFile] = {}
|
|
1316
|
+
queue = list(roots)
|
|
1317
|
+
while queue:
|
|
1318
|
+
path = queue.pop(0)
|
|
1319
|
+
if path in files:
|
|
1320
|
+
continue
|
|
1321
|
+
content = self._read(path)
|
|
1322
|
+
if content is None:
|
|
1323
|
+
continue
|
|
1324
|
+
files[path] = _parse_requirements(path, content, Scope.RUNTIME)
|
|
1325
|
+
self.notices.extend(files[path].notices)
|
|
1326
|
+
queue.extend(files[path].includes)
|
|
1327
|
+
|
|
1328
|
+
closures = {root: _closure(root, files) for root in roots if root in files}
|
|
1329
|
+
runtime = set().union(
|
|
1330
|
+
*(closure for root, closure in closures.items() if _file_scope(root) is Scope.RUNTIME)
|
|
1331
|
+
)
|
|
1332
|
+
|
|
1333
|
+
def scope_of(path: str) -> Scope:
|
|
1334
|
+
return Scope.RUNTIME if path in runtime else Scope.DEV
|
|
1335
|
+
|
|
1336
|
+
# Le .txt compilé de chaque .in (pip-tools), puis les fichiers de contraintes.
|
|
1337
|
+
compiled = {
|
|
1338
|
+
path[: -len(".txt")].lower(): path
|
|
1339
|
+
for pattern in ("requirements*.txt", "requirements/*.txt")
|
|
1340
|
+
for path in self.locks.get(pattern, [])
|
|
1341
|
+
}
|
|
1342
|
+
pairs: dict[str, Lockfile] = {}
|
|
1343
|
+
for path in files:
|
|
1344
|
+
target = compiled.get(path[: -len(".in")].lower()) if path.endswith(".in") else None
|
|
1345
|
+
if target is not None and (lock := self._pinned_file(target, scope_of(path))):
|
|
1346
|
+
pairs[path] = lock
|
|
1347
|
+
constraints: dict[str, Lockfile] = {}
|
|
1348
|
+
for path in dict.fromkeys(c for parsed in files.values() for c in parsed.constraints):
|
|
1349
|
+
users = [p for p, parsed in files.items() if path in parsed.constraints]
|
|
1350
|
+
scope = Scope.RUNTIME if any(scope_of(p) is Scope.RUNTIME for p in users) else Scope.DEV
|
|
1351
|
+
if lock := self._pinned_file(path, scope):
|
|
1352
|
+
constraints[path] = lock
|
|
1353
|
+
|
|
1354
|
+
declared: list[Declared] = []
|
|
1355
|
+
for path, parsed in files.items():
|
|
1356
|
+
reaching = [root for root, closure in closures.items() if path in closure]
|
|
1357
|
+
locks = [pairs[p] for p in (path, *reaching) if p in pairs]
|
|
1358
|
+
locks += [
|
|
1359
|
+
constraints[c]
|
|
1360
|
+
for root in reaching
|
|
1361
|
+
for member in closures[root]
|
|
1362
|
+
for c in files[member].constraints
|
|
1363
|
+
if c in constraints
|
|
1364
|
+
]
|
|
1365
|
+
items = [
|
|
1366
|
+
Declared(dataclasses.replace(item.dependency, scope=scope_of(path)), item.extras)
|
|
1367
|
+
for item in parsed.declared
|
|
1368
|
+
]
|
|
1369
|
+
declared.extend(items)
|
|
1370
|
+
self.directs.extend(_with_installed(items, locks))
|
|
1371
|
+
|
|
1372
|
+
names = {item.dependency.name for item in declared}
|
|
1373
|
+
# Un même .txt peut être à la fois compilé et donné à « -c » : lu une fois.
|
|
1374
|
+
locks_by_path = {lock.path: lock for lock in [*pairs.values(), *constraints.values()]}
|
|
1375
|
+
for lock in locks_by_path.values():
|
|
1376
|
+
self.transitives.extend(_transitives(lock, declared, names))
|
|
1377
|
+
|
|
1378
|
+
def _pinned_file(self, path: str, scope: Scope) -> Lockfile | None:
|
|
1379
|
+
"""Un ``.txt`` compilé ou un fichier de contraintes, lu une seule fois.
|
|
1380
|
+
|
|
1381
|
+
Le scope de ses paquets est celui de la première lecture : les ``.txt``
|
|
1382
|
+
compilés sont lus avant les contraintes.
|
|
1383
|
+
"""
|
|
1384
|
+
if path not in self.pinned_files:
|
|
1385
|
+
self.used_locks.add(path)
|
|
1386
|
+
content = self._read(path)
|
|
1387
|
+
lock = None
|
|
1388
|
+
if content is not None:
|
|
1389
|
+
parsed = _parse_requirements(path, content, scope)
|
|
1390
|
+
self.notices.extend(parsed.notices)
|
|
1391
|
+
lock = _requirements_lock(parsed, scope)
|
|
1392
|
+
self.pinned_files[path] = lock
|
|
1393
|
+
return self.pinned_files[path]
|
|
1394
|
+
|
|
1395
|
+
# -- lockfiles seuls ----------------------------------------------------
|
|
1396
|
+
|
|
1397
|
+
def _orphan_locks(self) -> None:
|
|
1398
|
+
"""Les lockfiles qu'aucun manifeste n'a pris : tout leur contenu est transitif."""
|
|
1399
|
+
for pattern, paths in self.locks.items():
|
|
1400
|
+
for path in paths:
|
|
1401
|
+
if path in self.used_locks:
|
|
1402
|
+
continue
|
|
1403
|
+
if pattern in _LOCK_PARSERS:
|
|
1404
|
+
lock = self._lock(path, _LOCK_PARSERS[pattern])
|
|
1405
|
+
else: # un .txt compilé dont le .in n'a pas pu être lu
|
|
1406
|
+
lock = self._pinned_file(path, _file_scope(path))
|
|
1407
|
+
if lock is not None:
|
|
1408
|
+
self.transitives.extend(_transitives(lock, [], set()))
|
|
1409
|
+
|
|
1410
|
+
|
|
1411
|
+
def _closure(root: str, files: Mapping[str, _RequirementsFile]) -> set[str]:
|
|
1412
|
+
"""``root`` et les fichiers qu'il inclut avec ``-r``, de proche en proche."""
|
|
1413
|
+
found: set[str] = set()
|
|
1414
|
+
stack = [root]
|
|
1415
|
+
while stack:
|
|
1416
|
+
path = stack.pop()
|
|
1417
|
+
if path in found or path not in files:
|
|
1418
|
+
continue
|
|
1419
|
+
found.add(path)
|
|
1420
|
+
stack.extend(files[path].includes)
|
|
1421
|
+
return found
|