GRASS Programmer's Manual  6.4.2(2012)
setup.py
Go to the documentation of this file.
00001 """!@package grass.script.setup
00002 
00003 @brief GRASS Python scripting module (setup)
00004 
00005 Setup functions to be used in Python scripts.
00006 
00007 Usage:
00008 
00009 @code
00010 from grass.script import setup as grass
00011 
00012 grass.init()
00013 ...
00014 @endcode
00015 
00016 (C) 2010-2011 by the GRASS Development Team
00017 This program is free software under the GNU General Public
00018 License (>=v2). Read the file COPYING that comes with GRASS
00019 for details.
00020 
00021 @author Martin Landa <landa.martin gmail.com>
00022 """
00023 
00024 import os
00025 import tempfile as tmpfile
00026 
00027 def init(gisbase, dbase = '', location = 'demolocation', mapset = 'PERMANENT'):
00028     """!Initialize system variables to run scripts without starting
00029     GRASS explicitly.
00030 
00031     User is resposible to delete gisrc file.
00032 
00033     @param gisbase path to GRASS installation
00034     @param dbase   path to GRASS database (default: '')
00035     @param location location name (default: 'demolocation')
00036     @param mapset   mapset within given location (default: 'PERMANENT')
00037     @return path to gisrc file
00038     """
00039     os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'bin') + \
00040         os.pathsep + os.path.join(gisbase, 'scripts')
00041     if 'LD_LIBRARY_PATH' not in os.environ:
00042         os.environ['LD_LIBRARY_PATH'] = ''
00043     os.environ['LD_LIBRARY_PATH'] += os.path.join(gisbase, 'lib')
00044     
00045     os.environ['GIS_LOCK'] = str(os.getpid())
00046     
00047     # Set PYTHONPATH to find GRASS Python modules
00048     path = os.getenv('PYTHONPATH')
00049     dir  = os.path.join(gisbase, 'etc', 'python')
00050     if path:
00051         path = dir + os.pathsep + path
00052     else:
00053         path = dir
00054     os.environ['PYTHONPATH'] = path
00055     
00056     if not dbase:
00057         dbase = gisbase
00058     
00059     fd, gisrc = tmpfile.mkstemp()
00060     os.environ['GISRC'] = gisrc
00061     os.write(fd, "GISDBASE: %s\n" % dbase)
00062     os.write(fd, "LOCATION_NAME: %s\n" % location)
00063     os.write(fd, "MAPSET: %s\n" % mapset)
00064     os.close(fd)
00065     
00066     return gisrc
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines