[PATCH] utils.py from cvs-head to branch_1_0

Sean Jensen-Grey <[email protected]> Fri, 18 Nov 2005 15:13:13 -0800
Newsgroups gmane.comp.python.ctypes.devel
Message-ID <[email protected]>
I was using OpenGL-ctypes and notice that ctypes-cvs-head had a better util.py than branch_1_0 specifically when attempting to load libgle.

The head version correctly finds libgle.so.3 while branch_1_0 assumes libgle.so

Please review the attached util.py, it exports the same find_library function as the orginal. 

Sean
util.py (text/plain, 1.4 KB)
import os, re, tempfile

if os.name == "nt":
    # LoadLibrary already does what we need.
    def find_library(name):
        return name

if os.name == "posix":

    def __findLib_gcc(name):
        expr = '[^\(\)\s]*lib%s\.[^\(\)\s]*' % name
        cmd = 'if type gcc &>/dev/null; then CC=gcc; else CC=cc; fi;' \
              '$CC -Wl,-t -o /dev/null 2>&1 -l' + name
        try:
            fdout, outfile =  tempfile.mkstemp()
            fd = os.popen(cmd)
            trace = fd.read()
            err = fd.close()
        finally:
            try:
                os.unlink(outfile)
            except OSError, e:
                if e.errno != errno.ENOENT:
                    raise
        res = re.search(expr, trace)
        if not res:
            return None
        return res.group(0)

    def __findLib_ld(name):
        expr = '/[^\(\)\s]*lib%s\.[^\(\)\s]*' % name
        res = re.search(expr, os.popen('/sbin/ldconfig -p 2>/dev/null').read())
        if not res:
            return None
        return res.group(0)

    def __get_soname(f):
        cmd = "objdump -p -j .dynamic 2>/dev/null " + f
        res = re.search(r'\sSONAME\s+([^\s]+)', os.popen(cmd).read())
        if not res:
            return f
        return res.group(1)

    def find_library(name):
        lib = __findLib_ld(name)
        if not lib:
            lib = __findLib_gcc(name)
            if not lib:
                return [name]
        return [__get_soname(lib)]