On Fri, Sep 10, 2010 at 4:13 AM,  <[email protected]> wrote:
>> On Thu, Sep 9, 2010 at 12:52 AM, oekopez <[email protected]> wrote:
>> >>On Wed, Sep 8, 2010 at 2:32 AM,  <[email protected]> wrote:
>> >>> Dear Shapely-Developers,
>> >>>
>> >>> First: thank you for Shapely!
>> >>> I have to install Shapely on a cluster (scientific linux), where i
>> don't
>> > have root privilegs. Python, as well as libgeos_c are installed (from
>> > source) under $HOME/local/lib and $HOME/local/bin. The first is in
>> > $LD_LIBRARY_PATH and the latter in $PATH. Unfortunatly
>> ctypes.find_library
>> > seems to fail under these circumstances. Can you fix geos.py to fall
>> back to
>> > the any *nix variant, for loading libgeos_c, if find_library does not
>> find
>> > the library?
>> >>>
>> >>> A possible solution (working for me) could be changing line 28-32 in
>> > geos.py to (inspired by your code line 83-92):
>> >>>
>> >>> if sys.platform == 'linux2':
>> >>>    lib = find_library('geos_c')
>> >>>    if lib is None:
>> >>>        try:
>> >>>            _lgeos = CDLL('libgeos_c.so.1')
>> >>>        except (OSError, ImportError):
>> >>>            _lgeos = CDLL('libgeos_c.so')
>> >>>        except:
>> >>>            raise
>> >>>    else:
>> >>>        _lgeos = CDLL(lib)
>> >>>    free = CDLL(find_library('c')).free
>> >>>    free.argtypes = [c_void_p]
>> >>>    free.restype = None
>> >>>
>> >>>
>> >>> Thank you,
>> >>>
>> >>> Philipp
>> >>
>> >>Hi Philipp,
>> >>
>> >>You are very welcome! I'm glad you like it.
>> >>
>> >>I won't rule out the change you suggested, but first I'd like to make
>> >>sure that we can't solve this by sorting your library path. I am
>> >>routinely testing and deploying Shapely using zc.buildout and
>> >>modifying LD_LIBRARY_PATH so that Shapely (in a virtualenv) can find a
>> >>specific, local libgeos_c.so (also built from source). Without any
>> >>root privileges or system-wide installs. Can you check that
>> >>$HOME/local/lib and $HOME/local/bin are at the head of LD_LIBRARY_PATH
>> >>and PATH?
>> >>
>> >>Cheers,
>> >>
>> >>--
>> >>Sean
>> >>
>> >>
>> > Dear Sean,
>> > thank you for your prompt answer.
>> > I checked the paths, but since I'm not a Linux-pro I attach some output
>> > (variables, errormsg's, tests). However, it may always happen, for
>> different
>> > resaons, that find_library fails (e.g. geos missing). Some kind of error
>> > handling with a clear error message (like "libgeos_c not found") might
>> be
>> > nice anyway.
>> > The system I am using is a commercial cluster system with scientific
>> linux
>> > 5.5.
>> >
>> > So here's my environment:
>> >
>> > [gh1...@skylla1 ~]$ echo $PATH
>> >
>> /home/gh1961/local/lib:/home/gh1961/local/bin:/cm/shared/apps/gcc/4.3.4/bin:
>> >
>> /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/cm/shared/ap
>> >
>> ps/mvapich2/gcc/64/1.2/bin:/cm/shared/apps/mvapich2/gcc/64/1.2/sbin:/home/gh
>> > 1961/bin
>> > [gh1...@skylla1 ~]$ echo $LD_LIBRARY_PATH
>> >
>> /home/gh1961/local/bin:/cm/shared/apps/gcc/4.3.4/lib:/cm/shared/apps/gcc/4.3
>> > .4/lib64:/cm/shared/apps/mvapich2/gcc/64/1.2/lib
>> > [gh1...@skylla1 ~]$
>> >
>>
>> Thanks for printing out those env vars. You need
>> /home/gh1961/local/lib at the start of LD_LIBRARY_PATH and
>> /home/gh1961/local/bin at the start of PATH. You have them reversed.
>>
>> Good idea about raising something other than AttributeError.
>>
>> Cheers,
>>
>> --
>> Sean
>
> Dear Sean,
>
> thanks again, but it is still not working (ctypes.find_library). This is my 
> new environment:
> [gh1...@skylla1 ~]$ echo $PATH
> /home/gh1961/local/bin:/home/gh1961/local/lib:/cm/shared/apps/gcc/4.3.4/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/cm/shared/apps/mvapich2/gcc/64/1.2/bin:/cm/shared/apps/mvapich2/gcc/64/1.2/sbin:/cm/shared/apps/sge/6.2u5/bin/lx26-amd64:/home/gh1961/bin
> [gh1...@skylla1 ~]$ echo $LD_LIBRARY_PATH
> /home/gh1961/local/lib:/home/gh1961/local/bin:/cm/shared/apps/gcc/4.3.4/lib:/cm/shared/apps/gcc/4.3.4/lib64:/cm/shared/apps/mvapich2/gcc/64/1.2/lib:/cm/shared/apps/sge/6.2u5/lib/lx26-amd64
>
> The same errors prevail.
> I don't see a reason why not implementing a fallback in case of find_library 
> fails, as you have for any other OS. Therefore I've got a findgeos-module 
> attached which is working on the cluster and on my workstation (windows). 
> Unfortunatly I do not have other systems to test it. But basically it is 
> still your code, just refactored and with the fallback for a failed 
> find_library.
> To use the module lines 28-101 in geos.py simplify to:
>
> from .findgeos import findlibrary
> _lgeos,free, geos_c_version = findlibrary(warn=False)
>
> Since I am using Shapely in a hydrological modeling toolkit, fixing this 
> problem is crucial for the deployment of my software.
>
> BTW: I appreciate Shapely for being IO free, but I am happy to share an 
> extremely lightweight (or primitive) pure Python ESRI Shapefile reader, based 
> on Shapely and struct, in case anyone needs one.
>
> Cheers,
>
> Philipp
>
>
> findgeos.py
> """
> finds the geos_c library for different platforms
> """
>
> import os
> import sys
> import ctypes
> from ctypes import cdll, CDLL, PyDLL, CFUNCTYPE, c_char_p, c_void_p
> from ctypes.util import find_library
>
> def linux(warn):
>    geos_name = find_library('geos_c')
>    c_name=find_library('c')
>    if not c_name:
>        raise ImportError("""Shapely: The C runtime of your system has not been
>                found. This is a problem with your system setup""")
>    free = CDLL(c_name).free
>    free.argtypes = [c_void_p]
>    free.restype = None
>
>    if geos_name:
>        return CDLL(geos_name),free
>    else:
>        if warn:
>            warnmsg="Shapely: libgeos_c not found with standard way. "
>            warnmsg+="Trying alternative method."
>            print warnmsg
>        try:
>            _lgeos = CDLL('libgeos_c.so.1')
>        except (OSError, ImportError):
>            try:
>                _lgeos = CDLL('libgeos_c.so')
>            except(OSError, ImportError):
>                errmsg= "'geos_c' not found. Please check your geos 
> installation"
>                errmsg+="and $PATH and $LD_LIBRARY_PATH"
>                raise ImportError(errmsg)
>        except:
>            raise
>        return _lgeos,free
>
> def darwin():
>    lib = find_library('geos_c')
>    if lib is None:
>        ## try a few more locations
>        lib_paths = [
>            # The Framework build from Kyng Chaos:
>            "/Library/Frameworks/GEOS.framework/Versions/Current/GEOS",
>            # macports
>            '/opt/local/lib/libgeos_c.dylib',
>        ]
>        for path in lib_paths:
>            if os.path.exists(path):
>                lib = path
>                break
>    if lib is None:
>        raise ImportError("Could not find geos_c library")
>    _lgeos = CDLL(lib)
>    c_name=find_library('c')
>    if not c_name:
>        raise ImportError("""Shapely: The C runtime of your system has not been
>                found. This is a problem with your system setup""")
>    free = CDLL(c_name).free
>    free.argtypes = [c_void_p]
>    free.restype = None
>    return _lgeos,free
>
> def sunos5():
>    # Try the major versioned name first, falling back on the unversioned name.
>    try:
>        _lgeos = CDLL('libgeos_c.so.1')
>    except (OSError, ImportError):
>        _lgeos = CDLL('libgeos_c.so')
>    except:
>        raise
>    try:
>        free = CDLL('libc.so.1').free
>    except (OSError,ImportError):
>        raise ImportError("""Shapely: The C runtime of your system has not been
>                found. This is a problem with your system setup""")
>    free.argtypes = [c_void_p]
>    free.restype = None
>    return _lgeos,free
>
> def win32():
>    try:
>        local_dlls = os.path.abspath(os.__file__ + "../../../DLLs")
>        original_path = os.environ['PATH']
>        os.environ['PATH'] = "%s;%s" % (local_dlls, original_path)
>        _lgeos = CDLL("geos.dll")
>    except (ImportError, WindowsError):
>        raise
>    def free(m):
>        try:
>            cdll.msvcrt.free(m)
>        except WindowsError:
>            # XXX: See http://trac.gispython.org/projects/PCL/ticket/149
>            pass
>    return _lgeos,free
>
>
> def findlibrary(warn=False):
>    """The function is searching platform dependent for the geos_c library
>    and the free function from system dependent C runtime.
>    Returns: ctypes.CDLL(geos_c), free, a tuple containing the version of 
> geos_c
>    Parameters: warn, if True warns for linux systems, if the library is not
>                found using ctypes.find_library('geos_c')
>    """
>
>    # Find and load the GEOS and C libraries
>    if sys.platform == 'linux2':
>        _lgeos,free = linux(warn)
>    elif sys.platform == 'darwin':
>        _lgeos,free = darwin()
>
>    elif sys.platform == 'win32':
>        _lgeos,free = win32()
>
>    elif sys.platform == 'sunos5':
>        _lgeos,free = linux(warn)
>
>    # Check if the version of GEOS is ok
>    def _geos_c_version():
>        func = _lgeos.GEOSversion
>        func.restype = c_char_p
>        v = func().split('-')[2]
>        return tuple(int(n) for n in v.split('.'))
>
>    geos_c_version = _geos_c_version()
>    return _lgeos,free,geos_c_version
>

I'm not familiar with the scientific linux distro, but if find_library
doesn't work, it doesn't work. I pushed some changes that attempt to
do the same thing as your code:

http://github.com/sgillies/shapely/commit/b89fd614653a1d636a51cb2d27d75039769a27d5

Would you be willing to give that a try?

-- 
Sean Gillies
_______________________________________________
Community mailing list
[email protected]
http://lists.gispython.org/mailman/listinfo/community

Reply via email to