forked from pex-tool/pex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtargets.py
More file actions
487 lines (431 loc) · 18.5 KB
/
Copy pathtargets.py
File metadata and controls
487 lines (431 loc) · 18.5 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# Copyright 2025 Pex project contributors.
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import absolute_import
import itertools
import os.path
import tempfile
from collections import OrderedDict, defaultdict
from pex import atexit
from pex.common import pluralize, safe_delete
from pex.interpreter_constraints import iter_compatible_versions
from pex.interpreter_implementation import InterpreterImplementation
from pex.network_configuration import NetworkConfiguration
from pex.orderedset import OrderedSet
from pex.pep_503 import ProjectName
from pex.resolve.package_repository import ReposConfiguration
from pex.resolve.requirement_configuration import RequirementConfiguration
from pex.resolve.target_system import (
ExtraMarkers,
MarkerEnv,
TargetSystem,
UniversalTarget,
has_marker,
)
from pex.resolver import DownloadRequest
from pex.targets import LocalInterpreter, Targets
from pex.third_party.packaging.markers import Marker
from pex.third_party.packaging.specifiers import SpecifierSet
from pex.typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import (
DefaultDict,
Dict,
FrozenSet,
Iterable,
Iterator,
List,
Mapping,
Optional,
Text,
Tuple,
)
import attr # vendor:skip
else:
from pex.third_party import attr
def _calculate_split_markers(
requirement_configuration, # type: RequirementConfiguration
network_configuration, # type: NetworkConfiguration
repos_configuration, # type: ReposConfiguration
):
# type: (...) -> Mapping[str, Marker]
split_markers = {
str(scope.marker): scope.marker
for repo in itertools.chain(
repos_configuration.index_repos, repos_configuration.find_links_repos
)
for scope in repo.scopes
if scope.marker
}
projects_with_markers = defaultdict(dict) # type: DefaultDict[ProjectName, Dict[str, Marker]]
for requirement in requirement_configuration.parse_requirements(network_configuration):
if requirement.project_name and requirement.marker:
projects_with_markers[requirement.project_name][
str(requirement.marker)
] = requirement.marker
for constraint in requirement_configuration.parse_constraints(network_configuration):
if constraint.marker:
projects_with_markers[constraint.project_name][
str(constraint.marker)
] = constraint.marker
split_markers.update(
(marker_str, marker)
for markers in projects_with_markers.values()
for marker_str, marker in markers.items()
# N.B.: We only split the universal resolve for root requirements that have two or
# more marker variants. If there is just one, that represents a conditional
# dependency which can be included in a single resolve without splitting.
if len(markers) > 1
)
return split_markers
def _group_patch_versions(patch_version_iter):
# type: (Iterator[Tuple[int, int, int]]) -> OrderedDict[Tuple[int, int], OrderedSet[Tuple[int, int, int]]]
grouped = OrderedDict() # type: OrderedDict[Tuple[int, int], OrderedSet[Tuple[int, int, int]]]
for version in patch_version_iter:
major_minor = version[:2] # type: Tuple[int, int]
value = grouped.get(major_minor, None)
if value is not None:
version_set = value
else:
version_set = OrderedSet()
grouped[major_minor] = version_set
version_set.add(version)
return grouped
def _iter_universal_targets(
universal_target, # type: UniversalTarget
split_markers, # type: Mapping[str, Marker]
):
# type: (...) -> Iterator[UniversalTarget]
target_systems = universal_target.systems or TargetSystem.values()
interpreter_implementations = (
(universal_target.implementation,)
if universal_target.implementation
else InterpreterImplementation.values()
)
requires_pythons = OrderedSet() # type: OrderedSet[SpecifierSet]
has_python_version = any(
has_marker(marker, "python_version") for marker in split_markers.values()
)
has_python_full_version = any(
has_marker(marker, "python_full_version") for marker in split_markers.values()
)
if has_python_full_version:
complete_version_sets = _group_patch_versions(iter_compatible_versions([]))
actual_version_sets = _group_patch_versions(
iter_compatible_versions(universal_target.requires_python)
)
for major_minor, version_set in actual_version_sets.items():
if version_set == complete_version_sets.get(major_minor):
requires_pythons.add(
SpecifierSet(
"=={major}.{minor}.*".format(major=major_minor[0], minor=major_minor[1])
)
)
else:
for version in version_set:
requires_pythons.add(
SpecifierSet(
"=={major}.{minor}.{patch}".format(
major=version[0], minor=version[1], patch=version[2]
)
)
)
elif has_python_version:
requires_pythons.update(
SpecifierSet("=={major}.{minor}.*".format(major=version[0], minor=version[1]))
for version in OrderedSet(
version[:2]
for version in iter_compatible_versions(universal_target.requires_python)
)
)
else:
requires_pythons.update(universal_target.requires_python)
if not requires_pythons:
requires_pythons.add(SpecifierSet())
systems_by_markers = defaultdict(
list
) # type: DefaultDict[FrozenSet[str], List[Tuple[TargetSystem.Value, InterpreterImplementation.Value, SpecifierSet]]]
for system in target_systems:
for implementation in interpreter_implementations:
for python_specifier in requires_pythons:
marker_env = MarkerEnv.create(
extras=(),
universal_target=UniversalTarget(
implementation=implementation,
systems=(system,),
requires_python=(python_specifier,),
),
)
system_repo_markers = frozenset(
marker_str
for marker_str, marker in split_markers.items()
if marker_env.evaluate(marker)
)
systems_by_markers[system_repo_markers].append(
(system, implementation, python_specifier)
)
for markers, value in systems_by_markers.items():
systems = OrderedSet() # type: OrderedSet[TargetSystem.Value]
implementations = OrderedSet() # type: OrderedSet[InterpreterImplementation.Value]
requires_python = OrderedSet() # type: OrderedSet[SpecifierSet]
for system, implementation, python_specifier in value:
systems.add(system)
implementations.add(implementation)
requires_python.add(python_specifier)
impl = implementations.pop() if len(implementations) == 1 else None
conflicts = ExtraMarkers.extract_conflicts(
tuple(split_markers[marker] for marker in markers)
)
if conflicts:
for extra_markers in conflicts:
yield UniversalTarget(
implementation=impl,
systems=tuple(systems),
requires_python=tuple(requires_python),
extra_markers=extra_markers,
)
else:
yield UniversalTarget(
implementation=impl,
systems=tuple(systems),
requires_python=tuple(requires_python),
)
if TYPE_CHECKING:
from pex.requirements import ParsedRequirement
@attr.s(frozen=True)
class DownloadInput(object):
download_requests = attr.ib() # type: Tuple[DownloadRequest, ...]
direct_requirements = attr.ib() # type: Tuple[ParsedRequirement, ...]
def _comment_out_requirements(
requirements_file, # type: Text
requirements, # type: Iterable[ParsedRequirement]
):
# type: (...) -> Text
lines_to_comment_out = set(
itertools.chain.from_iterable(
range(requirement.line.start_line, requirement.line.end_line + 1)
for requirement in requirements
)
)
# N.B.: We create the edited requirements file in the same directory as the original so that any
# relative path references in the requirements file are still valid.
out_fd, edited_requirements_file = tempfile.mkstemp(
dir=os.path.dirname(requirements_file),
prefix="pex_lock_split.",
suffix=".{file_name}".format(file_name=os.path.basename(requirements_file)),
)
atexit.register(safe_delete, edited_requirements_file)
try:
with open(requirements_file, "rb") as in_fp:
for line_no, text in enumerate(in_fp, start=1):
if line_no in lines_to_comment_out:
os.write(out_fd, b"# ")
os.write(out_fd, text)
finally:
os.close(out_fd)
return edited_requirements_file
@attr.s
class Split(object):
requirements_by_project_name = attr.ib(
factory=OrderedDict
) # type: OrderedDict[ProjectName, ParsedRequirement]
provenance = attr.ib(factory=OrderedSet) # type: OrderedSet[ParsedRequirement]
def applies(
self,
universal_target, # type: UniversalTarget
requirement, # type: ParsedRequirement
):
# type: (...) -> bool
if not requirement.marker:
return True
requirements = [(req.marker, str(req)) for req in self.provenance if req.marker]
if not requirements:
return True
marker_env = attr.evolve(
universal_target, extra_markers=ExtraMarkers.extract(requirements)
).marker_env()
return marker_env.evaluate(requirement.marker)
def add(
self,
universal_target, # type: UniversalTarget
project_name, # type: ProjectName
requirement, # type: ParsedRequirement
):
# type: (...) -> Optional[Split]
if not self.applies(universal_target, requirement):
return None
existing_requirement = self.requirements_by_project_name.setdefault(
project_name, requirement
)
if existing_requirement == requirement:
return None
self.provenance.add(existing_requirement)
provenance = OrderedSet(req for req in self.provenance if req != existing_requirement)
provenance.add(requirement)
requirements_by_project_name = self.requirements_by_project_name.copy()
requirements_by_project_name[project_name] = requirement
return Split(
requirements_by_project_name=requirements_by_project_name, provenance=provenance
)
def requirement_configuration(
self,
unnamed_requirements, # type: Iterable[ParsedRequirement]
requirement_configuration, # type: RequirementConfiguration
network_configuration=None, # type: Optional[NetworkConfiguration]
):
# type: (...) -> RequirementConfiguration
if not self.provenance:
return requirement_configuration
requirements = list(unnamed_requirements)
requirement_files = OrderedSet(
os.path.realpath(requirement_file)
for requirement_file in requirement_configuration.requirement_files
) # type: OrderedSet[Text]
if requirement_files:
provenance_by_project_name = {
parsed_requirement.project_name: parsed_requirement
for parsed_requirement in self.provenance
if parsed_requirement.project_name
}
requirements_to_comment_out_by_source = defaultdict(
list
) # type: DefaultDict[Text, List[ParsedRequirement]]
for parsed_requirement in requirement_configuration.parse_requirements(
network_configuration=network_configuration
):
if not parsed_requirement.project_name:
continue
provenance = provenance_by_project_name.get(parsed_requirement.project_name)
if provenance and (parsed_requirement != provenance):
if parsed_requirement.line.source in requirement_files:
# We comment out the requirement
requirements_to_comment_out_by_source[
parsed_requirement.line.source
].append(parsed_requirement)
else:
# We drop the requirement
pass
elif parsed_requirement.line.source not in requirement_files:
requirements.append(parsed_requirement)
if requirements_to_comment_out_by_source:
new_requirement_files = OrderedSet() # type: OrderedSet[Text]
for source, parsed_requirements in requirements_to_comment_out_by_source.items():
if source in requirement_files:
new_requirement_files.add(
_comment_out_requirements(source, parsed_requirements)
)
else:
new_requirement_files.add(source)
requirement_files = new_requirement_files
else:
requirements.extend(self.requirements_by_project_name.values())
return RequirementConfiguration(
requirements=tuple(requirements),
requirement_files=tuple(requirement_files),
constraint_files=requirement_configuration.constraint_files,
)
def calculate_download_input(
targets, # type: Targets
requirement_configuration, # type: RequirementConfiguration
network_configuration, # type: NetworkConfiguration
repos_configuration, # type: ReposConfiguration
universal_target=None, # type: Optional[UniversalTarget]
):
# type: (...) -> DownloadInput
direct_requirements = requirement_configuration.parse_requirements(network_configuration)
if not universal_target:
return DownloadInput(
download_requests=tuple(
DownloadRequest.create(
target=target, requirement_configuration=requirement_configuration
)
for target in targets.unique_targets()
),
direct_requirements=direct_requirements,
)
target = LocalInterpreter.create(targets.interpreter)
split_markers = _calculate_split_markers(
requirement_configuration, network_configuration, repos_configuration
)
if not split_markers:
return DownloadInput(
download_requests=tuple(
[
DownloadRequest.create(
target=target,
universal_target=universal_target,
requirement_configuration=requirement_configuration,
)
]
),
direct_requirements=direct_requirements,
)
named_requirements = (
OrderedDict()
) # type: OrderedDict[ProjectName, OrderedSet[ParsedRequirement]]
unnamed_requirements = OrderedSet() # type: OrderedSet[ParsedRequirement]
for direct_requirement in direct_requirements:
if direct_requirement.project_name:
named_requirements.setdefault(direct_requirement.project_name, OrderedSet()).add(
direct_requirement
)
else:
unnamed_requirements.add(direct_requirement)
requirement_splits_by_universal_target = defaultdict(
lambda: [Split()]
) # type: DefaultDict[UniversalTarget, List[Split]]
for universal_target in _iter_universal_targets(universal_target, split_markers):
marker_env = universal_target.marker_env()
requirement_splits = requirement_splits_by_universal_target[universal_target]
for project_name, remote_requirements in named_requirements.items():
for requirement_split in list(requirement_splits):
for remote_requirement in remote_requirements:
if remote_requirement.marker and not marker_env.evaluate(
remote_requirement.marker
):
continue
new_split = requirement_split.add(
universal_target, project_name, remote_requirement
)
if new_split:
requirement_splits.append(new_split)
download_requests = []
for universal_target, splits in requirement_splits_by_universal_target.items():
if len(splits) == 1:
download_requests.append(
DownloadRequest.create(
target=target,
universal_target=universal_target,
requirement_configuration=splits[0].requirement_configuration(
unnamed_requirements,
requirement_configuration,
network_configuration=network_configuration,
),
)
)
continue
for split in splits:
download_requests.append(
DownloadRequest.create(
target=target,
universal_target=attr.evolve(
universal_target,
extra_markers=ExtraMarkers.extract(
(requirement.marker, str(requirement))
for requirement in split.provenance
if requirement.marker
),
),
requirement_configuration=split.requirement_configuration(
unnamed_requirements,
requirement_configuration,
network_configuration=network_configuration,
),
provenance="split by {requirements} {reqs}".format(
requirements=pluralize(split.provenance, "requirement"),
reqs=", ".join("'{req}'".format(req=req) for req in split.provenance),
),
)
)
return DownloadInput(
download_requests=tuple(download_requests), direct_requirements=direct_requirements
)