diff --git a/Makefile.am b/Makefile.am
index 031c860ae..f06792fa2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,6 @@
ACLOCAL_AMFLAGS = -I m4
#DISTCHECK_CONFIGURE_FLAGS = --enable-tests
-SUBDIRS = include zlib src examples test
+SUBDIRS = include zlib src examples test bindings/python
EXTRA_DIST = autotool.sh docs/manual.html docs/manual.rst docs/extension_protocol.rst \
docs/extension_protocol.html \
docs/projects.rst docs/projects.html \
diff --git a/bindings/python/Makefile.am b/bindings/python/Makefile.am
new file mode 100644
index 000000000..962ea58ed
--- /dev/null
+++ b/bindings/python/Makefile.am
@@ -0,0 +1,15 @@
+if ENABLE_PYTHON_BINDING
+
+all-local:
+ $(PYTHON) setup.py build
+
+install-exec-local:
+ $(PYTHON) setup.py install --prefix=$(DESTDIR)$(prefix)
+
+uninstall-local:
+ rm -rf $(DESTDIR)$(libdir)/python*/site-packages/*libtorrentrb*
+
+clean-local:
+ $(PYTHON) setup.py clean --all
+
+endif
diff --git a/bindings/python/setup.py b/bindings/python/setup.py
deleted file mode 100644
index 03738b5ca..000000000
--- a/bindings/python/setup.py
+++ /dev/null
@@ -1,39 +0,0 @@
-#!/usr/bin/env python
-
-from distutils.core import setup, Extension
-import commands
-import os
-
-def pkgconfig(*packages, **kw):
- flag_map = {'-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries' }
- for token in commands.getoutput("pkg-config --libs --cflags %s" % ' '.join(packages)).split():
- if flag_map.has_key(token[:2]):
- kw.setdefault(flag_map.get(token[:2]), []).append(token[2:])
- else: # throw others to extra_link_args
- kw.setdefault('extra_link_args', []).append(token)
- for k, v in kw.iteritems(): # remove duplicated
- kw[k] = list(set(v))
- return kw
-
-def build_extension():
- this_dir = os.path.dirname(__file__)
- source_list = os.listdir(os.path.join(this_dir, "src"))
- source_list = [os.path.join("src", s) for s in source_list if s.endswith(".cpp")]
-
- libtorrent_pkg_config = pkgconfig('libtorrent-rasterbar', libraries=['boost_python-mt'])
-
- return Extension(
- 'libtorrent',
- sources = source_list,
- **libtorrent_pkg_config
- )
-
-libtorrent_extension = build_extension()
-
-setup( name = 'py-libtorrent',
- version = '0.14',
- description = 'Python bindings for libtorrent (rasterbar)',
- author = 'Arvid Norberg',
- url = 'http://www.rasterbar.com/products/libtorrent/index.html',
- ext_modules = [libtorrent_extension]
-)
diff --git a/bindings/python/setup.py.in b/bindings/python/setup.py.in
new file mode 100644
index 000000000..870c1588b
--- /dev/null
+++ b/bindings/python/setup.py.in
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+
+from distutils.core import setup, Extension
+import os
+import platform
+
+def parse_cmd(cmdline, prefix, keep_prefix = False):
+ ret = []
+ for token in cmdline.split():
+ if token[:len(prefix)] == prefix:
+ if keep_prefix:
+ ret.append(token)
+ else:
+ ret.append(token[len(prefix):])
+ return ret
+
+def arch():
+ if platform.system() != 'Darwin': return []
+ a = os.uname()[4]
+ if a == 'Power Macintosh': a = 'ppc'
+ return ['-arch', a]
+
+
+source_list = os.listdir(os.path.join(os.path.dirname(__file__), "src"))
+source_list = [os.path.join("src", s) for s in source_list if s.endswith(".cpp")]
+
+extra_cmd = '@COMPILETIME_OPTIONS@ @CPPFLAGS@'
+
+setup( name='python-@PACKAGE_NAME@',
+ version='@PACKAGE_VERSION@',
+ author = 'Arvid Norberg',
+ author_email='@PACKAGE_BUGREPORT@',
+ description = 'Python bindings for libtorrent-rasterbar',
+ long_description = 'Python bindings for libtorrent-rasterbar',
+ url = 'http://www.rasterbar.com/products/libtorrent/index.html',
+ platforms = 'any',
+ license = 'Boost Software License - Version 1.0 - August 17th, 2003',
+ ext_modules = [Extension('libtorrent',
+ sources = source_list,
+ language='c++',
+ include_dirs = ['@top_srcdir@/include','@top_srcdir@/include/libtorrent'] + parse_cmd(extra_cmd, '-I'),
+ library_dirs = ['@top_builddir@/src/.libs'] + parse_cmd(extra_cmd, '-L'),
+ extra_link_args = '@LDFLAGS@'.split() + arch(),
+ extra_compile_args = parse_cmd(extra_cmd, '-D', True) + arch(),
+ libraries = ['torrent-rasterbar','@BOOST_PYTHON_LIB@'] + parse_cmd(extra_cmd, '-l'))],
+)
diff --git a/build_dist.sh b/build_dist.sh
index 74373d37a..7741f321c 100755
--- a/build_dist.sh
+++ b/build_dist.sh
@@ -8,6 +8,6 @@ chmod a-x docs/*.rst docs/*.htm*
/sw/bin/libtoolize -f
/opt/local/bin/automake -ac
/opt/local/bin/autoconf
-./configure --with-zlib=shipped --enable-examples=yes --enable-tests=yes --with-boost-thread=mt-1_35 --with-boost-system=mt-1_35 --with-boost-filesystem=mt-1_35 --with-boost-regex=mt-1_35 --with-boost-program-options=mt-1_35
+./configure --enable-python-binding --with-zlib=shipped --enable-examples=yes --enable-tests=yes --with-boost-python=mt-1_35 --with-boost-thread=mt-1_35 --with-boost-system=mt-1_35 --with-boost-filesystem=mt-1_35 --with-boost-regex=mt-1_35 --with-boost-program-options=mt-1_35
make dist check
diff --git a/configure.in b/configure.in
index 4632e7867..91f08b72d 100644
--- a/configure.in
+++ b/configure.in
@@ -313,6 +313,55 @@ dnl make ZLIB and ZLIBDIR visible to Makefiles
AC_SUBST([ZLIB])
AC_SUBST([ZLIBINCL])
+dnl Check whether python bindings should be build
+AC_ARG_ENABLE(
+ [python-binding],
+ AS_HELP_STRING([--enable-python-binding],[Build python binding. Default is to not build them.]),
+ [[ac_python_binding=$enableval]],
+ [[ac_python_binding=no]]
+)
+
+AC_MSG_CHECKING([if python bindings should be build])
+case "$ac_python_binding" in
+ "yes")
+ AC_MSG_RESULT(yes)
+
+ AM_PATH_PYTHON([2.5],,AC_MSG_ERROR([Python not found. Python is required to build python binding.]))
+
+ PYTHON_CONFIG=`type -p python$PYTHON_VERSION-config`
+ if test "$PYTHON_CONFIG" != ""; then
+ PYTHON_CFLAGS=`$PYTHON_CONFIG --cflags` dnl --includes
+ PYTHON_LIBS=`$PYTHON_CONFIG --libs`
+ else
+ PYTHON_CFLAGS=``
+ PYTHON_LIBS=``
+ fi
+ AC_SUBST(PYTHON_CFLAGS)
+ AC_SUBST(PYTHON_LIBS)
+
+ AX_BOOST_PYTHON
+ dnl check that Boost.Python was found:
+ if test -z "$BOOST_PYTHON_LIB"; then
+ AC_MSG_ERROR([Unable to find Boost.Python library, this is required to build python bindings.])
+ fi
+
+ AM_CONDITIONAL([ENABLE_PYTHON_BINDING],true)
+
+ ;;
+ "no")
+ AC_MSG_RESULT(no)
+ AM_CONDITIONAL([ENABLE_PYTHON_BINDING],false)
+ ;;
+ "")
+ AC_MSG_RESULT(no)
+ AM_CONDITIONAL([ENABLE_PYTHON_BINDING],false)
+ ;;
+ *)
+ AC_MSG_RESULT()
+ AC_MSG_ERROR([Unknown --enable-python option "$python". Use either "yes" or "no".])
+ ;;
+esac
+
dnl Check whether the examples should be build
AC_ARG_ENABLE(
[examples],
@@ -401,4 +450,12 @@ AC_SUBST(DEBUGFLAGS)
dnl Compile time options.
AC_SUBST(COMPILETIME_OPTIONS)
-AC_OUTPUT([Makefile src/Makefile include/Makefile zlib/Makefile examples/Makefile test/Makefile libtorrent-rasterbar.pc])
+AC_OUTPUT([Makefile
+ src/Makefile
+ include/Makefile
+ zlib/Makefile
+ examples/Makefile
+ test/Makefile
+ bindings/python/Makefile
+ bindings/python/setup.py
+ libtorrent-rasterbar.pc])
diff --git a/m4/ax_boost_python-fixed.m4 b/m4/ax_boost_python-fixed.m4
new file mode 100644
index 000000000..188c7a7aa
--- /dev/null
+++ b/m4/ax_boost_python-fixed.m4
@@ -0,0 +1,99 @@
+# ===========================================================================
+# http://autoconf-archive.cryp.to/ax_boost_python.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+# AX_BOOST_PYTHON
+#
+# DESCRIPTION
+#
+# This macro checks to see if the Boost.Python library is installed. It
+# also attempts to guess the currect library name using several attempts.
+# It tries to build the library name using a user supplied name or suffix
+# and then just the raw library.
+#
+# If the library is found, HAVE_BOOST_PYTHON is defined and
+# BOOST_PYTHON_LIB is set to the name of the library.
+#
+# This macro calls AC_SUBST(BOOST_PYTHON_LIB).
+#
+# In order to ensure that the Python headers are specified on the include
+# path, this macro requires AX_PYTHON to be called.
+#
+# LAST MODIFICATION
+#
+# 2008-04-12
+#
+# COPYLEFT
+#
+# Copyright (c) 2008 Michael Tindal
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+# Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program. If not, see .
+#
+# As a special exception, the respective Autoconf Macro's copyright owner
+# gives unlimited permission to copy, distribute and modify the configure
+# scripts that are the output of Autoconf when processing the Macro. You
+# need not follow the terms of the GNU General Public License when using
+# or distributing such scripts, even though portions of the text of the
+# Macro appear in them. The GNU General Public License (GPL) does govern
+# all other use of the material that constitutes the Autoconf Macro.
+#
+# This special exception to the GPL applies to versions of the Autoconf
+# Macro released by the Autoconf Macro Archive. When you make and
+# distribute a modified version of the Autoconf Macro, you may extend this
+# special exception to the GPL to apply to your modified version as well.
+
+AC_DEFUN([AX_BOOST_PYTHON],
+[AC_REQUIRE([AX_PYTHON])dnl
+AC_CACHE_CHECK(whether the Boost::Python library is available,
+ac_cv_boost_python,
+[AC_LANG_SAVE
+ AC_LANG_CPLUSPLUS
+ CPPFLAGS_SAVE=$CPPFLAGS
+ if test x$PYTHON_INCLUDE_DIR != x; then
+ CPPFLAGS="-I$PYTHON_INCLUDE_DIR $CPPFLAGS"
+ fi
+ AC_COMPILE_IFELSE(AC_LANG_PROGRAM([[
+ #include
+ using namespace boost::python;
+ BOOST_PYTHON_MODULE(test) { throw "Boost::Python test."; }]],
+ [[return 0;]]),
+ ac_cv_boost_python=yes, ac_cv_boost_python=no)
+ AC_LANG_RESTORE
+ CPPFLAGS=$CPPFLAGS_SAVE
+])
+if test "$ac_cv_boost_python" = "yes"; then
+ AC_DEFINE(HAVE_BOOST_PYTHON,,[define if the Boost::Python library is available])
+ dnl
+ LDFLAGS_SAVE=$LDFLAGS
+ if test x$PYTHON_LIB != x; then
+ LDFLAGS="$LDFLAGS -l$PYTHON_LIB"
+ fi
+ dnl
+ ax_python_lib=boost_python
+ AC_ARG_WITH([boost-python],AS_HELP_STRING([--with-boost-python],[specify the boost python library or suffix to use]),
+ [if test "x$with_boost_python" != "xno"; then
+ ax_python_lib="$with_boost_python"
+ ax_boost_python_lib="boost_python-$with_boost_python"
+ fi])
+ for ax_lib in $ax_python_lib $ax_boost_python_lib boost_python; do
+ AC_CHECK_LIB($ax_lib, main, [BOOST_PYTHON_LIB=$ax_lib break])
+ done
+ dnl
+ LDFLAGS=$LDFLAGS_SAVE
+ dnl
+ AC_SUBST(BOOST_PYTHON_LIB)
+fi
+])dnl
diff --git a/m4/ax_python.m4 b/m4/ax_python.m4
new file mode 100644
index 000000000..54433e4ef
--- /dev/null
+++ b/m4/ax_python.m4
@@ -0,0 +1,99 @@
+# ===========================================================================
+# http://autoconf-archive.cryp.to/ax_python.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+# AX_PYTHON
+#
+# DESCRIPTION
+#
+# This macro does a complete Python development environment check.
+#
+# It recurses through several python versions (from 2.1 to 2.5 in this
+# version), looking for an executable. When it finds an executable, it
+# looks to find the header files and library.
+#
+# It sets PYTHON_BIN to the name of the python executable,
+# PYTHON_INCLUDE_DIR to the directory holding the header files, and
+# PYTHON_LIB to the name of the Python library.
+#
+# This macro calls AC_SUBST on PYTHON_BIN (via AC_CHECK_PROG),
+# PYTHON_INCLUDE_DIR and PYTHON_LIB.
+#
+# LAST MODIFICATION
+#
+# 2008-04-12
+#
+# COPYLEFT
+#
+# Copyright (c) 2008 Michael Tindal
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+# Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program. If not, see .
+#
+# As a special exception, the respective Autoconf Macro's copyright owner
+# gives unlimited permission to copy, distribute and modify the configure
+# scripts that are the output of Autoconf when processing the Macro. You
+# need not follow the terms of the GNU General Public License when using
+# or distributing such scripts, even though portions of the text of the
+# Macro appear in them. The GNU General Public License (GPL) does govern
+# all other use of the material that constitutes the Autoconf Macro.
+#
+# This special exception to the GPL applies to versions of the Autoconf
+# Macro released by the Autoconf Macro Archive. When you make and
+# distribute a modified version of the Autoconf Macro, you may extend this
+# special exception to the GPL to apply to your modified version as well.
+
+AC_DEFUN([AX_PYTHON],
+[AC_MSG_CHECKING(for python build information)
+AC_MSG_RESULT([])
+for python in python2.5 python2.4 python2.3 python2.2 python2.1 python; do
+AC_CHECK_PROGS(PYTHON_BIN, [$python])
+ax_python_bin=$PYTHON_BIN
+if test x$ax_python_bin != x; then
+ AC_CHECK_LIB($ax_python_bin, main, ax_python_lib=$ax_python_bin, ax_python_lib=no)
+ AC_CHECK_HEADER([$ax_python_bin/Python.h],
+ [[ax_python_header=`locate $ax_python_bin/Python.h | sed -e s,/Python.h,,`]],
+ ax_python_header=no)
+ if test $ax_python_lib != no; then
+ if test $ax_python_header != no; then
+ break;
+ fi
+ fi
+fi
+done
+if test x$ax_python_bin = x; then
+ ax_python_bin=no
+fi
+if test x$ax_python_header = x; then
+ ax_python_header=no
+fi
+if test x$ax_python_lib = x; then
+ ax_python_lib=no
+fi
+
+AC_MSG_RESULT([ results of the Python check:])
+AC_MSG_RESULT([ Binary: $ax_python_bin])
+AC_MSG_RESULT([ Library: $ax_python_lib])
+AC_MSG_RESULT([ Include Dir: $ax_python_header])
+
+if test x$ax_python_header != xno; then
+ PYTHON_INCLUDE_DIR=$ax_python_header
+ AC_SUBST(PYTHON_INCLUDE_DIR)
+fi
+if test x$ax_python_lib != xno; then
+ PYTHON_LIB=$ax_python_lib
+ AC_SUBST(PYTHON_LIB)
+fi
+])dnl