1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

gis: Now use ctypes.util.find_library to get the C library names. This is preferrable to manually specifying the different library naming schemes used by different platforms/distributions.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@8012 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2008-07-20 21:30:46 +00:00
parent fff69c18c1
commit 7011fc6d3c
3 changed files with 57 additions and 42 deletions

View File

@ -6,28 +6,35 @@ from django.contrib.gis.gdal.error import OGRException
# Custom library path set? # Custom library path set?
try: try:
from django.conf import settings from django.conf import settings
lib_name = settings.GDAL_LIBRARY_PATH lib_path = settings.GDAL_LIBRARY_PATH
except (AttributeError, EnvironmentError, ImportError): except (AttributeError, EnvironmentError, ImportError):
lib_name = None lib_path = None
if lib_name: if lib_path:
pass lib_names = None
elif os.name == 'nt': elif os.name == 'nt':
# Windows NT shared library # Windows NT shared library
lib_name = 'gdal15.dll' lib_names = ['gdal15']
elif os.name == 'posix': elif os.name == 'posix':
platform = os.uname()[0] # *NIX library names.
if platform == 'Darwin': lib_names = ['gdal', 'gdal1.5.0']
# Mac OSX shared library
lib_name = 'libgdal.dylib'
else:
# Attempting to use .so extension for all other platforms.
lib_name = 'libgdal.so'
else: else:
raise OGRException('Unsupported OS "%s"' % os.name) raise OGRException('Unsupported OS "%s"' % os.name)
# Using the ctypes `find_library` utility to find the
# path to the GDAL library from the list of library names.
if lib_names:
for lib_name in lib_names:
lib_path = find_library(lib_name)
if not lib_path is None: break
if lib_path is None:
raise OGRException('Could not find the GDAL library (tried "%s"). '
'Try setting GDAL_LIBRARY_PATH in your settings.' %
'", "'.join(lib_names))
# This loads the GDAL/OGR C library # This loads the GDAL/OGR C library
lgdal = CDLL(lib_name) lgdal = CDLL(lib_path)
# On Windows, the GDAL binaries have some OSR routines exported with # On Windows, the GDAL binaries have some OSR routines exported with
# STDCALL, while others are not. Thus, the library will also need to # STDCALL, while others are not. Thus, the library will also need to

View File

@ -8,6 +8,7 @@
""" """
import atexit, os, re, sys import atexit, os, re, sys
from ctypes import c_char_p, Structure, CDLL, CFUNCTYPE, POINTER from ctypes import c_char_p, Structure, CDLL, CFUNCTYPE, POINTER
from ctypes.util import find_library
from django.contrib.gis.geos.error import GEOSException from django.contrib.gis.geos.error import GEOSException
# NumPy supported? # NumPy supported?
@ -20,33 +21,41 @@ except ImportError:
# Custom library path set? # Custom library path set?
try: try:
from django.conf import settings from django.conf import settings
lib_name = settings.GEOS_LIBRARY_PATH lib_path = settings.GEOS_LIBRARY_PATH
except (AttributeError, EnvironmentError, ImportError): except (AttributeError, EnvironmentError, ImportError):
lib_name = None lib_path = None
# Setting the appropriate name for the GEOS-C library, depending on which # Setting the appropriate names for the GEOS-C library.
# OS and POSIX platform we're running. if lib_path:
if lib_name: lib_names = None
pass
elif os.name == 'nt': elif os.name == 'nt':
# Windows NT library # Windows NT libraries
lib_name = 'libgeos_c-1.dll' lib_names = ['libgeos_c-1']
elif os.name == 'posix': elif os.name == 'posix':
platform = os.uname()[0] # Using os.uname() # *NIX libraries
if platform == 'Darwin': lib_names = ['geos_c']
# Mac OSX Shared Library (Thanks Matt!)
lib_name = 'libgeos_c.dylib'
else:
# Attempting to use the .so extension for all other platforms
lib_name = 'libgeos_c.so'
else: else:
raise GEOSException('Unsupported OS "%s"' % os.name) raise GEOSException('Unsupported OS "%s"' % os.name)
# Using the ctypes `find_library` utility to find the the path to the GEOS
# shared library. This is better than manually specifiying each library name
# and extension (e.g., libgeos_c.[so|so.1|dylib].).
if lib_names:
for lib_name in lib_names:
lib_path = find_library(lib_name)
if not lib_path is None: break
# No GEOS library could be found.
if lib_path is None:
raise GEOSException('Could not find the GEOS library (tried "%s"). '
'Try setting GEOS_LIBRARY_PATH in your settings.' %
'", "'.join(lib_names))
# Getting the GEOS C library. The C interface (CDLL) is used for # Getting the GEOS C library. The C interface (CDLL) is used for
# both *NIX and Windows. # both *NIX and Windows.
# See the GEOS C API source code for more details on the library function calls: # See the GEOS C API source code for more details on the library function calls:
# http://geos.refractions.net/ro/doxygen_docs/html/geos__c_8h-source.html # http://geos.refractions.net/ro/doxygen_docs/html/geos__c_8h-source.html
lgeos = CDLL(lib_name) lgeos = CDLL(lib_path)
# The notice and error handler C function callback definitions. # The notice and error handler C function callback definitions.
# Supposed to mimic the GEOS message handler (C below): # Supposed to mimic the GEOS message handler (C below):

