git » libfiu » commit 83fb1e6

Properly wrap python to use the built bindings

author Alberto Bertogli
2012-04-03 20:59:31 UTC
committer Alberto Bertogli
2012-04-03 20:59:31 UTC
parent 65614be02e3952b7ad47843156d2b480f998d01e

Properly wrap python to use the built bindings

The Python tests should run using the built bindings, not the installed ones.

This patch adds a wrapper to do the invocation of the python binary with the
appropriate environment variables to make that happen.

Thanks to Chris Lamb for reporting this issue.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

tests/Makefile +3 -6
tests/wrap-python +59 -0

diff --git a/tests/Makefile b/tests/Makefile
index 9fb7875..40a22a9 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -14,14 +14,11 @@ endif
 ifneq ($(V), 1)
 	NICE_CC = @echo "  CC  $@"; $(CC)
 	NICE_RUN = @echo "  RUN $<"; LD_LIBRARY_PATH=../libfiu/
-	NICE_PY_RUN = @echo "  PY  $<"; \
-		      LD_LIBRARY_PATH=../libfiu/ \
-		      PYTHONPATH=../bindings/python/
+	NICE_PY = @echo "  PY  $<"; ./wrap-python 2
 else
 	NICE_CC = $(CC)
 	NICE_RUN = LD_LIBRARY_PATH=../libfiu/
-	NICE_PY_RUN = LD_LIBRARY_PATH=../libfiu/ \
-		      PYTHONPATH=../bindings/python/
+	NICE_PY = ./wrap-python 2
 endif
 
 default: tests
@@ -69,7 +66,7 @@ PY_TESTS := $(wildcard test-*.py)
 py-tests: $(patsubst %.py,py-run-%,$(PY_TESTS))
 
 py-run-%: %.py
-	$(NICE_PY_RUN) python ./$<
+	$(NICE_PY) ./$<
 
 
 clean:
diff --git a/tests/wrap-python b/tests/wrap-python
new file mode 100755
index 0000000..aab0667
--- /dev/null
+++ b/tests/wrap-python
@@ -0,0 +1,59 @@
+#!/usr/bin/env python
+
+# Python wrapper, which makes it able to import the built (and not the
+# installed) version of libfiu.
+#
+# The first parameter must be the python version (2 or 3)
+
+import sys
+import os
+import glob
+
+
+if len(sys.argv) < 2 or sys.argv[1] not in ("2", "3"):
+	sys.stderr.write("Error: the first argument must be the " +
+				"version (2 or 3)\n")
+	sys.exit(1)
+
+py_ver = sys.argv[1]
+
+
+# Find the path where the library was built and add it to the lookup paths, so
+# we run against it
+lib_bin = os.path.dirname(sys.argv[0]) + "/../libfiu/libfiu.so"
+
+if not os.path.exists(lib_bin):
+	sys.stderr.write("Can't find library (run make)\n")
+	sys.exit(1)
+
+lib_path = os.path.dirname(os.path.abspath(lib_bin))
+os.environ["LD_LIBRARY_PATH"] = ":".join([lib_path, \
+				os.environ.get("LD_LIBRARY_PATH", "")])
+
+
+# Find out the corresponding module path for the desired python version. The
+# path must be absolute
+mod_bins = glob.glob(os.path.dirname(sys.argv[0]) +
+			"/../bindings/python/build/lib*-%s.*/fiu_ll.so" \
+				% py_ver)
+if not mod_bins:
+	sys.stderr.write(("Can't find python%s bindings, run " +
+				"make python%s\n") % (py_ver, py_ver))
+	sys.exit(1)
+
+if len(mod_bins) > 1:
+	sys.stderr.write("Found too many matching python%s bindings" \
+				% py_ver)
+	sys.exit(1)
+
+mod_path = os.path.dirname(os.path.abspath(mod_bins[0]))
+os.environ["PYTHONPATH"] = ":".join([mod_path,
+					os.environ.get("PYTHONPATH", "")])
+
+if py_ver == '2':
+	py_bin = "python"
+else:
+	py_bin = "python3"
+
+os.execvp(py_bin, [py_bin] + sys.argv[2:])
+