Skip to content

Commit

Permalink
Python: We need to migrate from distutils to sysconfig. (dm)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveMielke committed Apr 15, 2021
1 parent 3305fae commit e6ed237
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 78 deletions.
1 change: 0 additions & 1 deletion Bindings/Python/Makefile.in
Expand Up @@ -21,7 +21,6 @@ include $(SRC_TOP)bindings.mk

PYTHON_OK = @PYTHON_OK@
PYTHON = @PYTHON@
PYTHON_PROLOGUE = @PYTHON_PROLOGUE@
PYTHON_VERSION = @PYTHON_VERSION@
PYTHON_CPPFLAGS = @PYTHON_CPPFLAGS@
PYTHON_LIBS = @PYTHON_LIBS@
Expand Down
137 changes: 60 additions & 77 deletions Bindings/Python/bindings.m4
Expand Up @@ -31,100 +31,79 @@ PYTHON_OK=true
export PYTHONCOERCECLOCALE=0
AC_PATH_PROGS([PYTHON], [python3 python python2], [python])
if test -z "${PYTHON}"
if test -n "${PYTHON}"
then
AC_MSG_WARN([Python interpreter not found])
PYTHON_OK=false
else
PYTHON_PROLOGUE=""
for python_module in sys distutils.sysconfig
do
python_error="`"${PYTHON}" -c "import ${python_module};" 2>&1`"
if test -n "${python_error}"
then
AC_MSG_WARN([Python module not found: ${python_module}: ${python_error}])
test -n "${PYTHON_VERSION}" || {
BRLTTY_PYTHON_QUERY([PYTHON_VERSION], [version])
test -n "${PYTHON_VERSION}" || {
AC_MSG_WARN([Python version not defined])
PYTHON_OK=false
else
PYTHON_PROLOGUE="${PYTHON_PROLOGUE}import ${python_module}; "
fi
done
AC_SUBST([PYTHON_PROLOGUE])
}
}
AC_SUBST([PYTHON_VERSION])
if "${PYTHON_OK}"
then
if test -z "${PYTHON_VERSION}"
then
[PYTHON_VERSION="`"${PYTHON}" -c "${PYTHON_PROLOGUE} print(sys.version_info[0]);"`"]
if test -z "${PYTHON_VERSION}"
then
AC_MSG_WARN([Python version not defined])
fi
fi
AC_SUBST([PYTHON_VERSION])
test -n "${PYTHON_CPPFLAGS}" || {
BRLTTY_PYTHON_QUERY([python_include_directory], [incdir])
if test -z "${PYTHON_CPPFLAGS}"
if test -n "${python_include_directory}"
then
[python_include_directory="`"${PYTHON}" -c "${PYTHON_PROLOGUE} print(distutils.sysconfig.get_python_inc());"`"]
if test -z "${python_include_directory}"
then
AC_MSG_WARN([Python include directory not found])
PYTHON_CPPFLAGS="-I${python_include_directory}"
test -f "${python_include_directory}/Python.h" || {
AC_MSG_WARN([Python developer environment not installed])
PYTHON_OK=false
else
PYTHON_CPPFLAGS="-I${python_include_directory}"
if test ! -f "${python_include_directory}/Python.h"
then
AC_MSG_WARN([Python developer environment not installed])
PYTHON_OK=false
fi
fi
}
else
AC_MSG_WARN([Python include directory not found])
PYTHON_OK=false
fi
AC_SUBST([PYTHON_CPPFLAGS])
}
AC_SUBST([PYTHON_CPPFLAGS])
if test -z "${PYTHON_LIBS}"
then
PYTHON_LIBS="-lpython${PYTHON_VERSION}"
[python_library_directory="`"${PYTHON}" -c "${PYTHON_PROLOGUE} print(distutils.sysconfig.get_python_lib(0,1));"`"]
if test -z "${python_library_directory}"
then
AC_MSG_WARN([Python library directory not found])
else
PYTHON_LIBS="-L${python_library_directory}/config ${PYTHON_LIBS}"
fi
fi
AC_SUBST([PYTHON_LIBS])
test -n "${PYTHON_LIBS}" || {
PYTHON_LIBS="-lpython${PYTHON_VERSION}"
BRLTTY_PYTHON_QUERY([python_library_directory], [libdir])
if test -z "${PYTHON_EXTRA_LIBS}"
if test -n "${python_library_directory}"
then
[PYTHON_EXTRA_LIBS="`"${PYTHON}" -c "${PYTHON_PROLOGUE} print(distutils.sysconfig.get_config_var('LOCALMODLIBS'), distutils.sysconfig.get_config_var('LIBS'));"`"]
PYTHON_LIBS="-L${python_library_directory}/config ${PYTHON_LIBS}"
else
AC_MSG_WARN([Python library directory not found])
PYTHON_OK=false
fi
AC_SUBST([PYTHON_EXTRA_LIBS])
}
AC_SUBST([PYTHON_LIBS])
if test -z "${PYTHON_EXTRA_LDFLAGS}"
then
[PYTHON_EXTRA_LDFLAGS="`"${PYTHON}" -c "${PYTHON_PROLOGUE} print(distutils.sysconfig.get_config_var('LINKFORSHARED'));"`"]
fi
AC_SUBST([PYTHON_EXTRA_LDFLAGS])
test -n "${PYTHON_EXTRA_LIBS}" || {
BRLTTY_PYTHON_QUERY([PYTHON_EXTRA_LIBS], [libopts])
}
AC_SUBST([PYTHON_EXTRA_LIBS])
if test -z "${PYTHON_SITE_PKG}"
then
[PYTHON_SITE_PKG="`"${PYTHON}" -c "${PYTHON_PROLOGUE} print(distutils.sysconfig.get_python_lib(1,0));"`"]
if test -z "${PYTHON_SITE_PKG}"
then
AC_MSG_WARN([Python package directory not found])
fi
fi
AC_SUBST([PYTHON_SITE_PKG])
fi
test -n "${PYTHON_EXTRA_LDFLAGS}" || {
BRLTTY_PYTHON_QUERY([PYTHON_EXTRA_LDFLAGS], [linkopts])
}
AC_SUBST([PYTHON_EXTRA_LDFLAGS])
test -n "${PYTHON_SITE_PKG}" || {
BRLTTY_PYTHON_QUERY([PYTHON_SITE_PKG], [pkgdir])
test -n "${PYTHON_SITE_PKG}" || {
AC_MSG_WARN([Python packages directory not found])
PYTHON_OK=false
}
}
AC_SUBST([PYTHON_SITE_PKG])
else
AC_MSG_WARN([Python interpreter not found])
PYTHON_OK=false
fi
AC_PATH_PROGS([CYTHON], [cython cython3])
if test -z "${CYTHON}"
then
AC_PATH_PROGS([CYTHON], [cython3 cython])
test -n "${CYTHON}" || {
AC_MSG_WARN([Cython compiler not found])
PYTHON_OK=false
fi
}
if test "${GCC}" = "yes"
then
Expand All @@ -141,3 +120,7 @@ AC_SUBST([CYTHON_CFLAGS])
AC_SUBST([PYTHON_OK])
])

