forked from gentoo/gentoo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
python-utils-r1.eclass
1516 lines (1323 loc) · 42 KB
/
python-utils-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-2018 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# @ECLASS: python-utils-r1.eclass
# @MAINTAINER:
# Python team <[email protected]>
# @AUTHOR:
# Author: Michał Górny <[email protected]>
# Based on work of: Krzysztof Pawlik <[email protected]>
# @SUPPORTED_EAPIS: 0 1 2 3 4 5 6 7
# @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 wiki:
# https://wiki.gentoo.org/wiki/Project:Python/python-utils-r1
case "${EAPI:-0}" in
0|1|2|3|4|5|6|7)
;;
*)
die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
;;
esac
if [[ ${_PYTHON_ECLASS_INHERITED} ]]; then
die 'python-r1 suite eclasses can not be used with python.eclass.'
fi
if [[ ! ${_PYTHON_UTILS_R1} ]]; then
[[ ${EAPI:-0} == [012345] ]] && inherit eutils multilib
inherit toolchain-funcs
# @ECLASS-VARIABLE: _PYTHON_ALL_IMPLS
# @INTERNAL
# @DESCRIPTION:
# All supported Python implementations, most preferred last.
_PYTHON_ALL_IMPLS=(
jython2_7
pypy pypy3
python2_7
python3_5 python3_6 python3_7
)
readonly _PYTHON_ALL_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_impl_supported
# @USAGE: <impl>
# @INTERNAL
# @DESCRIPTION:
# Check whether the implementation <impl> (PYTHON_COMPAT-form)
# is still supported.
#
# Returns 0 if the implementation is valid and supported. If it is
# unsupported, returns 1 -- and the caller should ignore the entry.
# If it is invalid, dies with an appopriate error messages.
_python_impl_supported() {
debug-print-function ${FUNCNAME} "${@}"
[[ ${#} -eq 1 ]] || die "${FUNCNAME}: takes exactly 1 argument (impl)."
local impl=${1}
# keep in sync with _PYTHON_ALL_IMPLS!
# (not using that list because inline patterns shall be faster)
case "${impl}" in
python2_7|python3_[567]|jython2_7)
return 0
;;
pypy1_[89]|pypy2_0|python2_[56]|python3_[1234])
return 1
;;
pypy|pypy3)
if [[ ${EAPI:-0} == [01234] ]]; then
die "PyPy is supported in EAPI 5 and newer only."
fi
;;
*)
[[ ${PYTHON_COMPAT_NO_STRICT} ]] && return 1
die "Invalid implementation in PYTHON_COMPAT: ${impl}"
esac
}
# @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
if ! declare -p PYTHON_COMPAT &>/dev/null; then
die 'PYTHON_COMPAT not declared.'
fi
if [[ $(declare -p PYTHON_COMPAT) != "declare -a"* ]]; then
die 'PYTHON_COMPAT must be an array.'
fi
for i in "${PYTHON_COMPAT[@]}"; do
# trigger validity checks
_python_impl_supported "${i}"
done
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.
#
# <impl> can be in PYTHON_COMPAT or EPYTHON form. The patterns can be
# either:
# a) fnmatch-style patterns, e.g. 'python2*', 'pypy'...
# b) '-2' to indicate all Python 2 variants (= !python_is_python3)
# c) '-3' to indicate all Python 3 variants (= python_is_python3)
_python_impl_matches() {
[[ ${#} -ge 2 ]] || die "${FUNCNAME}: takes at least 2 parameters"
local impl=${1} pattern
shift
for pattern; do
if [[ ${pattern} == -2 ]]; then
! python_is_python3 "${impl}"
return
elif [[ ${pattern} == -3 ]]; then
python_is_python3 "${impl}"
return
# unify value style to allow lax matching
elif [[ ${impl/./_} == ${pattern/./_} ]]; then
return 0
fi
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_export_best().
#
# 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_export_best().
#
# 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
# @ECLASS-VARIABLE: PYTHON_SITEDIR
# @DEFAULT_UNSET
# @DESCRIPTION:
# The path to Python site-packages directory.
#
# Set and exported on request using python_export().
# Requires a proper build-time dependency on the Python implementation.
#
# Example value:
# @CODE
# /usr/lib64/python2.7/site-packages
# @CODE
# @ECLASS-VARIABLE: PYTHON_INCLUDEDIR
# @DEFAULT_UNSET
# @DESCRIPTION:
# The path to Python include directory.
#
# Set and exported on request using python_export().
# Requires a proper build-time dependency on the Python implementation.
#
# Example value:
# @CODE
# /usr/include/python2.7
# @CODE
# @ECLASS-VARIABLE: PYTHON_LIBPATH
# @DEFAULT_UNSET
# @DESCRIPTION:
# The path to Python library.
#
# Set and exported on request using python_export().
# Valid only for CPython. Requires a proper build-time dependency
# on the Python implementation.
#
# Example value:
# @CODE
# /usr/lib64/libpython2.7.so
# @CODE
# @ECLASS-VARIABLE: PYTHON_CFLAGS
# @DEFAULT_UNSET
# @DESCRIPTION:
# Proper C compiler flags for building against Python. Obtained from
# pkg-config or python-config.
#
# Set and exported on request using python_export().
# Valid only for CPython. Requires a proper build-time dependency
# on the Python implementation and on pkg-config.
#
# Example value:
# @CODE
# -I/usr/include/python2.7
# @CODE
# @ECLASS-VARIABLE: PYTHON_LIBS
# @DEFAULT_UNSET
# @DESCRIPTION:
# Proper C compiler flags for linking against Python. Obtained from
# pkg-config or python-config.
#
# Set and exported on request using python_export().
# Valid only for CPython. Requires a proper build-time dependency
# on the Python implementation and on pkg-config.
#
# Example value:
# @CODE
# -lpython2.7
# @CODE
# @ECLASS-VARIABLE: PYTHON_CONFIG
# @DEFAULT_UNSET
# @DESCRIPTION:
# Path to the python-config executable.
#
# Set and exported on request using python_export().
# Valid only for CPython. Requires a proper build-time dependency
# on the Python implementation and on pkg-config.
#
# Example value:
# @CODE
# /usr/bin/python2.7-config
# @CODE
# @ECLASS-VARIABLE: PYTHON_PKG_DEP
# @DEFAULT_UNSET
# @DESCRIPTION:
# The complete dependency on a particular Python package as a string.
#
# Set and exported on request using python_export().
#
# Example value:
# @CODE
# dev-lang/python:2.7[xml]
# @CODE
# @ECLASS-VARIABLE: PYTHON_SCRIPTDIR
# @DEFAULT_UNSET
# @DESCRIPTION:
# The location where Python scripts must be installed for current impl.
#
# Set and exported on request using python_export().
#
# Example value:
# @CODE
# /usr/lib/python-exec/python2.7
# @CODE
# @FUNCTION: python_export
# @USAGE: [<impl>] <variables>...
# @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)
export PYTHON=${EPREFIX}/usr/bin/${impl}
debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
;;
PYTHON_SITEDIR)
[[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
# sysconfig can't be used because:
# 1) pypy doesn't give site-packages but stdlib
# 2) jython gives paths with wrong case
PYTHON_SITEDIR=$("${PYTHON}" -c 'import distutils.sysconfig; print(distutils.sysconfig.get_python_lib())') || die
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}" -c 'import distutils.sysconfig; print(distutils.sysconfig.get_python_inc())') || die
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}" -c '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 "")') || die
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*)
# python-2.7, python-3.2, etc.
val=$($(tc-getPKG_CONFIG) --libs ${impl/n/n-}) || 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}" -c 'import sysconfig; print(sysconfig.get_config_var("ABIFLAGS") or "")') || die
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
python2.7)
PYTHON_PKG_DEP='>=dev-lang/python-2.7.5-r2:2.7';;
python3.3)
PYTHON_PKG_DEP='>=dev-lang/python-3.3.2-r2:3.3';;
python*)
PYTHON_PKG_DEP="dev-lang/python:${impl#python}";;
pypy)
PYTHON_PKG_DEP='>=virtual/pypy-5:0=';;
pypy3)
PYTHON_PKG_DEP='>=virtual/pypy3-5:0=';;
jython2.7)
PYTHON_PKG_DEP='dev-java/jython:2.7';;
*)
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}
debug-print "${FUNCNAME}: PYTHON_SCRIPTDIR = ${PYTHON_SCRIPTDIR}"
;;
*)
die "python_export: unknown variable ${var}"
esac
done
}
# @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.
#
# If you just need to have PYTHON_SITEDIR set (and exported), then it is
# better to use python_export() directly instead.
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.
#
# If you just need to have PYTHON_INCLUDEDIR set (and exported), then it
# is better to use python_export() directly instead.
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_ln_rel
# @USAGE: <from> <to>
# @INTERNAL
# @DESCRIPTION:
# Create a relative symlink.
_python_ln_rel() {
debug-print-function ${FUNCNAME} "${@}"
local target=${1}
local symname=${2}
local tgpath=${target%/*}/
local sympath=${symname%/*}/
local rel_target=
while [[ ${sympath} ]]; do
local tgseg= symseg=
while [[ ! ${tgseg} && ${tgpath} ]]; do
tgseg=${tgpath%%/*}
tgpath=${tgpath#${tgseg}/}
done
while [[ ! ${symseg} && ${sympath} ]]; do
symseg=${sympath%%/*}
sympath=${sympath#${symseg}/}
done
if [[ ${tgseg} != ${symseg} ]]; then
rel_target=../${rel_target}${tgseg:+${tgseg}/}
fi
done
rel_target+=${tgpath}${target##*/}
debug-print "${FUNCNAME}: ${symname} -> ${target}"
debug-print "${FUNCNAME}: rel_target = ${rel_target}"
ln -fs "${rel_target}" "${symname}"
}
# @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} "${@}"
if [[ ${EBUILD_PHASE} == pre* || ${EBUILD_PHASE} == post* ]]; then
eerror "The new Python eclasses expect the compiled Python files to"
eerror "be controlled by the Package Manager. For this reason,"
eerror "the python_optimize function can be used only during src_* phases"
eerror "(src_install most commonly) and not during pkg_* phases."
echo
die "python_optimize is not to be used in pre/post* phases"
fi
[[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
local PYTHON=${PYTHON}
[[ ${PYTHON} ]] || python_export PYTHON
# 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}" -c 'import sys; print("\0".join(sys.path))' || die)
debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
fi
local d
for d; do
# make sure to get a nice path without //
local instpath=${d#${D%/}}
instpath=/${instpath##/}
case "${EPYTHON}" in
python2.7|python3.[34])
"${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
"${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
;;
python*|pypy3)
# both levels of optimization are separate since 3.5
"${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
"${PYTHON}" -O -m compileall -q -f -d "${instpath}" "${d}"
"${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
;;
*)
"${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
;;
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} "${@}"
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} "${@}"
[[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
[[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
if [[ ${EAPI:-0} == [0123] ]]; then
die "python_do* and python_new* helpers are banned in EAPIs older than 4."
fi
local wrapd=${python_scriptroot:-/usr/bin}
local f=${1}
local newfn=${2}
local PYTHON_SCRIPTDIR d
python_export PYTHON_SCRIPTDIR
d=${PYTHON_SCRIPTDIR#${EPREFIX}}
(
dodir "${wrapd}"
exeopts -m 0755
exeinto "${d}"
newexe "${f}" "${newfn}" || return ${?}
)
# install the wrapper
_python_ln_rel "${ED%/}"/usr/lib/python-exec/python-exec2 \
"${ED%/}/${wrapd}/${newfn}" || die
# 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} "${@}"
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} "${@}"
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_doscript/python_newscript function. 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.
#
# 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).'
if [[ ${EAPI:-0} == [0123] ]]; then
die "python_do* and python_new* helpers are banned in EAPIs older than 4."
fi
local d
if [[ ${python_moduleroot} == /* ]]; then
# absolute path
d=${python_moduleroot}
else
# relative to site-packages
local PYTHON_SITEDIR=${PYTHON_SITEDIR}
[[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot//.//}
fi
(
insopts -m 0644
insinto "${d}"
doins -r "${@}" || return ${?}
)
python_optimize "${ED%/}/${d}"
}
# @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} "${@}"
[[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
if [[ ${EAPI:-0} == [0123] ]]; then
die "python_do* and python_new* helpers are banned in EAPIs older than 4."
fi
local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
[[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
d=${PYTHON_INCLUDEDIR#${EPREFIX}}
(
insopts -m 0644
insinto "${d}"
doins -r "${@}" || return ${?}
)
}
# @FUNCTION: python_wrapper_setup
# @USAGE: [<path> [<impl>]]
# @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
_python_check_dead_variables
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}.pc || die
local EPYTHON PYTHON
python_export "${impl}" EPYTHON PYTHON