GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
setup.py
Go to the documentation of this file.
1 """!@package grass.script.setup
2 
3 @brief GRASS Python scripting module (setup)
4 
5 Setup functions to be used in Python scripts.
6 
7 Usage:
8 
9 @code
10 from grass.script import setup as grass
11 
12 grass.init()
13 ...
14 @endcode
15 
16 (C) 2010-2011 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
19 for details.
20 
21 @author Martin Landa <landa.martin gmail.com>
22 """
23 
24 import os
25 import tempfile as tmpfile
26 
27 def init(gisbase, dbase = '', location = 'demolocation', mapset = 'PERMANENT'):
28  """!Initialize system variables to run scripts without starting
29  GRASS explicitly.
30 
31  User is resposible to delete gisrc file.
32 
33  @param gisbase path to GRASS installation
34  @param dbase path to GRASS database (default: '')
35  @param location location name (default: 'demolocation')
36  @param mapset mapset within given location (default: 'PERMANENT')
37  @return path to gisrc file
38  """
39  os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'bin') + \
40  os.pathsep + os.path.join(gisbase, 'scripts')
41  if 'LD_LIBRARY_PATH' not in os.environ:
42  os.environ['LD_LIBRARY_PATH'] = ''
43  os.environ['LD_LIBRARY_PATH'] += os.path.join(gisbase, 'lib')
44 
45  os.environ['GIS_LOCK'] = str(os.getpid())
46 
47  # Set PYTHONPATH to find GRASS Python modules
48  path = os.getenv('PYTHONPATH')
49  dir = os.path.join(gisbase, 'etc', 'python')
50  if path:
51  path = dir + os.pathsep + path
52  else:
53  path = dir
54  os.environ['PYTHONPATH'] = path
55 
56  if not dbase:
57  dbase = gisbase
58 
59  fd, gisrc = tmpfile.mkstemp()
60  os.environ['GISRC'] = gisrc
61  os.write(fd, "GISDBASE: %s\n" % dbase)
62  os.write(fd, "LOCATION_NAME: %s\n" % location)
63  os.write(fd, "MAPSET: %s\n" % mapset)
64  os.close(fd)
65 
66  return gisrc