-
Notifications
You must be signed in to change notification settings - Fork 1
/
noxfile.py
1318 lines (1114 loc) · 36.9 KB
/
noxfile.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Config file for nox."""
from __future__ import annotations
import shutil
from dataclasses import replace # noqa
from itertools import product
from pathlib import Path
from textwrap import dedent
from typing import (
Annotated,
Any,
Callable,
Collection,
Iterator,
Literal,
Sequence,
TypeAlias,
TypeVar,
cast,
)
import nox
from noxopt import NoxOpt, Option, Session
from tools.noxtools import (
combine_list_str,
load_nox_config,
open_webpage,
prepend_flag,
session_install_envs,
session_install_envs_lock,
# session_install_package,
session_install_pip,
session_run_commands,
sort_like,
update_target,
)
# * nox options ------------------------------------------------------------------------
ROOT = Path(__file__).parent
nox.options.reuse_existing_virtualenvs = True
nox.options.sessions = ["test"]
# * Options ----------------------------------------------------------------------------
PACKAGE_NAME = "module-utilities"
IMPORT_NAME = "module_utilities"
KERNEL_BASE = "module_utilities"
PYTHON_ALL_VERSIONS = ["3.8", "3.9", "3.10", "3.11"]
PYTHON_DEFAULT_VERSION = "3.10"
# conda/mamba
if shutil.which("mamba"):
CONDA_BACKEND = "mamba"
elif shutil.which("conda"):
CONDA_BACKEND = "conda" # pyright: ignore
else:
raise ValueError("neither conda or mamba found")
SESSION_DEFAULT_KWS = {"python": PYTHON_DEFAULT_VERSION, "venv_backend": CONDA_BACKEND}
SESSION_ALL_KWS = {"python": PYTHON_ALL_VERSIONS, "venv_backend": CONDA_BACKEND}
# * User config ------------------------------------------------------------------------
CONFIG = load_nox_config()
# * noxopt -----------------------------------------------------------------------------
group = NoxOpt(auto_tag=True)
F = TypeVar("F", bound=Callable[..., Any])
C: TypeAlias = Callable[[F], F]
DEFAULT_SESSION = cast(C[F], group.session(**SESSION_DEFAULT_KWS)) # type: ignore
ALL_SESSION = cast(C[F], group.session(**SESSION_ALL_KWS)) # type: ignore
DEFAULT_SESSION_VENV = cast(C[F], group.session(python=PYTHON_DEFAULT_VERSION)) # type: ignore
ALL_SESSION_VENV = cast(C[F], group.session(python=PYTHON_ALL_VERSIONS)) # type: ignore
OPTS_OPT = Option(nargs="*", type=str)
# SET_KERNEL_OPT = Option(type=bool, help="If True, try to set the kernel name")
RUN_OPT = Option(
nargs="*",
type=str,
action="append",
help="run passed command_demo using `external=True` flag",
)
CMD_OPT = Option(nargs="*", type=str, help="cmd to be run")
LOCK_OPT = Option(type=bool, help="If True, use conda-lock")
def opts_annotated(**kwargs: Any): # type: ignore
return Annotated[list[str], replace(OPTS_OPT, **kwargs)]
def cmd_annotated(**kwargs): # type: ignore
return Annotated[list[str], replace(CMD_OPT, **kwargs)]
def run_annotated(**kwargs): # type: ignore
return Annotated[list[list[str]], replace(RUN_OPT, **kwargs)]
LOCK_CLI = Annotated[bool, LOCK_OPT]
RUN_CLI = Annotated[list[list[str]], RUN_OPT]
TEST_OPTS_CLI = opts_annotated(help="extra arguments/flags to pytest")
# CMD_CLI = Annotated[list[str], CMD_OPT]
FORCE_REINSTALL_CLI = Annotated[
bool,
Option(
type=bool,
help="If True, force reinstall requirements and package even if environment unchanged",
),
]
VERSION_CLI = Annotated[
str, Option(type=str, help="Version to substitute or check against")
]
LOG_SESSION_CLI = Annotated[
bool,
Option(
type=bool,
help="If flag included, log python and package (if installed) version",
),
]
# * Installation command ---------------------------------------------------------------
def py_prefix(python_version: Any) -> str:
if isinstance(python_version, str):
return "py" + python_version.replace(".", "")
else:
raise ValueError(f"passed non-string value {python_version}")
def session_environment_filename(
name: str | None,
ext: str | None = None,
python_version: str | None = None,
) -> str:
if name is None:
raise ValueError("must supply name")
filename = name
if ext is not None:
filename = filename + ext
if python_version is not None:
filename = f"{py_prefix(python_version)}-{filename}"
return f"./environment/{filename}"
def pkg_install_condaenv(
session: nox.Session,
name: str,
lock: bool = False,
display_name: str | None = None,
install_package: bool = True,
force_reinstall: bool = False,
log_session: bool = False,
deps: Collection[str] | None = None,
reqs: Collection[str] | None = None,
channels: Collection[str] | None = None,
filename: str | None = None,
**kwargs: Any,
) -> None:
"""Install requirements. If need fine control, do it in calling func."""
def check_filename(filename: str | Path) -> str:
if not Path(filename).exists():
raise ValueError(f"file {filename} does not exist")
session.log(f"Environment file: {filename}")
return str(filename)
if lock:
filename = (
filename
or f"./environment/lock/{py_prefix(session.python)}-{name}-conda-lock.yml"
)
session_install_envs_lock(
session=session,
lockfile=check_filename(filename),
display_name=display_name,
force_reinstall=force_reinstall,
install_package=install_package,
**kwargs,
)
else:
assert isinstance(session.python, str)
filename = filename or session_environment_filename(
name=name,
ext=".yaml",
python_version=session.python,
)
session_install_envs(
session,
check_filename(filename),
display_name=display_name,
force_reinstall=force_reinstall,
deps=deps,
reqs=reqs,
channels=channels,
install_package=install_package,
**kwargs,
)
if log_session:
session_log_session(session, install_package)
def pkg_install_venv(
session: nox.Session,
name: str, # pyright: ignore
lock: bool = False,
requirement_paths: Collection[str] | None = None,
constraint_paths: Collection[str] | None = None,
extras: str | Collection[str] | None = None,
reqs: Collection[str] | None = None,
display_name: str | None = None,
force_reinstall: bool = False,
install_package: bool = False,
no_deps: bool = False,
log_session: bool = False,
) -> None:
if lock:
raise ValueError("lock not yet supported for install_pip")
else:
session_install_pip(
session=session,
requirement_paths=requirement_paths,
constraint_paths=constraint_paths,
extras=extras,
reqs=reqs,
display_name=display_name,
force_reinstall=force_reinstall,
install_package=install_package,
no_deps=no_deps,
)
if log_session:
session_log_session(session, install_package)
def session_log_session(session: nox.Session, has_package: bool = False) -> None:
session.run("python", "--version")
if has_package:
session.run(
"python",
"-c",
dedent(
f"""
import {IMPORT_NAME}
print({IMPORT_NAME}.__version__)
"""
),
)
# * Environments------------------------------------------------------------------------
# ** Development (conda)
@DEFAULT_SESSION
def dev(
session: Session,
dev_run: RUN_CLI = [], # noqa
lock: LOCK_CLI = False,
force_reinstall: FORCE_REINSTALL_CLI = False,
log_session: bool = False,
) -> None:
"""Create dev env."""
# using conda
pkg_install_condaenv(
session=session,
name="dev",
lock=lock,
display_name=f"{PACKAGE_NAME}-dev",
install_package=True,
force_reinstall=force_reinstall,
log_session=log_session,
)
session_run_commands(session, dev_run)
@DEFAULT_SESSION_VENV
def dev_venv(
session: Session,
dev_run: RUN_CLI = [], # noqa
lock: LOCK_CLI = False,
force_reinstall: FORCE_REINSTALL_CLI = False,
log_session: bool = False,
) -> None:
"""Create dev env."""
# using conda
pkg_install_venv(
session=session,
name="dev-venv",
lock=lock,
extras=["dev"],
display_name=f"{PACKAGE_NAME}-dev-venv",
install_package=True,
force_reinstall=force_reinstall,
log_session=log_session,
)
session_run_commands(session, dev_run)
# ** pyproject2conda (create environment.yaml and requirement.txt files)
@DEFAULT_SESSION_VENV
def pyproject2conda(
session: Session,
force_reinstall: FORCE_REINSTALL_CLI = False,
pyproject2conda_force: bool = False,
) -> None:
"""Create environment.yaml files from pyproject.toml using pyproject2conda."""
pkg_install_venv(
session=session,
name="pyporject2conda",
reqs=["pyproject2conda>=0.4.0"],
force_reinstall=force_reinstall,
)
def create_env(
python_version: str | None = None,
cmd: Literal["yaml", "requirements"] = "yaml",
name: str | None = None,
output: str | None = None,
extras: str | Sequence[str] | None = None,
python_include: str | bool = True,
base: bool = True,
) -> None:
def _to_args(flag: str, val: str | Sequence[str] | None) -> list[str]:
if val is None:
return []
if isinstance(val, str):
val = [val]
return prepend_flag(flag, *val)
if output is None:
assert name is not None
output = session_environment_filename(
python_version=python_version,
name=name,
ext={"yaml": ".yaml", "requirements": ".txt"}[cmd],
)
if pyproject2conda_force or update_target(output, "pyproject.toml"):
args = [cmd, "-o", output] + _to_args("-e", extras)
if cmd == "yaml":
if python_version is not None:
args.extend(["--python-version", python_version])
if isinstance(python_include, bool) and python_include:
python_include = f"python={python_version}"
if isinstance(python_include, str):
args.extend(["--python-include", python_include])
if not base:
args.append("--no-base")
session.run("pyproject2conda", *args)
else:
session.log(
f"{output} up to data. Pass --pyproject2conda-force to force recreation"
)
extras = CONFIG.get("environment-extras", {"dev": ["dev", "nox"]})
# All versions:
for env, python_version in product(
["test", "typing", "test-noopt"], PYTHON_ALL_VERSIONS
):
create_env(
name=env,
extras=extras.get(env, env),
base=True,
python_version=python_version,
)
for env, python_version in product(["docs", "dev"], [PYTHON_DEFAULT_VERSION]):
create_env(
name=env,
extras=extras.get(env, env),
base=True,
python_version=python_version,
)
# need an isolated set of test requirements
for python_version in PYTHON_ALL_VERSIONS:
create_env(
name="test-extras",
extras="test",
base=False,
python_version=python_version,
)
# isolated
for env in ["dist-pypi", "dist-conda"]:
create_env(
name=f"{env}",
extras=env,
base=False,
python_version=PYTHON_DEFAULT_VERSION,
)
# isolated requirement files.
# no python versioning for these
for env, extras in [("test-extras", "test"), ("dist-pypi", "dist-pypi")]:
create_env(name=f"{env}", cmd="requirements", extras=extras, base=False)
# ** conda-lock
@DEFAULT_SESSION_VENV
def conda_lock(
session: Session,
force_reinstall: FORCE_REINSTALL_CLI = False,
conda_lock_channel: cmd_annotated(help="conda channels to use") = (), # type: ignore
conda_lock_platform: cmd_annotated( # type: ignore
help="platforms to build lock files for",
choices=["osx-64", "linux-64", "win-64", "all"],
) = (),
conda_lock_cmd: cmd_annotated( # type: ignore
help="lock files to create",
choices=["test", "typing", "dev", "dist-pypi", "dist-conda", "all"],
) = (),
conda_lock_run: RUN_CLI = [], # noqa
conda_lock_mamba: bool = False,
conda_lock_force: bool = False,
) -> None:
"""Create lock files using conda-lock."""
pkg_install_venv(
session,
name="conda-lock",
reqs=["conda-lock>=2.0.0"],
force_reinstall=force_reinstall,
)
session.run("conda-lock", "--version")
platform = cast(Sequence[str], conda_lock_platform)
if not platform:
platform = ["osx-64"]
elif "all" in platform:
platform = ["linux-64", "osx-64", "win-64"]
channel = cast(Sequence[str], conda_lock_channel)
if not channel:
channel = ["conda-forge"]
lock_dir = ROOT / "environment" / "lock"
def create_lock(
py: str,
name: str,
env_path: str | None = None,
) -> None:
py = "py" + py.replace(".", "")
if env_path is None:
env_path = f"environment/{py}-{name}.yaml"
lockfile = lock_dir / f"{py}-{name}-conda-lock.yml"
deps = [env_path]
# make sure this is last to make python version last
# deps.append(lock_dir / f"{py}.yaml")
if conda_lock_force or update_target(lockfile, *deps):
session.log(f"creating {lockfile}")
# insert -f for each arg
if lockfile.exists():
lockfile.unlink()
session.run(
"conda-lock",
"--mamba" if conda_lock_mamba else "--no-mamba",
*prepend_flag("-c", *channel),
*prepend_flag("-p", *platform),
*prepend_flag("-f", *deps),
f"--lockfile={lockfile}",
)
session_run_commands(session, conda_lock_run)
if not conda_lock_run and not conda_lock_cmd:
conda_lock_cmd = ["all"] # pyright: ignore
if "all" in conda_lock_cmd:
conda_lock_cmd = ["test", "typing", "dev", "dist-pypi", "dist-conda"]
conda_lock_cmd = list(set(conda_lock_cmd))
for c in conda_lock_cmd:
if c == "test":
for py in PYTHON_ALL_VERSIONS:
create_lock(py, "test")
elif c == "typing":
for py in PYTHON_ALL_VERSIONS:
create_lock(py, "typing")
elif c == "dev":
create_lock(PYTHON_DEFAULT_VERSION, "dev")
elif c == "dist-pypi":
create_lock(
PYTHON_DEFAULT_VERSION,
"dist-pypi",
)
elif c == "dist-conda":
create_lock(
PYTHON_DEFAULT_VERSION,
"dist-conda",
)
# ** testing
def _test(
session: nox.Session,
run: list[list[str]],
test_no_pytest: bool,
test_opts: list[str],
no_cov: bool,
) -> None:
session_run_commands(session, run)
if not test_no_pytest:
opts = combine_list_str(test_opts)
if not no_cov:
session.env["COVERAGE_FILE"] = str(Path(session.create_tmp()) / ".coverage")
if "--cov" not in opts:
opts.append("--cov")
session.run("pytest", *opts)
@ALL_SESSION
def test(
session: Session,
test_no_pytest: bool = False,
test_opts: TEST_OPTS_CLI = (), # type: ignore
test_run: RUN_CLI = [], # noqa
lock: LOCK_CLI = False,
force_reinstall: FORCE_REINSTALL_CLI = False,
log_session: bool = False,
no_cov: bool = False,
) -> None:
"""Test environments with conda installs."""
pkg_install_condaenv(
session=session,
name="test",
lock=lock,
install_package=True,
force_reinstall=force_reinstall,
log_session=log_session,
)
_test(
session=session,
run=test_run,
test_no_pytest=test_no_pytest,
test_opts=test_opts,
no_cov=no_cov,
)
@ALL_SESSION
def test_noopt(
session: Session,
test_no_pytest: bool = False,
test_opts: TEST_OPTS_CLI = (), # type: ignore
test_run: RUN_CLI = [], # noqa
lock: LOCK_CLI = False,
force_reinstall: FORCE_REINSTALL_CLI = False,
log_session: bool = False,
no_cov: bool = False,
) -> None:
"""Test environments with conda installs."""
pkg_install_condaenv(
session=session,
name="test-noopt",
lock=lock,
install_package=True,
force_reinstall=force_reinstall,
log_session=log_session,
)
_test(
session=session,
run=test_run,
test_no_pytest=test_no_pytest,
test_opts=test_opts,
no_cov=no_cov,
)
@ALL_SESSION_VENV
def test_venv(
session: Session,
test_no_pytest: bool = False,
test_opts: TEST_OPTS_CLI = (), # type: ignore
test_run: RUN_CLI = [], # noqa
lock: LOCK_CLI = False, # pyright: ignore
force_reinstall: FORCE_REINSTALL_CLI = False,
log_session: bool = False,
no_cov: bool = False,
) -> None:
"""Test environments virtualenv and pip installs."""
pkg_install_venv(
session=session,
name="test-venv",
extras="test",
install_package=True,
force_reinstall=force_reinstall,
log_session=log_session,
)
_test(
session=session,
run=test_run,
test_no_pytest=test_no_pytest,
test_opts=test_opts,
no_cov=no_cov,
)
# ** coverage
def _coverage(
session: nox.Session,
run: list[list[str]],
cmd: list[str],
run_internal: list[list[str]],
) -> None:
session_run_commands(session, run)
if not cmd and not run and not run_internal:
cmd = ["combine", "report"]
session.log(f"{cmd}")
for c in cmd:
if c == "combine":
paths = list(Path(".nox").glob("test-3*/tmp/.coverage"))
if update_target(".coverage", *paths):
session.run("coverage", "combine", "--keep", "-a", *map(str, paths))
elif c == "open":
open_webpage(path="htmlcov/index.html")
else:
session.run("coverage", c)
session_run_commands(session, run_internal, external=False)
@DEFAULT_SESSION_VENV
def coverage(
session: Session,
coverage_cmd: cmd_annotated( # type: ignore
choices=["erase", "combine", "report", "html", "open"]
) = (),
coverage_run: RUN_CLI = [], # noqa
coverage_run_internal: run_annotated( # type: ignore
help="Arbitrary commands to run within the session"
) = [], # noqa
force_reinstall: FORCE_REINSTALL_CLI = False,
) -> None:
pkg_install_venv(
session,
name="coverage",
reqs=["coverage[toml]"],
force_reinstall=force_reinstall,
)
_coverage(
session=session,
run=coverage_run,
cmd=cast(list[str], coverage_cmd),
run_internal=cast(list[list[str]], coverage_run_internal),
)
# ** Docs
def _docs(
session: nox.Session, run: list[list[str]], cmd: list[str], version: str
) -> None:
if version:
session.env["SETUPTOOLS_SCM_PRETEND_VERSION"] = version
session_run_commands(session, run)
if not run and not cmd:
cmd = ["html"]
if "symlink" in cmd:
cmd.remove("symlink")
_create_doc_examples_symlinks(session)
if open_page := "open" in cmd:
cmd.remove("open")
if cmd:
args = ["make", "-C", "docs"] + combine_list_str(cmd)
session.run(*args, external=True)
if open_page:
open_webpage(path="./docs/_build/html/index.html")
@DEFAULT_SESSION
def docs(
session: nox.Session,
docs_cmd: cmd_annotated( # type: ignore
choices=[
"html",
"build",
"symlink",
"clean",
"livehtml",
"linkcheck",
"spelling",
"showlinks",
"release",
"open",
],
flags=("--docs-cmd", "-d"),
) = (),
docs_run: RUN_CLI = [], # noqa
lock: LOCK_CLI = False,
force_reinstall: FORCE_REINSTALL_CLI = False,
version: VERSION_CLI = "",
log_session: bool = False,
) -> None:
"""Runs make in docs directory. For example, 'nox -s docs -- --docs-cmd html' -> 'make -C docs html'. With 'release' option, you can set the message with 'message=...' in posargs."""
pkg_install_condaenv(
session=session,
name="docs",
lock=lock,
display_name=f"{PACKAGE_NAME}-docs",
install_package=True,
force_reinstall=force_reinstall,
log_session=log_session,
)
_docs(
session=session, cmd=docs_cmd, run=docs_run, version=version
) # pyright: ignore
@DEFAULT_SESSION_VENV
def docs_venv(
session: nox.Session,
docs_cmd: cmd_annotated( # type: ignore
choices=[
"html",
"build",
"symlink",
"clean",
"livehtml",
"linkcheck",
"spelling",
"showlinks",
"release",
"open",
],
flags=("--docs-cmd", "-d"),
) = (),
docs_run: RUN_CLI = [], # noqa
lock: LOCK_CLI = False,
force_reinstall: FORCE_REINSTALL_CLI = False,
version: VERSION_CLI = "",
log_session: bool = False,
) -> None:
"""Runs make in docs directory. For example, 'nox -s docs -- --docs-cmd html' -> 'make -C docs html'. With 'release' option, you can set the message with 'message=...' in posargs."""
pkg_install_venv(
session=session,
name="docs-venv",
lock=lock,
display_name=f"{PACKAGE_NAME}-docs-venv",
install_package=True,
force_reinstall=force_reinstall,
log_session=log_session,
extras="docs",
)
_docs(
session=session, cmd=docs_cmd, run=docs_run, version=version
) # pyright: ignore
# ** Dist pypi
def _dist_pypi(
session: nox.Session, run: list[list[str]], cmd: list[str], version: str
) -> None:
if version:
session.env["SETUPTOOLS_SCM_PRETEND_VERSION"] = version
session_run_commands(session, run)
if not run and not cmd:
cmd = ["build"]
if cmd:
if "build" in cmd:
cmd.append("clean")
cmd = sort_like(cmd, ["clean", "build", "testrelease", "release"])
session.log(f"cmd={cmd}")
for command in cmd:
if command == "clean":
session.run("rm", "-rf", "dist", external=True)
elif command == "build":
session.run("python", "-m", "build", "--outdir", "dist/")
elif command == "testrelease":
session.run("twine", "upload", "--repository", "testpypi", "dist/*")
elif command == "release":
session.run("twine", "upload", "dist/*")
@DEFAULT_SESSION_VENV
def dist_pypi(
session: nox.Session,
dist_pypi_run: RUN_CLI = [], # noqa
dist_pypi_cmd: cmd_annotated( # type: ignore
choices=["clean", "build", "testrelease", "release"],
flags=("--dist-pypi-cmd", "-p"),
) = (),
lock: LOCK_CLI = False, # pyright: ignore
force_reinstall: FORCE_REINSTALL_CLI = False,
version: VERSION_CLI = "",
log_session: bool = False,
) -> None:
"""Run 'nox -s dist-pypi -- {clean, build, testrelease, release}'."""
pkg_install_venv(
session=session,
name="dist-pypi",
requirement_paths=[session_environment_filename(name="dist-pypi.txt")],
force_reinstall=force_reinstall,
install_package=False,
log_session=log_session,
)
_dist_pypi(
session=session,
run=dist_pypi_run,
cmd=dist_pypi_cmd, # pyright: ignore
version=version,
)
@DEFAULT_SESSION
def dist_pypi_condaenv(
session: nox.Session,
dist_pypi_run: RUN_CLI = [], # noqa
dist_pypi_cmd: cmd_annotated( # type: ignore
choices=["clean", "build", "testrelease", "release"],
flags=("--dist-pypi-cmd", "-p"),
) = (),
lock: LOCK_CLI = False, # pyright: ignore
force_reinstall: FORCE_REINSTALL_CLI = False,
version: VERSION_CLI = "",
log_session: bool = False,
) -> None:
"""Run 'nox -s dist_pypi -- {clean, build, testrelease, release}'."""
# conda
pkg_install_condaenv(
session=session,
name="dist-pypi",
install_package=False,
force_reinstall=force_reinstall,
log_session=log_session,
)
_dist_pypi(
session=session,
run=dist_pypi_run,
cmd=dist_pypi_cmd, # pyright: ignore
version=version,
)
# ** Dist conda
@DEFAULT_SESSION
def dist_conda(
session: nox.Session,
dist_conda_run: RUN_CLI = [], # noqa
dist_conda_cmd: cmd_annotated( # type: ignore
choices=[
"recipe",
"build",
"clean",
"clean-recipe",
"clean-build",
"recipe-cat-full",
],
flags=("--dist-conda-cmd", "-c"),
) = (),
# lock: LOCK_CLI = False,
sdist_path: str = "",
force_reinstall: FORCE_REINSTALL_CLI = False,
log_session: bool = False,
version: VERSION_CLI = "",
) -> None:
"""Runs make -C dist-conda posargs."""
pkg_install_condaenv(
session=session,
name="dist-conda",
install_package=False,
force_reinstall=force_reinstall,
log_session=log_session,
)
run, cmd = dist_conda_run, dist_conda_cmd
session_run_commands(session, run)
if not run and not cmd:
cmd = ["recipe"]
if cmd:
if "recipe" in cmd:
cmd.append("clean-recipe")
if "build" in cmd:
cmd.append("clean-build")
if "clean" in cmd:
cmd.extend(["clean-recipe", "clean-build"])
cmd.remove("clean")
cmd = sort_like(
cmd, ["recipe-cat-full", "clean-recipe", "recipe", "clean-build", "build"]
)
if not sdist_path:
sdist_path = PACKAGE_NAME
if version:
sdist_path = f"{sdist_path}=={version}"
for command in cmd:
if command == "clean-recipe":
session.run("rm", "-rf", f"dist-conda/{PACKAGE_NAME}", external=True)
elif command == "clean-build":
session.run("rm", "-rf", "dist-conda/build", external=True)
elif command == "recipe":
session.run(
"grayskull",
"pypi",
sdist_path,
"--sections",
"package",
"source",
"build",
"requirements",
"-o",
"dist-conda",
)
_append_recipe(
f"dist-conda/{PACKAGE_NAME}/meta.yaml", ".recipe-append.yaml"
)
session.run(
"cat", f"dist-conda/{PACKAGE_NAME}/meta.yaml", external=True
)
elif command == "recipe-cat-full":
import tempfile
with tempfile.TemporaryDirectory() as d:
session.run(
"grayskull",
"pypi",
sdist_path,
"-o",
d,
)
session.run(
"cat", str(Path(d) / PACKAGE_NAME / "meta.yaml"), external=True
)
elif command == "build":
session.run(
"conda",
"mambabuild",
"--output-folder=dist-conda/build",
"--no-anaconda-upload",
"dist-conda",
)
def _append_recipe(recipe_path: str, append_path: str) -> None:
with open(recipe_path) as f:
recipe = f.readlines()
with open(append_path) as f:
append = f.readlines()
with open(recipe_path, "w") as f:
f.writelines(recipe + ["\n"] + append)
# type checking
def _typing(