forked from gentoo/gentoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distutils-r1.eclass
2193 lines (1965 loc) · 60.9 KB
/
distutils-r1.eclass
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
# Copyright 1999-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# @ECLASS: distutils-r1.eclass
# @MAINTAINER:
# Python team <[email protected]>
# @AUTHOR:
# Author: Michał Górny <[email protected]>
# Based on the work of: Krzysztof Pawlik <[email protected]>
# @SUPPORTED_EAPIS: 7 8
# @PROVIDES: python-r1 python-single-r1
# @BLURB: A simple eclass to build Python packages using distutils.
# @DESCRIPTION:
# A simple eclass providing functions to build Python packages using
# the distutils build system. It exports phase functions for all
# the src_* phases. Each of the phases runs two pseudo-phases:
# python_..._all() (e.g. python_prepare_all()) once in ${S}, then
# python_...() (e.g. python_prepare()) for each implementation
# (see: python_foreach_impl() in python-r1).
#
# In distutils-r1_src_prepare(), the 'all' function is run before
# per-implementation ones (because it creates the implementations),
# per-implementation functions are run in a random order.
#
# In remaining phase functions, the per-implementation functions are run
# before the 'all' one, and they are ordered from the least to the most
# preferred implementation (so that 'better' files overwrite 'worse'
# ones).
#
# If the ebuild doesn't specify a particular pseudo-phase function,
# the default one will be used (distutils-r1_...). Defaults are provided
# for all per-implementation pseudo-phases, python_prepare_all()
# and python_install_all(); whenever writing your own pseudo-phase
# functions, you should consider calling the defaults (and especially
# distutils-r1_python_prepare_all).
#
# Please note that distutils-r1 sets RDEPEND and BDEPEND (or DEPEND
# in earlier EAPIs) unconditionally for you.
#
# Also, please note that distutils-r1 will always inherit python-r1
# as well. Thus, all the variables defined and documented there are
# relevant to the packages using distutils-r1.
#
# For more information, please see the Python Guide:
# https://projects.gentoo.org/python/guide/
case ${EAPI} in
7|8) ;;
*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac
# @ECLASS_VARIABLE: DISTUTILS_EXT
# @PRE_INHERIT
# @DEFAULT_UNSET
# @DESCRIPTION:
# Set this variable to a non-null value if the package (possibly
# optionally) builds Python extensions (loadable modules written in C,
# Cython, Rust, etc.).
#
# When enabled, the eclass:
#
# - adds PYTHON_DEPS to DEPEND (for cross-compilation support), unless
# DISTUTILS_OPTIONAL is used
#
# - adds `debug` flag to IUSE that controls assertions (i.e. -DNDEBUG)
#
# - calls `build_ext` command if setuptools build backend is used
# and there is potential benefit from parallel builds
# @ECLASS_VARIABLE: DISTUTILS_OPTIONAL
# @DEFAULT_UNSET
# @DESCRIPTION:
# If set to a non-null value, distutils part in the ebuild will
# be considered optional. No dependencies will be added and no phase
# functions will be exported.
#
# If you enable DISTUTILS_OPTIONAL, you have to set proper dependencies
# for your package (using ${PYTHON_DEPS}) and to either call
# distutils-r1 default phase functions or call the build system
# manually.
# @ECLASS_VARIABLE: DISTUTILS_SINGLE_IMPL
# @DEFAULT_UNSET
# @DESCRIPTION:
# If set to a non-null value, the ebuild will support setting a single
# Python implementation only. It will effectively replace the python-r1
# eclass inherit with python-single-r1.
#
# Note that inheriting python-single-r1 will cause pkg_setup()
# to be exported. It must be run in order for the eclass functions
# to function properly.
# @ECLASS_VARIABLE: DISTUTILS_USE_PEP517
# @PRE_INHERIT
# @DEFAULT_UNSET
# @DESCRIPTION:
# Enable the PEP 517 mode for the specified build system. In this mode,
# the complete build and install is done in python_compile(),
# a venv-style install tree is provided to python_test(),
# and python_install() just merges the temporary install tree
# into the real fs.
#
# This mode is recommended for Python packages. However, some packages
# using custom hacks on top of distutils/setuptools may not install
# correctly in this mode. Please verify the list of installed files
# when using it.
#
# The variable specifies the build system used. Currently,
# the following values are supported:
#
# - flit - flit-core backend
#
# - flit_scm - flit_scm backend
#
# - hatchling - hatchling backend (from hatch)
#
# - jupyter - jupyter_packaging backend
#
# - maturin - maturin backend
#
# - meson-python - meson-python (mesonpy) backend
#
# - no - no PEP517 build system (see below)
#
# - pbr - pbr backend
#
# - pdm - pdm.pep517 backend
#
# - pdm-backend - pdm.backend backend
#
# - poetry - poetry-core backend
#
# - scikit-build-core - scikit-build-core backend
#
# - setuptools - distutils or setuptools (incl. legacy mode)
#
# - sip - sipbuild backend
#
# - standalone - standalone build systems without external deps
# (used for bootstrapping).
#
# The variable needs to be set before the inherit line. The eclass
# adds appropriate build-time dependencies and verifies the value.
#
# The special value "no" indicates that the package has no build system.
# This is not equivalent to unset DISTUTILS_USE_PEP517 (legacy mode).
# It causes the eclass not to include any build system dependencies
# and to disable default python_compile() and python_install()
# implementations. Baseline Python deps and phase functions will still
# be set (depending on the value of DISTUTILS_OPTIONAL). Most of
# the other eclass functions will work. Testing venv will be provided
# in ${BUILD_DIR}/install after python_compile(), and if any (other)
# files are found in ${BUILD_DIR}/install after python_install(), they
# will be merged into ${D}.
# @ECLASS_VARIABLE: DISTUTILS_USE_SETUPTOOLS
# @DEFAULT_UNSET
# @PRE_INHERIT
# @DESCRIPTION:
# Controls adding dev-python/setuptools dependency. The allowed values
# are:
#
# - no -- do not add the dependency (pure distutils package)
#
# - bdepend -- add it to BDEPEND (the default)
#
# - rdepend -- add it to BDEPEND+RDEPEND (e.g. when using pkg_resources)
#
# - manual -- do not add the dependency and suppress the checks
# (assumes you will take care of doing it correctly)
#
# This variable is effective only if DISTUTILS_OPTIONAL is disabled.
# It is available only in non-PEP517 mode. It needs to be set before
# the inherit line.
# @ECLASS_VARIABLE: DISTUTILS_DEPS
# @OUTPUT_VARIABLE
# @DESCRIPTION:
# This is an eclass-generated build-time dependency string for the build
# system packages. This string is automatically appended to BDEPEND
# unless DISTUTILS_OPTIONAL is used. This variable is available only
# in PEP 517 mode.
#
# Example use:
# @CODE
# DISTUTILS_OPTIONAL=1
# # ...
# RDEPEND="${PYTHON_DEPS}"
# BDEPEND="
# ${PYTHON_DEPS}
# ${DISTUTILS_DEPS}"
# @CODE
if [[ -z ${_DISTUTILS_R1_ECLASS} ]]; then
_DISTUTILS_R1_ECLASS=1
inherit flag-o-matic
inherit multibuild multilib multiprocessing ninja-utils toolchain-funcs
if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
inherit python-r1
else
inherit python-single-r1
fi
_distutils_set_globals() {
local rdep bdep
if [[ ${DISTUTILS_USE_PEP517} ]]; then
if [[ ${DISTUTILS_USE_SETUPTOOLS} ]]; then
die "DISTUTILS_USE_SETUPTOOLS is not used in PEP517 mode"
fi
bdep='
>=dev-python/gpep517-15[${PYTHON_USEDEP}]
'
case ${DISTUTILS_USE_PEP517} in
flit)
bdep+='
>=dev-python/flit-core-3.9.0[${PYTHON_USEDEP}]
'
;;
flit_scm)
bdep+='
>=dev-python/flit_scm-1.7.0[${PYTHON_USEDEP}]
'
;;
hatchling)
bdep+='
>=dev-python/hatchling-1.17.0[${PYTHON_USEDEP}]
'
;;
jupyter)
bdep+='
>=dev-python/jupyter-packaging-0.12.3[${PYTHON_USEDEP}]
'
;;
maturin)
bdep+='
>=dev-util/maturin-1.0.1[${PYTHON_USEDEP}]
'
;;
no)
# undo the generic deps added above
bdep=
;;
meson-python)
bdep+='
>=dev-python/meson-python-0.13.1[${PYTHON_USEDEP}]
'
;;
pbr)
bdep+='
>=dev-python/pbr-5.11.1[${PYTHON_USEDEP}]
'
;;
pdm)
bdep+='
>=dev-python/pdm-pep517-1.1.4[${PYTHON_USEDEP}]
'
;;
pdm-backend)
bdep+='
>=dev-python/pdm-backend-2.1.0[${PYTHON_USEDEP}]
'
;;
poetry)
bdep+='
>=dev-python/poetry-core-1.6.1[${PYTHON_USEDEP}]
'
;;
scikit-build-core)
bdep+='
>=dev-python/scikit-build-core-0.4.6[${PYTHON_USEDEP}]
'
;;
setuptools)
bdep+='
>=dev-python/setuptools-67.8.0-r1[${PYTHON_USEDEP}]
'
;;
sip)
bdep+='
>=dev-python/sip-6.7.9[${PYTHON_USEDEP}]
'
;;
standalone)
;;
*)
die "Unknown DISTUTILS_USE_PEP517=${DISTUTILS_USE_PEP517}"
;;
esac
elif [[ ${DISTUTILS_OPTIONAL} ]]; then
if [[ ${DISTUTILS_USE_SETUPTOOLS} ]]; then
eqawarn "QA Notice: DISTUTILS_USE_SETUPTOOLS is not used when DISTUTILS_OPTIONAL"
eqawarn "is enabled."
fi
else
local setuptools_dep='>=dev-python/setuptools-67.8.0-r1[${PYTHON_USEDEP}]'
case ${DISTUTILS_USE_SETUPTOOLS:-bdepend} in
no|manual)
;;
bdepend)
bdep+=" ${setuptools_dep}"
;;
rdepend)
bdep+=" ${setuptools_dep}"
rdep+=" ${setuptools_dep}"
;;
pyproject.toml)
die "DISTUTILS_USE_SETUPTOOLS=pyproject.toml is no longer supported, use DISTUTILS_USE_PEP517"
;;
*)
die "Invalid DISTUTILS_USE_SETUPTOOLS=${DISTUTILS_USE_SETUPTOOLS}"
;;
esac
fi
if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
bdep=${bdep//\$\{PYTHON_USEDEP\}/${PYTHON_USEDEP}}
rdep=${rdep//\$\{PYTHON_USEDEP\}/${PYTHON_USEDEP}}
else
[[ -n ${bdep} ]] && bdep="$(python_gen_cond_dep "${bdep}")"
[[ -n ${rdep} ]] && rdep="$(python_gen_cond_dep "${rdep}")"
fi
if [[ ${DISTUTILS_USE_PEP517} ]]; then
if [[ ${DISTUTILS_DEPS+1} ]]; then
if [[ ${DISTUTILS_DEPS} != "${bdep}" ]]; then
eerror "DISTUTILS_DEPS have changed between inherits!"
eerror "Before: ${DISTUTILS_DEPS}"
eerror "Now : ${bdep}"
die "DISTUTILS_DEPS integrity check failed"
fi
else
DISTUTILS_DEPS=${bdep}
readonly DISTUTILS_DEPS
fi
fi
if [[ ! ${DISTUTILS_OPTIONAL} ]]; then
RDEPEND="${PYTHON_DEPS} ${rdep}"
BDEPEND="${PYTHON_DEPS} ${bdep}"
REQUIRED_USE=${PYTHON_REQUIRED_USE}
if [[ ${DISTUTILS_EXT} ]]; then
DEPEND="${PYTHON_DEPS}"
fi
fi
if [[ ${DISTUTILS_EXT} ]]; then
IUSE="debug"
fi
}
_distutils_set_globals
unset -f _distutils_set_globals
# @ECLASS_VARIABLE: PATCHES
# @DEFAULT_UNSET
# @DESCRIPTION:
# An array containing patches to be applied to the sources before
# copying them.
#
# If unset, no custom patches will be applied.
#
# Please note, however, that at some point the eclass may apply
# additional distutils patches/quirks independently of this variable.
#
# Example:
# @CODE
# PATCHES=( "${FILESDIR}"/${P}-make-gentoo-happy.patch )
# @CODE
# @ECLASS_VARIABLE: DOCS
# @DEFAULT_UNSET
# @DESCRIPTION:
# An array containing documents installed using dodoc. The files listed
# there must exist in the directory from which
# distutils-r1_python_install_all() is run (${S} by default).
#
# If unset, the function will instead look up files matching default
# filename pattern list (from the Package Manager Specification),
# and install those found.
#
# Example:
# @CODE
# DOCS=( NEWS README )
# @CODE
# @ECLASS_VARIABLE: HTML_DOCS
# @DEFAULT_UNSET
# @DESCRIPTION:
# An array containing documents installed using dohtml. The files
# and directories listed there must exist in the directory from which
# distutils-r1_python_install_all() is run (${S} by default).
#
# If unset, no HTML docs will be installed.
#
# Example:
# @CODE
# HTML_DOCS=( doc/html/. )
# @CODE
# @ECLASS_VARIABLE: DISTUTILS_IN_SOURCE_BUILD
# @DEFAULT_UNSET
# @DESCRIPTION:
# If set to a non-null value, in-source builds will be enabled.
# If unset, the default is to use in-source builds when python_prepare()
# is declared, and out-of-source builds otherwise.
#
# If in-source builds are used, the eclass will create a copy of package
# sources for each Python implementation in python_prepare_all(),
# and work on that copy afterwards.
#
# If out-of-source builds are used, the eclass will instead work
# on the sources directly, prepending setup.py arguments with
# 'build --build-base ${BUILD_DIR}' to enforce keeping & using built
# files in the specific root.
# @ECLASS_VARIABLE: DISTUTILS_ALL_SUBPHASE_IMPLS
# @DEFAULT_UNSET
# @DESCRIPTION:
# An array of patterns specifying which implementations can be used
# for *_all() sub-phase functions. If undefined, defaults to '*'
# (allowing any implementation). If multiple values are specified,
# implementations matching any of the patterns will be accepted.
#
# For the pattern syntax, please see _python_impl_matches
# in python-utils-r1.eclass.
#
# If the restriction needs to apply conditionally to a USE flag,
# the variable should be set conditionally as well (e.g. in an early
# phase function or other convenient location).
#
# Please remember to add a matching || block to REQUIRED_USE,
# to ensure that at least one implementation matching the patterns will
# be enabled.
#
# Example:
# @CODE
# REQUIRED_USE="doc? ( || ( $(python_gen_useflags 'python2*') ) )"
#
# pkg_setup() {
# use doc && DISTUTILS_ALL_SUBPHASE_IMPLS=( 'python2*' )
# }
# @CODE
# @ECLASS_VARIABLE: DISTUTILS_ARGS
# @DEFAULT_UNSET
# @DESCRIPTION:
# An array containing options to be passed to the build system.
# Supported by a subset of build systems used by the eclass.
#
# For setuptools, the arguments will be passed as first parameters
# to setup.py invocations (via esetup.py), as well as to the PEP517
# backend. For future compatibility, only global options should be used
# and specifying commands should be avoided.
#
# For sip, the options are passed to the PEP517 backend in a form
# resembling sip-build calls. Options taking arguments need to
# be specified in the "--key=value" form, while flag options as "--key".
# If an option takes multiple arguments, it can be specified multiple
# times, same as for sip-build.
#
# Example:
# @CODE
# python_configure_all() {
# DISTUTILS_ARGS=( --enable-my-hidden-option )
# }
# @CODE
# @FUNCTION: distutils_enable_sphinx
# @USAGE: <subdir> [--no-autodoc | <plugin-pkgs>...]
# @DESCRIPTION:
# Set up IUSE, BDEPEND, python_check_deps() and python_compile_all() for
# building HTML docs via dev-python/sphinx. python_compile_all() will
# append to HTML_DOCS if docs are enabled.
#
# This helper is meant for the most common case, that is a single Sphinx
# subdirectory with standard layout, building and installing HTML docs
# behind USE=doc. It assumes it's the only consumer of the three
# aforementioned functions. If you need to use a custom implementation,
# you can't use it.
#
# If your package uses additional Sphinx plugins, they should be passed
# (without PYTHON_USEDEP) as <plugin-pkgs>. The function will take care
# of setting appropriate any-of dep and python_check_deps().
#
# If no plugin packages are specified, the eclass will still utilize
# any-r1 API to support autodoc (documenting source code).
# If the package uses neither autodoc nor additional plugins, you should
# pass --no-autodoc to disable this API and simplify the resulting code.
#
# This function must be called in global scope. Take care not to
# overwrite the variables set by it. If you need to extend
# python_compile_all(), you can call the original implementation
# as sphinx_compile_all.
distutils_enable_sphinx() {
debug-print-function ${FUNCNAME} "${@}"
[[ ${#} -ge 1 ]] || die "${FUNCNAME} takes at least one arg: <subdir>"
_DISTUTILS_SPHINX_SUBDIR=${1}
shift
_DISTUTILS_SPHINX_PLUGINS=( "${@}" )
local deps autodoc=1 d
deps=">=dev-python/sphinx-5.3.0[\${PYTHON_USEDEP}]"
for d; do
if [[ ${d} == --no-autodoc ]]; then
autodoc=
else
deps+="
${d}[\${PYTHON_USEDEP}]"
if [[ ! ${autodoc} ]]; then
die "${FUNCNAME}: do not pass --no-autodoc if external plugins are used"
fi
fi
done
if [[ ${autodoc} ]]; then
if [[ ${DISTUTILS_SINGLE_IMPL} ]]; then
deps="$(python_gen_cond_dep "${deps}")"
else
deps="$(python_gen_any_dep "${deps}")"
fi
python_check_deps() {
use doc || return 0
local p
for p in ">=dev-python/sphinx-5.3.0" \
"${_DISTUTILS_SPHINX_PLUGINS[@]}"
do
python_has_version "${p}[${PYTHON_USEDEP}]" ||
return 1
done
}
else
deps=">=dev-python/sphinx-5.3.0"
fi
sphinx_compile_all() {
use doc || return
local confpy=${_DISTUTILS_SPHINX_SUBDIR}/conf.py
[[ -f ${confpy} ]] ||
die "${confpy} not found, distutils_enable_sphinx call wrong"
if [[ ${_DISTUTILS_SPHINX_PLUGINS[0]} == --no-autodoc ]]; then
if grep -F -q 'sphinx.ext.autodoc' "${confpy}"; then
die "distutils_enable_sphinx: --no-autodoc passed but sphinx.ext.autodoc found in ${confpy}"
fi
elif [[ -z ${_DISTUTILS_SPHINX_PLUGINS[@]} ]]; then
if ! grep -F -q 'sphinx.ext.autodoc' "${confpy}"; then
die "distutils_enable_sphinx: sphinx.ext.autodoc not found in ${confpy}, pass --no-autodoc"
fi
fi
build_sphinx "${_DISTUTILS_SPHINX_SUBDIR}"
}
python_compile_all() { sphinx_compile_all; }
IUSE+=" doc"
BDEPEND+=" doc? ( ${deps} )"
# we need to ensure successful return in case we're called last,
# otherwise Portage may wrongly assume sourcing failed
return 0
}
# @FUNCTION: distutils_enable_tests
# @USAGE: [--install] <test-runner>
# @DESCRIPTION:
# Set up IUSE, RESTRICT, BDEPEND and python_test() for running tests
# with the specified test runner. Also copies the current value
# of RDEPEND to test?-BDEPEND. The test-runner argument must be one of:
#
# - nose: nosetests (dev-python/nose)
#
# - pytest: dev-python/pytest
#
# - setup.py: setup.py test (no deps included)
#
# - unittest: for built-in Python unittest module
#
# Additionally, if --install is passed as the first parameter,
# 'distutils_install_for_testing --via-root' is called before running
# the test suite.
#
# This function is meant as a helper for common use cases, and it only
# takes care of basic setup. You still need to list additional test
# dependencies manually. If you have uncommon use case, you should
# not use it and instead enable tests manually.
#
# This function must be called in global scope, after RDEPEND has been
# declared. Take care not to overwrite the variables set by it.
distutils_enable_tests() {
debug-print-function ${FUNCNAME} "${@}"
_DISTUTILS_TEST_INSTALL=
case ${1} in
--install)
if [[ ${DISTUTILS_USE_PEP517} ]]; then
die "${FUNCNAME} --install is not implemented in PEP517 mode"
fi
_DISTUTILS_TEST_INSTALL=1
shift
;;
esac
[[ ${#} -eq 1 ]] || die "${FUNCNAME} takes exactly one argument: test-runner"
local test_deps=${RDEPEND}
local test_pkgs
case ${1} in
nose)
test_pkgs='>=dev-python/nose-1.3.7_p20221026[${PYTHON_USEDEP}]'
;;
pytest)
test_pkgs='>=dev-python/pytest-7.3.1[${PYTHON_USEDEP}]'
if [[ -n ${EPYTEST_TIMEOUT} ]]; then
test_pkgs+=' dev-python/pytest-timeout[${PYTHON_USEDEP}]'
fi
if [[ ${EPYTEST_XDIST} ]]; then
test_pkgs+=' dev-python/pytest-xdist[${PYTHON_USEDEP}]'
fi
;;
setup.py)
;;
unittest)
# unittest-or-fail is needed in py<3.12
test_deps+="
$(python_gen_cond_dep '
dev-python/unittest-or-fail[${PYTHON_USEDEP}]
' 3.10 3.11)
"
;;
*)
die "${FUNCNAME}: unsupported argument: ${1}"
esac
_DISTUTILS_TEST_RUNNER=${1}
python_test() { distutils-r1_python_test; }
if [[ -n ${test_pkgs} ]]; then
if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
test_deps+=" ${test_pkgs//'${PYTHON_USEDEP}'/${PYTHON_USEDEP}}"
else
test_deps+=" $(python_gen_cond_dep "
${test_pkgs}
")"
fi
fi
if [[ -n ${test_deps} ]]; then
IUSE+=" test"
RESTRICT+=" !test? ( test )"
BDEPEND+=" test? ( ${test_deps} )"
fi
# we need to ensure successful return in case we're called last,
# otherwise Portage may wrongly assume sourcing failed
return 0
}
# @FUNCTION: esetup.py
# @USAGE: [<args>...]
# @DESCRIPTION:
# Run setup.py using currently selected Python interpreter
# (if ${EPYTHON} is set; fallback 'python' otherwise).
#
# setup.py will be passed the following, in order:
#
# 1. ${DISTUTILS_ARGS[@]}
#
# 2. ${mydistutilsargs[@]} (deprecated)
#
# 3. additional arguments passed to the esetup.py function.
#
# Please note that setup.py will respect defaults (unless overridden
# via command-line options) from setup.cfg that is created
# in distutils-r1_python_compile and in distutils-r1_python_install.
#
# This command dies on failure.
esetup.py() {
debug-print-function ${FUNCNAME} "${@}"
_python_check_EPYTHON
if [[ ${BUILD_DIR} && ! ${DISTUTILS_USE_PEP517} ]]; then
_distutils-r1_create_setup_cfg
fi
local setup_py=( setup.py )
if [[ ! -f setup.py ]]; then
# The following call can succeed even if the package does not
# feature any setuptools configuration. In non-PEP517 mode this
# could lead to installing an "empty" package. In PEP517 mode,
# we verify the build system when invoking the backend,
# rendering this check redundant (and broken for projects using
# pyproject.toml configuration).
if [[ ! ${DISTUTILS_USE_PEP517} && ! -f setup.cfg ]]; then
die "${FUNCNAME}: setup.py nor setup.cfg not found"
fi
setup_py=( -c "from setuptools import setup; setup()" )
fi
if [[ ${EAPI} != 7 && ${mydistutilsargs[@]} ]]; then
die "mydistutilsargs is banned in EAPI ${EAPI} (use DISTUTILS_ARGS)"
fi
set -- "${EPYTHON}" "${setup_py[@]}" "${DISTUTILS_ARGS[@]}" \
"${mydistutilsargs[@]}" "${@}"
echo "${@}" >&2
"${@}" || die -n
local ret=${?}
if [[ ${BUILD_DIR} && ! ${DISTUTILS_USE_PEP517} ]]; then
rm "${HOME}"/.pydistutils.cfg || die -n
fi
return ${ret}
}
# @FUNCTION: distutils_install_for_testing
# @USAGE: [--via-root|--via-home|--via-venv] [<args>...]
# @DESCRIPTION:
# Install the package into a temporary location for running tests.
# Update PYTHONPATH appropriately and set TEST_DIR to the test
# installation root. The Python packages will be installed in 'lib'
# subdir, and scripts in 'scripts' subdir (like in BUILD_DIR).
#
# Please note that this function should be only used if package uses
# namespaces (and therefore proper install needs to be done to enforce
# PYTHONPATH) or tests rely on the results of install command.
# For most of the packages, tests built in BUILD_DIR are good enough.
#
# The function supports three install modes. These are:
#
# --via-root (the default) that uses 'setup.py install --root=...'
# combined with PYTHONPATH and is recommended for the majority
# of packages.
#
# --via-venv that creates a (non-isolated) venv and installs the package
# into it via 'setup.py install'. This mode does not use PYTHONPATH
# but requires python to be called via PATH. It may solve a few corner
# cases that --via-root do not support.
#
# --via-home that uses 'setup.py install --home=...'. This is
# a historical mode that was mostly broken by setuptools 50.3.0+.
# If your package does not work with the other two modes but works with
# this one, please report a bug.
#
# Please note that in order to test the solution properly you need
# to unmerge the package first.
#
# This function is not available in PEP517 mode. The eclass provides
# a venv-style install unconditionally and therefore it should no longer
# be necessary.
distutils_install_for_testing() {
debug-print-function ${FUNCNAME} "${@}"
if [[ ${DISTUTILS_USE_PEP517} ]]; then
die "${FUNCNAME} is not implemented in PEP517 mode"
fi
# A few notes about --via-home mode:
# 1) 'install --home' is terribly broken on pypy, so we need
# to override --install-lib and --install-scripts,
# 2) non-root 'install' complains about PYTHONPATH and missing dirs,
# so we need to set it properly and mkdir them,
# 3) it runs a bunch of commands which write random files to cwd,
# in order to avoid that, we add the necessary path overrides
# in _distutils-r1_create_setup_cfg.
local install_method=root
case ${1} in
--via-home)
[[ ${EAPI} == 7 ]] || die "${*} is banned in EAPI ${EAPI}"
install_method=home
shift
;;
--via-root)
install_method=root
shift
;;
--via-venv)
install_method=venv
shift
;;
esac
TEST_DIR=${BUILD_DIR}/test
local add_args=()
if [[ ${install_method} == venv ]]; then
# create a quasi-venv
mkdir -p "${TEST_DIR}"/bin || die
ln -s "${PYTHON}" "${TEST_DIR}/bin/${EPYTHON}" || die
ln -s "${EPYTHON}" "${TEST_DIR}/bin/python3" || die
ln -s "${EPYTHON}" "${TEST_DIR}/bin/python" || die
cat > "${TEST_DIR}"/pyvenv.cfg <<-EOF || die
include-system-site-packages = true
EOF
# we only do the minimal necessary subset of activate script
PATH=${TEST_DIR}/bin:${PATH}
# unset PYTHONPATH in order to prevent BUILD_DIR from overriding
# venv packages
unset PYTHONPATH
# force root-style install (note: venv adds TEST_DIR to prefixes,
# so we need to pass --root=/)
add_args=(
--root=/
)
else
local bindir=${TEST_DIR}/scripts
local libdir=${TEST_DIR}/lib
PATH=${bindir}:${PATH}
PYTHONPATH=${libdir}:${PYTHONPATH}
case ${install_method} in
home)
add_args=(
--home="${TEST_DIR}"
--install-lib="${libdir}"
--install-scripts="${bindir}"
)
mkdir -p "${libdir}" || die
;;
root)
add_args=(
--root="${TEST_DIR}"
--install-lib=lib
--install-scripts=scripts
)
;;
esac
fi
esetup.py install "${add_args[@]}" "${@}"
}
# @FUNCTION: distutils_write_namespace
# @USAGE: <namespace>...
# @DESCRIPTION:
# Write the __init__.py file for the requested namespace into PEP517
# install tree, in order to fix running tests when legacy namespace
# packages are installed (dev-python/namespace-*).
#
# This function must only be used in python_test(). The created file
# will automatically be removed upon leaving the test phase.
distutils_write_namespace() {
debug-print-function ${FUNCNAME} "${@}"
if [[ ! ${DISTUTILS_USE_PEP517:-no} != no ]]; then
die "${FUNCNAME} is available only in PEP517 mode"
fi
if [[ ${EBUILD_PHASE} != test || ! ${BUILD_DIR} ]]; then
die "${FUNCNAME} should only be used in python_test"
fi
local namespace
for namespace; do
if [[ ${namespace} == *[./]* ]]; then
die "${FUNCNAME} does not support nested namespaces at the moment"
fi
local path=${BUILD_DIR}/install$(python_get_sitedir)/${namespace}/__init__.py
if [[ -f ${path} ]]; then
die "Requested namespace ${path} exists already!"
fi
cat > "${path}" <<-EOF || die
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
EOF
_DISTUTILS_POST_PHASE_RM+=( "${path}" )
done
}
# @FUNCTION: _distutils-r1_disable_ez_setup
# @INTERNAL
# @DESCRIPTION:
# Stub out ez_setup.py and distribute_setup.py to prevent packages
# from trying to download a local copy of setuptools.
_distutils-r1_disable_ez_setup() {
if [[ ${DISTUTILS_USE_PEP517} ]]; then
die "${FUNCNAME} is not implemented in PEP517 mode"
fi
local stub="def use_setuptools(*args, **kwargs): pass"
if [[ -f ez_setup.py ]]; then
echo "${stub}" > ez_setup.py || die
fi
if [[ -f distribute_setup.py ]]; then
echo "${stub}" > distribute_setup.py || die
fi
}
# @FUNCTION: _distutils-r1_handle_pyproject_toml
# @INTERNAL
# @DESCRIPTION:
# Verify whether DISTUTILS_USE_SETUPTOOLS is set correctly
# for pyproject.toml build systems (in non-PEP517 mode).
_distutils-r1_handle_pyproject_toml() {
if [[ ${DISTUTILS_USE_PEP517} ]]; then
die "${FUNCNAME} is not implemented in PEP517 mode"
fi
[[ ${DISTUTILS_USE_SETUPTOOLS} == manual ]] && return
if [[ ! -f setup.py && -f pyproject.toml ]]; then
eerror "No setup.py found but pyproject.toml is present. Please migrate"
eerror "the package to use DISTUTILS_USE_PEP517. See:"
eerror " https://projects.gentoo.org/python/guide/distutils.html"
die "No setup.py found and PEP517 mode not enabled"
fi
}
# @FUNCTION: _distutils-r1_check_all_phase_mismatch
# @INTERNAL
# @DESCRIPTION:
# Verify whether *_all phase impls is not called from from non-*_all
# subphase.
_distutils-r1_check_all_phase_mismatch() {
if has "python_${EBUILD_PHASE}" "${FUNCNAME[@]}"; then
eqawarn "QA Notice: distutils-r1_python_${EBUILD_PHASE}_all called"
eqawarn "from python_${EBUILD_PHASE}. Did you mean to use"
eqawarn "python_${EBUILD_PHASE}_all()?"
[[ ${EAPI} != 7 ]] &&
die "distutils-r1_python_${EBUILD_PHASE}_all called from python_${EBUILD_PHASE}."
fi
}
# @FUNCTION: _distutils-r1_print_package_versions
# @INTERNAL
# @DESCRIPTION:
# Print the version of the relevant build system packages to aid
# debugging.
_distutils-r1_print_package_versions() {
local packages=()
if [[ ${DISTUTILS_USE_PEP517} ]]; then
packages+=(
dev-python/gpep517
dev-python/installer
)
if [[ ${DISTUTILS_EXT} ]]; then
packages+=(
dev-python/cython
)
fi
case ${DISTUTILS_USE_PEP517} in
flit)
packages+=(
dev-python/flit-core
)
;;
flit_scm)
packages+=(
dev-python/flit-core
dev-python/flit_scm
dev-python/setuptools-scm
)
;;
hatchling)
packages+=(
dev-python/hatchling
dev-python/hatch-fancy-pypi-readme
dev-python/hatch-vcs
)
;;
jupyter)
packages+=(
dev-python/jupyter-packaging
dev-python/setuptools
dev-python/setuptools-scm
dev-python/wheel
)
;;
maturin)
packages+=(
dev-util/maturin
)
;;
no)
return
;;
meson-python)
packages+=(
dev-python/meson-python
)
;;
pbr)
packages+=(
dev-python/pbr
dev-python/setuptools
dev-python/wheel
)
;;