summaryrefslogtreecommitdiff
blob: e5f8f23be4bff84011bd0865f5c8f9669e793435 (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
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: $

# Heavily based on profile.eselect.

DESCRIPTION="Manage the /usr/bin/python symlink."
MAINTAINER="marienz@gentoo.org"
VERSION="0.1"


INTERPRETER_PATH="${ROOT}/usr/bin/"

find_targets() {
	local interpreter
	# Think twice before adding jython to this list. /usr/bin/jython
	# is a bash wrapper that calls java-config, which is a python
	# script, so you need a valid /usr/bin/python to start jython.
	for interpreter in "${INTERPRETER_PATH}"python?.? ; do
		echo ${interpreter#${INTERPRETER_PATH}}
	done
}


describe_show() {
	echo "Show the active python interpreter"
}

do_show() {
	active=$(canonicalise "${INTERPRETER_PATH}python")
	echo ${active#${INTERPRETER_PATH}}
}


describe_list() {
	echo "List installed python interpreters"
}

do_list() {
	local targets=( $(find_targets) )

	write_list_start "Available python interpreters:"

	if [[ -n ${targets[@]} ]] ; then
		# mark the active python
		local i active=$(do_show)
		for (( i = 0 ; i < ${#targets[@]} ; i = i + 1 )) ; do
			[[ ${targets[${i}]} == $active ]] && \
				targets[${i}]="${targets[${i}]} $(highlight '*' )"
		done
		write_numbered_list "${targets[@]}"
	else
		write_kv_list_entry "(none found)" ""
	fi
}


describe_set() {
	echo "Set active python interpreter."
}

do_set() {
	if [[ -z ${1} ]] ; then
		die -q "You didn't tell me which python interpreter to use"
	fi

	local targets=( $(find_targets) ) target=${1}

	if is_number "${target}"; then
		target=${targets[$(( ${target} - 1 ))]}
	fi

	if ! has ${target} "${targets[@]}" ; then
		die -q "Invalid target ${target}"
	fi

	pushd "${INTERPRETER_PATH}" 1>/dev/null
	ln -nfs ${target} python
	popd 1>/dev/null
}


describe_update() {
	echo "Switch to the most recent CPython."
}

do_update() {
	local targets=( $(cd "${INTERPRETER_PATH}"; ls python?.?|sort -r) ) target
	target=${targets[0]}
	echo "Switching to ${target}"
	do_set ${target}
}