View File

@ -40,6 +40,7 @@
""" """
import os, re import os, re
from ctypes import c_char_p, c_float, c_int, Structure, CDLL, POINTER from ctypes import c_char_p, c_float, c_int, Structure, CDLL, POINTER
from ctypes.util import find_library
from django.conf import settings from django.conf import settings
if not settings._target: settings.configure() if not settings._target: settings.configure()
@ -47,26 +48,24 @@ if not settings._target: settings.configure()
GEOIP_SETTINGS = dict((key, getattr(settings, key)) GEOIP_SETTINGS = dict((key, getattr(settings, key))
for key in ('GEOIP_PATH', 'GEOIP_LIBRARY_PATH', 'GEOIP_COUNTRY', 'GEOIP_CITY') for key in ('GEOIP_PATH', 'GEOIP_LIBRARY_PATH', 'GEOIP_COUNTRY', 'GEOIP_CITY')
if hasattr(settings, key)) if hasattr(settings, key))
lib_name = GEOIP_SETTINGS.get('GEOIP_LIBRARY_PATH', None) lib_path = GEOIP_SETTINGS.get('GEOIP_LIBRARY_PATH', None)
# GeoIP Exception class. # GeoIP Exception class.
class GeoIPException(Exception): pass class GeoIPException(Exception): pass
# The shared library for the GeoIP C API. May be downloaded # The shared library for the GeoIP C API. May be downloaded
# from http://www.maxmind.com/download/geoip/api/c/ # from http://www.maxmind.com/download/geoip/api/c/
if lib_name: if lib_path:
pass lib_name = None
elif os.name == 'nt':
lib_name = 'libGeoIP.dll'
elif os.name == 'posix':
platform = os.uname()[0]
if platform == 'Darwin':
lib_name = 'libGeoIP.dylib'
else:
lib_name = 'libGeoIP.so'
else: else:
raise GeoIPException('Unknown POSIX platform "%s"' % platform) # TODO: Is this really the library name for Windows?
lgeoip = CDLL(lib_name) lib_name = 'GeoIP'
# Getting the path to the GeoIP library.
if lib_name: lib_path = find_library(lib_name)
if lib_path is None: raise GeoIPException('Could not find the GeoIP library (tried "%s"). '
'Try setting GEOIP_LIBRARY_PATH in your settings.' % lib_name)
lgeoip = CDLL(lib_path)
# Regular expressions for recognizing IP addresses and the GeoIP # Regular expressions for recognizing IP addresses and the GeoIP
# free database editions. # free database editions.