/
conda_solver.py
565 lines (505 loc) · 18.4 KB
/
conda_solver.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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import json
import logging
import os
import pathlib
import re
import shlex
import subprocess
import sys
import tempfile
from contextlib import contextmanager
from typing import (
Any,
Dict,
Iterable,
Iterator,
List,
MutableSequence,
Optional,
Sequence,
cast,
)
from urllib.parse import urlsplit, urlunsplit
from typing_extensions import TypedDict
from conda_lock.invoke_conda import (
PathLike,
_get_conda_flags,
conda_env_override,
conda_pkgs_dir,
is_micromamba,
)
from conda_lock.models.channel import Channel
from conda_lock.src_parser import (
Dependency,
HashModel,
LockedDependency,
VersionedDependency,
_apply_categories,
)
from conda_lock.vendor.conda.models.match_spec import MatchSpec
logger = logging.getLogger(__name__)
class FetchAction(TypedDict):
"""
FETCH actions include all the entries from the corresponding package's
repodata.json
"""
channel: str
constrains: Optional[List[str]]
depends: Optional[List[str]]
fn: str
md5: str
sha256: Optional[str]
name: str
subdir: str
timestamp: int
url: str
version: str
class LinkAction(TypedDict):
"""
LINK actions include only entries from conda-meta, notably missing
dependency and constraint information
"""
base_url: str
channel: str
dist_name: str
name: str
platform: str
version: str
class InstallActions(TypedDict):
LINK: List[LinkAction]
FETCH: List[FetchAction]
class DryRunInstall(TypedDict):
actions: InstallActions
def _to_match_spec(
conda_dep_name: str, conda_version: Optional[str], build: Optional[str]
) -> str:
kwargs = dict(name=conda_dep_name)
if conda_version:
kwargs["version"] = conda_version
if build:
kwargs["build"] = build
if "version" not in kwargs:
kwargs["version"] = "*"
ms = MatchSpec(**kwargs)
# Since MatchSpec doesn't round trip to the cli well
return ms.conda_build_form()
def extract_json_object(proc_stdout: str) -> str:
try:
return proc_stdout[proc_stdout.index("{") : proc_stdout.rindex("}") + 1]
except ValueError:
return proc_stdout
def solve_conda(
conda: PathLike,
specs: Dict[str, Dependency],
locked: Dict[str, LockedDependency],
update: List[str],
platform: str,
channels: List[Channel],
) -> Dict[str, LockedDependency]:
"""
Solve (or update a previous solution of) conda specs for the given platform
Parameters
----------
conda :
Path to conda, mamba, or micromamba
specs :
Conda package specifications
locked :
Previous solution for the given platform (conda packages only)
update :
Named of packages to update to the latest version compatible with specs
platform :
Target platform
channels :
Channels to query
"""
conda_specs = [
_to_match_spec(dep.name, dep.version, dep.build)
for dep in specs.values()
if isinstance(dep, VersionedDependency) and dep.manager == "conda"
]
conda_locked = {dep.name: dep for dep in locked.values() if dep.manager == "conda"}
to_update = set(update).intersection(conda_locked)
if to_update:
dry_run_install = update_specs_for_arch(
conda=conda,
platform=platform,
channels=channels,
specs=conda_specs,
locked=conda_locked,
update=list(to_update),
)
else:
dry_run_install = solve_specs_for_arch(
conda=conda,
platform=platform,
channels=channels,
specs=conda_specs,
)
logging.debug("dry_run_install:\n%s", dry_run_install)
def normalize_url(url: str) -> str:
for channel in channels:
candidate1 = channel.conda_token_replaced_url()
if url.startswith(candidate1):
url = url.replace(candidate1, channel.url, 1)
candidate2 = channel.env_replaced_url()
if url.startswith(candidate2):
url = url.replace(candidate2, channel.url, 1)
return url
# extract dependencies from package plan
planned = {
action["name"]: LockedDependency(
name=action["name"],
version=action["version"],
manager="conda",
platform=platform,
dependencies={
item.split()[0]: " ".join(item.split(" ")[1:])
for item in action.get("depends") or []
},
# TODO: Normalize URL here and inject env vars
url=normalize_url(action["url"]),
# NB: virtual packages may have no hash
hash=HashModel(
md5=action["md5"] if "md5" in action else "",
sha256=action.get("sha256"),
),
)
for action in dry_run_install["actions"]["FETCH"]
}
# propagate categories from explicit to transitive dependencies
_apply_categories(
requested={k: v for k, v in specs.items() if v.manager == "conda"},
planned=planned,
)
return planned
def _reconstruct_fetch_actions(
conda: PathLike, platform: str, dry_run_install: DryRunInstall
) -> DryRunInstall:
"""
Conda may choose to link a previously downloaded distribution from pkgs_dirs rather
than downloading a fresh one. Find the repodata record in existing distributions
that have only a LINK action, and use it to synthesize a corresponding FETCH action
with the metadata we need to extract for the package plan.
"""
if "LINK" not in dry_run_install["actions"]:
dry_run_install["actions"]["LINK"] = []
if "FETCH" not in dry_run_install["actions"]:
dry_run_install["actions"]["FETCH"] = []
link_actions = {p["name"]: p for p in dry_run_install["actions"]["LINK"]}
fetch_actions = {p["name"]: p for p in dry_run_install["actions"]["FETCH"]}
link_only_names = set(link_actions.keys()).difference(fetch_actions.keys())
# NB: micromamba does not support info --json, nor does it appear to honor pkgs_dirs from .condarc
if not is_micromamba(conda):
if link_only_names:
pkgs_dirs = [
pathlib.Path(d)
for d in json.loads(
extract_json_object(
subprocess.check_output(
[str(conda), "info", "--json"],
env=conda_env_override(platform),
).decode()
)
)["pkgs_dirs"]
]
else:
pkgs_dirs = []
for link_pkg_name in link_only_names:
link_action = link_actions[link_pkg_name]
for pkgs_dir in pkgs_dirs:
record = (
pkgs_dir
/ link_action["dist_name"]
/ "info"
/ "repodata_record.json"
)
if record.exists():
with open(record) as f:
repodata: FetchAction = json.load(f)
break
else:
raise FileExistsError(
f'Distribution \'{link_action["dist_name"]}\' not found in pkgs_dirs {pkgs_dirs}'
)
dry_run_install["actions"]["FETCH"].append(repodata)
else:
# NB: micromamba LINK actions contain the same metadata as FETCH
# actions, and so can be used to fill out the FETCH section.
# Explicitly copy key-by-key to make missing keys obvious, should
# this change in the future.
for link_pkg_name in link_only_names:
item = cast(Dict[str, Any], link_actions[link_pkg_name])
repodata = {
"channel": item["channel"],
"constrains": item.get("constrains"),
"depends": item.get("depends"),
"fn": item["fn"],
"md5": item["md5"],
"name": item["name"],
"subdir": item["subdir"],
"timestamp": item["timestamp"],
"url": item["url"],
"version": item["version"],
"sha256": item.get("sha256"),
}
dry_run_install["actions"]["FETCH"].append(repodata)
return dry_run_install
def solve_specs_for_arch(
conda: PathLike,
channels: Sequence[Channel],
specs: List[str],
platform: str,
) -> DryRunInstall:
"""
Solve conda specifications for the given platform
Parameters
----------
conda :
Path to conda, mamba, or micromamba
channels :
Channels to query
specs :
Conda package specifications
platform :
Target platform
"""
args: MutableSequence[str] = [
str(conda),
"create",
"--prefix",
os.path.join(conda_pkgs_dir(), "prefix"),
"--dry-run",
"--json",
]
conda_flags = os.environ.get("CONDA_FLAGS")
if conda_flags:
args.extend(shlex.split(conda_flags))
if channels:
args.append("--override-channels")
for channel in channels:
args.extend(["--channel", channel.env_replaced_url()])
if channel == "defaults" and platform in {"win-64", "win-32"}:
# msys2 is a windows-only channel that conda automatically
# injects if the host platform is Windows. If our host
# platform is not Windows, we need to add it manually
args.extend(["--channel", "msys2"])
args.extend(specs)
logger.info("%s using specs %s", platform, specs)
proc = subprocess.run(
[str(arg) for arg in args],
env=conda_env_override(platform),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf8",
)
def print_proc(proc: subprocess.CompletedProcess) -> None:
print(f" Command: {proc.args}", file=sys.stderr)
if proc.stdout:
print(f" STDOUT:\n{proc.stdout}", file=sys.stderr)
if proc.stderr:
print(f" STDERR:\n{proc.stderr}", file=sys.stderr)
try:
proc.check_returncode()
except subprocess.CalledProcessError:
try:
err_json = json.loads(proc.stdout)
try:
message = err_json["message"]
except KeyError:
print("Message key not found in json! returning the full json text")
message = err_json
except json.JSONDecodeError as e:
print(f"Failed to parse json, {e}", file=sys.stderr)
message = proc.stdout
print(
f"Could not lock the environment for platform {platform}", file=sys.stderr
)
if message:
print(message, file=sys.stderr)
print_proc(proc)
raise
try:
dryrun_install: DryRunInstall = json.loads(extract_json_object(proc.stdout))
return _reconstruct_fetch_actions(conda, platform, dryrun_install)
except json.JSONDecodeError:
raise
def update_specs_for_arch(
conda: PathLike,
specs: List[str],
locked: Dict[str, LockedDependency],
update: List[str],
platform: str,
channels: Sequence[Channel],
) -> DryRunInstall:
"""
Update a previous solution for the given platform
Parameters
----------
conda :
Path to conda, mamba, or micromamba
specs :
Conda package specifications
locked :
Previous solution for the given platform (conda packages only)
update :
Named of packages to update to the latest version compatible with specs
platform :
Target platform
channels :
Channels to query
"""
with fake_conda_environment(locked.values(), platform=platform) as prefix:
installed: Dict[str, LinkAction] = {
entry["name"]: entry
for entry in json.loads(
subprocess.check_output(
[str(conda), "list", "-p", prefix, "--json"],
env=conda_env_override(platform),
)
)
}
spec_for_name = {MatchSpec(v).name: v for v in specs}
to_update = [
spec_for_name[name] for name in set(installed).intersection(update)
]
if to_update:
# NB: [micro]mamba and mainline conda have different semantics for `install` and `update`
# - conda:
# * update -> apply all nonmajor updates unconditionally (unless pinned)
# * install -> install or update target to latest version compatible with constraint
# - micromamba:
# * update -> update target to latest version compatible with constraint
# * install -> update target if current version incompatible with constraint, otherwise _do nothing_
# - mamba:
# * update -> apply all nonmajor updates unconditionally (unless pinned)
# * install -> update target if current version incompatible with constraint, otherwise _do nothing_
# Our `update` should always update the target to the latest version compatible with the constraint,
# while updating as few other packages as possible. With mamba this can only be done with pinning.
if pathlib.Path(conda).name.startswith("mamba"):
# pin non-updated packages to prevent _any_ movement
pinned_filename = pathlib.Path(prefix) / "conda-meta" / "pinned"
assert not pinned_filename.exists()
with open(pinned_filename, "w") as pinned:
for name in set(installed.keys()).difference(update):
pinned.write(f'{name} =={installed[name]["version"]}\n')
args = [
str(conda),
"update",
*_get_conda_flags(channels=channels, platform=platform),
]
print(
"Warning: mamba cannot update single packages without resorting to pinning. "
"If the update fails to solve, try with conda or micromamba instead.",
file=sys.stderr,
)
else:
args = [
str(conda),
"update" if is_micromamba(conda) else "install",
*_get_conda_flags(channels=channels, platform=platform),
]
proc = subprocess.run(
[
str(arg)
for arg in args + ["-p", prefix, "--json", "--dry-run", *to_update]
],
env=conda_env_override(platform),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf8",
)
try:
proc.check_returncode()
except subprocess.CalledProcessError as exc:
err_json = json.loads(proc.stdout)
raise RuntimeError(
f"Could not lock the environment for platform {platform}: {err_json.get('message')}"
) from exc
dryrun_install: DryRunInstall = json.loads(proc.stdout)
else:
dryrun_install = {"actions": {"LINK": [], "FETCH": []}}
if "actions" not in dryrun_install:
dryrun_install["actions"] = {"LINK": [], "FETCH": []}
updated = {entry["name"]: entry for entry in dryrun_install["actions"]["LINK"]}
for package in set(installed).difference(updated):
entry = installed[package]
fn = f'{entry["dist_name"]}.tar.bz2'
if is_micromamba(conda):
channel = f'{entry["base_url"]}'
else:
channel = f'{entry["base_url"]}/{entry["platform"]}'
url = f"{channel}/{fn}"
md5 = locked[package].hash.md5
if md5 is None:
raise RuntimeError("Conda packages require non-null md5 hashes")
sha256 = locked[package].hash.sha256
dryrun_install["actions"]["FETCH"].append(
{
"name": entry["name"],
"channel": channel,
"url": url,
"fn": fn,
"md5": md5,
"sha256": sha256,
"version": entry["version"],
"depends": [
f"{k} {v}".strip()
for k, v in locked[entry["name"]].dependencies.items()
],
"constrains": [],
"subdir": entry["platform"],
"timestamp": 0,
}
)
dryrun_install["actions"]["LINK"].append(entry)
return _reconstruct_fetch_actions(conda, platform, dryrun_install)
@contextmanager
def fake_conda_environment(
locked: Iterable[LockedDependency], platform: str
) -> Iterator[str]:
"""
Create a fake conda prefix containing metadata corresponding to the provided dependencies
Parameters
----------
locked :
Previous solution
platform :
Target platform
"""
with tempfile.TemporaryDirectory() as prefix:
conda_meta = pathlib.Path(prefix) / "conda-meta"
conda_meta.mkdir()
(conda_meta / "history").touch()
for dep in (
dep for dep in locked if dep.manager == "conda" and dep.platform == platform
):
url = urlsplit(dep.url)
path = pathlib.PurePosixPath(url.path)
channel = urlunsplit(
(url.scheme, url.hostname, str(path.parent), None, None)
)
while path.suffix in {".tar", ".bz2", ".gz", ".conda"}:
path = path.with_suffix("")
build = path.name.split("-")[-1]
try:
build_number = int(build.split("_")[-1])
except ValueError:
build_number = 0
entry = {
"name": dep.name,
"channel": channel,
"url": dep.url,
"md5": dep.hash.md5,
"build": build,
"build_number": build_number,
"version": dep.version,
"subdir": path.parent.name,
"depends": [f"{k} {v}".strip() for k, v in dep.dependencies.items()],
}
# mamba requires these to be stringlike so null are not allowed here
if dep.hash.sha256 is not None:
entry["sha256"] = dep.hash.sha256
with open(conda_meta / (path.name + ".json"), "w") as f:
json.dump(entry, f, indent=2)
yield prefix