python: Build for both Python2 and Python3.
authorJustus Winter <justus@g10code.com>
Wed, 14 Sep 2016 08:22:07 +0000 (10:22 +0200)
committerJustus Winter <justus@g10code.com>
Wed, 14 Sep 2016 10:18:15 +0000 (12:18 +0200)
* NEWS: Update.
* configure.ac: Check for multiple Python versions.
* lang/python/Makefile.am: Build and install for both Python versions.
* lang/python/tests/Makefile.am: Test both versions.
* lang/python/tests/run-tests.py: New test runner.

Signed-off-by: Justus Winter <justus@g10code.com>
NEWS
configure.ac
lang/python/Makefile.am
lang/python/tests/Makefile.am
lang/python/tests/run-tests.py [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 621ce3f..e428bd5 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -5,7 +5,7 @@ Noteworthy changes in version 1.7.0 (unreleased) [C25/A14/R_]
 
  * Notation flags are now correctly set on verify.
 
- * Bindings for Python 3 are now included.
+ * Bindings for Python 2 and 3 are now included.
 
  * New global flag "require-gnupg" to set a minimal gnupg version.
 
index bac7908..bcac13f 100644 (file)
@@ -176,7 +176,7 @@ have_w32_system=no
 have_w64_system=no
 build_w32_glib=no
 build_w32_qt=no
-available_languages="cl cpp python qt"
+available_languages="cl cpp python python2 python3 qt"
 default_languages="cl cpp python qt"
 case "${host}" in
     x86_64-*mingw32*)
@@ -369,8 +369,13 @@ fi
 AC_SUBST(HAVE_DOT)
 
 # Python bindings.
+LIST_MEMBER("python2", $enabled_languages)
+found_py2=$found
+LIST_MEMBER("python3", $enabled_languages)
+found_py3=$found
 LIST_MEMBER("python", $enabled_languages)
-if test "$found" = "1"; then
+found_py=$found
+if test "$found_py" = "1" -o "$found_py2" = "1" -o "$found_py3" = "1"; then
     AX_PKG_SWIG
     if test -z "$SWIG"; then
         if test "$explicit_languages" = "1"; then
@@ -382,11 +387,49 @@ if test "$found" = "1"; then
             enabled_languages=$(echo $enabled_languages | sed 's/python//')
         fi
     else
