Author: jbronn
Date: 2008-07-20 16:30:46 -0500 (Sun, 20 Jul 2008)
New Revision: 8012
Modified:
django/branches/gis/django/contrib/gis/gdal/libgdal.py
django/branches/gis/django/contrib/gis/geos/libgeos.py
django/branches/gis/django/contrib/gis/utils/geoip.py
Log:
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.
Modified: django/branches/gis/django/contrib/gis/gdal/libgdal.py
===================================================================
--- django/branches/gis/django/contrib/gis/gdal/libgdal.py 2008-07-20
21:12:36 UTC (rev 8011)
+++ django/branches/gis/django/contrib/gis/gdal/libgdal.py 2008-07-20
21:30:46 UTC (rev 8012)
@@ -6,28 +6,35 @@
# 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
Modified: django/branches/gis/django/contrib/gis/geos/libgeos.py
===================================================================
--- django/branches/gis/django/contrib/gis/geos/libgeos.py 2008-07-20
21:12:36 UTC (rev 8011)
+++ django/branches/gis/django/contrib/gis/geos/libgeos.py 2008-07-20
21:30:46 UTC (rev 8012)
@@ -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 @@
# 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):
Modified: django/branches/gis/django/contrib/gis/utils/geoip.py
===================================================================
--- django/branches/gis/django/contrib/gis/utils/geoip.py 2008-07-20
21:12:36 UTC (rev 8011)
+++ django/branches/gis/django/contrib/gis/utils/geoip.py 2008-07-20
21:30:46 UTC (rev 8012)
@@ -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,27 +48,25 @@
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.
ipregex =
re.compile(r'^(?P<w>\d\d?\d?)\.(?P<x>\d\d?\d?)\.(?P<y>\d\d?\d?)\.(?P<z>\d\d?\d?)$')
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---