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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
|
# Copyright 1999-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# @ECLASS: python-utils-r1.eclass
# @MAINTAINER:
# Python team <python@gentoo.org>
# @AUTHOR:
# Author: Michał Górny <mgorny@gentoo.org>
# Based on work of: Krzysztof Pawlik <nelchael@gentoo.org>
# @SUPPORTED_EAPIS: 7 8
# @BLURB: Utility functions for packages with Python parts.
# @DESCRIPTION:
# A utility eclass providing functions to query Python implementations,
# install Python modules and scripts.
#
# This eclass does not set any metadata variables nor export any phase
# functions. It can be inherited safely.
#
# For more information, please see the Python Guide:
# https://projects.gentoo.org/python/guide/
# NOTE: When dropping support for EAPIs here, we need to update
# metadata/install-qa-check.d/60python-pyc
# See bug #704286, bug #781878
case ${EAPI} in
7|8) ;;
*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac
if [[ ! ${_PYTHON_UTILS_R1_ECLASS} ]]; then
_PYTHON_UTILS_R1_ECLASS=1
[[ ${EAPI} == 7 ]] && inherit eapi8-dosym
inherit multiprocessing toolchain-funcs
# @ECLASS_VARIABLE: _PYTHON_ALL_IMPLS
# @INTERNAL
# @DESCRIPTION:
# All supported Python implementations, most preferred last.
_PYTHON_ALL_IMPLS=(
pypy3
python3_{10..13}
)
readonly _PYTHON_ALL_IMPLS
# @ECLASS_VARIABLE: _PYTHON_HISTORICAL_IMPLS
# @INTERNAL
# @DESCRIPTION:
# All historical Python implementations that are no longer supported.
_PYTHON_HISTORICAL_IMPLS=(
jython2_7
pypy pypy1_{8,9} pypy2_0
python2_{5..7}
python3_{1..9}
)
readonly _PYTHON_HISTORICAL_IMPLS
# @ECLASS_VARIABLE: PYTHON_COMPAT_NO_STRICT
# @INTERNAL
# @DESCRIPTION:
# Set to a non-empty value in order to make eclass tolerate (ignore)
# unknown implementations in PYTHON_COMPAT.
#
# This is intended to be set by the user when using ebuilds that may
# have unknown (newer) implementations in PYTHON_COMPAT. The assumption
# is that the ebuilds are intended to be used within multiple contexts
# which can involve revisions of this eclass that support a different
# set of Python implementations.
# @FUNCTION: _python_verify_patterns
# @USAGE: <pattern>...
# @INTERNAL
# @DESCRIPTION:
# Verify whether the patterns passed to the eclass function are correct
# (i.e. can match any valid implementation). Dies on wrong pattern.
_python_verify_patterns() {
debug-print-function ${FUNCNAME} "${@}"
local impl pattern
for pattern; do
case ${pattern} in
-[23]|3.[89]|3.1[0-3])
continue
;;
esac
for impl in "${_PYTHON_ALL_IMPLS[@]}" "${_PYTHON_HISTORICAL_IMPLS[@]}"
do
[[ ${impl} == ${pattern/./_} ]] && continue 2
done
die "Invalid implementation pattern: ${pattern}"
done
}
# @FUNCTION: _python_set_impls
# @INTERNAL
# @DESCRIPTION:
# Check PYTHON_COMPAT for well-formedness and validity, then set
# two global variables:
#
# - _PYTHON_SUPPORTED_IMPLS containing valid implementations supported
# by the ebuild (PYTHON_COMPAT - dead implementations),
#
# - and _PYTHON_UNSUPPORTED_IMPLS containing valid implementations that
# are not supported by the ebuild.
#
# Implementations in both variables are ordered using the pre-defined
# eclass implementation ordering.
#
# This function must be called once in global scope by an eclass
# utilizing PYTHON_COMPAT.
_python_set_impls() {
local i
# TODO: drop BASH_VERSINFO check when we require EAPI 8
if [[ ${BASH_VERSINFO[0]} -ge 5 ]]; then
[[ ${PYTHON_COMPAT@a} == *a* ]]
else
[[ $(declare -p PYTHON_COMPAT) == "declare -a"* ]]
fi
if [[ ${?} -ne 0 ]]; then
if ! declare -p PYTHON_COMPAT &>/dev/null; then
die 'PYTHON_COMPAT not declared.'
else
die 'PYTHON_COMPAT must be an array.'
fi
fi
local obsolete=()
if [[ ! ${PYTHON_COMPAT_NO_STRICT} ]]; then
for i in "${PYTHON_COMPAT[@]}"; do
# check for incorrect implementations
# we're using pattern matching as an optimization
# please keep them in sync with _PYTHON_ALL_IMPLS
# and _PYTHON_HISTORICAL_IMPLS
case ${i} in
pypy3|python3_9|python3_1[0-3])
;;
jython2_7|pypy|pypy1_[89]|pypy2_0|python2_[5-7]|python3_[1-9])
obsolete+=( "${i}" )
;;
*)
if has "${i}" "${_PYTHON_ALL_IMPLS[@]}" \
"${_PYTHON_HISTORICAL_IMPLS[@]}"
then
die "Mis-synced patterns in _python_set_impls: missing ${i}"
else
die "Invalid implementation in PYTHON_COMPAT: ${i}"
fi
esac
done
fi
local supp=() unsupp=()
for i in "${_PYTHON_ALL_IMPLS[@]}"; do
if has "${i}" "${PYTHON_COMPAT[@]}"; then
supp+=( "${i}" )
else
unsupp+=( "${i}" )
fi
done
if [[ ! ${supp[@]} ]]; then
die "No supported implementation in PYTHON_COMPAT."
fi
if [[ ${_PYTHON_SUPPORTED_IMPLS[@]} ]]; then
# set once already, verify integrity
if [[ ${_PYTHON_SUPPORTED_IMPLS[@]} != ${supp[@]} ]]; then
eerror "Supported impls (PYTHON_COMPAT) changed between inherits!"
eerror "Before: ${_PYTHON_SUPPORTED_IMPLS[*]}"
eerror "Now : ${supp[*]}"
die "_PYTHON_SUPPORTED_IMPLS integrity check failed"
fi
if [[ ${_PYTHON_UNSUPPORTED_IMPLS[@]} != ${unsupp[@]} ]]; then
eerror "Unsupported impls changed between inherits!"
eerror "Before: ${_PYTHON_UNSUPPORTED_IMPLS[*]}"
eerror "Now : ${unsupp[*]}"
die "_PYTHON_UNSUPPORTED_IMPLS integrity check failed"
fi
else
_PYTHON_SUPPORTED_IMPLS=( "${supp[@]}" )
_PYTHON_UNSUPPORTED_IMPLS=( "${unsupp[@]}" )
readonly _PYTHON_SUPPORTED_IMPLS _PYTHON_UNSUPPORTED_IMPLS
fi
}
# @FUNCTION: _python_impl_matches
# @USAGE: <impl> [<pattern>...]
# @INTERNAL
# @DESCRIPTION:
# Check whether the specified <impl> matches at least one
# of the patterns following it. Return 0 if it does, 1 otherwise.
# Matches if no patterns are provided.
#
# <impl> can be in PYTHON_COMPAT or EPYTHON form. The patterns
# can either be fnmatch-style or stdlib versions, e.g. "3.8", "3.9".
# In the latter case, pypy3 will match if there is at least one pypy3
# version matching the stdlib version.
_python_impl_matches() {
[[ ${#} -ge 1 ]] || die "${FUNCNAME}: takes at least 1 parameter"
[[ ${#} -eq 1 ]] && return 0
local impl=${1/./_} pattern
shift
for pattern; do
case ${pattern} in
-2|python2*|pypy)
if [[ ${EAPI} != 7 ]]; then
eerror
eerror "Python 2 is no longer supported in Gentoo, please remove Python 2"
eerror "${FUNCNAME[1]} calls."
die "Passing ${pattern} to ${FUNCNAME[1]} is banned in EAPI ${EAPI}"
fi
;;
-3)
# NB: "python3*" is fine, as "not pypy3"
if [[ ${EAPI} != 7 ]]; then
eerror
eerror "Python 2 is no longer supported in Gentoo, please remove Python 2"
eerror "${FUNCNAME[1]} calls."
die "Passing ${pattern} to ${FUNCNAME[1]} is banned in EAPI ${EAPI}"
fi
return 0
;;
3.10)
[[ ${impl} == python${pattern/./_} || ${impl} == pypy3 ]] &&
return 0
;;
3.8|3.9|3.1[1-3])
[[ ${impl} == python${pattern/./_} ]] && return 0
;;
*)
# unify value style to allow lax matching
[[ ${impl} == ${pattern/./_} ]] && return 0
;;
esac
done
return 1
}
# @ECLASS_VARIABLE: PYTHON
# @DEFAULT_UNSET
# @DESCRIPTION:
# The absolute path to the current Python interpreter.
#
# This variable is set automatically in the following contexts:
#
# python-r1: Set in functions called by python_foreach_impl() or after
# calling python_setup().
#
# python-single-r1: Set after calling python-single-r1_pkg_setup().
#
# distutils-r1: Set within any of the python sub-phase functions.
#
# Example value:
# @CODE
# /usr/bin/python2.7
# @CODE
# @ECLASS_VARIABLE: EPYTHON
# @DEFAULT_UNSET
# @DESCRIPTION:
# The executable name of the current Python interpreter.
#
# This variable is set automatically in the following contexts:
#
# python-r1: Set in functions called by python_foreach_impl() or after
# calling python_setup().
#
# python-single-r1: Set after calling python-single-r1_pkg_setup().
#
# distutils-r1: Set within any of the python sub-phase functions.
#
# Example value:
# @CODE
# python2.7
# @CODE
# @FUNCTION: _python_export
# @USAGE: [<impl>] <variables>...
# @INTERNAL
# @DESCRIPTION:
# Set and export the Python implementation-relevant variables passed
# as parameters.
#
# The optional first parameter may specify the requested Python
# implementation (either as PYTHON_TARGETS value, e.g. python2_7,
# or an EPYTHON one, e.g. python2.7). If no implementation passed,
# the current one will be obtained from ${EPYTHON}.
#
# The variables which can be exported are: PYTHON, EPYTHON,
# PYTHON_SITEDIR. They are described more completely in the eclass
# variable documentation.
_python_export() {
debug-print-function ${FUNCNAME} "${@}"
local impl var
case "${1}" in
python*|jython*)
impl=${1/_/.}
shift
;;
pypy|pypy3)
impl=${1}
shift
;;
*)
impl=${EPYTHON}
if [[ -z ${impl} ]]; then
die "_python_export called without a python implementation and EPYTHON is unset"
fi
;;
esac
debug-print "${FUNCNAME}: implementation: ${impl}"
for var; do
case "${var}" in
EPYTHON)
export EPYTHON=${impl}
debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
;;
PYTHON)
# Under EAPI 7+, this should just use ${BROOT}, but Portage
# <3.0.50 was buggy, and prefix users need this to update.
export PYTHON=${BROOT-${EPREFIX}}/usr/bin/${impl}
if [[ " python jython pypy pypy3 " != *" ${PN} "* ]] \
&& [[ ! -x ${EPREFIX}/usr/bin/${impl} ]] \
&& { has prefix-stack ${USE} || has stacked-prefix ${FEATURES} ;} ; then
# Need to look in build prefix
if [[ -x ${BROOT-${PORTAGE_OVERRIDE_EPREFIX}}/usr/bin/${impl} ]]; then
PYTHON=${BROOT-${PORTAGE_OVERRIDE_EPREFIX}}/usr/bin/${impl}
fi
fi
debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
;;
PYTHON_STDLIB)
[[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
PYTHON_STDLIB=$(
"${PYTHON}" - "${EPREFIX}/usr" <<-EOF || die
import sys, sysconfig
print(sysconfig.get_path("stdlib", vars={"installed_base": sys.argv[1]}))
EOF
)
export PYTHON_STDLIB
debug-print "${FUNCNAME}: PYTHON_STDLIB = ${PYTHON_STDLIB}"
;;
PYTHON_SITEDIR)
[[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
PYTHON_SITEDIR=$(
"${PYTHON}" - "${EPREFIX}/usr" <<-EOF || die
import sys, sysconfig
print(sysconfig.get_path("purelib", vars={"base": sys.argv[1]}))
EOF
)
export PYTHON_SITEDIR
debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
;;
PYTHON_INCLUDEDIR)
[[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
PYTHON_INCLUDEDIR=$(
"${PYTHON}" - "${ESYSROOT}/usr" <<-EOF || die
import sys, sysconfig
print(sysconfig.get_path("platinclude", vars={"installed_platbase": sys.argv[1]}))
EOF
)
export PYTHON_INCLUDEDIR
debug-print "${FUNCNAME}: PYTHON_INCLUDEDIR = ${PYTHON_INCLUDEDIR}"
# Jython gives a non-existing directory
if [[ ! -d ${PYTHON_INCLUDEDIR} ]]; then
die "${impl} does not install any header files!"
fi
;;
PYTHON_LIBPATH)
[[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
PYTHON_LIBPATH=$(
"${PYTHON}" - <<-EOF || die
import os.path, sysconfig
print(
os.path.join(
sysconfig.get_config_var("LIBDIR"),
sysconfig.get_config_var("LDLIBRARY"))
if sysconfig.get_config_var("LDLIBRARY")
else "")
EOF
)
export PYTHON_LIBPATH
debug-print "${FUNCNAME}: PYTHON_LIBPATH = ${PYTHON_LIBPATH}"
if [[ ! ${PYTHON_LIBPATH} ]]; then
die "${impl} lacks a (usable) dynamic library"
fi
;;
PYTHON_CFLAGS)
local val
case "${impl}" in
python*)
# python-2.7, python-3.2, etc.
val=$($(tc-getPKG_CONFIG) --cflags ${impl/n/n-}) || die
;;
*)
die "${impl}: obtaining ${var} not supported"
;;
esac
export PYTHON_CFLAGS=${val}
debug-print "${FUNCNAME}: PYTHON_CFLAGS = ${PYTHON_CFLAGS}"
;;
PYTHON_LIBS)
local val
case "${impl}" in
python*)
# python3.8+
val=$($(tc-getPKG_CONFIG) --libs ${impl/n/n-}-embed) || die
;;
*)
die "${impl}: obtaining ${var} not supported"
;;
esac
export PYTHON_LIBS=${val}
debug-print "${FUNCNAME}: PYTHON_LIBS = ${PYTHON_LIBS}"
;;
PYTHON_CONFIG)
local flags val
case "${impl}" in
python*)
[[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
flags=$(
"${PYTHON}" - <<-EOF || die
import sysconfig
print(sysconfig.get_config_var("ABIFLAGS")
or "")
EOF
)
val=${PYTHON}${flags}-config
;;
*)
die "${impl}: obtaining ${var} not supported"
;;
esac
export PYTHON_CONFIG=${val}
debug-print "${FUNCNAME}: PYTHON_CONFIG = ${PYTHON_CONFIG}"
;;
PYTHON_PKG_DEP)
local d
case ${impl} in
python*)
PYTHON_PKG_DEP="dev-lang/python:${impl#python}"
;;
pypy3)
PYTHON_PKG_DEP="dev-python/${impl}:="
;;
*)
die "Invalid implementation: ${impl}"
esac
# use-dep
if [[ ${PYTHON_REQ_USE} ]]; then
PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
fi
export PYTHON_PKG_DEP
debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
;;
PYTHON_SCRIPTDIR)
local dir
export PYTHON_SCRIPTDIR=${EPREFIX}/usr/lib/python-exec/${impl}
if [[ " python jython pypy pypy3 " != *" ${PN} "* ]] \
&& [[ ! -x ${EPREFIX}/usr/bin/${impl} ]] \
&& { has prefix-stack ${USE} || has stacked-prefix ${FEATURES} ;} ; then
# Need to look in build prefix
if [[ -x ${BROOT-${PORTAGE_OVERRIDE_EPREFIX}}/usr/bin/${impl} ]]; then
PYTHON_SCRIPTDIR=${BROOT-${PORTAGE_OVERRIDE_EPREFIX}}/usr/lib/python-exec/${impl}
fi
fi
debug-print "${FUNCNAME}: PYTHON_SCRIPTDIR = ${PYTHON_SCRIPTDIR}"
;;
*)
die "_python_export: unknown variable ${var}"
esac
done
}
# @FUNCTION: python_get_stdlib
# @USAGE: [<impl>]
# @DESCRIPTION:
# Obtain and print the 'stdlib' path for the given implementation. If no
# implementation is provided, ${EPYTHON} will be used.
python_get_stdlib() {
debug-print-function ${FUNCNAME} "${@}"
_python_export "${@}" PYTHON_STDLIB
echo "${PYTHON_STDLIB}"
}
# @FUNCTION: python_get_sitedir
# @USAGE: [<impl>]
# @DESCRIPTION:
# Obtain and print the 'site-packages' path for the given
# implementation. If no implementation is provided, ${EPYTHON} will
# be used.
python_get_sitedir() {
debug-print-function ${FUNCNAME} "${@}"
_python_export "${@}" PYTHON_SITEDIR
echo "${PYTHON_SITEDIR}"
}
# @FUNCTION: python_get_includedir
# @USAGE: [<impl>]
# @DESCRIPTION:
# Obtain and print the include path for the given implementation. If no
# implementation is provided, ${EPYTHON} will be used.
python_get_includedir() {
debug-print-function ${FUNCNAME} "${@}"
_python_export "${@}" PYTHON_INCLUDEDIR
echo "${PYTHON_INCLUDEDIR}"
}
# @FUNCTION: python_get_library_path
# @USAGE: [<impl>]
# @DESCRIPTION:
# Obtain and print the Python library path for the given implementation.
# If no implementation is provided, ${EPYTHON} will be used.
#
# Please note that this function can be used with CPython only. Use
# in another implementation will result in a fatal failure.
python_get_library_path() {
debug-print-function ${FUNCNAME} "${@}"
_python_export "${@}" PYTHON_LIBPATH
echo "${PYTHON_LIBPATH}"
}
# @FUNCTION: python_get_CFLAGS
# @USAGE: [<impl>]
# @DESCRIPTION:
# Obtain and print the compiler flags for building against Python,
# for the given implementation. If no implementation is provided,
# ${EPYTHON} will be used.
#
# Please note that this function can be used with CPython only.
# It requires Python and pkg-config installed, and therefore proper
# build-time dependencies need be added to the ebuild.
python_get_CFLAGS() {
debug-print-function ${FUNCNAME} "${@}"
_python_export "${@}" PYTHON_CFLAGS
echo "${PYTHON_CFLAGS}"
}
# @FUNCTION: python_get_LIBS
# @USAGE: [<impl>]
# @DESCRIPTION:
# Obtain and print the compiler flags for linking against Python,
# for the given implementation. If no implementation is provided,
# ${EPYTHON} will be used.
#
# Please note that this function can be used with CPython only.
# It requires Python and pkg-config installed, and therefore proper
# build-time dependencies need be added to the ebuild.
python_get_LIBS() {
debug-print-function ${FUNCNAME} "${@}"
_python_export "${@}" PYTHON_LIBS
echo "${PYTHON_LIBS}"
}
# @FUNCTION: python_get_PYTHON_CONFIG
# @USAGE: [<impl>]
# @DESCRIPTION:
# Obtain and print the PYTHON_CONFIG location for the given
# implementation. If no implementation is provided, ${EPYTHON} will be
# used.
#
# Please note that this function can be used with CPython only.
# It requires Python installed, and therefore proper build-time
# dependencies need be added to the ebuild.
python_get_PYTHON_CONFIG() {
debug-print-function ${FUNCNAME} "${@}"
_python_export "${@}" PYTHON_CONFIG
echo "${PYTHON_CONFIG}"
}
# @FUNCTION: python_get_scriptdir
# @USAGE: [<impl>]
# @DESCRIPTION:
# Obtain and print the script install path for the given
# implementation. If no implementation is provided, ${EPYTHON} will
# be used.
python_get_scriptdir() {
debug-print-function ${FUNCNAME} "${@}"
_python_export "${@}" PYTHON_SCRIPTDIR
echo "${PYTHON_SCRIPTDIR}"
}
# @FUNCTION: python_optimize
# @USAGE: [<directory>...]
# @DESCRIPTION:
# Compile and optimize Python modules in specified directories (absolute
# paths). If no directories are provided, the default system paths
# are used (prepended with ${D}).
python_optimize() {
debug-print-function ${FUNCNAME} "${@}"
[[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
local PYTHON=${PYTHON}
[[ ${PYTHON} ]] || _python_export PYTHON
[[ -x ${PYTHON} ]] || die "PYTHON (${PYTHON}) is not executable"
# default to sys.path
if [[ ${#} -eq 0 ]]; then
local f
while IFS= read -r -d '' f; do
# 1) accept only absolute paths
# (i.e. skip '', '.' or anything like that)
# 2) skip paths which do not exist
# (python2.6 complains about them verbosely)
if [[ ${f} == /* && -d ${D%/}${f} ]]; then
set -- "${D%/}${f}" "${@}"
fi
done < <(
"${PYTHON}" - <<-EOF || die
import sys
print("".join(x + "\0" for x in sys.path))
EOF
)
debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
fi
local jobs=$(makeopts_jobs)
local d
for d; do
# make sure to get a nice path without //
local instpath=${d#${D%/}}
instpath=/${instpath##/}
einfo "Optimize Python modules for ${instpath}"
case "${EPYTHON}" in
python3.8)
# both levels of optimization are separate since 3.5
"${PYTHON}" -m compileall -j "${jobs}" -q -f -d "${instpath}" "${d}"
"${PYTHON}" -O -m compileall -j "${jobs}" -q -f -d "${instpath}" "${d}"
"${PYTHON}" -OO -m compileall -j "${jobs}" -q -f -d "${instpath}" "${d}"
;;
python*|pypy3)
# Python 3.9+
"${PYTHON}" -m compileall -j "${jobs}" -o 0 -o 1 -o 2 --hardlink-dupes -q -f -d "${instpath}" "${d}"
;;
pypy|jython2.7)
"${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
;;
*)
die "${FUNCNAME}: unexpected EPYTHON=${EPYTHON}"
;;
esac
done
}
# @FUNCTION: python_scriptinto
# @USAGE: <new-path>
# @DESCRIPTION:
# Set the directory to which files passed to python_doexe(),
# python_doscript(), python_newexe() and python_newscript()
# are going to be installed. The new value needs to be relative
# to the installation root (${ED}).
#
# If not set explicitly, the directory defaults to /usr/bin.
#
# Example:
# @CODE
# src_install() {
# python_scriptinto /usr/sbin
# python_foreach_impl python_doscript foo
# }
# @CODE
python_scriptinto() {
debug-print-function ${FUNCNAME} "${@}"
_PYTHON_SCRIPTROOT=${1}
}
# @FUNCTION: python_doexe
# @USAGE: <files>...
# @DESCRIPTION:
# Install the given executables into the executable install directory,
# for the current Python implementation (${EPYTHON}).
#
# The executable will be wrapped properly for the Python implementation,
# though no shebang mangling will be performed.
python_doexe() {
debug-print-function ${FUNCNAME} "${@}"
[[ ${EBUILD_PHASE} != install ]] &&
die "${FUNCNAME} can only be used in src_install"
local f
for f; do
python_newexe "${f}" "${f##*/}"
done
}
# @FUNCTION: python_newexe
# @USAGE: <path> <new-name>
# @DESCRIPTION:
# Install the given executable into the executable install directory,
# for the current Python implementation (${EPYTHON}).
#
# The executable will be wrapped properly for the Python implementation,
# though no shebang mangling will be performed. It will be renamed
# to <new-name>.
python_newexe() {
debug-print-function ${FUNCNAME} "${@}"
[[ ${EBUILD_PHASE} != install ]] &&
die "${FUNCNAME} can only be used in src_install"
[[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
[[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
local wrapd=${_PYTHON_SCRIPTROOT:-/usr/bin}
local f=${1}
local newfn=${2}
local scriptdir=$(python_get_scriptdir)
local d=${scriptdir#${EPREFIX}}
(
dodir "${wrapd}"
exeopts -m 0755
exeinto "${d}"
newexe "${f}" "${newfn}" || return ${?}
)
# install the wrapper
local dosym=dosym
[[ ${EAPI} == 7 ]] && dosym=dosym8
"${dosym}" -r /usr/lib/python-exec/python-exec2 "${wrapd}/${newfn}"
# don't use this at home, just call python_doscript() instead
if [[ ${_PYTHON_REWRITE_SHEBANG} ]]; then
python_fix_shebang -q "${ED%/}/${d}/${newfn}"
fi
}
# @FUNCTION: python_doscript
# @USAGE: <files>...
# @DESCRIPTION:
# Install the given scripts into the executable install directory,
# for the current Python implementation (${EPYTHON}).
#
# All specified files must start with a 'python' shebang. The shebang
# will be converted, and the files will be wrapped properly
# for the Python implementation.
#
# Example:
# @CODE
# src_install() {
# python_foreach_impl python_doscript ${PN}
# }
# @CODE
python_doscript() {
debug-print-function ${FUNCNAME} "${@}"
[[ ${EBUILD_PHASE} != install ]] &&
die "${FUNCNAME} can only be used in src_install"
local _PYTHON_REWRITE_SHEBANG=1
python_doexe "${@}"
}
# @FUNCTION: python_newscript
# @USAGE: <path> <new-name>
# @DESCRIPTION:
# Install the given script into the executable install directory
# for the current Python implementation (${EPYTHON}), and name it
# <new-name>.
#
# The file must start with a 'python' shebang. The shebang will be
# converted, and the file will be wrapped properly for the Python
# implementation. It will be renamed to <new-name>.
#
# Example:
# @CODE
# src_install() {
# python_foreach_impl python_newscript foo.py foo
# }
# @CODE
python_newscript() {
debug-print-function ${FUNCNAME} "${@}"
[[ ${EBUILD_PHASE} != install ]] &&
die "${FUNCNAME} can only be used in src_install"
local _PYTHON_REWRITE_SHEBANG=1
python_newexe "${@}"
}
# @FUNCTION: python_moduleinto
# @USAGE: <new-path>
# @DESCRIPTION:
# Set the Python module install directory for python_domodule().
# The <new-path> can either be an absolute target system path (in which
# case it needs to start with a slash, and ${ED} will be prepended to
# it) or relative to the implementation's site-packages directory
# (then it must not start with a slash). The relative path can be
# specified either using the Python package notation (separated by dots)
# or the directory notation (using slashes).
#
# When not set explicitly, the modules are installed to the top
# site-packages directory.
#
# In the relative case, the exact path is determined directly
# by each python_domodule invocation. Therefore, python_moduleinto
# can be safely called before establishing the Python interpreter and/or
# a single call can be used to set the path correctly for multiple
# implementations, as can be seen in the following example.
#
# Example:
# @CODE
# src_install() {
# python_moduleinto bar
# # installs ${PYTHON_SITEDIR}/bar/baz.py
# python_foreach_impl python_domodule baz.py
# }
# @CODE
python_moduleinto() {
debug-print-function ${FUNCNAME} "${@}"
_PYTHON_MODULEROOT=${1}
}
# @FUNCTION: python_domodule
# @USAGE: <files>...
# @DESCRIPTION:
# Install the given modules (or packages) into the current Python module
# installation directory. The list can mention both modules (files)
# and packages (directories). All listed files will be installed
# for all enabled implementations, and compiled afterwards.
#
# The files are installed into ${D} when run in src_install() phase.
# Otherwise, they are installed into ${BUILD_DIR}/install location
# that is suitable for picking up by distutils-r1 in PEP517 mode.
#
# Example:
# @CODE
# src_install() {
# # (${PN} being a directory)
# python_foreach_impl python_domodule ${PN}
# }
# @CODE
python_domodule() {
debug-print-function ${FUNCNAME} "${@}"
[[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
local d
if [[ ${_PYTHON_MODULEROOT} == /* ]]; then
# absolute path
d=${_PYTHON_MODULEROOT}
else
# relative to site-packages
local sitedir=$(python_get_sitedir)
d=${sitedir#${EPREFIX}}/${_PYTHON_MODULEROOT//.//}
fi
if [[ ${EBUILD_PHASE} == install ]]; then
(
insopts -m 0644
insinto "${d}"
doins -r "${@}" || return ${?}
)
python_optimize "${ED%/}/${d}"
elif [[ -n ${BUILD_DIR} ]]; then
local dest=${BUILD_DIR}/install${EPREFIX}/${d}
mkdir -p "${dest}" || die
cp -pR "${@}" "${dest}/" || die
(
cd "${dest}" &&
chmod -R a+rX "${@##*/}"
) || die
else
die "${FUNCNAME} can only be used in src_install or with BUILD_DIR set"
fi
}
# @FUNCTION: python_doheader
# @USAGE: <files>...
# @DESCRIPTION:
# Install the given headers into the implementation-specific include
# directory. This function is unconditionally recursive, i.e. you can
# pass directories instead of files.
#
# Example:
# @CODE
# src_install() {
# python_foreach_impl python_doheader foo.h bar.h
# }
# @CODE
python_doheader() {
debug-print-function ${FUNCNAME} "${@}"
[[ ${EBUILD_PHASE} != install ]] &&
die "${FUNCNAME} can only be used in src_install"
[[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
local includedir=$(python_get_includedir)
local d=${includedir#${ESYSROOT}}
(
insopts -m 0644
insinto "${d}"
doins -r "${@}" || return ${?}
)
}
# @FUNCTION: _python_wrapper_setup
# @USAGE: [<path> [<impl>]]
# @INTERNAL
# @DESCRIPTION:
# Create proper 'python' executable and pkg-config wrappers
# (if available) in the directory named by <path>. Set up PATH
# and PKG_CONFIG_PATH appropriately. <path> defaults to ${T}/${EPYTHON}.
#
# The wrappers will be created for implementation named by <impl>,
# or for one named by ${EPYTHON} if no <impl> passed.
#
# If the named directory contains a python symlink already, it will
# be assumed to contain proper wrappers already and only environment
# setup will be done. If wrapper update is requested, the directory
# shall be removed first.
_python_wrapper_setup() {
debug-print-function ${FUNCNAME} "${@}"
local workdir=${1:-${T}/${EPYTHON}}
local impl=${2:-${EPYTHON}}
[[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
[[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
if [[ ! -x ${workdir}/bin/python ]]; then
mkdir -p "${workdir}"/{bin,pkgconfig} || die
# Clean up, in case we were supposed to do a cheap update.
rm -f "${workdir}"/bin/python{,2,3}{,-config} || die
rm -f "${workdir}"/bin/2to3 || die
rm -f "${workdir}"/pkgconfig/python{2,3}{,-embed}.pc || die
local EPYTHON PYTHON
_python_export "${impl}" EPYTHON PYTHON
# Python interpreter
# note: we don't use symlinks because python likes to do some
# symlink reading magic that breaks stuff
# https://bugs.gentoo.org/show_bug.cgi?id=555752
cat > "${workdir}/bin/python" <<-_EOF_ || die
#!/bin/sh
exec "${PYTHON}" "\${@}"
_EOF_
cp "${workdir}/bin/python" "${workdir}/bin/python3" || die
chmod +x "${workdir}/bin/python" "${workdir}/bin/python3" || die
local nonsupp=( python2 python2-config )
# CPython-specific
if [[ ${EPYTHON} == python* ]]; then
cat > "${workdir}/bin/python-config" <<-_EOF_ || die
#!/bin/sh
exec "${PYTHON}-config" "\${@}"
_EOF_
cp "${workdir}/bin/python-config" \
"${workdir}/bin/python3-config" || die
chmod +x "${workdir}/bin/python-config" \
"${workdir}/bin/python3-config" || die
# Python 2.6+.
ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
# Python 2.7+.
ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}.pc \
"${workdir}"/pkgconfig/python3.pc || die
# Python 3.8+.
ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}-embed.pc \
"${workdir}"/pkgconfig/python3-embed.pc || die
else
nonsupp+=( 2to3 python-config python3-config )
fi
local x
for x in "${nonsupp[@]}"; do
cat >"${workdir}"/bin/${x} <<-_EOF_ || die
#!/bin/sh
echo "${ECLASS}: ${FUNCNAME}: ${x} is not supported by ${EPYTHON} (PYTHON_COMPAT)" >&2
exit 127
_EOF_
chmod +x "${workdir}"/bin/${x} || die
done
fi
# Now, set the environment.
# But note that ${workdir} may be shared with something else,
# and thus already on top of PATH.
if [[ ${PATH##:*} != ${workdir}/bin ]]; then
PATH=${workdir}/bin${PATH:+:${PATH}}
fi
if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
fi
export PATH PKG_CONFIG_PATH
}
# @FUNCTION: python_fix_shebang
# @USAGE: [-f|--force] [-q|--quiet] <path>...
# @DESCRIPTION:
# Replace the shebang in Python scripts with the full path
# to the current Python implementation (PYTHON, including EPREFIX).
# If a directory is passed, works recursively on all Python scripts
# found inside the directory tree.
#
# Only files having a Python shebang (a path to any known Python
# interpreter, optionally preceded by env(1) invocation) will
# be processed. Files with any other shebang will either be skipped
# silently when a directory was passed, or an error will be reported
# for any files without Python shebangs specified explicitly.
#
# Shebangs that are compatible with the current Python version will be
# mangled unconditionally. Incompatible shebangs will cause a fatal
# error, unless --force is specified.
#
# --force causes the function to replace shebangs with incompatible
# Python version (but not non-Python shebangs). --quiet causes
# the function not to list modified files verbosely.
python_fix_shebang() {
debug-print-function ${FUNCNAME} "${@}"
[[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
local force quiet
while [[ ${@} ]]; do
case "${1}" in
-f|--force) force=1; shift;;
-q|--quiet) quiet=1; shift;;
--) shift; break;;
*) break;;
esac
done
[[ ${1} ]] || die "${FUNCNAME}: no paths given"
local path f
for path; do
local any_fixed is_recursive
[[ -d ${path} ]] && is_recursive=1
while IFS= read -r -d '' f; do
local shebang i
local error= match=
# note: we can't ||die here since read will fail if file
# has no newline characters
IFS= read -r shebang <"${f}"
# First, check if it's shebang at all...
if [[ ${shebang} == '#!'* ]]; then
local split_shebang=()
read -r -a split_shebang <<<${shebang#"#!"} || die
local in_path=${split_shebang[0]}
local from='^#! *[^ ]*'
# if the first component is env(1), skip it
if [[ ${in_path} == */env ]]; then
in_path=${split_shebang[1]}
from+=' *[^ ]*'
fi
case ${in_path##*/} in
"${EPYTHON}")
match=1
;;
python|python3)
match=1
;;
python2|python[23].[0-9]|python3.[1-9][0-9]|pypy|pypy3|jython[23].[0-9])
# Explicit mismatch.
match=1
error=1
;;
esac
fi
# disregard mismatches in force mode
[[ ${force} ]] && error=
if [[ ! ${match} ]]; then
# Non-Python shebang. Allowed in recursive mode,
# disallowed when specifying file explicitly.
[[ ${is_recursive} ]] && continue
error=1
fi
if [[ ! ${quiet} ]]; then
einfo "Fixing shebang in ${f#${D%/}}."
fi
if [[ ! ${error} ]]; then
debug-print "${FUNCNAME}: in file ${f#${D%/}}"
debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
sed -i -e "1s@${from}@#!${EPREFIX}/usr/bin/${EPYTHON}@" "${f}" || die
any_fixed=1
else
eerror "The file has incompatible shebang:"
eerror " file: ${f#${D%/}}"
eerror " current shebang: ${shebang}"
eerror " requested impl: ${EPYTHON}"
die "${FUNCNAME}: conversion of incompatible shebang requested"
fi
done < <(find -H "${path}" -type f -print0 || die)
if [[ ! ${any_fixed} ]]; then
eerror "QA error: ${FUNCNAME}, ${path#${D%/}} did not match any fixable files."
eerror "There are no Python files in specified directory."
die "${FUNCNAME} did not match any fixable files"
fi
done
}
# @FUNCTION: _python_check_locale_sanity
# @USAGE: <locale>
# @RETURN: 0 if sane, 1 otherwise
# @INTERNAL
# @DESCRIPTION:
# Check whether the specified locale sanely maps between lowercase
# and uppercase ASCII characters.
_python_check_locale_sanity() {
local -x LC_ALL=${1}
local IFS=
local lc=( {a..z} )
local uc=( {A..Z} )
local input="${lc[*]}${uc[*]}"
local output=$(tr '[:lower:][:upper:]' '[:upper:][:lower:]' <<<"${input}")
[[ ${output} == "${uc[*]}${lc[*]}" ]]
}
# @FUNCTION: python_export_utf8_locale
# @RETURN: 0 on success, 1 on failure.
# @DESCRIPTION:
# Attempts to export a usable UTF-8 locale in the LC_CTYPE variable. Does
# nothing if LC_ALL is defined, or if the current locale uses a UTF-8 charmap.
# This may be used to work around the quirky open() behavior of python3.
python_export_utf8_locale() {
debug-print-function ${FUNCNAME} "${@}"
# If the locale program isn't available, just return.
type locale &>/dev/null || return 0
if [[ $(locale charmap) != UTF-8 ]]; then
# Try English first, then everything else.
local lang locales="C.UTF-8 en_US.UTF-8 en_GB.UTF-8 $(locale -a)"
for lang in ${locales}; do
if [[ $(LC_ALL=${lang} locale charmap 2>/dev/null) == UTF-8 ]]; then
if _python_check_locale_sanity "${lang}"; then
export LC_CTYPE=${lang}
if [[ -n ${LC_ALL} ]]; then
export LC_NUMERIC=${LC_ALL}
export LC_TIME=${LC_ALL}
export LC_COLLATE=${LC_ALL}
export LC_MONETARY=${LC_ALL}
export LC_MESSAGES=${LC_ALL}
export LC_PAPER=${LC_ALL}
export LC_NAME=${LC_ALL}
export LC_ADDRESS=${LC_ALL}
export LC_TELEPHONE=${LC_ALL}
export LC_MEASUREMENT=${LC_ALL}
export LC_IDENTIFICATION=${LC_ALL}
export LC_ALL=
fi
return 0
fi
fi
done
ewarn "Could not find a UTF-8 locale. This may trigger build failures in"
ewarn "some python packages. Please ensure that a UTF-8 locale is listed in"
ewarn "/etc/locale.gen and run locale-gen."
return 1
fi
return 0
}
# @FUNCTION: build_sphinx
# @USAGE: <directory>
# @DESCRIPTION:
# Build HTML documentation using dev-python/sphinx in the specified
# <directory>. Takes care of disabling Intersphinx and appending
# to HTML_DOCS.
#
# If <directory> is relative to the current directory, care needs
# to be taken to run einstalldocs from the same directory
# (usually ${S}).
build_sphinx() {
debug-print-function ${FUNCNAME} "${@}"
[[ ${#} -eq 1 ]] || die "${FUNCNAME} takes 1 arg: <directory>"
local dir=${1}
sed -i -e 's:^intersphinx_mapping:disabled_&:' \
"${dir}"/conf.py || die
# 1. not all packages include the Makefile in pypi tarball,
# so we call sphinx-build directly
# 2. if autodoc is used, we need to call sphinx via EPYTHON,
# to ensure that PEP 517 venv is respected
# 3. if autodoc is not used, then sphinx might not be installed
# for the current impl, so we need a fallback to sphinx-build
local command=( "${EPYTHON}" -m sphinx.cmd.build )
if ! "${EPYTHON}" -c "import sphinx.cmd.build" 2>/dev/null; then
command=( sphinx-build )
fi
command+=(
-b html
-d "${dir}"/_build/doctrees
"${dir}"
"${dir}"/_build/html
)
echo "${command[@]}" >&2
"${command[@]}" || die
HTML_DOCS+=( "${dir}/_build/html/." )
}
# @FUNCTION: _python_check_EPYTHON
# @INTERNAL
# @DESCRIPTION:
# Check if EPYTHON is set, die if not.
_python_check_EPYTHON() {
if [[ -z ${EPYTHON} ]]; then
die "EPYTHON unset, invalid call context"
fi
}
# @FUNCTION: _python_check_occluded_packages
# @INTERNAL
# @DESCRIPTION:
# Check if the current directory does not contain any incomplete
# package sources that would block installed packages from being used
# (and effectively e.g. make it impossible to load compiled extensions).
_python_check_occluded_packages() {
debug-print-function ${FUNCNAME} "${@}"
[[ -z ${BUILD_DIR} || ! -d ${BUILD_DIR}/install ]] && return
local sitedir="${BUILD_DIR}/install$(python_get_sitedir)"
# avoid unnecessarily checking if we are inside install dir
[[ ${sitedir} -ef . ]] && return
local f fn diff l
for f in "${sitedir}"/*/; do
f=${f%/}
fn=${f##*/}
# skip metadata directories
[[ ${fn} == *.dist-info || ${fn} == *.egg-info ]] && continue
if [[ -d ${fn} ]]; then
diff=$(
comm -1 -3 <(
find "${fn}" -type f -not -path '*/__pycache__/*' |
sort
assert
) <(
cd "${sitedir}" &&
find "${fn}" -type f -not -path '*/__pycache__/*' |
sort
assert
)
)
if [[ -n ${diff} ]]; then
eqawarn "The directory ${fn} occludes package installed for ${EPYTHON}."
eqawarn "The installed package includes additional files:"
eqawarn
while IFS= read -r l; do
eqawarn " ${l}"
done <<<"${diff}"
eqawarn
if [[ ! ${_PYTHON_WARNED_OCCLUDED_PACKAGES} ]]; then
eqawarn "For more information on occluded packages, please see:"
eqawarn "https://projects.gentoo.org/python/guide/test.html#importerrors-for-c-extensions"
_PYTHON_WARNED_OCCLUDED_PACKAGES=1
fi
fi
fi
done
}
# @VARIABLE: EPYTEST_DESELECT
# @DEFAULT_UNSET
# @DESCRIPTION:
# Specifies an array of tests to be deselected via pytest's --deselect
# parameter, when calling epytest. The list can include file paths,
# specific test functions or parametrized test invocations.
#
# Note that the listed files will still be subject to collection,
# i.e. modules imported in global scope will need to be available.
# If this is undesirable, EPYTEST_IGNORE can be used instead.
# @VARIABLE: EPYTEST_IGNORE
# @DEFAULT_UNSET
# @DESCRIPTION:
# Specifies an array of paths to be ignored via pytest's --ignore
# parameter, when calling epytest. The listed files will be entirely
# skipped from test collection.
# @ECLASS_VARIABLE: EPYTEST_TIMEOUT
# @DEFAULT_UNSET
# @DESCRIPTION:
# If set to a non-empty value, enables pytest-timeout plugin and sets
# test timeout to the specified value. This variable can be either set
# in ebuilds that are known to hang, or by user to prevent hangs
# in automated test environments. If this variable is set prior
# to calling distutils_enable_tests in distutils-r1, a test dependency
# on dev-python/pytest-timeout is added automatically.
# @ECLASS_VARIABLE: EPYTEST_XDIST
# @DEFAULT_UNSET
# @DESCRIPTION:
# If set to a non-empty value, enables running tests in parallel
# via pytest-xdist plugin. If this variable is set prior to calling
# distutils_enable_tests in distutils-r1, a test dependency
# on dev-python/pytest-xdist is added automatically.
# @ECLASS_VARIABLE: EPYTEST_JOBS
# @USER_VARIABLE
# @DEFAULT_UNSET
# @DESCRIPTION:
# Specifies the number of jobs for parallel (pytest-xdist) test runs.
# When unset, defaults to -j from MAKEOPTS, or the current nproc.
# @ECLASS_VARIABLE: EPYTEST_FLAGS
# @USER_VARIABLE
# @DEFAULT_UNSET
# @DESCRIPTION:
# Additional flags to pass to pytest. This is intended to be set
# in the environment when debugging packages (options such as -x or -s
# are useful here), rather than globally. It must not be set
# in ebuilds.
# @FUNCTION: epytest
# @USAGE: [<args>...]
# @DESCRIPTION:
# Run pytest, passing the standard set of pytest options, then
# --deselect and --ignore options based on EPYTEST_DESELECT
# and EPYTEST_IGNORE, then user-specified options.
#
# This command dies on failure and respects nonfatal.
epytest() {
debug-print-function ${FUNCNAME} "${@}"
_python_check_EPYTHON
_python_check_occluded_packages
local color=yes
[[ ${NO_COLOR} ]] && color=no
local args=(
# verbose progress reporting and tracebacks
-vv
# list all non-passed tests in the summary for convenience
# (includes failures, skips, xfails...)
-ra
# print local variables in tracebacks, useful for debugging
-l
# override filterwarnings=error, we do not really want -Werror
# for end users, as it tends to fail on new warnings from deps
-Wdefault
# however, do error out if the package failed to load
# an appropriate async plugin
-Werror::pytest.PytestUnhandledCoroutineWarning
# override color output
"--color=${color}"
# count is more precise when we're dealing with a large number
# of tests
-o console_output_style=count
# minimize the temporary directory retention, the test suites
# of some packages can grow them pretty large and normally
# we don't need to preserve them
-o tmp_path_retention_count=0
-o tmp_path_retention_policy=failed
)
if [[ ! ${PYTEST_DISABLE_PLUGIN_AUTOLOAD} ]]; then
args+=(
# disable the undesirable-dependency plugins by default to
# trigger missing argument strips. strip options that require
# them from config files. enable them explicitly via "-p ..."
# if you *really* need them.
-p no:cov
-p no:flake8
-p no:flakes
-p no:pylint
# sterilize pytest-markdown as it runs code snippets from all
# *.md files found without any warning
-p no:markdown
# pytest-sugar undoes everything that's good about pytest output
# and makes it hard to read logs
-p no:sugar
# pytest-xvfb automatically spawns Xvfb for every test suite,
# effectively forcing it even when we'd prefer the tests
# not to have DISPLAY at all, causing crashes sometimes
# and causing us to miss missing virtualx usage
-p no:xvfb
# intrusive packages that break random test suites
-p no:pytest-describe
-p no:plus
-p no:tavern
# does something to logging
-p no:salt-factories
)
fi
if [[ -n ${EPYTEST_TIMEOUT} ]]; then
if [[ ${PYTEST_PLUGINS} != *pytest_timeout* ]]; then
args+=(
-p timeout
)
fi
args+=(
"--timeout=${EPYTEST_TIMEOUT}"
)
fi
if [[ ${EPYTEST_XDIST} ]]; then
local jobs=${EPYTEST_JOBS:-$(makeopts_jobs)}
if [[ ${jobs} -gt 1 ]]; then
if [[ ${PYTEST_PLUGINS} != *xdist.plugin* ]]; then
args+=(
# explicitly enable the plugin, in case the ebuild was
# using PYTEST_DISABLE_PLUGIN_AUTOLOAD=1
-p xdist
)
fi
args+=(
-n "${jobs}"
# worksteal ensures that workers don't end up idle when heavy
# jobs are unevenly distributed
--dist=worksteal
)
fi
fi
local x
for x in "${EPYTEST_DESELECT[@]}"; do
args+=( --deselect "${x}" )
done
for x in "${EPYTEST_IGNORE[@]}"; do
args+=( --ignore "${x}" )
done
set -- "${EPYTHON}" -m pytest "${args[@]}" "${@}" ${EPYTEST_FLAGS}
echo "${@}" >&2
"${@}"
local ret=${?}
# remove common temporary directories left over by pytest plugins
rm -rf .hypothesis .pytest_cache || die
# pytest plugins create additional .pyc files while testing
# see e.g. https://bugs.gentoo.org/847235
if [[ -n ${BUILD_DIR} && -d ${BUILD_DIR} ]]; then
find "${BUILD_DIR}" -name '*-pytest-*.pyc' -delete || die
fi
[[ ${ret} -ne 0 ]] && die -n "pytest failed with ${EPYTHON}"
return ${ret}
}
# @FUNCTION: eunittest
# @USAGE: [<args>...]
# @DESCRIPTION:
# Run unit tests using dev-python/unittest-or-fail, passing the standard
# set of options, followed by user-specified options.
#
# This command dies on failure and respects nonfatal.
eunittest() {
debug-print-function ${FUNCNAME} "${@}"
_python_check_EPYTHON
_python_check_occluded_packages
# unittest fails with "no tests" correctly since Python 3.12
local runner=unittest
if _python_impl_matches "${EPYTHON}" 3.{9..11}; then
runner=unittest_or_fail
fi
set -- "${EPYTHON}" -m "${runner}" discover -v "${@}"
echo "${@}" >&2
"${@}" || die -n "Tests failed with ${EPYTHON}"
return ${?}
}
# @FUNCTION: _python_run_check_deps
# @INTERNAL
# @USAGE: <impl>
# @DESCRIPTION:
# Verify whether <impl> is an acceptable choice to run any-r1 style
# code. Checks whether the interpreter is installed, runs
# python_check_deps() if declared.
_python_run_check_deps() {
debug-print-function ${FUNCNAME} "${@}"
local impl=${1}
einfo "Checking whether ${impl} is suitable ..."
local PYTHON_PKG_DEP
_python_export "${impl}" PYTHON_PKG_DEP
ebegin " ${PYTHON_PKG_DEP}"
has_version -b "${PYTHON_PKG_DEP}"
eend ${?} || return 1
declare -f python_check_deps >/dev/null || return 0
local PYTHON_USEDEP="python_targets_${impl}(-)"
local PYTHON_SINGLE_USEDEP="python_single_target_${impl}(-)"
ebegin " python_check_deps"
python_check_deps
eend ${?}
}
# @FUNCTION: python_has_version
# @USAGE: [-b|-d|-r] <atom>...
# @DESCRIPTION:
# A convenience wrapper for has_version() with verbose output and better
# defaults for use in python_check_deps().
#
# The wrapper accepts -b/-d/-r options to indicate the root to perform
# the lookup on. Unlike has_version, the default is -b.
#
# The wrapper accepts multiple package specifications. For the check
# to succeed, *all* specified atoms must match.
python_has_version() {
debug-print-function ${FUNCNAME} "${@}"
local root_arg=( -b )
case ${1} in
-b|-d|-r)
root_arg=( "${1}" )
shift
;;
esac
local pkg
for pkg; do
ebegin " ${pkg}"
has_version "${root_arg[@]}" "${pkg}"
eend ${?} || return
done
return 0
}
# @FUNCTION: _python_sanity_checks
# @INTERNAL
# @DESCRIPTION:
# Perform additional environment sanity checks.
_python_sanity_checks() {
debug-print-function ${FUNCNAME} "${@}"
[[ ${_PYTHON_SANITY_CHECKED} ]] && return
if [[ -v PYTHONPATH ]]; then
local x paths=()
mapfile -d ':' -t paths <<<${PYTHONPATH}
for x in "${paths[@]}"; do
if [[ ${x} != /* ]]; then
eerror "Relative path found in PYTHONPATH:"
eerror
eerror " PYTHONPATH=${PYTHONPATH@Q}"
eerror
eerror "This is guaranteed to cause random breakage. Please make sure that"
eerror "your PYTHONPATH contains absolute paths only (and only if necessary)."
eerror "Note that empty values (including ':' at either end and an empty"
eerror "PYTHONPATH) count as the current directory. If no PYTHONPATH"
eerror "is intended, it needs to be unset instead."
die "Relative paths in PYTHONPATH are forbidden: ${x@Q}"
fi
done
elog "PYTHONPATH=${PYTHONPATH@Q}"
fi
_PYTHON_SANITY_CHECKED=1
}
fi
|