casasglobal-org / casas-gis

GNU General Public License v3.0
0 stars 0 forks source link

Setting platform-specific env vars #15

Open luisponti opened 3 years ago

luisponti commented 3 years ago

Develop a module that sets up GRASS environment based on platform (e.g., Linux, Mac, Windows), before GRASS starts up.

See for example the following coode:

if sys.platform.startswith('linux'):
    # we assume that the GRASS GIS start script is available and in the PATH
    # query GRASS 7 itself for its GISBASE
    grass7bin = grass7bin_lin
elif sys.platform.startswith('win'):
    grass7bin = grass7bin_win
elif sys.platform.startswith('darwin'):
    grass7bin = grass7bin_win
else:
    OSError('Platform not configured.')
bbelderbos commented 3 years ago

I usually refactor this into a dictionary / mapping, but more elegant.

luisponti commented 3 years ago

Do you mean something like this?

https://stackoverflow.com/a/12229070

bbelderbos commented 3 years ago

Btw you can use the platform module:

>>> import platform
>>> platform.machine()
'x86_64'

Then for the mapping

binaries = {
    "x86_64": "grass7bin_lin", 
    "win": "grass7bin_win",
    ...
}

my_os = platform.machine()
default = "x86_64"
binary = binaries.get(my_os, default)
bbelderbos commented 3 years ago

Ah we already have this issue for the mapping refactoring, good.

Instead of machine() I would actually use system(): https://docs.python.org/3/library/platform.html#platform.system

Here are the common return values:

image
luisponti commented 3 years ago

Some relevant comments I removed from code:

""" GRASS enviornment
https://grasswiki.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly

See also issue #15 https://github.com/luisponti/casas-gis/issues/15

# List of variables that GRASS modules need to run without starting
# the main GRASS program

export GISBASE=/Applications/GRASS-8.0.app/Contents/Resources
export GISRC="$HOME.grass8"
export PATH="$GISBASE/bin:$GISBASE/scripts:$PATH"
export PYTHONPATH="$GISBASE/etc/python:$PYTHONPATH"

export DYLD_LIBRARY_PATH="$GISBASE/lib:$DYLD_LIBRARY_PATH"
export LD_LIBRARY_PATH="$GISBASE/lib:$LD_LIBRARY_PATH"
export MANPATH="$MANPATH:$GISBASE/man"

export GRASS_LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
export GRASS_LD_LIBRARY_PATH="$DYLD_LIBRARY_PATH"

If the GRASS libraries are shared libraries, the loader needs to be able 
to find them. This normally means that LD_LIBRARY_PATH (Linux, Solaris), 
DYLD_LIBRARY_PATH (MacOSX) or PATH (Windows) need to contain $GISBASE/lib

"""
neteler commented 1 week ago

FWIW, this has been greatly simplified in GRASS GIS 8.4+.

See for example: https://github.com/OSGeo/grass/blob/main/docker/testdata/test_grass_session.py