AC_DEFUN([BRLTTY_PYTHON_QUERY], [dnl
$1="`${PYTHON} Tools/pythoncmd $2`"
])
107 changes: 107 additions & 0 deletions Tools/pythoncmd
@@ -0,0 +1,107 @@
#!/usr/bin/env python
###############################################################################
# libbrlapi - A library providing access to braille terminals for applications.
#
# Copyright (C) 2005-2021 by
# Alexis Robert <alexissoft@free.fr>
# Samuel Thibault <Samuel.Thibault@ens-lyon.org>
#
# libbrlapi comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
#
# Web Page: http://brltty.app/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

import sys
import os

def nextProgramArgument():
return sys.argv.pop(0)

def putProgramMessage(message):
print("%s: %s" % (programName, message))

def syntaxError(message):
putProgramMessage(message)
sys.exit(2)

def verifyNoMoreProgramArguments():
if (len(sys.argv) > 0):
syntaxError("too many parameters")

try:
import sysconfig
# print("sysconfig")

def getIncludeDirectory():
return sysconfig.get_path("include")

def getLibraryDirectory():
return sysconfig.get_path("stdlib")

def getPackageDirectory():
return sysconfig.get_path("platlib")

def getConfigurationVariable(name):
return sysconfig.get_config_var(name)
except ModuleNotFoundError:
import distutils.sysconfig
# print("distutils")

def getIncludeDirectory():
return distutils.sysconfig.get_python_inc()

def getLibraryDirectory():
return distutils.sysconfig.get_python_lib(0, 1)

def getPackageDirectory():
return distutils.sysconfig.get_python_lib(True, False)

def getConfigurationVariable(name):
return distutils.sysconfig.get_config_var(name)

programPath = nextProgramArgument()
programName = os.path.basename(programPath)

class ProgramActions:
def version():
verifyNoMoreProgramArguments()
print(sys.version_info[0])

def incdir():
verifyNoMoreProgramArguments()
print(getIncludeDirectory())

def libdir():
verifyNoMoreProgramArguments()
print(getLibraryDirectory())

def pkgdir():
verifyNoMoreProgramArguments()
print(getPackageDirectory())

def libopts():
verifyNoMoreProgramArguments()
print(getConfigurationVariable("LIBS"))

def linkopts():
verifyNoMoreProgramArguments()
print(getConfigurationVariable("LINKFORSHARED"))

try:
programAction = nextProgramArgument()

try:
getattr(globals()["ProgramActions"], programAction)()
except AttributeError:
syntaxError("unknown action: %s" % programAction)
except IndexError:
syntaxError("action not specified")

sys.exit(0)

0 comments on commit e6ed237

Please sign in to comment.