summaryrefslogtreecommitdiff
blob: 98708bc4bed25b3bb55d0460d3382abf0afbd0df (plain)
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
# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

EAPI=5

#if LIVE
EGIT_REPO_URI="https://github.com/mgorny/${PN}.git"

inherit autotools git-r3
#endif

# Kids, don't do this at home!
inherit python-utils-r1
PYTHON_COMPAT=( "${_PYTHON_ALL_IMPLS[@]}" )

# Inherited purely to have PYTHON_TARGET flags which will satisfy USE
# dependencies and trigger necessary rebuilds.
inherit python-r1

DESCRIPTION="Python script wrapper"
HOMEPAGE="https://github.com/mgorny/python-exec/"
SRC_URI="https://github.com/mgorny/${PN}/releases/download/${P}/${P}.tar.bz2"

LICENSE="BSD-2"
SLOT="2"
KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~m68k ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~ppc-aix ~amd64-fbsd ~x86-fbsd ~amd64-linux ~arm-linux ~x86-linux ~ppc-macos ~x64-macos ~x86-macos ~m68k-mint ~sparc-solaris ~sparc64-solaris ~x64-solaris ~x86-solaris"
IUSE=""

# eselect-python because of /usr/bin/python* collisions and new config
# python versions because of missing $scriptdir/python* symlinks
RDEPEND="
	!<app-eselect/eselect-python-20160206
	!<dev-lang/python-2.7.10-r4:2.7
	!<dev-lang/python-3.3.5-r4:3.3
	!<dev-lang/python-3.4.3-r4:3.4
	!<dev-lang/python-3.5.0-r3:3.5"

#if LIVE
KEYWORDS=
SRC_URI=

src_prepare() {
	eautoreconf
}
#endif

src_configure() {
	local pyimpls=() i EPYTHON
	for i in "${PYTHON_COMPAT[@]}"; do
		python_export "${i}" EPYTHON
		pyimpls+=( "${EPYTHON}" )
	done

	local myconf=(
		--with-fallback-path="${EPREFIX}/usr/local/sbin:${EPREFIX}/usr/local/bin:${EPREFIX}/usr/sbin:${EPREFIX}/usr/bin:${EPREFIX}/sbin:${EPREFIX}/bin"
		--with-python-impls="${pyimpls[*]}"
	)

	econf "${myconf[@]}"
}

src_install() {
	default

	# Prepare and own the template
	sed -n -e '/^#/p' config/python-exec.conf.example \
		> "${T}"/python-exec.conf || die
	insinto /etc/python-exec
	doins "${T}"/python-exec.conf

	local f
	for f in python{,2,3}; do
		# symlink the C wrapper for python to avoid shebang recursion
		# bug #568974
		dosym python-exec2c /usr/bin/"${f}"
	done
	for f in python{,2,3}-config 2to3 idle pydoc pyvenv; do
		# those are python scripts (except for new python-configs)
		# so symlink them via the python wrapper
		dosym ../lib/python-exec/python-exec2 /usr/bin/"${f}"
	done
}

pkg_preinst() {
	if [[ -e ${EROOT}etc/python-exec/python-exec.conf ]]; then
		# preserve current configuration
		cp "${EROOT}"etc/python-exec/python-exec.conf \
			"${ED}"etc/python-exec/python-exec.conf || die
	else
		# preserve previous Python version preference
		local py old_pythons=()
		local config_base=${EROOT}etc/env.d/python

		# start with the 'global' preference (2 vs 3)
		if [[ -f ${config_base}/config ]]; then
			old_pythons+=( "$(<${config_base}/config)" )
		fi

		# then try specific py3 selection
		for py in 3; do
			local target=

			if [[ -f ${config_base}/python${py} ]]; then
				# try the newer config files
				target=$(<${config_base}/python${py})
			elif [[ -L ${EROOT}/usr/bin/python${py} ]]; then
				# check the older symlink format
				target=$(readlink "${EROOT}/usr/bin/python${py}")

				# check if it's actually old eselect symlink
				[[ ${target} == python?.? ]] || target=
			fi

			# add the extra target if found and != global
			if [[ ${target} && ${old_pythons[0]} != ${target} ]]; then
				old_pythons+=( "${target}" )
			fi
		done

		if [[ ${old_pythons[@]} ]]; then
			elog "You seem to have just upgraded into the new version of python-exec"
			elog "that uses python-exec.conf for configuration. The ebuild has attempted"
			elog "to convert your previous configuration to the new format, resulting"
			elog "in the following preferences (most preferred version first):"
			elog
			for py in "${old_pythons[@]}"; do
				elog "  ${py}"
			done
			elog
			elog "Those interpreters will be preferred when running Python scripts or"
			elog "calling wrapped Python executables (python, python2, pydoc...)."
			elog "If none of the preferred interpreters are supported, python-exec will"
			elog "fall back to the newest supported Python version."
			elog
			elog "Please note that due to the ambiguous character of the old settings,"
			elog "you may want to modify the preference list yourself. In order to do so,"
			elog "open the following file in your favorite editor:"
			elog
			elog "  ${EROOT}etc/python-exec/python-exec.conf"
			elog
			elog "For more information on the new configuration format, please read"
			elog "the comment on top of the installed configuration file."

			local IFS=$'\n'
			echo "${old_pythons[*]}" \
				>> "${ED}"etc/python-exec/python-exec.conf || die
		fi
	fi
}