Django

Code

Show
Ignore:
Timestamp:
07/20/08 16:30:46 (6 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/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.