1
"""!@package grass.script.setup
3
@brief GRASS Python scripting module (setup)
5
Setup functions to be used in Python scripts.
10
from grass.script import setup as grass
16
(C) 2010-2012 by the GRASS Development Team
17
This program is free software under the GNU General Public
18
License (>=v2). Read the file COPYING that comes with GRASS
21
@author Martin Landa <landa.martin gmail.com>
26
import tempfile as tmpfile
29
def write_gisrc(dbase, location, mapset):
30
"""Write the gisrc file and return the gisrc path."""
31
fd, gisrc = tmpfile.mkstemp()
32
os.write(fd, "GISDBASE: %s\n" % dbase)
33
os.write(fd, "LOCATION_NAME: %s\n" % location)
34
os.write(fd, "MAPSET: %s\n" % mapset)
39
def init(gisbase, dbase='', location='demolocation', mapset='PERMANENT'):
40
"""!Initialize system variables to run scripts without starting
43
User is resposible to delete gisrc file.
45
@param gisbase path to GRASS installation
46
@param dbase path to GRASS database (default: '')
47
@param location location name (default: 'demolocation')
48
@param mapset mapset within given location (default: 'PERMANENT')
49
@return path to gisrc file
52
os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'bin')
53
os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'scripts')
54
if sys.platform.startswith('win'): # added for winGRASS
55
os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'extrabin')
57
# define LD_LIBRARY_PATH
58
if '@LD_LIBRARY_PATH_VAR@' not in os.environ:
59
os.environ['@LD_LIBRARY_PATH_VAR@'] = ''
60
os.environ['@LD_LIBRARY_PATH_VAR@'] += os.pathsep + os.path.join(gisbase, 'lib')
62
os.environ['GIS_LOCK'] = str(os.getpid())
64
# Set PYTHONPATH to find GRASS Python modules
65
path = os.getenv('PYTHONPATH')
66
etcpy = os.path.join(gisbase, 'etc', 'python')
68
path = etcpy + os.pathsep + path
71
os.environ['PYTHONPATH'] = path
76
os.environ['GISRC'] = write_gisrc(dbase, location, mapset)
77
return os.environ['GISRC']