diff --git a/django/contrib/gis/gdal/libgdal.py b/django/contrib/gis/gdal/libgdal.py index 4e26cf0ff3..bf8933dffb 100644 --- a/django/contrib/gis/gdal/libgdal.py +++ b/django/contrib/gis/gdal/libgdal.py @@ -6,28 +6,35 @@ from django.contrib.gis.gdal.error import OGRException # Custom library path set? try: from django.conf import settings - lib_name = settings.GDAL_LIBRARY_PATH + lib_path = settings.GDAL_LIBRARY_PATH except (AttributeError, EnvironmentError, ImportError): - lib_name = None + lib_path = None -if lib_name: - pass +if lib_path: + lib_names = None elif os.name == 'nt': # Windows NT shared library - lib_name = 'gdal15.dll' + lib_names = ['gdal15'] elif os.name == 'posix': - platform = os.uname()[0] - if platform == 'Darwin': - # Mac OSX shared library - lib_name = 'libgdal.dylib' - else: - # Attempting to use .so extension for all other platforms. - lib_name = 'libgdal.so' + # *NIX library names. + lib_names = ['gdal', 'gdal1.5.0'] else: 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 -lgdal = CDLL(lib_name) +lgdal = CDLL(lib_path) # On Windows, the GDAL binaries have some OSR routines exported with # STDCALL, while others are not. Thus, the library will also need to diff --git a/django/contrib/gis/geos/libgeos.py b/django/contrib/gis/geos/libgeos.py index 9cbf5ae4ac..d2990a6d0e 100644 --- a/django/contrib/gis/geos/libgeos.py +++ b/django/contrib/gis/geos/libgeos.py @@ -8,6 +8,7 @@ """ import atexit, os, re, sys from ctypes import c_char_p, Structure, CDLL, CFUNCTYPE, POINTER +from ctypes.util import find_library from django.contrib.gis.geos.error import GEOSException # NumPy supported? @@ -20,33 +21,41 @@ except ImportError: # Custom library path set? try: from django.conf import settings - lib_name = settings.GEOS_LIBRARY_PATH + lib_path = settings.GEOS_LIBRARY_PATH except (AttributeError, EnvironmentError, ImportError): - lib_name = None + lib_path = None -# Setting the appropriate name for the GEOS-C library, depending on which -# OS and POSIX platform we're running. -if lib_name: - pass +# Setting the appropriate names for the GEOS-C library. +if lib_path: + lib_names = None elif os.name == 'nt': - # Windows NT library - lib_name = 'libgeos_c-1.dll' + # Windows NT libraries + lib_names = ['libgeos_c-1'] elif os.name == 'posix': - platform = os.uname()[0] # Using os.uname() - if platform == 'Darwin': - # 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' + # *NIX libraries + lib_names = ['geos_c'] else: 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 # both *NIX and Windows. # 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 -lgeos = CDLL(lib_name) +lgeos = CDLL(lib_path) # The notice and error handler C function callback definitions. # Supposed to mimic the GEOS message handler (C below): diff --git a/django/contrib/gis/utils/geoip.py b/django/contrib/gis/utils/geoip.py index 3e05cc74fd..4d163aab9a 100644 --- a/django/contrib/gis/utils/geoip.py +++ b/django/contrib/gis/utils/geoip.py @@ -40,6 +40,7 @@ """ import os, re from ctypes import c_char_p, c_float, c_int, Structure, CDLL, POINTER +from ctypes.util import find_library from django.conf import settings if not settings._target: settings.configure() @@ -47,26 +48,24 @@ if not settings._target: settings.configure() GEOIP_SETTINGS = dict((key, getattr(settings, key)) for key in ('GEOIP_PATH', 'GEOIP_LIBRARY_PATH', 'GEOIP_COUNTRY', 'GEOIP_CITY') 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. class GeoIPException(Exception): pass # The shared library for the GeoIP C API. May be downloaded # from http://www.maxmind.com/download/geoip/api/c/ -if lib_name: - pass -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' +if lib_path: + lib_name = None else: - raise GeoIPException('Unknown POSIX platform "%s"' % platform) -lgeoip = CDLL(lib_name) + # TODO: Is this really the library name for Windows? + 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 # free database editions.