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

DESCRIPTION="Manage the /usr/bin/python and python.1 man symlinks."
MAINTAINER="python@gentoo.org"
SVN_DATE='$Date$'
VERSION=$(svn_date_to_version "${SVN_DATE}" )

ENV_D_PATH="${ROOT%/}/etc/env.d"
INTERPRETER_PATH="${ROOT%/}/usr/bin/"
MAN_PATH="${ROOT%/}/usr/share/man/man1/"

# Find a list of python versions
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
}

# Try to remove python and python.1 symlinks
remove_symlinks() {
	local symlink symlink_target symlink_target_found
	rm -f "${INTERPRETER_PATH}"{idle,pydoc,python,python-config} &>/dev/null && \
	rm -f "${MAN_PATH}"python.1{,.gz,.bz2,.lzma} &>/dev/null
	for symlink in "${INTERPRETER_PATH}python"?; do
		[[ ! -L "${symlink}" ]] && continue
		symlink_target_found=0
		for symlink_target in "${symlink}".?; do
			[[ -f "${symlink_target}" ]] && symlink_target_found=1
		done
		if [[ "${symlink_target_found}" -eq 0 ]]; then
			rm -f "${symlink}"
		fi
	done
}

# Set a man page symlink
set_man_symlink() {
	local target="${1}" x extension

	for x in ".1" ".1.bz2" ".1.gz" ".1.lzma"; do
		if [[ -e "${MAN_PATH}${target}${x}" ]]; then
			extension="${x}"
			break
		fi
	done

	if [[ -z "${extension}" ]]; then
		echo "Couldn't find a man page for ${target}; skipping." 1>&2
		return 1
	fi

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

# Set python and python.1 symlinks
set_symlinks() {
	local target="${1}" targets=($(find_targets ))
	if is_number "${target}" && [[ ${target} -ge 1 ]]; then
		target=${targets[$(( ${target} - 1 ))]}
	fi

	if ! has ${target} "${targets[@]}"; then
		die -q "Invalid target ${target}"
	fi
	if [[ -f "${INTERPRETER_PATH}${target}" ]]; then
		remove_symlinks
		set_man_symlink "${target}"
	
		pushd "${INTERPRETER_PATH}" 1>/dev/null
		ln -nfs "${target}" python
		ln -nfs "${target}" "${target%.*}"
		ln -nfs "${target/python/python-config-}" python-config
		ln -nfs "${target/python/pydoc}" pydoc
		ln -nfs "${target/python/idle}" idle
		# 2to3 for >=2.6
		if [[ -f "${INTERPRETER_PATH}${target/python/2to3-}" ]]; then
			ln -nfs "${target/python/2to3-}" 2to3
		fi

		popd 1>/dev/null
	else
		die -q "Target \"${1}\" doesn't appear to be valid!"
	fi
}

# Set the content of /etc/env.d/65python-docs
set_python_docs() {
	local path target="${1#python}" variable
	rm -f "${ENV_D_PATH}/65python-docs"
	if [[ -f "${ENV_D_PATH}/60python-docs-${target}" ]]; then
		variable="PYTHONDOCS_${target//./_}"
		path="$(. "${ENV_D_PATH}/60python-docs-${target}"; echo -n "${!variable}")"
		if [[ -d "${path}" ]]; then
			echo "PYTHONDOCS=\"${path}\"" > "${ENV_D_PATH}/65python-docs"
		fi
	fi
}

### show action

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

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

### list action ###

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
}

### set action ###

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"
	elif [[ -n "${2}" ]]; then
		die -q "Too many parameters"
	elif [[ -L "${INTERPRETER_PATH}python" ]]; then
		if ! remove_symlinks; then
			die -q "Can't remove existing provider"
		elif ! set_symlinks "${1}"; then
			die -q "Can't set new provider"
		fi
		set_python_docs "${1}"
	elif [[ -e "${INTERPRETER_PATH}python" ]]; then
		die -q "Sorry, ${INTERPRETER_PATH}python is not a symlink"
	else
		set_symlinks "${1}" || die -q "Can't set a new provider"
	fi
}

### update action ###

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

describe_update_options() {
	echo "--if-unset    : Do not override existing implementation"
	echo "--ignore SLOT : Ignore SLOT when setting symlinks"
}

do_update() {
	local if_unset=false ignored_slots=() slot=
	while [[ $# > 0 ]]; do
		case $1 in
			--if-unset)
				if_unset=true
				;;
			--ignore)
				ignored_slots=( ${ignored_slots[@]} "${2}" )
				shift;;
			--*)
				die -q "Unrecognized option '$1'"
				;;
		esac
		shift
	done

	if [[ -L "${ROOT%/}/usr/bin/python" ]]; then
		${if_unset} && return
	fi

	local targets=( $(cd "${INTERPRETER_PATH}"; ls python?.?|sort -r) ) target
	
	# Ignore slots
	for slot in ${ignored_slots[@]}; do
		targets=( ${targets[@]/python${slot}/} )
	done

	target=${targets[0]}
	echo "Switching to ${target}"
	do_set ${target}
}

# vim: set ft=eselect :