-        AM_PATH_PYTHON([3.4])
-        AX_SWIG_PYTHON
-       if test -z "$PYTHON_VERSION"; then
-           if test "$explicit_languages" = "1"; then
-              AC_MSG_ERROR([[
+        # Reset all the stuff, just to be sure.
+       PYTHONS=
+       PYTHON_VERSIONS=
+       unset PYTHON
+       unset PYTHON_VERSION
+       unset ac_cv_path_PYTHON
+       unset am_cv_pathless_PYTHON
+       unset am_cv_python_version
+       unset am_cv_python_platform
+       unset am_cv_python_pythondir
+       unset am_cv_python_pyexecdir
+
+       if test "$found_py" = "1" -o "$found_py2" = "1"; then
+           AM_PATH_PYTHON([2.7])
+           if test "$PYTHON"; then
+               PYTHONS="$(echo $PYTHONS $PYTHON)"
+               PYTHON_VERSIONS="$(echo $PYTHON_VERSIONS $PYTHON_VERSION)"
+           fi
+       fi
+
+       if test "$found_py" = "1" -o "$found_py3" = "1"; then
+           # Reset everything, so that we can look for another Python.
+           unset PYTHON
+           unset PYTHON_VERSION
+           unset ac_cv_path_PYTHON
+           unset am_cv_pathless_PYTHON
+           unset am_cv_python_version
+           unset am_cv_python_platform
+           unset am_cv_python_pythondir
+           unset am_cv_python_pyexecdir
+           AM_PATH_PYTHON([3.4])
+           if test "$PYTHON"; then
+               PYTHONS="$(echo $PYTHONS $PYTHON)"
+               PYTHON_VERSIONS="$(echo $PYTHON_VERSIONS $PYTHON_VERSION)"
+           fi
+       fi
+
+       if test "$PYTHON_VERSIONS"; then
+          enabled_languages_v=$(echo $enabled_languages | sed "s/python\([[23]]\)\?/python ($PYTHON_VERSIONS)/")
+          enabled_languages=$(echo $enabled_languages | sed "s/python\([[23]]\)\?/python/")
+       else
+            if test "$explicit_languages" = "1"; then
+                AC_MSG_ERROR([[
 ***
 *** Please install the python development packages.
 ***]])
@@ -394,6 +437,9 @@ if test "$found" = "1"; then
                 enabled_languages=$(echo $enabled_languages | sed 's/python//')
             fi
        fi
+
+       AC_SUBST(PYTHONS, $PYTHONS)
+       AC_SUBST(PYTHON_VERSIONS, $PYTHON_VERSIONS)
     fi
 fi
 
@@ -843,7 +889,7 @@ echo "
         FD Passing:        $use_descriptor_passing
         GPGME Pthread:     $have_pthread
 
-        Language bindings: $enabled_languages
+        Language bindings: ${enabled_languages_v:-$enabled_languages}
 "
 if test "x${gpg_config_script_warn}" != x; then
 cat <<G10EOF
index 8e18dab..9ec9c4a 100644 (file)
@@ -54,8 +54,10 @@ copystamp: $(COPY_FILES) $(COPY_FILES_PYME)
        touch $@
 
 all-local: copystamp
-       CFLAGS="$(CFLAGS) -I$(top_srcdir)" \
-         $(PYTHON) setup.py build --verbose
+       for PYTHON in $(PYTHONS); do \
+         CFLAGS="$(CFLAGS) -I$(top_srcdir)" \
+           $$PYTHON setup.py build --verbose ; \
+       done
 
 dist/pyme3-$(VERSION).tar.gz dist/pyme3-$(VERSION).tar.gz.asc: copystamp
        CFLAGS="$(CFLAGS) -I$(top_srcdir)" \
@@ -87,10 +89,16 @@ clean-local:
        fi
 
 install-exec-local:
-       $(PYTHON) setup.py install \
+       rm -f install_files.txt
+       for PYTHON in $(PYTHONS); do \
+         $$PYTHON setup.py install \
          --prefix $(DESTDIR)$(prefix) \
-         --record $(DESTDIR)$(pythondir)/pyme/install_files.txt \
-         --verbose
+         --record files.txt \
+         --verbose ; \
+         cat files.txt >> install_files.txt ; \
+         rm files.txt ; \
+       done
+       mv install_files.txt $(DESTDIR)$(pythondir)/pyme
 
 uninstall-local:
        xargs <$(DESTDIR)$(pythondir)/pyme/install_files.txt -- rm -rf --
index 83c4d8e..6a315d7 100644 (file)
@@ -26,8 +26,8 @@ test_srcdir = $(top_srcdir)/tests/gpg
 TESTS_ENVIRONMENT = GNUPGHOME=$(abs_builddir) \
        LC_ALL=C GPG_AGENT_INFO= \
        top_srcdir=$(top_srcdir) \
-       LD_LIBRARY_PATH="../../../src/.libs:$(LD_LIBRARY_PATH)" \
-       PYTHONPATH=`echo $(abs_builddir)/../build/lib.*`
+       srcdir=$(srcdir) \
+       LD_LIBRARY_PATH="../../../src/.libs:$(LD_LIBRARY_PATH)"
 
 py_tests = t-wrapper.py \
        t-callbacks.py \
@@ -52,8 +52,23 @@ py_tests = t-wrapper.py \
        t-idiomatic.py \
        t-protocol-assuan.py
 
-TESTS = initial.py $(py_tests) final.py
-EXTRA_DIST = support.py $(TESTS) encrypt-only.asc sign-only.asc
+XTESTS = initial.py $(py_tests) final.py
+EXTRA_DIST = support.py $(XTESTS) encrypt-only.asc sign-only.asc \
+             run-tests.py
+
+# XXX: Currently, one cannot override automake's 'check' target.  As a
+# workaround, we avoid defining 'TESTS', thus automake will not emit
+# the 'check' target.  For extra robustness, we merely define a
+# dependency on 'xcheck', so this hack should also work even if
+# automake would emit the 'check' target, as adding dependencies to
+# targets is okay.
+check: xcheck
+
+.PHONY: xcheck
+xcheck:
+       $(TESTS_ENVIRONMENT) $(PYTHON) $(srcdir)/run-tests.py \
+         --interpreters="$(PYTHONS)" --srcdir=$(srcdir) $(TESTFLAGS) \
+         $(XTESTS)
 
 CLEANFILES = secring.gpg pubring.gpg pubring.kbx trustdb.gpg dirmngr.conf \
        gpg-agent.conf pubring.kbx~ gpg.conf pubring.gpg~ \
diff --git a/lang/python/tests/run-tests.py b/lang/python/tests/run-tests.py
new file mode 100644 (file)
index 0000000..55d3f11
--- /dev/null
@@ -0,0 +1,90 @@
+#!/usr/bin/env python
+
+# Copyright (C) 2016 g10 Code GmbH
+#
+# This file is part of GPGME.
+#
+# GPGME 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.
+#
+# GPGME 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 Lesser General
+# Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+from __future__ import unicode_literals
+
+import argparse
+import glob
+import os
+import subprocess
+import sys
+
+class SplitAndAccumulate(argparse.Action):
+    def __call__(self, parser, namespace, values, option_string=None):
+        current = getattr(namespace, self.dest, list())
+        current.extend(values.split())
+        setattr(namespace, self.dest, current)
+
+parser = argparse.ArgumentParser(description='Run tests.')
+parser.add_argument('tests', metavar='TEST', type=str, nargs='+',
+                    help='A test to run')
+parser.add_argument('-v', '--verbose', action="store_true", default=False,
+                    help='Be verbose.')
+parser.add_argument('--interpreters', metavar='PYTHON', type=str,
+                    default=[], action=SplitAndAccumulate,
+                    help='Use these interpreters to run the tests, ' +
+                    'separated by spaces.')
+parser.add_argument('--srcdir', type=str,
+                    default=os.environ.get("srcdir", ""),
+                    help='Location of the tests.')
+parser.add_argument('--builddir', type=str,
+                    default=os.environ.get("abs_builddir", ""),
+                    help='Location of the tests.')
+
+args = parser.parse_args()
+if not args.interpreters:
+    args.interpreters = [sys.executable]
+
+out = sys.stdout if args.verbose else None
+err = sys.stderr if args.verbose else None
+
+def status_to_str(code):
+    return {0: "PASS", 77: "SKIP", 99: "ERROR"}.get(code, "FAIL")
+
+results = list()
+for interpreter in args.interpreters:
+    version = subprocess.check_output(
+        [interpreter, "-c", "import sys; print('{0}.{1}'.format(sys.version_info[0], sys.version_info[1]))"]).strip().decode()
+
+    builddirs = glob.glob(os.path.join(args.builddir, "..", "build",
+                                       "lib*"+version))
+    assert len(builddirs) == 1, \
+        "Expected one build directory, got {0}".format(builddirs)
+    env = dict(os.environ)
+    env["PYTHONPATH"] = builddirs[0]
+
+    print("Running tests using {0} ({1})...".format(interpreter, version))
+    for test in args.tests:
+        status = subprocess.call(
+            [interpreter, os.path.join(args.srcdir, test)],
+            env=env, stdout=out, stderr=err)
+        print("{0}: {1}".format(status_to_str(status), test))
+        results.append(status)
+
+def count(status):
+    return len(list(filter(lambda x: x == status, results)))
+def failed():
+    return len(list(filter(lambda x: x not in (0, 77, 99), results)))
+
+print("{0} tests run, {1} succeeded, {2} failed, {3} skipped.".format(
+    len(results), count(0), failed(), count(77)))
+sys.exit(len(results) - count(0))