patch #1610795: BSD version of ctypes.util.find_library, by Martin

Kammerhofer.

release25-maint backport candidate, but the release manager has to
decide.
This commit is contained in:
Thomas Heller 2007-01-12 20:17:34 +00:00
parent 572104ff5c
commit d059e7b423
1 changed files with 50 additions and 28 deletions

View File

@ -46,23 +46,16 @@ elif os.name == "posix":
import re, tempfile, errno import re, tempfile, errno
def _findLib_gcc(name): def _findLib_gcc(name):
expr = '[^\(\)\s]*lib%s\.[^\(\)\s]*' % name expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
fdout, ccout = tempfile.mkstemp() fdout, ccout = tempfile.mkstemp()
os.close(fdout) os.close(fdout)
cmd = 'if type gcc &>/dev/null; then CC=gcc; else CC=cc; fi;' \ cmd = 'if type gcc >/dev/null 2>&1; then CC=gcc; else CC=cc; fi;' \
'$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name '$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name
try: try:
fdout, outfile = tempfile.mkstemp() f = os.popen(cmd)
os.close(fdout) trace = f.read()
fd = os.popen(cmd) f.close()
trace = fd.read()
err = fd.close()
finally: finally:
try:
os.unlink(outfile)
except OSError, e:
if e.errno != errno.ENOENT:
raise
try: try:
os.unlink(ccout) os.unlink(ccout)
except OSError, e: except OSError, e:
@ -73,9 +66,48 @@ elif os.name == "posix":
return None return None
return res.group(0) return res.group(0)
def _findLib_ld(name): def _get_soname(f):
expr = '/[^\(\)\s]*lib%s\.[^\(\)\s]*' % name # assuming GNU binutils / ELF
res = re.search(expr, os.popen('/sbin/ldconfig -p 2>/dev/null').read()) if not f:
return None
cmd = "objdump -p -j .dynamic 2>/dev/null " + f
res = re.search(r'\sSONAME\s+([^\s]+)', os.popen(cmd).read())
if not res:
return None
return res.group(1)
if (sys.platform.startswith("freebsd")
or sys.platform.startswith("openbsd")
or sys.platform.startswith("dragonfly")):
def _num_version(libname):
# "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
parts = libname.split(".")
nums = []
try:
while parts:
nums.insert(0, int(parts.pop()))
except ValueError:
pass
return nums or [ sys.maxint ]
def find_library(name):
ename = re.escape(name)
expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
res = re.findall(expr,
os.popen('/sbin/ldconfig -r 2>/dev/null').read())
if not res:
return _get_soname(_findLib_gcc(name))
res.sort(cmp= lambda x,y: cmp(_num_version(x), _num_version(y)))
return res[-1]
else:
def _findLib_ldconfig(name):
# XXX assuming GLIBC's ldconfig (with option -p)
expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
res = re.search(expr,
os.popen('/sbin/ldconfig -p 2>/dev/null').read())
if not res: if not res:
# Hm, this works only for libs needed by the python executable. # Hm, this works only for libs needed by the python executable.
cmd = 'ldd %s 2>/dev/null' % sys.executable cmd = 'ldd %s 2>/dev/null' % sys.executable
@ -84,18 +116,8 @@ elif os.name == "posix":
return None return None
return res.group(0) 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 None
return res.group(1)
def find_library(name): def find_library(name):
lib = _findLib_ld(name) or _findLib_gcc(name) return _get_soname(_findLib_ldconfig(name) or _findLib_gcc(name))
if not lib:
return None
return _get_soname(lib)
################################################################ ################################################################
# test code # test code