* [gentoo-commits] proj/eselect-python:mgornys-hackery commit in: /
@ 2015-11-12 16:53 Michał Górny
0 siblings, 0 replies; 5+ messages in thread
From: Michał Górny @ 2015-11-12 16:53 UTC (permalink / raw
To: gentoo-commits
commit: a3421a53b669fd15c6449203700163712532e4f2
Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Thu Nov 12 16:40:08 2015 +0000
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Thu Nov 12 16:53:37 2015 +0000
URL: https://gitweb.gentoo.org/proj/eselect-python.git/commit/?id=a3421a53
Store selected python[23] version in a file
Use /etc/env.d/python/python[23] file to store the active Python2/3
interpreter rather than /usr/bin/python[23] symlink target. This is
simpler, more portable and allows us to replace symlinks with real
wrappers.
python.eselect.in | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/python.eselect.in b/python.eselect.in
index 8baa44e..32d9cbf 100644
--- a/python.eselect.in
+++ b/python.eselect.in
@@ -33,6 +33,13 @@ find_targets() {
done
}
+set_python_subver() {
+ local target=${1}
+ local subver=${target%.*}
+ mkdir -p "${ENV_D_PATH}/python"
+ echo "${target}" > "${ENV_D_PATH}/python/${subver}"
+}
+
set_python() {
local symlink="${INTERPRETER_PATH}python" target="${1}"
ln -s python-wrapper "${symlink}"
@@ -119,6 +126,7 @@ set_scripts_and_symlinks() {
pushd "${INTERPRETER_PATH}" 1> /dev/null
ln -nfs "${target}" "${target%.*}"
+ set_python_subver "${target}"
if [[ "${SET_MAIN_ACTIVE_PYTHON_INTERPRETER}" == "1" ]]; then
set_python "${target}"
set_python_config "${target/python/python-config-}"
@@ -205,9 +213,9 @@ do_show() {
fi
if [[ "${python2}" == "1" ]]; then
- interpreter="$(readlink "${INTERPRETER_PATH}python2")"
+ interpreter="$(<"${ENV_D_PATH}/python/python2")"
elif [[ "${python3}" == "1" ]]; then
- interpreter="$(readlink "${INTERPRETER_PATH}python3")"
+ interpreter="$(<"${ENV_D_PATH}/python/python3")"
elif [[ -f "${ENV_D_PATH}/python/config" ]]; then
interpreter="$(<"${ENV_D_PATH}/python/config")"
fi
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [gentoo-commits] proj/eselect-python:mgornys-hackery commit in: /
2015-11-17 18:36 [gentoo-commits] proj/eselect-python:master " Michał Górny
@ 2015-11-12 16:53 ` Michał Górny
0 siblings, 0 replies; 5+ messages in thread
From: Michał Górny @ 2015-11-12 16:53 UTC (permalink / raw
To: gentoo-commits
commit: bb5b98cacf492697f74d9dde87dc974b541b79ad
Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Thu Nov 12 16:53:25 2015 +0000
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Thu Nov 12 16:53:37 2015 +0000
URL: https://gitweb.gentoo.org/proj/eselect-python.git/commit/?id=bb5b98ca
manpage: Support .lz compression
python.eselect.in | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/python.eselect.in b/python.eselect.in
index 9a52791..8baa44e 100644
--- a/python.eselect.in
+++ b/python.eselect.in
@@ -58,7 +58,7 @@ remove_symlinks() {
local symlink symlink_target symlink_target_found
if [[ "${SET_MAIN_ACTIVE_PYTHON_INTERPRETER}" == "1" ]]; then
rm -f "${INTERPRETER_PATH}"{idle,pydoc,python,python-config,pythonw} &> /dev/null || return 1
- rm -f "${MAN_PATH}"python.1{,.gz,.bz2,.lzma,.xz} &> /dev/null || return 1
+ rm -f "${MAN_PATH}"python.1{,.gz,.bz2,.lzma,.xz,.lz} &> /dev/null || return 1
fi
for symlink in "${INTERPRETER_PATH}python"?; do
@@ -80,7 +80,7 @@ remove_symlinks() {
set_man_symlink() {
local target="${1}" x extension
- for x in ".1" ".1.bz2" ".1.gz" ".1.lzma" ".1.xz"; do
+ for x in ".1" ".1.bz2" ".1.gz" ".1.lzma" ".1.xz" ".1.lz"; do
if [[ -e "${MAN_PATH}${target}${x}" ]]; then
extension="${x}"
break
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [gentoo-commits] proj/eselect-python:mgornys-hackery commit in: /
@ 2015-11-12 17:07 Michał Górny
0 siblings, 0 replies; 5+ messages in thread
From: Michał Górny @ 2015-11-12 17:07 UTC (permalink / raw
To: gentoo-commits
commit: 2e082de29ee3707d11d1097132aaeeae16d0ea83
Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Thu Nov 12 17:07:35 2015 +0000
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Thu Nov 12 17:07:35 2015 +0000
URL: https://gitweb.gentoo.org/proj/eselect-python.git/commit/?id=2e082de2
Remove python-wrapper
Makefile.am | 3 -
configure.ac | 26 ------
python-wrapper.c | 244 -------------------------------------------------------
3 files changed, 273 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index 06f7d1c..beb4029 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,5 +1,2 @@
-bin_PROGRAMS = python-wrapper
-python_wrapper_CPPFLAGS = -DENVD='"$(sysconfdir)/env.d"'
-
eselectdir = $(datadir)/eselect/modules
nodist_eselect_DATA = python.eselect
diff --git a/configure.ac b/configure.ac
index dc142ae..e782129 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,36 +1,10 @@
AC_INIT([eselect-python], [20140125])
AM_INIT_AUTOMAKE([-Wall foreign no-dist-gzip dist-bzip2])
-AC_PROG_CC
AC_PATH_PROG(INSTALL, install)
AC_PROG_MKDIR_P
-AC_USE_SYSTEM_EXTENSIONS
-
-# setenv() was introduced in POSIX.1-2008.
-# strtok_r() was introduced in POSIX.1-2001.
-AC_CHECK_FUNCS([setenv strtok_r])
-
-# strndup() was introduced in POSIX.1-2008 and is also an implicitly declared built-in function in GCC.
-AC_MSG_CHECKING([for strndup])
-old_CFLAGS="${CFLAGS}"
-CFLAGS="${CFLAGS} -Wall -Werror -Wextra"
-AC_LINK_IFELSE([AC_LANG_SOURCE([[#include <string.h>
-int main()
-{
- strndup("", 0);
- return 0;
-}]])], [have_strndup="1"], [have_strndup="0"])
-if test "${have_strndup}" = "1"; then
- AC_MSG_RESULT([yes])
- AC_DEFINE([HAVE_STRNDUP], [1], [Define to 1 if you have the 'strndup' function.])
-else
- AC_MSG_RESULT([no])
-fi
-CFLAGS="${old_CFLAGS}"
-
# Create output files.
-AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile python.eselect])
AC_OUTPUT
diff --git a/python-wrapper.c b/python-wrapper.c
deleted file mode 100644
index 75a1de4..0000000
--- a/python-wrapper.c
+++ /dev/null
@@ -1,244 +0,0 @@
-/* Copyright 1999-2010 Gentoo Foundation
- * Distributed under the terms of the GNU General Public License v2
- */
-
-#include "config.h"
-
-#include <dirent.h>
-#include <limits.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/stat.h>
-
-#ifndef ENVD
-#define ENVD "/etc/env.d"
-#endif
-
-#define ENVD_CONFIG ENVD "/python/config"
-
-/* 127 is the standard return code for "command not found" */
-#define EXIT_ERROR 127
-
-char* dir_cat(const char* dir, const char* file)
-{
- size_t dir_len = strlen(dir);
- char* abs = malloc(dir_len + strlen(file) + 2);
- strcpy(abs, dir);
- abs[dir_len] = '/';
- abs[dir_len + 1] = 0;
- return strcat(abs, file);
-}
-
-const char* find_path(const char* exe)
-{
- const char* last_slash = strrchr(exe, '/');
- if (last_slash)
- {
-#ifdef HAVE_STRNDUP
- return strndup(exe, last_slash - exe);
-#else
- size_t len = last_slash - exe;
- char* ret = malloc(sizeof(char) * (len + 1));
- memcpy(ret, exe, len);
- ret[len] = '\0';
- return(ret);
-#endif
- }
- const char* PATH = getenv("PATH");
- if (! PATH)
- {
- /* If PATH is unset, then it defaults to ":/bin:/usr/bin", per
- * execvp(3).
- */
- PATH = ":/bin:/usr/bin";
- }
- char* path = strdup(PATH);
-#ifdef HAVE_STRTOK_R
- char* state = NULL;
- const char* token = strtok_r(path, ":", &state);
-#else
- const char* token = strtok(path, ":");
-#endif
- while (token)
- {
- /* If an element of PATH is empty ("::"), then it is "." */
- if (! *token)
- {
- token = ".";
- }
- struct stat sbuf;
- char* str = dir_cat(token, exe);
- if (stat(str, &sbuf) == 0 && (S_ISREG(sbuf.st_mode) || S_ISLNK(sbuf.st_mode)))
- {
- return token;
- }
-#ifdef HAVE_STRTOK_R
- token = strtok_r(NULL, ":", &state);
-#else
- token = strtok(NULL, ":");
-#endif
- }
- return NULL;
-}
-
-/* True if a valid file name, and not "python" */
-int valid_interpreter(const char* name)
-{
- if (! name || ! *name || (strcmp(name, "python") == 0))
- {
- return 0;
- }
- return 1;
-}
-
-int get_version(const char* name)
-{
- /* Only find files beginning with "python" - this is a fallback,
- * so we only want CPython
- */
- if (! valid_interpreter(name) || strncmp(name, "python", 6) != 0)
- return -1;
- int pos = 6;
- int major = 0;
- int minor = 0;
- if (name[pos] < '0' || name[pos] > '9')
- return -1;
- do
- {
- major = major * 10 + name[pos] - '0';
- if (! name[++pos])
- return -1;
- }
- while (name[pos] >= '0' && name[pos] <= '9');
- if (name[pos++] != '.')
- return -1;
- if (name[pos] < '0' || name[pos] > '9')
- return -1;
- do
- {
- minor = minor * 10 + name[pos] - '0';
- if (! name[++pos])
- return (major << 8) | minor;
- }
- while (name[pos] >= '0' && name[pos] <= '9');
- return -1;
-}
-
-int filter_python(const struct dirent* file)
-{
- return get_version(file->d_name) != -1;
-}
-
-/* This implements a version sort, such that the following order applies:
- * <invalid file names> (should never be seen)
- * python2.6
- * python2.9
- * python2.10
- * python3.0
- * python3.1
- * python3.2
- * python9.1
- * python9.9
- * python9.10
- * python10.1
- * python10.9
- * python10.10
- */
-int sort_python(const struct dirent**f1, const struct dirent** f2)
-{
- int ver1 = get_version((*f1)->d_name);
- int ver2 = get_version((*f2)->d_name);
- return ver1 - ver2;
-}
-
-const char* find_latest(const char* exe)
-{
- const char* path = find_path(exe);
- if (! path || ! *path)
- {
- path = "/usr/bin";
- }
- struct dirent** namelist;
- int n = scandir(path, &namelist, filter_python, sort_python);
- const char* ret = NULL;
- if (n < 0)
- {
- return NULL;
- }
- /* Walk backwards through the list. */
- while (n--)
- {
- if (! ret)
- ret = strdup(namelist[n]->d_name);
- free(namelist[n]);
- }
- free(namelist);
- return ret;
-}
-
-int main(int argc, char** argv)
-{
- const char* EPYTHON = getenv("EPYTHON");
- int script_name_index = -1;
- if (! valid_interpreter(EPYTHON))
- {
- FILE* f = fopen(ENVD_CONFIG, "r");
- if (f)
- {
- struct stat st;
- fstat(fileno(f), &st);
- size_t size = st.st_size;
- char* cont = malloc(size + 1);
- cont = fgets(cont, size + 1, f);
- fclose(f);
- size_t len = strlen(cont);
- if (len && cont[len - 1] == '\n')
- cont[len - 1] = 0;
- EPYTHON = cont;
- }
- }
-
- if (! valid_interpreter(EPYTHON))
- EPYTHON = find_latest(argv[0]);
-
- if (! EPYTHON)
- return EXIT_ERROR;
-
- if (strchr(EPYTHON, '/'))
- {
- fprintf(stderr, "Invalid value of EPYTHON variable or invalid configuration of Python wrapper\n");
- return EXIT_ERROR;
- }
-
- /* Set GENTOO_PYTHON_PROCESS_NAME environmental variable, if a script with a Python shebang is probably being executed.
- * argv[0] can be "python", when "#!/usr/bin/env python" shebang is used. */
- if (argc >= 2 && (argv[0][0] == '/' || strcmp(argv[0], "python") == 0) && (argv[1][0] == '/' || strncmp(argv[1], "./", 2) == 0))
- script_name_index = 1;
- else if (argc >= 3 && argv[0][0] == '/' && argv[1][0] == '-' && (argv[2][0] == '/' || strncmp(argv[2], "./", 2) == 0))
- script_name_index = 2;
- if (script_name_index > 0)
- {
- char* script_name = strrchr(argv[script_name_index], '/') + 1;
-#ifdef HAVE_SETENV
- setenv("GENTOO_PYTHON_PROCESS_NAME", script_name, 1);
-#else
- char* script_name_variable = malloc(sizeof(char) * (strlen("GENTOO_PYTHON_PROCESS_NAME=") + strlen(script_name)));
- sprintf(script_name_variable, "GENTOO_PYTHON_PROCESS_NAME=%s", script_name);
- putenv(script_name_variable);
-#endif
- }
-
- const char* path = find_path(argv[0]);
- if (path)
- {
- argv[0] = dir_cat(path, EPYTHON);
- execv(argv[0], argv);
- /* If this failed, then just search the PATH. */
- }
-
- argv[0] = (char*) EPYTHON;
- execvp(EPYTHON, argv);
- return EXIT_ERROR;
-}
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [gentoo-commits] proj/eselect-python:mgornys-hackery commit in: /
2015-11-17 18:36 [gentoo-commits] proj/eselect-python:master " Michał Górny
@ 2015-11-12 17:07 ` Michał Górny
0 siblings, 0 replies; 5+ messages in thread
From: Michał Górny @ 2015-11-12 17:07 UTC (permalink / raw
To: gentoo-commits
commit: cf186abf6240fc116f4a38d74cf8d627de57da6d
Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Thu Nov 12 17:06:32 2015 +0000
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Thu Nov 12 17:06:32 2015 +0000
URL: https://gitweb.gentoo.org/proj/eselect-python.git/commit/?id=cf186abf
Stop creating symlinks to interpreters and the wrapper
python.eselect.in | 60 ++++++++++---------------------------------------------
1 file changed, 11 insertions(+), 49 deletions(-)
diff --git a/python.eselect.in b/python.eselect.in
index 32d9cbf..3d9b4bc 100644
--- a/python.eselect.in
+++ b/python.eselect.in
@@ -2,7 +2,7 @@
# Distributed under the terms of the GNU General Public License v2
# $Id: $
-DESCRIPTION="Manage Python symlinks"
+DESCRIPTION="Manage active Python interpreter"
MAINTAINER="python@gentoo.org"
SVN_DATE='$Date$'
VERSION=$(svn_date_to_version "${SVN_DATE}" )
@@ -41,44 +41,18 @@ set_python_subver() {
}
set_python() {
- local symlink="${INTERPRETER_PATH}python" target="${1}"
- ln -s python-wrapper "${symlink}"
+ local target="${1}"
mkdir -p "${ENV_D_PATH}/python"
echo "${target}" > "${ENV_D_PATH}/python/config"
}
-set_python_config() {
- local script="${INTERPRETER_PATH}python-config" target="${1}"
- cat << EOF > "${script}"
-#!/usr/bin/env bash
-# Gentoo python-config wrapper script
-
-[[ "\${EPYTHON}" =~ (/|^python\$) ]] && EPYTHON="${target/-config-/}"
-python_config="\${EPYTHON/python/python-config-}"
-"\${0%/*}/\${python_config:-${target}}" "\$@"
-EOF
- chmod a+rx "${script}"
-}
-
# Try to remove python and python.1 symlinks
remove_symlinks() {
local symlink symlink_target symlink_target_found
if [[ "${SET_MAIN_ACTIVE_PYTHON_INTERPRETER}" == "1" ]]; then
- rm -f "${INTERPRETER_PATH}"{idle,pydoc,python,python-config,pythonw} &> /dev/null || return 1
rm -f "${MAN_PATH}"python.1{,.gz,.bz2,.lzma,.xz,.lz} &> /dev/null || return 1
fi
- 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
-
# Files of Mac OS X framework
rm -f "${INTERPRETER_PATH%/bin/}/lib/Python.framework}"/{Headers,Python,Resources}
}
@@ -125,25 +99,9 @@ set_scripts_and_symlinks() {
pushd "${INTERPRETER_PATH}" 1> /dev/null
- ln -nfs "${target}" "${target%.*}"
set_python_subver "${target}"
if [[ "${SET_MAIN_ACTIVE_PYTHON_INTERPRETER}" == "1" ]]; then
set_python "${target}"
- set_python_config "${target/python/python-config-}"
- ln -nfs "${target/python/pydoc}" pydoc
- # idle is optionally installed
- if [[ -f "${target/python/idle}" ]]; then
- ln -nfs "${target/python/idle}" idle
- fi
- # 2to3 for >=2.6
- if [[ -f "${target/python/2to3-}" ]]; then
- ln -nfs "${target/python/2to3-}" 2to3
- fi
-
- # Wrapper for graphical applications on Mac OS X
- if [[ -f "${target/python/pythonw}" ]] ; then
- ln -nfs "${target/python/pythonw}" pythonw
- fi
# Files of Mac OS X framework
local framework_dir="${INTERPRETER_PATH%/bin/}/lib/Python.framework"
@@ -213,9 +171,13 @@ do_show() {
fi
if [[ "${python2}" == "1" ]]; then
- interpreter="$(<"${ENV_D_PATH}/python/python2")"
+ if [[ -f ${ENV_D_PATH}/python/python2 ]]; then
+ interpreter="$(<"${ENV_D_PATH}/python/python2")"
+ fi
elif [[ "${python3}" == "1" ]]; then
- interpreter="$(<"${ENV_D_PATH}/python/python3")"
+ if [[ -f ${ENV_D_PATH}/python/python3 ]]; then
+ interpreter="$(<"${ENV_D_PATH}/python/python3")"
+ fi
elif [[ -f "${ENV_D_PATH}/python/config" ]]; then
interpreter="$(<"${ENV_D_PATH}/python/config")"
fi
@@ -385,10 +347,10 @@ do_update() {
die -q "'--python2' and '--python3' options cannot be specified simultaneously"
fi
- if [[ "${if_unset}" == "1" && -f "${INTERPRETER_PATH}python" && -f "${ENV_D_PATH}/python/config" ]]; then
- if [[ "${python2}" == "1" && -f "${INTERPRETER_PATH}python2" ]]; then
+ if [[ "${if_unset}" == "1" && -f "${ENV_D_PATH}/python/config" ]]; then
+ if [[ "${python2}" == "1" && -f "${ENV_D_PATH}/python/python2" ]]; then
return
- elif [[ "${python3}" == "1" && -f "${INTERPRETER_PATH}python3" ]]; then
+ elif [[ "${python3}" == "1" && -f "${ENV_D_PATH}/python/python3" ]]; then
return
elif [[ "${python2}" == "0" && "${python3}" == "0" ]]; then
return
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [gentoo-commits] proj/eselect-python:mgornys-hackery commit in: /
2015-11-17 18:36 [gentoo-commits] proj/eselect-python:master " Michał Górny
@ 2015-11-17 12:59 ` Michał Górny
0 siblings, 0 replies; 5+ messages in thread
From: Michał Górny @ 2015-11-17 12:59 UTC (permalink / raw
To: gentoo-commits
commit: 6824d7d6841536d153cb56a350c6f89708725777
Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Thu Nov 12 17:07:35 2015 +0000
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Tue Nov 17 12:59:04 2015 +0000
URL: https://gitweb.gentoo.org/proj/eselect-python.git/commit/?id=6824d7d6
Remove python-wrapper
Makefile.am | 3 -
configure.ac | 26 +-----
python-wrapper.c | 244 -------------------------------------------------------
3 files changed, 1 insertion(+), 272 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index 06f7d1c..beb4029 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,5 +1,2 @@
-bin_PROGRAMS = python-wrapper
-python_wrapper_CPPFLAGS = -DENVD='"$(sysconfdir)/env.d"'
-
eselectdir = $(datadir)/eselect/modules
nodist_eselect_DATA = python.eselect
diff --git a/configure.ac b/configure.ac
index dc142ae..d9a6461 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,36 +1,12 @@
AC_INIT([eselect-python], [20140125])
AM_INIT_AUTOMAKE([-Wall foreign no-dist-gzip dist-bzip2])
+# note: we need AC_PROG_CC to determine EXEEXT (AC_EXEEXT does not work)
AC_PROG_CC
AC_PATH_PROG(INSTALL, install)
AC_PROG_MKDIR_P
-AC_USE_SYSTEM_EXTENSIONS
-
-# setenv() was introduced in POSIX.1-2008.
-# strtok_r() was introduced in POSIX.1-2001.
-AC_CHECK_FUNCS([setenv strtok_r])
-
-# strndup() was introduced in POSIX.1-2008 and is also an implicitly declared built-in function in GCC.
-AC_MSG_CHECKING([for strndup])
-old_CFLAGS="${CFLAGS}"
-CFLAGS="${CFLAGS} -Wall -Werror -Wextra"
-AC_LINK_IFELSE([AC_LANG_SOURCE([[#include <string.h>
-int main()
-{
- strndup("", 0);
- return 0;
-}]])], [have_strndup="1"], [have_strndup="0"])
-if test "${have_strndup}" = "1"; then
- AC_MSG_RESULT([yes])
- AC_DEFINE([HAVE_STRNDUP], [1], [Define to 1 if you have the 'strndup' function.])
-else
- AC_MSG_RESULT([no])
-fi
-CFLAGS="${old_CFLAGS}"
-
# Create output files.
-AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile python.eselect])
AC_OUTPUT
diff --git a/python-wrapper.c b/python-wrapper.c
deleted file mode 100644
index 75a1de4..0000000
--- a/python-wrapper.c
+++ /dev/null
@@ -1,244 +0,0 @@
-/* Copyright 1999-2010 Gentoo Foundation
- * Distributed under the terms of the GNU General Public License v2
- */
-
-#include "config.h"
-
-#include <dirent.h>
-#include <limits.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/stat.h>
-
-#ifndef ENVD
-#define ENVD "/etc/env.d"
-#endif
-
-#define ENVD_CONFIG ENVD "/python/config"
-
-/* 127 is the standard return code for "command not found" */
-#define EXIT_ERROR 127
-
-char* dir_cat(const char* dir, const char* file)
-{
- size_t dir_len = strlen(dir);
- char* abs = malloc(dir_len + strlen(file) + 2);
- strcpy(abs, dir);
- abs[dir_len] = '/';
- abs[dir_len + 1] = 0;
- return strcat(abs, file);
-}
-
-const char* find_path(const char* exe)
-{
- const char* last_slash = strrchr(exe, '/');
- if (last_slash)
- {
-#ifdef HAVE_STRNDUP
- return strndup(exe, last_slash - exe);
-#else
- size_t len = last_slash - exe;
- char* ret = malloc(sizeof(char) * (len + 1));
- memcpy(ret, exe, len);
- ret[len] = '\0';
- return(ret);
-#endif
- }
- const char* PATH = getenv("PATH");
- if (! PATH)
- {
- /* If PATH is unset, then it defaults to ":/bin:/usr/bin", per
- * execvp(3).
- */
- PATH = ":/bin:/usr/bin";
- }
- char* path = strdup(PATH);
-#ifdef HAVE_STRTOK_R
- char* state = NULL;
- const char* token = strtok_r(path, ":", &state);
-#else
- const char* token = strtok(path, ":");
-#endif
- while (token)
- {
- /* If an element of PATH is empty ("::"), then it is "." */
- if (! *token)
- {
- token = ".";
- }
- struct stat sbuf;
- char* str = dir_cat(token, exe);
- if (stat(str, &sbuf) == 0 && (S_ISREG(sbuf.st_mode) || S_ISLNK(sbuf.st_mode)))
- {
- return token;
- }
-#ifdef HAVE_STRTOK_R
- token = strtok_r(NULL, ":", &state);
-#else
- token = strtok(NULL, ":");
-#endif
- }
- return NULL;
-}
-
-/* True if a valid file name, and not "python" */
-int valid_interpreter(const char* name)
-{
- if (! name || ! *name || (strcmp(name, "python") == 0))
- {
- return 0;
- }
- return 1;
-}
-
-int get_version(const char* name)
-{
- /* Only find files beginning with "python" - this is a fallback,
- * so we only want CPython
- */
- if (! valid_interpreter(name) || strncmp(name, "python", 6) != 0)
- return -1;
- int pos = 6;
- int major = 0;
- int minor = 0;
- if (name[pos] < '0' || name[pos] > '9')
- return -1;
- do
- {
- major = major * 10 + name[pos] - '0';
- if (! name[++pos])
- return -1;
- }
- while (name[pos] >= '0' && name[pos] <= '9');
- if (name[pos++] != '.')
- return -1;
- if (name[pos] < '0' || name[pos] > '9')
- return -1;
- do
- {
- minor = minor * 10 + name[pos] - '0';
- if (! name[++pos])
- return (major << 8) | minor;
- }
- while (name[pos] >= '0' && name[pos] <= '9');
- return -1;
-}
-
-int filter_python(const struct dirent* file)
-{
- return get_version(file->d_name) != -1;
-}
-
-/* This implements a version sort, such that the following order applies:
- * <invalid file names> (should never be seen)
- * python2.6
- * python2.9
- * python2.10
- * python3.0
- * python3.1
- * python3.2
- * python9.1
- * python9.9
- * python9.10
- * python10.1
- * python10.9
- * python10.10
- */
-int sort_python(const struct dirent**f1, const struct dirent** f2)
-{
- int ver1 = get_version((*f1)->d_name);
- int ver2 = get_version((*f2)->d_name);
- return ver1 - ver2;
-}
-
-const char* find_latest(const char* exe)
-{
- const char* path = find_path(exe);
- if (! path || ! *path)
- {
- path = "/usr/bin";
- }
- struct dirent** namelist;
- int n = scandir(path, &namelist, filter_python, sort_python);
- const char* ret = NULL;
- if (n < 0)
- {
- return NULL;
- }
- /* Walk backwards through the list. */
- while (n--)
- {
- if (! ret)
- ret = strdup(namelist[n]->d_name);
- free(namelist[n]);
- }
- free(namelist);
- return ret;
-}
-
-int main(int argc, char** argv)
-{
- const char* EPYTHON = getenv("EPYTHON");
- int script_name_index = -1;
- if (! valid_interpreter(EPYTHON))
- {
- FILE* f = fopen(ENVD_CONFIG, "r");
- if (f)
- {
- struct stat st;
- fstat(fileno(f), &st);
- size_t size = st.st_size;
- char* cont = malloc(size + 1);
- cont = fgets(cont, size + 1, f);
- fclose(f);
- size_t len = strlen(cont);
- if (len && cont[len - 1] == '\n')
- cont[len - 1] = 0;
- EPYTHON = cont;
- }
- }
-
- if (! valid_interpreter(EPYTHON))
- EPYTHON = find_latest(argv[0]);
-
- if (! EPYTHON)
- return EXIT_ERROR;
-
- if (strchr(EPYTHON, '/'))
- {
- fprintf(stderr, "Invalid value of EPYTHON variable or invalid configuration of Python wrapper\n");
- return EXIT_ERROR;
- }
-
- /* Set GENTOO_PYTHON_PROCESS_NAME environmental variable, if a script with a Python shebang is probably being executed.
- * argv[0] can be "python", when "#!/usr/bin/env python" shebang is used. */
- if (argc >= 2 && (argv[0][0] == '/' || strcmp(argv[0], "python") == 0) && (argv[1][0] == '/' || strncmp(argv[1], "./", 2) == 0))
- script_name_index = 1;
- else if (argc >= 3 && argv[0][0] == '/' && argv[1][0] == '-' && (argv[2][0] == '/' || strncmp(argv[2], "./", 2) == 0))
- script_name_index = 2;
- if (script_name_index > 0)
- {
- char* script_name = strrchr(argv[script_name_index], '/') + 1;
-#ifdef HAVE_SETENV
- setenv("GENTOO_PYTHON_PROCESS_NAME", script_name, 1);
-#else
- char* script_name_variable = malloc(sizeof(char) * (strlen("GENTOO_PYTHON_PROCESS_NAME=") + strlen(script_name)));
- sprintf(script_name_variable, "GENTOO_PYTHON_PROCESS_NAME=%s", script_name);
- putenv(script_name_variable);
-#endif
- }
-
- const char* path = find_path(argv[0]);
- if (path)
- {
- argv[0] = dir_cat(path, EPYTHON);
- execv(argv[0], argv);
- /* If this failed, then just search the PATH. */
- }
-
- argv[0] = (char*) EPYTHON;
- execvp(EPYTHON, argv);
- return EXIT_ERROR;
-}
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-11-17 12:59 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-11-12 17:07 [gentoo-commits] proj/eselect-python:mgornys-hackery commit in: / Michał Górny
-- strict thread matches above, loose matches on Subject: below --
2015-11-17 18:36 [gentoo-commits] proj/eselect-python:master " Michał Górny
2015-11-17 12:59 ` [gentoo-commits] proj/eselect-python:mgornys-hackery " Michał Górny
2015-11-17 18:36 [gentoo-commits] proj/eselect-python:master " Michał Górny
2015-11-12 17:07 ` [gentoo-commits] proj/eselect-python:mgornys-hackery " Michał Górny
2015-11-17 18:36 [gentoo-commits] proj/eselect-python:master " Michał Górny
2015-11-12 16:53 ` [gentoo-commits] proj/eselect-python:mgornys-hackery " Michał Górny
2015-11-12 16:53 Michał Górny
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox