tests/util: Rewrite the scripts in Python
[libjio.git] / tests / util / wrap-python
blob930c13919fa8f40dedf3158641750c0f595f39ee
1 #!/usr/bin/env python
3 # Python wrapper, which makes it able to import the built (and not the
4 # installed) version of libjio.
6 # The first parameter must be the python version (2 or 3)
8 import sys
9 import os
10 import glob
13 if len(sys.argv) < 2 or sys.argv[1] not in ("2", "3"):
14 sys.stderr.write("Error: the first argument must be the " +
15 "version (2 or 3)\n")
16 sys.exit(1)
18 py_ver = sys.argv[1]
21 # Find the path where the library was built and add it to the lookup paths, so
22 # we run against it
23 lib_bin = os.path.dirname(sys.argv[0]) + "/../../libjio/build/libjio.so"
25 if not os.path.exists(lib_bin):
26 sys.stderr.write("Can't find library (run make)\n")
27 sys.exit(1)
29 lib_path = os.path.dirname(os.path.abspath(lib_bin))
30 os.environ["LD_LIBRARY_PATH"] = ":".join([lib_path, \
31 os.environ.get("LD_LIBRARY_PATH", "")])
34 # Find out the corresponding module path for the desired python version. The
35 # path must be absolute
36 mod_bins = glob.glob(os.path.dirname(sys.argv[0]) +
37 "/../../bindings/python/build/lib*-%s.*/libjio.so" \
38 % py_ver)
39 if not mod_bins:
40 sys.stderr.write(("Can't find python%s bindings, run " +
41 "make python%s\n") % (py_ver, py_ver))
42 sys.exit(1)
44 if len(mod_bins) > 1:
45 sys.stderr.write("Found too many matching python%s bindings" \
46 % py_ver)
47 sys.exit(1)
49 mod_path = os.path.dirname(os.path.abspath(mod_bins[0]))
50 os.environ["PYTHONPATH"] = ":".join([mod_path,
51 os.environ.get("PYTHONPATH", "")])
53 if py_ver == '2':
54 py_bin = "python"
55 else:
56 py_bin = "python3"
58 os.execvp(py_bin, [py_bin] + sys.argv[2:])