meson: use new python module
authorHavard Graff <havard.graff@gmail.com>
Mon, 14 May 2018 08:05:15 +0000 (10:05 +0200)
committerMathieu Duponchelle <mathieu@centricular.com>
Sun, 28 Oct 2018 16:53:44 +0000 (17:53 +0100)
This patch makes the tests pass running uninstalled and installed, with
python2 and python3 on linux, windows and osx.

The main gist is to use the new python-module to do the lifting done
by pythondetector, and with that add support for python2 and windows.

gi/overrides/Gst.py
gi/overrides/meson.build
meson.build
meson_options.txt
plugin/meson.build
scripts/pythondetector [deleted file]
testsuite/meson.build

index 57c3489..ae18f57 100644 (file)
@@ -566,7 +566,7 @@ def TIME_ARGS(time):
                                   time % Gst.SECOND)
 __all__.append('TIME_ARGS')
 
-from . import _gi_gst
+import _gi_gst
 _gi_gst
 
 # maybe more python and less C some day if core turns a bit more introspection
index e99236a..3c5a33d 100644 (file)
@@ -2,17 +2,18 @@ pysources = ['Gst.py', 'GstPbutils.py']
 install_data(pysources,
     install_dir: pygi_override_dir)
 
-gstpython = shared_library('_gi_gst',
+gstpython = python.extension_module('_gi_gst',
     sources: ['gstmodule.c'],
-    name_prefix: '',
-    name_suffix: py_so_suffix,
     install: true,
     install_dir : pygi_override_dir,
+    include_directories : [configinc],
     dependencies : [gst_dep, python_dep, pygobject_dep])
 
+gi_overrides_build_dir = meson.current_build_dir()
+
 # Workaround to get uninstalled working.
 foreach source: pysources
-    run_command(python3, '-c', 'import os; os.symlink("@0@/@1@", "@2@/@3@")'.format(
+    run_command(python, '-c', 'import os; os.symlink("@0@/@1@", "@2@/@3@")'.format(
         meson.current_source_dir(), source,
-        meson.current_build_dir(), source))
+        gi_overrides_build_dir, source))
 endforeach
index 1ef0de1..00898f8 100644 (file)
@@ -11,6 +11,8 @@ gst_version_major = version_arr[0]
 gst_version_minor = version_arr[1]
 api_version = '@0@.0'.format(gst_version_major)
 
+add_project_arguments('-DHAVE_CONFIG_H', language: 'c')
+
 gst_req = '>= @0@.@1@.0'.format(gst_version_major, gst_version_minor)
 
 gst_dep = dependency('gstreamer-1.0', version : gst_req,
@@ -21,32 +23,32 @@ gmodule_dep = dependency('gmodule-2.0')
 pygobject_dep = dependency('pygobject-3.0', fallback: ['pygobject', 'pygobject_dep'], version : '>= 3.8')
 python_dep = dependency('python3')
 
-python3 = import('python3').find_python()
-pythondetector = find_program('scripts/pythondetector')
-
-cres = run_command(pythondetector, '--sosuffix')
-if cres.returncode() != 0
-    error('Could not detect python sosuffix' + cres.stdout() + cres.stderr())
-endif
-py_so_suffix = cres.stdout().strip()
-
-cres = run_command(pythondetector, '--abiflags')
-if cres.returncode() != 0
-    error('Could not detect python abiflags' + cres.stdout() + cres.stderr())
-endif
-python_abi_flags = cres.stdout().strip()
+pymod = import('python')
+python = pymod.find_installation(get_option('python'))
+python_dep = python.dependency(required : true)
 
-cres = run_command(pythondetector, '--libloc')
-if cres.returncode() != 0
-    error('Could not detect python library location' + cres.stdout() + cres.stderr())
+python_abi_flags = python.get_variable('ABIFLAGS', '')
+pylib_loc = python.get_variable('LIBPL', '')
+if host_machine.system() != 'windows'
+    assert(pylib_loc != '', 'Python dynamic library path could not be determined')
 endif
-pylib_loc = cres.stdout().strip()
+message('python_abi_flags = @0@'.format(python_abi_flags))
+message('pylib_loc = @0@'.format(pylib_loc))
 
-assert(pylib_loc != 'None', 'Python dynamic library path could not be determined')
 pygi_override_dir = get_option('pygi-overrides-dir')
 if pygi_override_dir == ''
-    cres = run_command(pythondetector, '--pygi-overridedir',
-            get_option('prefix'))
+    cres = run_command(python, '-c','''
+import os, sys, gi.overrides
+paths = gi.overrides.__path__
+prefix = os.path.abspath(sys.argv[1])
+for path in paths:
+    path = os.path.abspath(path)
+    if os.path.commonprefix([path, prefix]) == prefix:
+        print(path)
+        exit(0)
+exit(1)
+''', get_option('prefix'))
+
     if cres.returncode() != 0
         error('Could not detect PyGObject overrides location' + cres.stdout() + cres.stderr())
     endif
@@ -55,7 +57,7 @@ if pygi_override_dir == ''
         message(cres.stderr())
     endif
 endif
-message('pygobject overrides directory ' + pygi_override_dir)
+message('pygobject overrides directory = @0@'.format(pygi_override_dir))
 
 pylib_suffix = 'so'
 if host_machine.system() == 'windows'
@@ -81,4 +83,4 @@ subdir('gi')
 subdir('plugin')
 subdir('testsuite')
 
-run_command(python3, '-c', 'import shutil; shutil.copy("hooks/pre-commit.hook", ".git/hooks/pre-commit")')
+run_command(python, '-c', 'import shutil; shutil.copy("hooks/pre-commit.hook", ".git/hooks/pre-commit")')
index b250179..68cf814 100644 (file)
@@ -1,2 +1,4 @@
 option('pygi-overrides-dir', type : 'string', value : '',
         description: 'Path to pygobject overrides directory')
+
+option('python', type : 'string', value : 'python3')
index 23a9c96..9f76d19 100644 (file)
@@ -1,6 +1,5 @@
 gst_elements_shared = shared_library('gstpython',
     ['gstpythonplugin.c'],
-    c_args : '-DHAVE_CONFIG_H -DPY_LIB_LOC=@0@'.format(pylib_loc),
     include_directories : [configinc],
     dependencies : [gst_dep, pygobject_dep, gstbase_dep, python_dep, gmodule_dep],
     install : true,
diff --git a/scripts/pythondetector b/scripts/pythondetector
deleted file mode 100644 (file)
index eec43da..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-#!/usr/bin/env python3
-import os
-import platform
-import subprocess
-import sys
-
-from distutils import sysconfig
-
-
-try:
-    sys.path.remove(os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)),
-                                                 '..')))
-except ValueError:
-    pass
-
-def get_python_abiflags():
-    return sysconfig.get_config_var("ABIFLAGS")
-
-def get_python_libloc():
-    # OSX is a pain. Python as shipped by apple installs libpython in /usr/lib
-    # so we hardcode that. Other systems can use --with-libpython-dir to
-    # override this.
-    if platform.system().lower() == 'darwin':
-        return '/usr/lib'
-
-    pylib_loc = sysconfig.get_config_var("LIBPL")
-    pylib_ldlibrary = sysconfig.get_config_var("LDLIBRARY")
-
-    py_sharedlib = os.path.join(pylib_loc, pylib_ldlibrary)
-    if os.path.exists(py_sharedlib):
-        return pylib_loc
-
-    # Workaround for Fedora
-    pylib_loc = sysconfig.get_config_var("LIBDIR")
-    pylib_ldlibrary = sysconfig.get_config_var("LDLIBRARY")
-
-    py_sharedlib = os.path.join(pylib_loc, pylib_ldlibrary)
-    if os.path.exists(py_sharedlib):
-        return pylib_loc
-
-    return "None"
-
-
-if __name__ == "__main__":
-    if len(sys.argv) > 3:
-        print("At most 2 arguments accepted")
-        exit(1)
-
-    if sys.argv[1] == '--abiflags':
-        print(get_python_abiflags())
-    elif sys.argv[1] == '--sosuffix':
-        get = sysconfig.get_config_var
-        suffix = get("EXT_SUFFIX") or get("SO") or ".so"
-        print(suffix[1:])
-    elif sys.argv[1] == '--pygi-overridedir':
-        prefix = sys.argv[2]
-        version = sys.version_info
-
-        # If we are installing in the same prefix as PyGobject
-        # make sure to install in the right place.
-        import gi.overrides
-
-        try:
-            gi.overrides.__path__.remove(os.path.abspath(os.path.join(
-                os.path.dirname(os.path.realpath(__file__)), '..', 'gi')))
-        except ValueError:
-            pass
-        overrides_path = gi.overrides.__path__[0]
-        if os.path.commonprefix([overrides_path, prefix]) == prefix:
-            print(overrides_path)
-            exit(0)
-
-        # Otherwise follow python's way of install site packages inside
-        # the provided prefix
-        if os.name == 'posix':
-            print(os.path.join(
-                prefix, 'lib', 'python%d.%d' % (version.major, version.minor),
-                'site-packages', 'gi', 'overrides'))
-        else:
-            print(os.path.join(
-                prefix, 'Lib', 'Python%d%d' % (version.major, version.minor),
-                'site-packages', 'gi', 'overrides'))
-    elif sys.argv[1] == '--libloc':
-        print(get_python_libloc())
index 24c0b58..6fe105b 100644 (file)
@@ -1,4 +1,4 @@
-runtests = find_program('runtests.py')
+runtests = files('runtests.py')
 
 tests = [
     ['Test gst', 'test_gst.py'],
@@ -17,22 +17,19 @@ if not meson.is_subproject()
   endif
 endif
 
-runcmd = run_command(python3, '-c', '''with open("@0@/mesonconfig.py", "w") as f:
+runcmd = run_command(python, '-c', '''with open("@0@/mesonconfig.py", "w") as f:
     f.write("path='@1@'")'''.format(
-  meson.current_source_dir(), join_paths(meson.current_build_dir(), '../')))
+  join_paths(meson.current_build_dir()), join_paths(meson.current_build_dir(), '..')))
 
 if runcmd.returncode() != 0
     error('Could not configure testsuite config file.' + runcmd.stderr())
 endif
 
+gi_dir = join_paths(pygi_override_dir, '..', '..')
+
 foreach i: tests
     test_name = i.get(0)
     env = environment()
-    env.set('GST_PLUGIN_PATH_1_0', meson.build_root())
-    env.set('GST_PLUGIN_SYSTEM_PATH_1_0', '')
-    foreach plugindir: pluginsdirs
-      env.append('GST_PLUGIN_PATH_1_0', plugindir)
-    endforeach
-    env.set('GST_REGISTRY', '@0@/@1@.registry'.format(meson.current_build_dir(), i.get(1)))
-    test(test_name, runtests, args: [i.get(1)], env: env)
+    env.prepend('PYTHONPATH', [gi_dir, gi_overrides_build_dir])
+    test(test_name, python, args: [runtests, i.get(1)], env: env)
 endforeach