GRASS Programmer's Manual  6.5.svn(2014)-r66266
 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-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
19 for details.
20 
21 @author Martin Landa <landa.martin gmail.com>
22 """
23 
24 import os
25 import sys
26 import tempfile as tmpfile
27 
28 def init(gisbase, dbase = '', location = 'demolocation', mapset = 'PERMANENT'):
29  """!Initialize system variables to run scripts without starting
30  GRASS explicitly.
31 
32  User is resposible to delete gisrc file.
33 
34  @param gisbase path to GRASS installation
35  @param dbase path to GRASS database (default: '')
36  @param location location name (default: 'demolocation')
37  @param mapset mapset within given location (default: 'PERMANENT')
38  @return path to gisrc file
39  """
40  # define PATH
41  os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'bin')
42  os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'scripts')
43  if sys.platform.startswith('win'): # added for winGRASS
44  os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'extralib')
45  os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'msys', 'bin')
46  # define LD_LIBRARY_PATH
47  if 'LD_LIBRARY_PATH' not in os.environ:
48  os.environ['LD_LIBRARY_PATH'] = ''
49  os.environ['LD_LIBRARY_PATH'] += os.pathsep + os.path.join(gisbase, 'lib')
50 
51  os.environ['GIS_LOCK'] = str(os.getpid())
52 
53  # Set PYTHONPATH to find GRASS Python modules
54  path = os.getenv('PYTHONPATH')
55  dir = os.path.join(gisbase, 'etc', 'python')
56  if path:
57  path = dir + os.pathsep + path
58  else:
59  path = dir
60  os.environ['PYTHONPATH'] = path
61 
62  if not dbase:
63  dbase = gisbase
64 
65  fd, gisrc = tmpfile.mkstemp()
66  os.environ['GISRC'] = gisrc
67  os.write(fd, "GISDBASE: %s\n" % dbase)
68  os.write(fd, "LOCATION_NAME: %s\n" % location)
69  os.write(fd, "MAPSET: %s\n" % mapset)
70  os.close(fd)
71 
72  return gisrc
def init
Initialize system variables to run scripts without starting GRASS explicitly.
Definition: setup.py:28