calkit-python 0.50.6__py3-none-any.whl → 0.51.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.
- calkit/__init__.py +0 -1
- calkit/agent_skills/check-questions/SKILL.md +8 -1
- calkit/cli/check.py +32 -545
- calkit/cli/hub/__init__.py +2 -0
- calkit/cli/{hub.py → hub/core.py} +6 -0
- calkit/cli/hub/link.py +468 -0
- calkit/cli/import_.py +121 -66
- calkit/cli/latex.py +13 -4
- calkit/cli/main/core.py +230 -217
- calkit/cli/new.py +114 -1
- calkit/cli/notebooks.py +2 -2
- calkit/cli/open_.py +14 -3
- calkit/cli/show.py +10 -2
- calkit/cli/sync.py +7 -5
- calkit/cli/update.py +113 -34
- calkit/conda.py +1 -1
- calkit/config.py +16 -4
- calkit/core.py +223 -35
- calkit/detect.py +50 -0
- calkit/docker.py +5 -320
- calkit/docx.py +17 -0
- calkit/dvc/core.py +188 -5
- calkit/dvc/zip.py +40 -1
- calkit/environments/__init__.py +3 -0
- calkit/{environments.py → environments/core.py} +244 -99
- calkit/environments/docker.py +1080 -0
- calkit/git.py +26 -0
- calkit/hub/__init__.py +8 -0
- calkit/{hub.py → hub/core.py} +108 -13
- calkit/hub/link.py +4827 -0
- calkit/install.py +33 -1
- calkit/markdown.py +454 -50
- calkit/models/core.py +101 -13
- calkit/notebooks.py +1 -1
- calkit/pipeline.py +65 -14
- calkit/provenance.py +141 -7
- calkit/questions.py +193 -30
- calkit/releases.py +5 -2
- calkit/reproducibility.py +6 -4
- calkit/tests/cli/hub/__init__.py +0 -0
- calkit/tests/cli/{test_hub.py → hub/test_core.py} +21 -18
- calkit/tests/cli/hub/test_link.py +249 -0
- calkit/tests/cli/main/test_core.py +182 -52
- calkit/tests/cli/test_check.py +129 -27
- calkit/tests/cli/test_import.py +98 -0
- calkit/tests/cli/test_latex.py +3 -1
- calkit/tests/cli/test_new.py +176 -0
- calkit/tests/cli/test_notebooks.py +6 -0
- calkit/tests/cli/test_open.py +11 -2
- calkit/tests/cli/test_show.py +7 -0
- calkit/tests/cli/test_update.py +166 -5
- calkit/tests/dvc/test_core.py +208 -4
- calkit/tests/dvc/test_zip.py +42 -0
- calkit/tests/environments/__init__.py +0 -0
- calkit/tests/{test_environments.py → environments/test_core.py} +174 -20
- calkit/tests/environments/test_docker.py +218 -0
- calkit/tests/hub/__init__.py +0 -0
- calkit/tests/{test_hub.py → hub/test_core.py} +136 -12
- calkit/tests/hub/test_link.py +3016 -0
- calkit/tests/models/test_core.py +57 -0
- calkit/tests/test_conda.py +2 -2
- calkit/tests/test_config.py +19 -0
- calkit/tests/test_core.py +59 -0
- calkit/tests/test_detect.py +57 -0
- calkit/tests/test_docker.py +0 -189
- calkit/tests/test_docx.py +21 -0
- calkit/tests/test_install.py +19 -0
- calkit/tests/test_markdown.py +262 -1
- calkit/tests/test_pipeline.py +41 -0
- calkit/tests/test_provenance.py +160 -0
- calkit/tests/test_questions.py +214 -2
- calkit/tests/test_reproducibility.py +13 -2
- calkit/tests/test_upgrade.py +46 -0
- calkit/upgrade.py +32 -7
- {calkit_python-0.50.6.dist-info → calkit_python-0.51.0.dist-info}/METADATA +18 -4
- {calkit_python-0.50.6.dist-info → calkit_python-0.51.0.dist-info}/RECORD +92 -81
- calkit/server.py +0 -652
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/etc/jupyter/jupyter_server_config.d/calkit.json +0 -0
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/share/jupyter/labextensions/calkit/package.json +0 -0
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/share/jupyter/labextensions/calkit/schemas/calkit/package.json.orig +0 -0
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/share/jupyter/labextensions/calkit/schemas/calkit/plugin.json +0 -0
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/share/jupyter/labextensions/calkit/static/506.c34b070098184e33.js +0 -0
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/share/jupyter/labextensions/calkit/static/57d13a7399cec3c5.png +0 -0
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/share/jupyter/labextensions/calkit/static/616.528427eb54a4a0c0.js +0 -0
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/share/jupyter/labextensions/calkit/static/740.6bf87276e9e5788a.js +0 -0
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/share/jupyter/labextensions/calkit/static/899.f9b9f9bf705a6493.js +0 -0
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/share/jupyter/labextensions/calkit/static/935.46ecc6bf99aa593a.js +0 -0
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/share/jupyter/labextensions/calkit/static/remoteEntry.109a3a8379365e59.js +0 -0
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/share/jupyter/labextensions/calkit/static/style.js +0 -0
- {calkit_python-0.50.6.data → calkit_python-0.51.0.data}/data/share/jupyter/labextensions/calkit/static/third-party-licenses.json +0 -0
- {calkit_python-0.50.6.dist-info → calkit_python-0.51.0.dist-info}/WHEEL +0 -0
- {calkit_python-0.50.6.dist-info → calkit_python-0.51.0.dist-info}/entry_points.txt +0 -0
- {calkit_python-0.50.6.dist-info → calkit_python-0.51.0.dist-info}/licenses/LICENSE +0 -0
calkit/__init__.py
CHANGED
|
@@ -38,6 +38,8 @@ Deterministic, done by `calkit check questions` — never re-derive by hand:
|
|
|
38
38
|
- every clause of a conditional answer parses, names only evidence, and
|
|
39
39
|
renders, including the clauses the current values don't select;
|
|
40
40
|
- every publication `label` still exists in the LaTeX source;
|
|
41
|
+
- every `lines` range on a plain file entry, one with no `kind`, is still
|
|
42
|
+
within the file;
|
|
41
43
|
- no evidence has changed (Git history for Git-tracked outputs, `dvc.lock`
|
|
42
44
|
for DVC-tracked ones) since the commit that last edited the question;
|
|
43
45
|
- no verification is stale, i.e., neither the approach nor any of the
|
|
@@ -54,7 +56,8 @@ Deterministic, done by `calkit check questions` — never re-derive by hand:
|
|
|
54
56
|
nothing computes is a magic number;
|
|
55
57
|
- each other evidence path is produced by a pipeline stage, or declared
|
|
56
58
|
with `imported_from` or `created_by`. This one is advisory: it is
|
|
57
|
-
reported as `unattributed` and does not fail the check.
|
|
59
|
+
reported as `unattributed` and does not fail the check. A file inside a
|
|
60
|
+
directory a stage lists as an output is produced by that stage.
|
|
58
61
|
|
|
59
62
|
Judgment, done here — the check reads paths and hashes, and cannot read a
|
|
60
63
|
sentence:
|
|
@@ -194,6 +197,10 @@ that is a false statement about the project, not a tidy-up.
|
|
|
194
197
|
precision the claim needs: `{ratio:.1f}x`, `{error:.0%}`. When several
|
|
195
198
|
come from one file, e.g., the outputs of one calculation, name them
|
|
196
199
|
together in one `result` entry's `values`, a map of name to key.
|
|
200
|
+
- To cite a file as it is, e.g., a log, a transcript excerpt, or a
|
|
201
|
+
script, give just its `path`, with no `kind`, and the `lines` that
|
|
202
|
+
matter, e.g., `lines: 8` or `lines: 12-20`. Don't use `document` for
|
|
203
|
+
these; that is for write-ups a Markdown stage builds.
|
|
197
204
|
- A `value` entry must read a file a pipeline stage writes. A placeholder
|
|
198
205
|
over a results file written by hand, including one you write, is a
|
|
199
206
|
retyped number with extra steps, and the check fails it. If no stage
|
calkit/cli/check.py
CHANGED
|
@@ -9,7 +9,6 @@ import platform as _platform
|
|
|
9
9
|
import re
|
|
10
10
|
import shutil
|
|
11
11
|
import subprocess
|
|
12
|
-
import textwrap
|
|
13
12
|
from typing import Annotated, Callable
|
|
14
13
|
|
|
15
14
|
import dotenv
|
|
@@ -17,7 +16,6 @@ import typer
|
|
|
17
16
|
|
|
18
17
|
import calkit
|
|
19
18
|
from calkit.cli import AliasGroup, raise_error, warn, warn_app_control
|
|
20
|
-
from calkit.core import get_md5
|
|
21
19
|
|
|
22
20
|
|
|
23
21
|
def stage_calkit_files(*args: object, **kwargs: object) -> None:
|
|
@@ -497,7 +495,6 @@ def check_environment(
|
|
|
497
495
|
"""
|
|
498
496
|
from calkit.environments import (
|
|
499
497
|
get_all_conda_lock_fpaths,
|
|
500
|
-
get_all_docker_lock_fpaths,
|
|
501
498
|
get_all_venv_lock_fpaths,
|
|
502
499
|
get_default_venv_prefix,
|
|
503
500
|
get_env_lock_fpath,
|
|
@@ -535,48 +532,9 @@ def check_environment(
|
|
|
535
532
|
if removed and verbose:
|
|
536
533
|
typer.echo(f"Removed {removed} to rebuild it")
|
|
537
534
|
if env["kind"] == "docker":
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
)
|
|
541
|
-
legacy_lock_fpath = get_env_lock_fpath(
|
|
542
|
-
env=env, env_name=env_name, as_posix=False, legacy=True
|
|
543
|
-
)
|
|
544
|
-
# Alt lock paths include other architectures
|
|
545
|
-
alt_lock_fpaths = get_all_docker_lock_fpaths(
|
|
546
|
-
env_name=env_name, as_posix=False
|
|
547
|
-
)
|
|
548
|
-
image = calkit.docker.get_image_name(env, env_name)
|
|
549
|
-
if image is None:
|
|
550
|
-
if not env.get("path"):
|
|
551
|
-
raise_error(
|
|
552
|
-
f"Environment '{env_name}' must define an image, since "
|
|
553
|
-
"it has no Dockerfile to build one from"
|
|
554
|
-
)
|
|
555
|
-
raise_error(
|
|
556
|
-
f"Cannot work out what to call the image for environment "
|
|
557
|
-
f"'{env_name}': set 'image' on it, or set 'owner' and "
|
|
558
|
-
"'name' in calkit.yaml"
|
|
559
|
-
)
|
|
560
|
-
check_docker_env(
|
|
561
|
-
tag=image,
|
|
562
|
-
fpath=env.get("path"),
|
|
563
|
-
lock_fpath=lock_fpath,
|
|
564
|
-
alt_lock_fpaths_delete=[str(legacy_lock_fpath)],
|
|
565
|
-
alt_lock_fpaths=alt_lock_fpaths,
|
|
566
|
-
platform=env.get("platform"),
|
|
567
|
-
deps=calkit.environments.get_env_input_paths(env, env_name),
|
|
568
|
-
env_vars=env.get("env_vars", []),
|
|
569
|
-
ports=env.get("ports", []),
|
|
570
|
-
gpus=env.get("gpus"),
|
|
571
|
-
user=env.get("user"),
|
|
572
|
-
wdir=env.get("wdir"),
|
|
573
|
-
args=env.get("args", []),
|
|
574
|
-
build_platforms=env.get("build_platforms", []),
|
|
575
|
-
registry=env.get("registry"),
|
|
576
|
-
lock_archs=calkit.docker.get_lock_archs(env),
|
|
577
|
-
quiet=not verbose,
|
|
578
|
-
rebuild=rebuild,
|
|
579
|
-
)
|
|
535
|
+
from calkit.environments import docker as env_docker
|
|
536
|
+
|
|
537
|
+
env_docker.check(env_name, env, rebuild=rebuild, verbose=verbose)
|
|
580
538
|
elif env["kind"] == "conda":
|
|
581
539
|
lock_fpath = get_env_lock_fpath(
|
|
582
540
|
env=env, env_name=env_name, as_posix=False
|
|
@@ -1278,504 +1236,29 @@ def check_docker_env(
|
|
|
1278
1236
|
] = False,
|
|
1279
1237
|
) -> None:
|
|
1280
1238
|
"""Check that Docker environment is up-to-date."""
|
|
1281
|
-
from calkit import docker as
|
|
1282
|
-
from calkit.core import is_tty
|
|
1283
|
-
from calkit.environments import get_docker_arch
|
|
1239
|
+
from calkit.environments import docker as env_docker
|
|
1284
1240
|
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
deps_md5s = {}
|
|
1305
|
-
for dep in deps:
|
|
1306
|
-
deps_md5s[dep] = get_md5(dep, exclude_files=[lock_fpath])
|
|
1307
|
-
|
|
1308
|
-
def read_lock(path: str) -> dict | None:
|
|
1309
|
-
# A lock that can't be read is treated as one that isn't there:
|
|
1310
|
-
# rebuilding is always an option, so there's no reason to fail
|
|
1311
|
-
try:
|
|
1312
|
-
with open(path) as f:
|
|
1313
|
-
loaded = json.load(f)
|
|
1314
|
-
except (OSError, json.JSONDecodeError):
|
|
1315
|
-
return None
|
|
1316
|
-
# Handle legacy lock files that are lists
|
|
1317
|
-
if isinstance(loaded, list):
|
|
1318
|
-
loaded = loaded[0] if loaded else None
|
|
1319
|
-
return loaded if isinstance(loaded, dict) else None
|
|
1320
|
-
|
|
1321
|
-
# Read the lock file, falling back to legacy paths and then to other
|
|
1322
|
-
# architectures, which still identify the image well enough to pull it
|
|
1323
|
-
lock = None
|
|
1324
|
-
lock_is_current_arch = False
|
|
1325
|
-
if os.path.isfile(lock_fpath):
|
|
1326
|
-
typer.echo(f"Reading lock file: {lock_fpath}", file=outfile)
|
|
1327
|
-
lock = read_lock(lock_fpath)
|
|
1328
|
-
lock_is_current_arch = lock is not None
|
|
1329
|
-
else:
|
|
1330
|
-
typer.echo(f"Lock file ({lock_fpath}) does not exist", file=outfile)
|
|
1331
|
-
for alt_lock_fpath in alt_lock_fpaths_delete:
|
|
1332
|
-
if os.path.isfile(alt_lock_fpath):
|
|
1333
|
-
typer.echo(
|
|
1334
|
-
f"Reading alternative lock file: {alt_lock_fpath}",
|
|
1335
|
-
file=outfile,
|
|
1336
|
-
)
|
|
1337
|
-
lock = read_lock(alt_lock_fpath)
|
|
1338
|
-
# A legacy lock was written before locks were kept per
|
|
1339
|
-
# architecture, by the machine that checked the
|
|
1340
|
-
# environment, so it describes this one. Taking it for
|
|
1341
|
-
# another architecture's would skip checking the local image
|
|
1342
|
-
# against it, and an image left under this tag by something
|
|
1343
|
-
# else would be locked in as this environment's
|
|
1344
|
-
lock_is_current_arch = lock is not None
|
|
1345
|
-
os.remove(alt_lock_fpath)
|
|
1346
|
-
break
|
|
1347
|
-
if lock is None:
|
|
1348
|
-
for alt_lock_fpath in alt_lock_fpaths:
|
|
1349
|
-
if os.path.isfile(alt_lock_fpath):
|
|
1350
|
-
typer.echo(
|
|
1351
|
-
f"Reading alternative lock file: {alt_lock_fpath}",
|
|
1352
|
-
file=outfile,
|
|
1353
|
-
)
|
|
1354
|
-
lock = read_lock(alt_lock_fpath)
|
|
1355
|
-
if lock is not None:
|
|
1356
|
-
break
|
|
1357
|
-
# A lock that doesn't describe the current spec is stale, not merely
|
|
1358
|
-
# out-of-date with the local image: the image it identifies was built
|
|
1359
|
-
# from a different Dockerfile or dependencies, so it must not be pulled
|
|
1360
|
-
# in place of the one asked for
|
|
1361
|
-
if lock is not None and not ck_docker.lock_matches_spec(
|
|
1362
|
-
lock, dockerfile_md5=dockerfile_md5, deps_md5s=deps_md5s
|
|
1363
|
-
):
|
|
1364
|
-
typer.echo(
|
|
1365
|
-
"Lock file does not match the current environment", file=outfile
|
|
1366
|
-
)
|
|
1367
|
-
lock = None
|
|
1368
|
-
lock_is_current_arch = False
|
|
1369
|
-
# Without a Dockerfile, rebuilding means pulling the image again
|
|
1370
|
-
if rebuild:
|
|
1371
|
-
lock = None
|
|
1372
|
-
lock_is_current_arch = False
|
|
1373
|
-
# Work out where this image lives in a registry, so it can be pulled
|
|
1374
|
-
# instead of rebuilt, and pushed after being built. Only an image built
|
|
1375
|
-
# from a Dockerfile goes to the project's registry; one named directly
|
|
1376
|
-
# already lives somewhere it can be pulled back from.
|
|
1377
|
-
remote_ref = None
|
|
1378
|
-
registry_prefix = ck_docker.resolve_registry_prefix(
|
|
1379
|
-
{"registry": registry, "path": fpath}
|
|
1380
|
-
)
|
|
1381
|
-
if registry_prefix is None and ck_docker.registry_is_auto(registry):
|
|
1382
|
-
warn(
|
|
1383
|
-
"Could not work out a registry for this project; name one "
|
|
1384
|
-
"explicitly, e.g., 'ghcr.io/someone/some-project', or set "
|
|
1385
|
-
"a GitHub remote"
|
|
1386
|
-
)
|
|
1387
|
-
remote_repo = None
|
|
1388
|
-
if registry_prefix is not None and fpath is not None:
|
|
1389
|
-
remote_ref = ck_docker.get_remote_image_ref(tag, registry_prefix)
|
|
1390
|
-
remote_repo = ck_docker.get_repo_from_ref(remote_ref)
|
|
1391
|
-
# Where a digest the lock records can be pulled from, since the lock
|
|
1392
|
-
# names the digest alone: the project's registry for an image we build,
|
|
1393
|
-
# or the image's own repo for one named directly. Working this out from
|
|
1394
|
-
# the environment definition rather than the lock is what stops a digest
|
|
1395
|
-
# left over from a different image resolving to anything
|
|
1396
|
-
digest_source_ref = remote_ref if remote_ref is not None else tag
|
|
1397
|
-
typer.echo(f"Checking for existing image with tag {tag}", file=outfile)
|
|
1398
|
-
identity = ck_docker.inspect_image_for_lock(tag)
|
|
1399
|
-
if identity is None:
|
|
1400
|
-
typer.echo(f"No image with tag {tag} found locally", file=outfile)
|
|
1401
|
-
# Only a lock for this architecture can say whether the image here is
|
|
1402
|
-
# the one it describes: another architecture's layers never match, so
|
|
1403
|
-
# its lock is taken as an instruction to fetch the image it names rather
|
|
1404
|
-
# than as a blessing for whatever happens to carry this tag
|
|
1405
|
-
up_to_date = (
|
|
1406
|
-
identity is not None
|
|
1407
|
-
and lock is not None
|
|
1408
|
-
and lock_is_current_arch
|
|
1409
|
-
and ck_docker.lock_matches_image(lock, identity)
|
|
1241
|
+
env_docker.check_image(
|
|
1242
|
+
tag=tag,
|
|
1243
|
+
fpath=fpath,
|
|
1244
|
+
lock_fpath=lock_fpath,
|
|
1245
|
+
alt_lock_fpaths=alt_lock_fpaths,
|
|
1246
|
+
alt_lock_fpaths_delete=alt_lock_fpaths_delete,
|
|
1247
|
+
platform=platform,
|
|
1248
|
+
user=user,
|
|
1249
|
+
wdir=wdir,
|
|
1250
|
+
deps=deps,
|
|
1251
|
+
env_vars=env_vars,
|
|
1252
|
+
ports=ports,
|
|
1253
|
+
gpus=gpus,
|
|
1254
|
+
args=args,
|
|
1255
|
+
build_platforms=build_platforms,
|
|
1256
|
+
registry=registry,
|
|
1257
|
+
lock_archs=lock_archs,
|
|
1258
|
+
quiet=quiet,
|
|
1259
|
+
rebuild=rebuild,
|
|
1410
1260
|
)
|
|
1411
1261
|
|
|
1412
|
-
def delete_lock_on_failure() -> None:
|
|
1413
|
-
if lock_fpath and os.path.exists(lock_fpath):
|
|
1414
|
-
os.remove(lock_fpath)
|
|
1415
|
-
|
|
1416
|
-
pulled_from_registry = False
|
|
1417
|
-
already_pushed = False
|
|
1418
|
-
built = False
|
|
1419
|
-
if not up_to_date:
|
|
1420
|
-
obtained = False
|
|
1421
|
-
# Prefer pulling the exact image the lock identifies, since a rebuild
|
|
1422
|
-
# can't reproduce it, and would silently pick up whatever its
|
|
1423
|
-
# undeclared upstream dependencies have become
|
|
1424
|
-
if lock is not None:
|
|
1425
|
-
for digest_ref in ck_docker.get_lock_digest_refs(
|
|
1426
|
-
lock, digest_source_ref
|
|
1427
|
-
):
|
|
1428
|
-
typer.echo(f"Pulling image by digest: {digest_ref}", err=True)
|
|
1429
|
-
# A private image needs credentials we may be able to get,
|
|
1430
|
-
# but only a registry that refused us is worth logging in to
|
|
1431
|
-
if not ck_docker.pull_image_with_login(
|
|
1432
|
-
digest_ref, platform=platform
|
|
1433
|
-
):
|
|
1434
|
-
warn(f"Failed to pull image by digest: {digest_ref}")
|
|
1435
|
-
continue
|
|
1436
|
-
if not ck_docker.tag_image(digest_ref, tag):
|
|
1437
|
-
warn(f"Failed to tag pulled image as {tag}")
|
|
1438
|
-
continue
|
|
1439
|
-
identity = ck_docker.inspect_image_for_lock(tag)
|
|
1440
|
-
if identity is None:
|
|
1441
|
-
continue
|
|
1442
|
-
if lock_is_current_arch and not ck_docker.lock_matches_image(
|
|
1443
|
-
lock, identity
|
|
1444
|
-
):
|
|
1445
|
-
warn(
|
|
1446
|
-
f"Image pulled from {digest_ref} does not match the "
|
|
1447
|
-
"lock file"
|
|
1448
|
-
)
|
|
1449
|
-
continue
|
|
1450
|
-
obtained = True
|
|
1451
|
-
pulled_from_registry = (
|
|
1452
|
-
remote_repo is not None
|
|
1453
|
-
and digest_ref.split("@", 1)[0] == remote_repo
|
|
1454
|
-
)
|
|
1455
|
-
break
|
|
1456
|
-
# A lock from another architecture named an image we've now tried
|
|
1457
|
-
# to pull. Failing that, an image already here was built from this
|
|
1458
|
-
# same spec, so it stands rather than being rebuilt for a lock that
|
|
1459
|
-
# was never able to describe it
|
|
1460
|
-
if (
|
|
1461
|
-
not obtained
|
|
1462
|
-
and identity is not None
|
|
1463
|
-
and lock is not None
|
|
1464
|
-
and not lock_is_current_arch
|
|
1465
|
-
):
|
|
1466
|
-
obtained = True
|
|
1467
|
-
# Fall back to an image archived in a release, since a registry makes
|
|
1468
|
-
# no promise to keep an image forever, and rebuilding can't reproduce
|
|
1469
|
-
# one whose upstream dependencies have moved on
|
|
1470
|
-
if not obtained and lock is not None and lock_is_current_arch:
|
|
1471
|
-
layers = (lock.get("RootFS") or {}).get("Layers") or []
|
|
1472
|
-
archived = calkit.releases.find_archived_docker_image(layers)
|
|
1473
|
-
if archived is not None:
|
|
1474
|
-
release_name, image_id, entry = archived
|
|
1475
|
-
typer.echo(
|
|
1476
|
-
f"Fetching image archived in release '{release_name}'",
|
|
1477
|
-
err=True,
|
|
1478
|
-
)
|
|
1479
|
-
if calkit.releases.fetch_archived_docker_image(
|
|
1480
|
-
release_name, entry
|
|
1481
|
-
) and ck_docker.tag_image(image_id, tag):
|
|
1482
|
-
identity = ck_docker.inspect_image_for_lock(tag)
|
|
1483
|
-
if identity is not None and ck_docker.lock_matches_image(
|
|
1484
|
-
lock, identity
|
|
1485
|
-
):
|
|
1486
|
-
obtained = True
|
|
1487
|
-
else:
|
|
1488
|
-
warn(
|
|
1489
|
-
f"Image archived in release '{release_name}' does "
|
|
1490
|
-
"not match the lock file"
|
|
1491
|
-
)
|
|
1492
|
-
else:
|
|
1493
|
-
warn(
|
|
1494
|
-
"Failed to fetch image archived in release "
|
|
1495
|
-
f"'{release_name}'"
|
|
1496
|
-
)
|
|
1497
|
-
if not obtained and fpath is not None:
|
|
1498
|
-
dockerfile_dir, dockerfile_name = os.path.split(fpath)
|
|
1499
|
-
build_cwd = dockerfile_dir if dockerfile_dir else None
|
|
1500
|
-
# A multi-platform image can't live in the local image store, so
|
|
1501
|
-
# it's built straight into the registry and pulled back for the
|
|
1502
|
-
# platform we're on
|
|
1503
|
-
multi_platform = len(build_platforms) > 1
|
|
1504
|
-
if multi_platform and remote_ref is None:
|
|
1505
|
-
raise_error(
|
|
1506
|
-
"Building for multiple platforms requires a registry; "
|
|
1507
|
-
"set 'registry' on this environment"
|
|
1508
|
-
)
|
|
1509
|
-
if multi_platform:
|
|
1510
|
-
assert remote_ref is not None
|
|
1511
|
-
cmd = [
|
|
1512
|
-
"docker",
|
|
1513
|
-
"buildx",
|
|
1514
|
-
"build",
|
|
1515
|
-
"--platform",
|
|
1516
|
-
",".join(build_platforms),
|
|
1517
|
-
"-t",
|
|
1518
|
-
remote_ref,
|
|
1519
|
-
"--push",
|
|
1520
|
-
"-f",
|
|
1521
|
-
dockerfile_name,
|
|
1522
|
-
]
|
|
1523
|
-
if rebuild:
|
|
1524
|
-
cmd += ["--no-cache", "--pull"]
|
|
1525
|
-
cmd.append(".")
|
|
1526
|
-
else:
|
|
1527
|
-
cmd = ["docker", "build", "-t", tag, "-f", dockerfile_name]
|
|
1528
|
-
if rebuild:
|
|
1529
|
-
cmd += ["--no-cache", "--pull"]
|
|
1530
|
-
build_platform = (
|
|
1531
|
-
build_platforms[0] if build_platforms else platform
|
|
1532
|
-
)
|
|
1533
|
-
if build_platform is not None:
|
|
1534
|
-
cmd += ["--platform", build_platform]
|
|
1535
|
-
cmd.append(".")
|
|
1536
|
-
try:
|
|
1537
|
-
subprocess.check_output(cmd, cwd=build_cwd)
|
|
1538
|
-
except subprocess.CalledProcessError:
|
|
1539
|
-
delete_lock_on_failure()
|
|
1540
|
-
raise_error(
|
|
1541
|
-
f"Failed to build Docker image with tag {tag} from {fpath}"
|
|
1542
|
-
)
|
|
1543
|
-
built = True
|
|
1544
|
-
if multi_platform:
|
|
1545
|
-
assert remote_ref is not None
|
|
1546
|
-
already_pushed = True
|
|
1547
|
-
if not ck_docker.pull_image_with_login(
|
|
1548
|
-
remote_ref, platform=platform
|
|
1549
|
-
) or not ck_docker.tag_image(remote_ref, tag):
|
|
1550
|
-
delete_lock_on_failure()
|
|
1551
|
-
raise_error(
|
|
1552
|
-
f"Failed to pull image back from {remote_ref} after "
|
|
1553
|
-
"building it"
|
|
1554
|
-
)
|
|
1555
|
-
elif not obtained:
|
|
1556
|
-
typer.echo(f"Pulling image: {tag}", err=True)
|
|
1557
|
-
if not ck_docker.pull_image_with_login(tag, platform=platform):
|
|
1558
|
-
delete_lock_on_failure()
|
|
1559
|
-
raise_error(f"Failed to pull image: {tag}")
|
|
1560
|
-
identity = ck_docker.inspect_image_for_lock(tag)
|
|
1561
|
-
if identity is None:
|
|
1562
|
-
delete_lock_on_failure()
|
|
1563
|
-
raise_error(f"Failed to inspect image with tag {tag}")
|
|
1564
|
-
assert identity is not None
|
|
1565
|
-
# Checking an environment doesn't publish it: that's what 'calkit push'
|
|
1566
|
-
# is for. A multi-platform build is the exception, since buildx has
|
|
1567
|
-
# nowhere but the registry to put the image it assembles.
|
|
1568
|
-
pushed = already_pushed
|
|
1569
|
-
# A digest already recorded for the project's own registry stands, since
|
|
1570
|
-
# it says the image got there on some earlier push or pull. The image's
|
|
1571
|
-
# own digests can't answer this: tagging one for a registry gives it a
|
|
1572
|
-
# digest under that repo whether or not anything was ever sent.
|
|
1573
|
-
lock_remote_digests = []
|
|
1574
|
-
if lock is not None and remote_repo is not None:
|
|
1575
|
-
lock_remote_digests = [
|
|
1576
|
-
d
|
|
1577
|
-
for d in (lock.get("RepoDigests") or [])
|
|
1578
|
-
# A digest recorded bare is one this project put in its own
|
|
1579
|
-
# registry, since that's the only kind it records; locks written
|
|
1580
|
-
# before digests were stored bare name their repo outright
|
|
1581
|
-
if "@" not in d or d.split("@", 1)[0] == remote_repo
|
|
1582
|
-
]
|
|
1583
|
-
# A digest belongs in the lock whenever there's a registry to pull it
|
|
1584
|
-
# from. An image store that keeps a manifest gives a build the digest it
|
|
1585
|
-
# will have once pushed, since a manifest is content-addressed, so it
|
|
1586
|
-
# can be recorded before the push and a clone pulls the image as soon as
|
|
1587
|
-
# anyone sends it. One that doesn't has to be pushed to learn it.
|
|
1588
|
-
if remote_ref is not None:
|
|
1589
|
-
if pushed or pulled_from_registry:
|
|
1590
|
-
remote_digests = ck_docker.keep_only_repo_digests(
|
|
1591
|
-
identity, remote_ref
|
|
1592
|
-
)["RepoDigests"]
|
|
1593
|
-
elif lock is not None and ck_docker.lock_matches_image(lock, identity):
|
|
1594
|
-
# Still the same image an earlier run verified, so what it
|
|
1595
|
-
# recorded stands and the lock doesn't churn. A lock written
|
|
1596
|
-
# before digests were taken from the build recorded none, and
|
|
1597
|
-
# keeping that would leave the gap in place for as long as the
|
|
1598
|
-
# image goes unrebuilt, so the image's own digest fills it
|
|
1599
|
-
remote_digests = (
|
|
1600
|
-
lock_remote_digests or ck_docker.get_content_digests(identity)
|
|
1601
|
-
)
|
|
1602
|
-
else:
|
|
1603
|
-
remote_digests = ck_docker.get_content_digests(identity)
|
|
1604
|
-
# An image store that keeps no manifest gives a build no digest of
|
|
1605
|
-
# its own: a manifest names the compressed layers, and nothing
|
|
1606
|
-
# compresses them until a push. Pushing is then the only way to
|
|
1607
|
-
# learn the digest, and a lock without one sends everyone else back
|
|
1608
|
-
# to rebuilding an image that's sitting in the registry.
|
|
1609
|
-
if not remote_digests and fpath is not None:
|
|
1610
|
-
typer.echo(
|
|
1611
|
-
f"Pushing image to {remote_ref} to record its digest, since "
|
|
1612
|
-
"this Docker engine only assigns one on a push",
|
|
1613
|
-
err=True,
|
|
1614
|
-
)
|
|
1615
|
-
if not ck_docker.tag_image(tag, remote_ref):
|
|
1616
|
-
warn(f"Failed to tag image as {remote_ref}")
|
|
1617
|
-
else:
|
|
1618
|
-
# Checks run inside pipelines, so a missing credential is
|
|
1619
|
-
# reported rather than prompted for
|
|
1620
|
-
pushed_ok, push_output = ck_docker.push_image_with_login(
|
|
1621
|
-
remote_ref
|
|
1622
|
-
)
|
|
1623
|
-
if pushed_ok:
|
|
1624
|
-
pushed_identity = ck_docker.inspect_image_for_lock(tag)
|
|
1625
|
-
if pushed_identity is not None:
|
|
1626
|
-
identity = pushed_identity
|
|
1627
|
-
remote_digests = ck_docker.keep_only_repo_digests(
|
|
1628
|
-
identity, remote_ref
|
|
1629
|
-
)["RepoDigests"]
|
|
1630
|
-
else:
|
|
1631
|
-
# Leaving the tag would fake a registry digest on the
|
|
1632
|
-
# image, making every later push look unnecessary
|
|
1633
|
-
ck_docker.untag_image(remote_ref)
|
|
1634
|
-
hint = ck_docker.registry_login_hint(
|
|
1635
|
-
remote_ref, push_output
|
|
1636
|
-
)
|
|
1637
|
-
warn(
|
|
1638
|
-
f"Failed to push image to {remote_ref}; its lock "
|
|
1639
|
-
"file will record no digest, so anyone else who "
|
|
1640
|
-
"uses this project will rebuild this image rather "
|
|
1641
|
-
"than pull it\n"
|
|
1642
|
-
+ textwrap.indent(push_output.strip()[-500:], " ")
|
|
1643
|
-
+ (f"\n{hint}" if hint else "")
|
|
1644
|
-
)
|
|
1645
|
-
identity = dict(identity, RepoDigests=remote_digests)
|
|
1646
|
-
elif fpath is None:
|
|
1647
|
-
# An environment named after someone else's image is pullable by
|
|
1648
|
-
# whatever digests it arrived with
|
|
1649
|
-
identity = ck_docker.keep_only_repo_digests(identity, tag)
|
|
1650
|
-
else:
|
|
1651
|
-
# An image built where the store assigns no digest, with no registry
|
|
1652
|
-
# to send it to, leaves nothing in the lock that anyone could pull
|
|
1653
|
-
if built and not ck_docker.get_content_digests(identity):
|
|
1654
|
-
warn(
|
|
1655
|
-
"This Docker engine only assigns an image a digest when "
|
|
1656
|
-
"it's pushed, so this environment's lock file will record "
|
|
1657
|
-
"none, and anyone else who uses this project will rebuild "
|
|
1658
|
-
"the image rather than pull it; set 'registry' on the "
|
|
1659
|
-
"environment to publish it and record its digest"
|
|
1660
|
-
)
|
|
1661
|
-
identity = ck_docker.keep_only_repo_digests(identity, None)
|
|
1662
|
-
# Read the other platforms from the exact image this one locked, rather
|
|
1663
|
-
# than from the tag. A tag moves, and asking it again would lock the
|
|
1664
|
-
# other platforms to whatever it points at now, leaving one set of lock
|
|
1665
|
-
# files describing two different builds. Going by digest gives every
|
|
1666
|
-
# platform the same image and its own layers within it.
|
|
1667
|
-
remote_source_ref = None
|
|
1668
|
-
if identity["RepoDigests"]:
|
|
1669
|
-
remote_source_ref = ck_docker.get_lock_digest_refs(
|
|
1670
|
-
identity, digest_source_ref
|
|
1671
|
-
)[0]
|
|
1672
|
-
elif fpath is None:
|
|
1673
|
-
remote_source_ref = tag
|
|
1674
|
-
# Run configuration doesn't affect which image we need, but does affect
|
|
1675
|
-
# how stages run in it, so it belongs in the lock to invalidate them
|
|
1676
|
-
run_config: dict = {}
|
|
1677
|
-
if platform is not None:
|
|
1678
|
-
run_config["Platform"] = platform
|
|
1679
|
-
if wdir is not None:
|
|
1680
|
-
run_config["WorkDir"] = wdir
|
|
1681
|
-
if user is not None:
|
|
1682
|
-
run_config["User"] = user
|
|
1683
|
-
if env_vars:
|
|
1684
|
-
run_config["EnvVars"] = env_vars
|
|
1685
|
-
if ports:
|
|
1686
|
-
run_config["Ports"] = ports
|
|
1687
|
-
if gpus:
|
|
1688
|
-
run_config["GPUs"] = gpus
|
|
1689
|
-
if args:
|
|
1690
|
-
run_config["Args"] = args
|
|
1691
|
-
|
|
1692
|
-
def write_lock(arch: str, arch_identity: dict) -> None:
|
|
1693
|
-
if arch == current_arch:
|
|
1694
|
-
arch_lock_fpath = lock_fpath
|
|
1695
|
-
else:
|
|
1696
|
-
arch_lock_fpath = os.path.join(lock_dir, arch + ".json")
|
|
1697
|
-
arch_lock = ck_docker.build_lock(
|
|
1698
|
-
identity=arch_identity,
|
|
1699
|
-
dockerfile_md5=dockerfile_md5,
|
|
1700
|
-
deps_md5s=deps_md5s,
|
|
1701
|
-
run_config=run_config,
|
|
1702
|
-
)
|
|
1703
|
-
# newline="\n" so the file is byte-identical on every platform,
|
|
1704
|
-
# rather than a Windows checkout flipping it to CRLF.
|
|
1705
|
-
with open(arch_lock_fpath, "w", newline="\n") as f:
|
|
1706
|
-
json.dump(arch_lock, f, indent=4)
|
|
1707
|
-
|
|
1708
|
-
current_arch = get_docker_arch()
|
|
1709
|
-
lock_dir = os.path.dirname(lock_fpath)
|
|
1710
|
-
if lock_dir:
|
|
1711
|
-
os.makedirs(lock_dir, exist_ok=True)
|
|
1712
|
-
write_lock(current_arch, identity)
|
|
1713
|
-
# Lock the platforms this machine can't run, so that moving the project
|
|
1714
|
-
# to one of them doesn't invalidate every stage in the environment. An
|
|
1715
|
-
# existing lock that still describes this spec is reused rather than
|
|
1716
|
-
# re-read from the registry, which keeps checks working offline
|
|
1717
|
-
stale_archs = []
|
|
1718
|
-
contradicted = False
|
|
1719
|
-
for arch in [a for a in lock_archs if a != current_arch]:
|
|
1720
|
-
arch_lock_fpath = os.path.join(lock_dir, arch + ".json")
|
|
1721
|
-
existing = (
|
|
1722
|
-
read_lock(arch_lock_fpath)
|
|
1723
|
-
if os.path.isfile(arch_lock_fpath)
|
|
1724
|
-
else None
|
|
1725
|
-
)
|
|
1726
|
-
if (
|
|
1727
|
-
existing is not None
|
|
1728
|
-
and ck_docker.lock_matches_spec(
|
|
1729
|
-
existing,
|
|
1730
|
-
dockerfile_md5=dockerfile_md5,
|
|
1731
|
-
deps_md5s=deps_md5s,
|
|
1732
|
-
)
|
|
1733
|
-
and ck_docker.get_content_digests(existing)
|
|
1734
|
-
== ck_docker.get_content_digests(identity)
|
|
1735
|
-
):
|
|
1736
|
-
write_lock(arch, existing)
|
|
1737
|
-
else:
|
|
1738
|
-
# A lock naming a different image than this platform's describes
|
|
1739
|
-
# another build entirely, and leaving one set of lock files
|
|
1740
|
-
# saying two things is worse than the round-trip to settle it
|
|
1741
|
-
contradicted = contradicted or existing is not None
|
|
1742
|
-
stale_archs.append(arch)
|
|
1743
|
-
# Asking the registry which platforms it serves costs a round-trip, and
|
|
1744
|
-
# the answer only changes when the image does. An image that's still the
|
|
1745
|
-
# one the lock describes was already asked about on the run that locked
|
|
1746
|
-
# it, so a platform the registry doesn't publish isn't asked about again
|
|
1747
|
-
# on every check from then on. Nothing is removed either, since a lock
|
|
1748
|
-
# can only be judged stale against an answer we actually have.
|
|
1749
|
-
if (
|
|
1750
|
-
stale_archs
|
|
1751
|
-
and remote_source_ref is not None
|
|
1752
|
-
and (not up_to_date or contradicted)
|
|
1753
|
-
):
|
|
1754
|
-
typer.echo(
|
|
1755
|
-
f"Reading platforms available for {remote_source_ref}",
|
|
1756
|
-
file=outfile,
|
|
1757
|
-
)
|
|
1758
|
-
remote_locks = ck_docker.get_remote_image_platform_locks(
|
|
1759
|
-
remote_source_ref
|
|
1760
|
-
)
|
|
1761
|
-
if remote_locks is None:
|
|
1762
|
-
# Being unable to ask says nothing about what the registry
|
|
1763
|
-
# serves, and a lock can only be judged stale against an answer
|
|
1764
|
-
# we actually have, so the other platforms' locks stand
|
|
1765
|
-
typer.echo(
|
|
1766
|
-
f"Could not read platforms available for {remote_source_ref}",
|
|
1767
|
-
file=outfile,
|
|
1768
|
-
)
|
|
1769
|
-
else:
|
|
1770
|
-
for arch in stale_archs:
|
|
1771
|
-
arch_lock_fpath = os.path.join(lock_dir, arch + ".json")
|
|
1772
|
-
if arch in remote_locks:
|
|
1773
|
-
write_lock(arch, remote_locks[arch])
|
|
1774
|
-
elif os.path.isfile(arch_lock_fpath):
|
|
1775
|
-
# A lock left behind for a platform this image no longer
|
|
1776
|
-
# has would describe an image built from something else
|
|
1777
|
-
os.remove(arch_lock_fpath)
|
|
1778
|
-
|
|
1779
1262
|
|
|
1780
1263
|
@check_app.command(
|
|
1781
1264
|
name="conda-env",
|
|
@@ -2182,12 +1665,15 @@ def check_matlab_env(
|
|
|
2182
1665
|
ck_info=ck_info,
|
|
2183
1666
|
env_name=env_name,
|
|
2184
1667
|
)
|
|
2185
|
-
|
|
1668
|
+
from calkit.environments import docker as env_docker
|
|
1669
|
+
|
|
1670
|
+
env_docker.check_image(
|
|
2186
1671
|
tag=tag,
|
|
2187
1672
|
fpath=dockerfile_fpath,
|
|
2188
1673
|
lock_fpath=output_fpath,
|
|
2189
1674
|
platform="linux/amd64", # Only one available for now
|
|
2190
1675
|
rebuild=rebuild,
|
|
1676
|
+
env_name=env_name,
|
|
2191
1677
|
)
|
|
2192
1678
|
|
|
2193
1679
|
|
|
@@ -2553,10 +2039,11 @@ def check_questions(
|
|
|
2553
2039
|
Reports, worst first: evidence that isn't there (never run, never
|
|
2554
2040
|
pushed, or pinned to a Git ref that doesn't exist); broken references
|
|
2555
2041
|
(a key that doesn't resolve, a placeholder that names no evidence, a
|
|
2556
|
-
label missing from the LaTeX
|
|
2557
|
-
and evidence from a frozen stage,
|
|
2558
|
-
matches dvc.lock, which won't be
|
|
2559
|
-
git_ref. Subprojects' questions are
|
|
2042
|
+
label missing from the LaTeX, lines a cited file no longer has);
|
|
2043
|
+
evidence the pipeline would rebuild; and evidence from a frozen stage,
|
|
2044
|
+
or downstream of one, that no longer matches dvc.lock, which won't be
|
|
2045
|
+
rerun, unless the citation pins a git_ref. Subprojects' questions are
|
|
2046
|
+
checked too.
|
|
2560
2047
|
|
|
2561
2048
|
Evidence pinned with a git_ref is checked at that ref rather than in
|
|
2562
2049
|
the working tree. Exits with an error if any answered question is
|