Repository navigation
Expand file tree
/
Copy pathnoxfile.py
More file actions
546 lines (496 loc) · 19.1 KB
/
Copy pathnoxfile.py
File metadata and controls
546 lines (496 loc) · 19.1 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
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
import os
import re
import shutil
from pathlib import Path
import nox
from laminci import convert_executable_md_files, upload_docs_artifact
from laminci.nox import (
build_docs,
login_testuser1,
login_testuser2,
run,
run_pre_commit,
)
# we'd like to aggregate coverage information across sessions
# and for this the code needs to be located in the same
# directory in every github action runner
# this also allows to break out an installation section
nox.options.default_venv_backend = "none"
IS_PR = os.getenv("GITHUB_EVENT_NAME") != "push"
CI = os.environ.get("CI")
# SpatialData.write() had a regression with ome-zarr>=0.14:
# https://github.com/scverse/spatialdata/issues/1090
# seems to be solved, unpinning for now
SPATIALDATA_OME_ZARR_CONSTRAINT = "ome-zarr"
# compatibility with anndata>=0.13.0
SPATIALDATA_CONSTRAINT = "spatialdata==0.8.0"
GROUPS = {}
GROUPS["tutorial"] = [
"README.ipynb",
"transfer.ipynb",
"tables.ipynb",
"manage-ontologies.ipynb",
"query-search.ipynb",
]
GROUPS["guide"] = [
"track.ipynb",
"acid.ipynb",
"idempotency.ipynb",
]
GROUPS["tiledbsoma"] = [
"curate.ipynb",
]
GROUPS["biology"] = [
"arrays.ipynb",
]
@nox.session
def lint(session: nox.Session) -> None:
run_pre_commit(session)
@nox.session
def install(session):
base_deps = [
"./sub/lamin-cli",
"./sub/lamindb-setup",
"./sub/bionty",
]
top_deps = [
".[full,dev]",
]
cmds = [
f"uv pip install {'--system' if CI else ''} --no-cache-dir --no-deps {' '.join(base_deps)}",
] + [
f"uv pip install {'--system' if CI else ''} --no-cache-dir -e {dep}"
for dep in top_deps
]
[run(session, line) for line in cmds]
@nox.session
@nox.parametrize(
"group",
[
"unit-pydata-sqlite",
"unit-pydata-postgres",
"unit-storage",
"no-instance",
"tutorial",
"guide",
"tiledbsoma",
"biology",
"faq",
"storage",
"transfer-sqlite",
"transfer-postgres",
"curator",
"integrations",
"docs",
"cli",
"permissions",
"profile",
"minimal",
],
)
def install_ci(session, group):
extras = ""
if group in ["unit-pydata-sqlite", "unit-pydata-postgres"]:
extras += "fcs"
run(session, "uv pip install --system scanpy")
run(session, "uv pip install --system mudata")
run(session, "uv pip install --system marimo")
# spatialdata dependency, specifying it here explicitly
# otherwise there are problems with uv resolver
run(session, "uv pip install --system xarray-dataclasses")
run(
session,
f"uv pip install --system {SPATIALDATA_CONSTRAINT} {SPATIALDATA_OME_ZARR_CONSTRAINT}",
)
elif group == "unit-storage":
extras += "gcp"
run(session, "uv pip install --system huggingface_hub")
run(session, "uv pip install --system anndata==0.13.2")
run(session, "uv pip install --system scanpy")
run(session, "uv pip install --system polars")
elif group == "tutorial":
# anndata here to prevent installing older version on release
run(
session,
"uv pip install --system huggingface_hub polars anndata==0.13.2 duckdb 'pyiceberg[sql]==0.11.1' lancedb==0.34.0 sqlalchemy",
)
elif group == "guide":
# spatialdata needs zarr with FsspecStore/LocalStore (zarr>=3)
# so do not force the zarr_v2 compatibility extra in this group.
run(
session,
f"uv pip install --system scanpy mudata {SPATIALDATA_CONSTRAINT} {SPATIALDATA_OME_ZARR_CONSTRAINT}",
)
elif group == "tiledbsoma":
# this group also exercises spatialdata through docs notebooks
# and should resolve against zarr>=3.
run(session, "uv pip install --system anndata==0.12.19")
run(
session,
f"uv pip install --system scanpy mudata {SPATIALDATA_CONSTRAINT} {SPATIALDATA_OME_ZARR_CONSTRAINT} tiledbsoma",
)
elif group == "biology":
extras += "fcs"
run(session, "uv pip install --system ipywidgets")
elif group == "faq":
extras += "zarr_v2"
elif group == "storage":
extras += "zarr_v2"
run(
session,
"uv pip install --system --no-deps ./sub/pertdb",
)
run(session, "uv pip install --system vitessce")
elif group == "curator":
run(
session,
"uv pip install --system --no-deps ./sub/pertdb",
)
# spatialdata dependency, specifying it here explicitly
# otherwise there are problems with uv resolver
run(session, "uv pip install --system xarray-dataclasses")
run(
session,
f"uv pip install --system {SPATIALDATA_CONSTRAINT} {SPATIALDATA_OME_ZARR_CONSTRAINT}",
)
elif group == "integrations":
run(session, "uv pip install --system lightning")
elif group == "docs":
# docs include spatialdata examples; avoid forcing zarr<3.
# spatialdata dependency, specifying it here explicitly
# otherwise there are problems with uv resolver
run(session, "uv pip install --system xarray-dataclasses")
run(
session,
f"uv pip install --system mudata {SPATIALDATA_CONSTRAINT} {SPATIALDATA_OME_ZARR_CONSTRAINT} lightning",
)
run(
session,
"uv pip install --system --no-deps ./sub/pertdb",
)
elif group == "cli":
pass
elif group == "permissions":
pass
elif group == "profile":
pass
elif group == "minimal":
run(session, "uv pip install --system pytest")
if IS_PR:
run(session, "uv pip install --system ./sub/lamindb-setup ./sub/lamin-cli")
run(session, "uv pip install --system .")
return
extras = "," + extras if extras != "" else extras
run(session, f"uv pip install --system -e .[full,dev{extras}]")
# on the release branch, do not use submodules but run with pypi install
# only exception is the docs group which should always use the submodule
# to push docs fixes fast
# installing this after lamindb to be sure that these packages won't be reinstaled
# during lamindb installation
if IS_PR or group == "docs" or group == "profile":
run(
session,
"uv pip install --system ./sub/lamindb-setup ./sub/lamin-cli ./sub/bionty ./sub/pertdb",
)
if group == "permissions":
# have to install after lamindb installation
# because lamindb downgrades django required by laminhub_rest
cmds = "uv pip install --system ./laminhub/backend"
cmds += "\nuv pip install --system ./laminhub/backend/utils"
cmds += "\nuv pip install --system ./laminhub/backend/services/central"
cmds += "\nuv pip install --system ./laminhub/backend/services/instancedb"
cmds += "\nuv pip install --system ./laminhub/backend/services/aws"
cmds += "\nuv pip install --system --no-deps ./laminhub/backend/services/instancedb/hubmodule"
cmds += "\nuv pip uninstall --system psycopg-binary psycopg"
[run(session, line) for line in cmds.splitlines()]
@nox.session
def configure_coverage(session) -> None:
"""Write a coverage config file, adding extra patterns to omit."""
import tomlkit
groups_str = session.posargs[0] # first positional argument
print(groups_str) # for debugging
# so that we don't change this away from string
assert isinstance(groups_str, str) # noqa: S101
if "curator" not in groups_str and "tiledbsoma" not in groups_str:
extra_omit_patterns = ["**/curators/*"]
else:
extra_omit_patterns = []
# Read patterns from pyproject.toml
base_config_path = Path("pyproject.toml")
with open(base_config_path) as f:
config = tomlkit.load(f)
# Update the omit patterns
base_patterns = config["tool"]["coverage"]["run"]["omit"]
all_patterns = base_patterns + extra_omit_patterns
config["tool"]["coverage"]["run"]["omit"] = all_patterns
# Write back to pyproject.toml
with open(base_config_path, "w") as f:
tomlkit.dump(config, f)
print(base_config_path.read_text())
@nox.session
def prepare(session):
"""Create executable files to run during a test session.
Not needed for unit tests or the `minimal` runner (no docs notebooks, no jupytext).
"""
content = open("README.md").read()
# cannot execute the flow after ln.track() was called
content = content.replace(" create_fasta()", " pass")
open("README_stripped.md", "w").write(
"\n".join(
line
for line in content.split("\n")
if not line.strip().startswith(
("accessor = artifact.open()", "ln.track(project=", "ln.Project(name=")
)
)
)
os.system("jupytext README_stripped.md --to notebook --output ./docs/README.ipynb")
convert_executable_md_files()
os.system("cp ./tests/pydata/test_artifact_parquet.py ./docs/scripts/")
os.system("cp ./lamindb/examples/schemas/define_valid_features.py ./docs/scripts/")
os.system(
"cp ./lamindb/examples/schemas/define_schema_anndata_ensembl_gene_ids_and_valid_features_in_obs.py ./docs/scripts/"
)
os.system(
"cp ./lamindb/examples/datasets/define_mini_immuno_features_labels.py ./docs/scripts/"
)
os.system(
"cp ./lamindb/examples/datasets/define_mini_immuno_schema_flexible.py ./docs/scripts/"
)
os.system(
"cp ./lamindb/examples/datasets/save_mini_immuno_datasets.py ./docs/scripts/"
)
@nox.session
@nox.parametrize(
"group",
[
"unit-pydata-sqlite",
"unit-pydata-postgres",
"unit-storage",
"no-instance",
"curator",
"integrations",
"tutorial",
"guide",
"tiledbsoma",
"biology",
"faq",
"storage",
"transfer-sqlite",
"transfer-postgres",
"cli",
"permissions",
"minimal",
],
)
def test(session, group):
# we likely don't need auth in many other groups, but have to carefully expand this
if group not in {"curator", "no-instance", "minimal"}:
login_testuser2(session)
login_testuser1(session)
# this is mostly needed for the docs so that we don't render Django's entire public API
run(session, "lamin settings set private-django-api true")
coverage_args = "--cov=lamindb --cov-config=pyproject.toml --cov-append --cov-report=term-missing"
duration_args = "--durations=10"
env = os.environ.copy()
if group == "unit-pydata-sqlite":
env["LAMINDB_TEST_DB_VENDOR"] = "sqlite"
run(
session,
f"pytest {coverage_args} ./tests/pydata {duration_args}",
env=env,
)
elif group == "unit-pydata-postgres":
env["LAMINDB_TEST_DB_VENDOR"] = "postgresql"
run(
session,
f"pytest {coverage_args} ./tests/pydata {duration_args}",
env=env,
)
elif group == "unit-storage":
login_testuser2(session) # shouldn't be necessary but is for now
run(session, f"pytest {coverage_args} ./tests/storage {duration_args}")
elif group == "no-instance":
run(session, "lamin disconnect")
run(session, f"pytest {coverage_args} ./tests/no_instance {duration_args}")
elif group == "tutorial":
run(session, "lamin logout")
run(
session,
"lamin init --modules bionty",
)
run(
session, f"pytest -s {coverage_args} ./docs/test_notebooks.py::test_{group}"
)
elif group == "guide":
run(
session,
f"pytest -s {coverage_args} ./docs/test_notebooks.py::test_{group}",
)
elif group == "tiledbsoma":
run(
session,
(
f"pytest {coverage_args} tests/tiledbsoma "
"./docs/test_notebooks.py::test_tiledbsoma "
f"{duration_args}"
),
)
elif group == "biology":
run(
session,
f"pytest -s {coverage_args} ./docs/test_notebooks.py::test_{group}",
)
elif group == "faq":
run(session, f"pytest -s {coverage_args} ./docs/faq")
elif group == "storage":
run(session, f"pytest -s {coverage_args} ./docs/storage")
elif group == "transfer-sqlite":
env["LAMINDB_TEST_DB_VENDOR"] = "sqlite"
run(
session,
f"pytest {coverage_args} tests/transfer {duration_args}",
env=env,
)
elif group == "transfer-postgres":
env["LAMINDB_TEST_DB_VENDOR"] = "postgresql"
run(
session,
f"pytest {coverage_args} tests/transfer {duration_args}",
env=env,
)
elif group == "curator":
run(
session,
f"pytest {coverage_args} tests/curators {duration_args}",
)
elif group == "integrations":
run(session, f"pytest -s {coverage_args} tests/integrations")
elif group == "cli":
run(
session,
f"pytest {coverage_args} ./sub/lamin-cli/tests/core {duration_args}",
)
elif group == "permissions":
run(session, f"pytest {coverage_args} ./tests/permissions")
elif group == "minimal":
env_minimal = os.environ.copy()
env_minimal["PYTEST_DISABLE_PLUGIN_AUTOLOAD"] = "1"
run(session, "pytest ./tests/minimal", env=env_minimal)
# move artifacts into right place
if group in {"tutorial", "guide", "tiledbsoma", "biology"}:
target_dir = Path(f"./docs/{group}")
target_dir.mkdir(exist_ok=True)
for filename in GROUPS[group]:
shutil.copy(Path("docs") / filename, target_dir / filename)
@nox.session
def clidocs(session):
def update_cli_docs():
def normalize_help_usage(help_text: str) -> str:
# Click versions differ on whether group subcommands are rendered
# as COMMAND or [COMMAND]. Keep docs stable across environments.
return help_text.replace("[COMMAND] [ARGS]...", "COMMAND [ARGS]...")
os.environ["NO_RICH"] = "1"
from lamin_cli.__main__ import COMMAND_GROUPS, _generate_help
page = ""
helps = _generate_help()
# First, add the main lamin command
main_help = helps.get("main")
if main_help:
help_string = main_help["help"].replace("Usage: main", "Usage: lamin")
help_docstring = main_help["docstring"]
if help_docstring:
page += f"{help_docstring}\n\n"
# below is ugly
# page += f"```text\n{help_string}\n```\n\n"
# Create a mapping of command names to their full keys in helps
command_to_key = {}
for name in helps.keys():
names = name.split(" ")
if len(names) == 2: # e.g., "lamin connect"
command_name = names[1]
command_to_key[command_name] = name
# Group commands by their categories
command_groups = COMMAND_GROUPS.get("lamin", [])
processed_commands = set()
for group in command_groups:
group_name = group["name"]
group_commands = group["commands"]
page += f"## {group_name}\n\n"
for command_name in group_commands:
if command_name in command_to_key:
full_key = command_to_key[command_name]
help_dict = helps[full_key]
processed_commands.add(command_name)
help_string = help_dict["help"].replace("Usage: main", "lamin")
help_string = normalize_help_usage(help_string)
help_docstring = help_dict["docstring"]
pyr_alt_delimiter = "→ Python/R alternative:"
if pyr_alt_delimiter in help_docstring:
help_docstring, pyr_alt_string = help_docstring.split(
pyr_alt_delimiter
)
else:
pyr_alt_string = ""
page += f"### {command_name}\n\n"
if help_docstring:
page += f"{help_docstring.strip()}\n"
command_block = f"```text\n{help_string}\n```"
page += f"\n\nOptions:\n\n{command_block}\n\n"
if pyr_alt_string:
page += f"{pyr_alt_delimiter} {pyr_alt_string.strip()}\n\n"
# Add any remaining commands that aren't in groups
remaining_commands = []
for command_name, full_key in command_to_key.items():
if command_name not in processed_commands:
remaining_commands.append((command_name, full_key))
if remaining_commands:
page += "## Other\n\n"
for command_name, full_key in remaining_commands:
help_dict = helps[full_key]
help_string = help_dict["help"].replace("Usage: main", "Usage: lamin")
help_string = normalize_help_usage(help_string)
help_docstring = help_dict["docstring"]
page += f"### lamin {command_name}\n\n"
if help_docstring:
page += f"{help_docstring.strip()}\n\n"
page += f"```text\n{help_string}\n```\n\n"
current_content = Path("./docs/cli.md").read_text()
preamble = current_content.split("<!-- auto-generated-docs-from-here -->")[0]
# Keep generated docs stable across environments by normalizing blank lines.
page = re.sub(r"\n{3,}", "\n\n", page).strip() + "\n"
new_content = (
preamble.rstrip() + "\n\n<!-- auto-generated-docs-from-here -->\n\n" + page
)
if current_content != new_content:
Path("./docs/cli.md").write_text(new_content)
if os.getenv("CI"):
run(session, "git add docs/cli.md")
run(
session,
"git -c user.name='lamin-cli-docs-bot' -c user.email='open-source@lamin.ai' "
"commit -m '📝 Re-generated the CLI docs'",
)
branch = os.getenv("GITHUB_HEAD_REF") or os.getenv("GITHUB_REF_NAME")
if branch:
run(session, f"git push origin HEAD:{branch}")
update_cli_docs()
@nox.session
def docs(session):
# move artifacts into right place
run(session, "lamin settings set private-django-api true")
for group in ["tutorial", "guide", "tiledbsoma", "biology", "faq", "storage"]:
if Path(f"./docs-{group}").exists():
if Path(f"./docs/{group}").exists():
shutil.rmtree(f"./docs/{group}")
Path(f"./docs-{group}").rename(f"./docs/{group}")
# move back to root level
if group in {"tutorial", "guide", "tiledbsoma", "biology"}:
for path in Path(f"./docs/{group}").glob("*"):
path.rename(f"./docs/{path.name}")
run(
session,
"lamin init --storage docsbuild --modules bionty,pertdb",
)
build_docs(session, strip_prefix=True, strict=True)
upload_docs_artifact()