Commit e43a0fcc authored by Thomas Heller's avatar Thomas Heller

Add missing svn properties.

parent 8f505aed
import sys, os import sys, os
import ctypes import ctypes
# find_library(name) returns the pathname of a library, or None. # find_library(name) returns the pathname of a library, or None.
if os.name == "nt": if os.name == "nt":
def find_library(name): def find_library(name):
# See MSDN for the REAL search order. # See MSDN for the REAL search order.
for directory in os.environ['PATH'].split(os.pathsep): for directory in os.environ['PATH'].split(os.pathsep):
fname = os.path.join(directory, name) fname = os.path.join(directory, name)
if os.path.exists(fname): if os.path.exists(fname):
return fname return fname
if fname.lower().endswith(".dll"): if fname.lower().endswith(".dll"):
continue continue
fname = fname + ".dll" fname = fname + ".dll"
if os.path.exists(fname): if os.path.exists(fname):
return fname return fname
return None return None
if os.name == "ce": if os.name == "ce":
# search path according to MSDN: # search path according to MSDN:
# - absolute path specified by filename # - absolute path specified by filename
# - The .exe launch directory # - The .exe launch directory
# - the Windows directory # - the Windows directory
# - ROM dll files (where are they?) # - ROM dll files (where are they?)
# - OEM specified search path: HKLM\Loader\SystemPath # - OEM specified search path: HKLM\Loader\SystemPath
def find_library(name): def find_library(name):
return name return name
if os.name == "posix" and sys.platform == "darwin": if os.name == "posix" and sys.platform == "darwin":
from ctypes.macholib.dyld import dyld_find as _dyld_find from ctypes.macholib.dyld import dyld_find as _dyld_find
def find_library(name): def find_library(name):
possible = ['lib%s.dylib' % name, possible = ['lib%s.dylib' % name,
'%s.dylib' % name, '%s.dylib' % name,
'%s.framework/%s' % (name, name)] '%s.framework/%s' % (name, name)]
for name in possible: for name in possible:
try: try:
return _dyld_find(name) return _dyld_find(name)
except ValueError: except ValueError:
continue continue
return None return None
elif os.name == "posix": elif os.name == "posix":
# Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
import re, tempfile import re, tempfile
def _findLib_gcc(name): def _findLib_gcc(name):
expr = '[^\(\)\s]*lib%s\.[^\(\)\s]*' % name expr = '[^\(\)\s]*lib%s\.[^\(\)\s]*' % name
cmd = 'if type gcc &>/dev/null; then CC=gcc; else CC=cc; fi;' \ cmd = 'if type gcc &>/dev/null; then CC=gcc; else CC=cc; fi;' \
'$CC -Wl,-t -o /dev/null 2>&1 -l' + name '$CC -Wl,-t -o /dev/null 2>&1 -l' + name
try: try:
fdout, outfile = tempfile.mkstemp() fdout, outfile = tempfile.mkstemp()
fd = os.popen(cmd) fd = os.popen(cmd)
trace = fd.read() trace = fd.read()
err = fd.close() err = fd.close()
finally: finally:
try: try:
os.unlink(outfile) os.unlink(outfile)
except OSError, e: except OSError, e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
raise raise
res = re.search(expr, trace) res = re.search(expr, trace)
if not res: if not res:
return None return None
return res.group(0) return res.group(0)
def _findLib_ld(name): def _findLib_ld(name):
expr = '/[^\(\)\s]*lib%s\.[^\(\)\s]*' % name expr = '/[^\(\)\s]*lib%s\.[^\(\)\s]*' % name
res = re.search(expr, os.popen('/sbin/ldconfig -p 2>/dev/null').read()) 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
res = re.search(expr, os.popen(cmd).read()) res = re.search(expr, os.popen(cmd).read())
if not res: if not res:
return None return None
return res.group(0) return res.group(0)
def _get_soname(f): def _get_soname(f):
cmd = "objdump -p -j .dynamic 2>/dev/null " + f cmd = "objdump -p -j .dynamic 2>/dev/null " + f
res = re.search(r'\sSONAME\s+([^\s]+)', os.popen(cmd).read()) res = re.search(r'\sSONAME\s+([^\s]+)', os.popen(cmd).read())
if not res: if not res:
return None return None
return res.group(1) return res.group(1)
def find_library(name): def find_library(name):
lib = _findLib_ld(name) or _findLib_gcc(name) lib = _findLib_ld(name) or _findLib_gcc(name)
if not lib: if not lib:
return None return None
return _get_soname(lib) return _get_soname(lib)
################################################################ ################################################################
# test code # test code
def test(): def test():
from ctypes import cdll from ctypes import cdll
if os.name == "nt": if os.name == "nt":
print cdll.msvcrt print cdll.msvcrt
print cdll.load("msvcrt") print cdll.load("msvcrt")
print find_library("msvcrt") print find_library("msvcrt")
if os.name == "posix": if os.name == "posix":
# find and load_version # find and load_version
print find_library("m") print find_library("m")
print find_library("c") print find_library("c")
print find_library("bz2") print find_library("bz2")
# getattr # getattr
## print cdll.m ## print cdll.m
## print cdll.bz2 ## print cdll.bz2
# load # load
if sys.platform == "darwin": if sys.platform == "darwin":
print cdll.LoadLibrary("libm.dylib") print cdll.LoadLibrary("libm.dylib")
print cdll.LoadLibrary("libcrypto.dylib") print cdll.LoadLibrary("libcrypto.dylib")
print cdll.LoadLibrary("libSystem.dylib") print cdll.LoadLibrary("libSystem.dylib")
print cdll.LoadLibrary("System.framework/System") print cdll.LoadLibrary("System.framework/System")
else: else:
print cdll.LoadLibrary("libm.so") print cdll.LoadLibrary("libm.so")
print cdll.LoadLibrary("libcrypt.so") print cdll.LoadLibrary("libcrypt.so")
print find_library("crypt") print find_library("crypt")
if __name__ == "__main__": if __name__ == "__main__":
test() test()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment