Django

Code

Changeset 8012

Show
Ignore:
Timestamp:
07/20/08 16:30:46 (4 months ago)
Author:
jbronn
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/gdal/libgdal.py

    r7127 r8012  
    77try: 
    88    from django.conf import settings 
    9     lib_name = settings.GDAL_LIBRARY_PATH 
     9    lib_path = settings.GDAL_LIBRARY_PATH 
    1010except (AttributeError, EnvironmentError, ImportError): 
    11     lib_name = None 
     11    lib_path = None 
    1212 
    13 if lib_name
    14     pass 
     13if lib_path
     14    lib_names = None 
    1515elif os.name == 'nt': 
    1616    # Windows NT shared library 
    17     lib_name = 'gdal15.dll' 
     17    lib_names = ['gdal15'] 
    1818elif os.name == 'posix': 
    19     platform = os.uname()[0] 
    20     if platform == 'Darwin': 
    21         # Mac OSX shared library 
    22         lib_name = 'libgdal.dylib' 
    23     else:  
    24         # Attempting to use .so extension for all other platforms. 
    25         lib_name = 'libgdal.so' 
     19    # *NIX library names. 
     20    lib_names = ['gdal', 'gdal1.5.0'] 
    2621else: 
    2722    raise OGRException('Unsupported OS "%s"' % os.name) 
    2823 
     24# Using the ctypes `find_library` utility  to find the  
     25# path to the GDAL library from the list of library names. 
     26if lib_names: 
     27    for lib_name in lib_names: 
     28        lib_path = find_library(lib_name) 
     29        if not lib_path is None: break 
     30         
     31if lib_path is None: 
     32    raise OGRException('Could not find the GDAL library (tried "%s"). ' 
     33                       'Try setting GDAL_LIBRARY_PATH in your settings.' %  
     34                       '", "'.join(lib_names)) 
     35 
    2936# This loads the GDAL/OGR C library 
    30 lgdal = CDLL(lib_name
     37lgdal = CDLL(lib_path
    3138 
    3239# On Windows, the GDAL binaries have some OSR routines exported with  
  • django/branches/gis/django/contrib/gis/geos/libgeos.py

    r7100 r8012  
    99import atexit, os, re, sys 
    1010from ctypes import c_char_p, Structure, CDLL, CFUNCTYPE, POINTER 
     11from ctypes.util import find_library 
    1112from django.contrib.gis.geos.error import GEOSException 
    1213 
     
    2122try: 
    2223    from django.conf import settings 
    23     lib_name = settings.GEOS_LIBRARY_PATH 
     24    lib_path = settings.GEOS_LIBRARY_PATH 
    2425except (AttributeError, EnvironmentError, ImportError): 
    25     lib_name = None 
     26    lib_path = None 
    2627 
    27 # Setting the appropriate name for the GEOS-C library, depending on which 
    28 # OS and POSIX platform we're running. 
    29 if lib_name: 
    30     pass 
     28# Setting the appropriate names for the GEOS-C library. 
     29if lib_path: 
     30    lib_names = None 
    3131elif os.name == 'nt': 
    32     # Windows NT library 
    33     lib_name = 'libgeos_c-1.dll' 
     32    # Windows NT libraries 
     33    lib_names = ['libgeos_c-1'] 
    3434elif os.name == 'posix': 
    35     platform = os.uname()[0] # Using os.uname() 
    36     if platform == 'Darwin': 
    37         # Mac OSX Shared Library (Thanks Matt!) 
    38         lib_name = 'libgeos_c.dylib' 
    39     else: 
    40         # Attempting to use the .so extension for all other platforms 
    41         lib_name = 'libgeos_c.so' 
     35    # *NIX libraries 
     36    lib_names = ['geos_c'] 
    4237else: 
    4338    raise GEOSException('Unsupported OS "%s"' % os.name) 
     39 
     40# Using the ctypes `find_library` utility to find the the path to the GEOS  
     41# shared library.  This is better than manually specifiying each library name  
     42# and extension (e.g., libgeos_c.[so|so.1|dylib].). 
     43if lib_names:  
     44    for lib_name in lib_names: 
     45        lib_path = find_library(lib_name) 
     46        if not lib_path is None: break 
     47 
     48# No GEOS library could be found. 
     49if lib_path is None:  
     50    raise GEOSException('Could not find the GEOS library (tried "%s"). ' 
     51                        'Try setting GEOS_LIBRARY_PATH in your settings.' %  
     52                        '", "'.join(lib_names)) 
    4453 
    4554# Getting the GEOS C library.  The C interface (CDLL) is used for 
     
    4756# See the GEOS C API source code for more details on the library function calls: 
    4857#  http://geos.refractions.net/ro/doxygen_docs/html/geos__c_8h-source.html 
    49 lgeos = CDLL(lib_name
     58lgeos = CDLL(lib_path
    5059 
    5160# The notice and error handler C function callback definitions. 
  • django/branches/gis/django/contrib/gis/utils/geoip.py

    r6918 r8012  
    4141import os, re 
    4242from ctypes import c_char_p, c_float, c_int, Structure, CDLL, POINTER 
     43from ctypes.util import find_library 
    4344from django.conf import settings 
    4445if not settings._target: settings.configure() 
     
    4849                      for key in ('GEOIP_PATH', 'GEOIP_LIBRARY_PATH', 'GEOIP_COUNTRY', 'GEOIP_CITY') 
    4950                      if hasattr(settings, key)) 
    50 lib_name = GEOIP_SETTINGS.get('GEOIP_LIBRARY_PATH', None) 
     51lib_path = GEOIP_SETTINGS.get('GEOIP_LIBRARY_PATH', None) 
    5152 
    5253# GeoIP Exception class. 
     
    5556# The shared library for the GeoIP C API.  May be downloaded 
    5657#  from http://www.maxmind.com/download/geoip/api/c/ 
    57 if lib_name: 
    58     pass 
    59 elif os.name == 'nt': 
    60     lib_name = 'libGeoIP.dll' 
    61 elif os.name == 'posix': 
    62     platform = os.uname()[0] 
    63     if platform == 'Darwin': 
    64         lib_name = 'libGeoIP.dylib' 
    65     else: 
    66         lib_name = 'libGeoIP.so' 
     58if lib_path: 
     59    lib_name = None 
    6760else: 
    68     raise GeoIPException('Unknown POSIX platform "%s"' % platform) 
    69 lgeoip = CDLL(lib_name) 
     61    # TODO: Is this really the library name for Windows? 
     62    lib_name = 'GeoIP' 
     63 
     64# Getting the path to the GeoIP library. 
     65if lib_name: lib_path = find_library(lib_name) 
     66if lib_path is None: raise GeoIPException('Could not find the GeoIP library (tried "%s"). ' 
     67                                          'Try setting GEOIP_LIBRARY_PATH in your settings.' % lib_name) 
     68lgeoip = CDLL(lib_path) 
    7069 
    7170# Regular expressions for recognizing IP addresses and the GeoIP