-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathutils.py
348 lines (277 loc) · 11 KB
/
utils.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from __future__ import annotations
import os
import posixpath
import re
import sys
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Dict
from typing import List
from typing import Tuple
from urllib.parse import unquote
from urllib.parse import urlsplit
from urllib.request import url2pathname
from poetry.core.semver.version_range import VersionRange
from poetry.core.version.markers import dnf
if TYPE_CHECKING:
from poetry.core.packages.constraints import BaseConstraint
from poetry.core.semver.version import Version
from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.semver.version_union import VersionUnion
from poetry.core.version.markers import BaseMarker
# Even though we've `from __future__ import annotations`, mypy doesn't seem to like
# this as `dict[str, ...]`
ConvertedMarkers = Dict[str, List[List[Tuple[str, str]]]]
BZ2_EXTENSIONS = (".tar.bz2", ".tbz")
XZ_EXTENSIONS = (".tar.xz", ".txz", ".tlz", ".tar.lz", ".tar.lzma")
ZIP_EXTENSIONS = (".zip", ".whl")
TAR_EXTENSIONS = (".tar.gz", ".tgz", ".tar")
ARCHIVE_EXTENSIONS = ZIP_EXTENSIONS + BZ2_EXTENSIONS + TAR_EXTENSIONS + XZ_EXTENSIONS
SUPPORTED_EXTENSIONS: tuple[str, ...] = ZIP_EXTENSIONS + TAR_EXTENSIONS
try:
import bz2 # noqa: F401, TC002
SUPPORTED_EXTENSIONS += BZ2_EXTENSIONS
except ImportError:
pass
try:
# Only for Python 3.3+
import lzma # noqa: F401, TC002
SUPPORTED_EXTENSIONS += XZ_EXTENSIONS
except ImportError:
pass
def path_to_url(path: str | Path) -> str:
"""
Convert a path to a file: URL. The path will be made absolute unless otherwise
specified and have quoted path parts.
"""
return Path(path).absolute().as_uri()
def url_to_path(url: str) -> Path:
"""
Convert an RFC8089 file URI to path.
The logic used here is borrowed from pip
https://github.com/pypa/pip/blob/4d1932fcdd1974c820ea60b3286984ebb0c3beaa/src/pip/_internal/utils/urls.py#L31
"""
if not url.startswith("file:"):
raise ValueError(f"{url} is not a valid file URI")
_, netloc, path, _, _ = urlsplit(url)
if not netloc or netloc == "localhost":
# According to RFC 8089, same as empty authority.
netloc = ""
elif netloc not in {".", ".."} and sys.platform == "win32":
# If we have a UNC path, prepend UNC share notation.
netloc = "\\\\" + netloc
else:
raise ValueError(
f"non-local file URIs are not supported on this platform: {url}"
)
return Path(url2pathname(netloc + unquote(path)))
def is_url(name: str) -> bool:
if ":" not in name:
return False
scheme = name.split(":", 1)[0].lower()
return scheme in [
"http",
"https",
"file",
"ftp",
"ssh",
"git",
"hg",
"bzr",
"sftp",
"svn",
"ssh",
]
def strip_extras(path: str) -> tuple[str, str | None]:
m = re.match(r"^(.+)(\[[^\]]+\])$", path)
extras = None
if m:
path_no_extras = m.group(1)
extras = m.group(2)
else:
path_no_extras = path
return path_no_extras, extras
def is_installable_dir(path: str) -> bool:
"""Return True if `path` is a directory containing a setup.py file."""
if not os.path.isdir(path):
return False
setup_py = os.path.join(path, "setup.py")
if os.path.isfile(setup_py):
return True
return False
def is_archive_file(name: str) -> bool:
"""Return True if `name` is a considered as an archive file."""
ext = splitext(name)[1].lower()
if ext in ARCHIVE_EXTENSIONS:
return True
return False
def splitext(path: str) -> tuple[str, str]:
"""Like os.path.splitext, but take off .tar too"""
base, ext = posixpath.splitext(path)
if base.lower().endswith(".tar"):
ext = base[-4:] + ext
base = base[:-4]
return base, ext
def convert_markers(marker: BaseMarker) -> ConvertedMarkers:
from poetry.core.version.markers import MarkerUnion
from poetry.core.version.markers import MultiMarker
from poetry.core.version.markers import SingleMarker
requirements: ConvertedMarkers = {}
marker = dnf(marker)
conjunctions = marker.markers if isinstance(marker, MarkerUnion) else [marker]
group_count = len(conjunctions)
def add_constraint(
marker_name: str, constraint: tuple[str, str], group_index: int
) -> None:
# python_full_version is equivalent to python_version
# for Poetry so we merge them
if marker_name == "python_full_version":
marker_name = "python_version"
if marker_name not in requirements:
requirements[marker_name] = [[] for _ in range(group_count)]
requirements[marker_name][group_index].append(constraint)
for i, sub_marker in enumerate(conjunctions):
if isinstance(sub_marker, MultiMarker):
for m in sub_marker.markers:
if isinstance(m, SingleMarker):
add_constraint(m.name, (m.operator, m.value), i)
elif isinstance(sub_marker, SingleMarker):
add_constraint(sub_marker.name, (sub_marker.operator, sub_marker.value), i)
for group_name in requirements:
# remove duplicates
seen = []
for r in requirements[group_name]:
if r not in seen:
seen.append(r)
requirements[group_name] = seen
return requirements
def contains_group_without_marker(markers: ConvertedMarkers, marker_name: str) -> bool:
return marker_name not in markers or [] in markers[marker_name]
def create_nested_marker(
name: str,
constraint: BaseConstraint | VersionUnion | Version | VersionConstraint,
) -> str:
from poetry.core.packages.constraints.constraint import Constraint
from poetry.core.packages.constraints.multi_constraint import MultiConstraint
from poetry.core.packages.constraints.union_constraint import UnionConstraint
from poetry.core.semver.version import Version
from poetry.core.semver.version_union import VersionUnion
if constraint.is_any():
return ""
if isinstance(constraint, (MultiConstraint, UnionConstraint)):
multi_parts = []
for c in constraint.constraints:
multi = isinstance(c, (MultiConstraint, UnionConstraint))
multi_parts.append((multi, create_nested_marker(name, c)))
glue = " and "
if isinstance(constraint, UnionConstraint):
parts = [f"({part[1]})" if part[0] else part[1] for part in multi_parts]
glue = " or "
else:
parts = [part[1] for part in multi_parts]
marker = glue.join(parts)
elif isinstance(constraint, Constraint):
marker = f'{name} {constraint.operator} "{constraint.version}"'
elif isinstance(constraint, VersionUnion):
parts = [create_nested_marker(name, c) for c in constraint.ranges]
glue = " or "
parts = [f"({part})" for part in parts]
marker = glue.join(parts)
elif isinstance(constraint, Version):
if name == "python_version" and constraint.precision >= 3:
name = "python_full_version"
marker = f'{name} == "{constraint.text}"'
else:
assert isinstance(constraint, VersionRange)
if constraint.min is not None:
op = ">="
if not constraint.include_min:
op = ">"
version = constraint.min
if constraint.max is not None:
min_name = max_name = name
if min_name == "python_version" and constraint.min.precision >= 3:
min_name = "python_full_version"
if max_name == "python_version" and constraint.max.precision >= 3:
max_name = "python_full_version"
text = f'{min_name} {op} "{version}"'
op = "<="
if not constraint.include_max:
op = "<"
version = constraint.max
text += f' and {max_name} {op} "{version}"'
return text
elif constraint.max is not None:
op = "<="
if not constraint.include_max:
op = "<"
version = constraint.max
else:
return ""
if name == "python_version" and version.precision >= 3:
name = "python_full_version"
marker = f'{name} {op} "{version}"'
return marker
def get_python_constraint_from_marker(
marker: BaseMarker,
) -> VersionConstraint:
from poetry.core.semver.empty_constraint import EmptyConstraint
from poetry.core.semver.helpers import parse_constraint
from poetry.core.semver.version import Version
from poetry.core.semver.version_range import VersionRange
python_marker = marker.only("python_version", "python_full_version")
if python_marker.is_any():
return VersionRange()
if python_marker.is_empty():
return EmptyConstraint()
markers = convert_markers(marker)
if contains_group_without_marker(markers, "python_version"):
# groups are in disjunctive normal form (DNF),
# an empty group means that python_version does not appear in this group,
# which means that python_version is arbitrary for this group
return VersionRange()
ors = []
for or_ in markers["python_version"]:
ands = []
for op, version in or_:
# Expand python version
if op == "==":
if "*" not in version:
version = "~" + version
op = ""
elif op == "!=":
if "*" not in version:
version += ".*"
elif op in ("<=", ">"):
parsed_version = Version.parse(version)
if parsed_version.precision == 1:
if op == "<=":
op = "<"
version = parsed_version.next_major().text
elif op == ">":
op = ">="
version = parsed_version.next_major().text
elif parsed_version.precision == 2:
if op == "<=":
op = "<"
version = parsed_version.next_minor().text
elif op == ">":
op = ">="
version = parsed_version.next_minor().text
elif op in ("in", "not in"):
versions = []
for v in re.split("[ ,]+", version):
split = v.split(".")
if len(split) in [1, 2]:
split.append("*")
op_ = "" if op == "in" else "!="
else:
op_ = "==" if op == "in" else "!="
versions.append(op_ + ".".join(split))
glue = " || " if op == "in" else ", "
if versions:
ands.append(glue.join(versions))
continue
ands.append(f"{op}{version}")
ors.append(" ".join(ands))
return parse_constraint(" || ".join(ors))