Pārlūkot izejas kodu

fix grass.init() & separated to new module 'setup'

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@44396 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 14 gadi atpakaļ
vecāks
revīzija
56ac45a770
6 mainītis faili ar 67 papildinājumiem un 22 dzēšanām
  1. 7 1
      lib/python/Makefile
  2. 2 18
      lib/python/core.py
  3. 1 1
      lib/python/db.py
  4. 1 1
      lib/python/raster.py
  5. 55 0
      lib/python/setup.py.sed
  6. 1 1
      lib/python/vector.py

+ 7 - 1
lib/python/Makefile

@@ -8,12 +8,13 @@ PYDIR = $(ETC)/python
 GDIR = $(PYDIR)/grass
 DSTDIR = $(GDIR)/script
 
-MODULES = core db raster vector array
+MODULES = core db raster vector array setup
 
 PYFILES := $(patsubst %,$(DSTDIR)/%.py,$(MODULES) __init__)
 PYCFILES := $(patsubst %,$(DSTDIR)/%.pyc,$(MODULES) __init__)
 
 CLEAN_SUBDIRS = ctypes
+EXTRA_CLEAN_FILES = setup.py
 
 default: $(PYFILES) $(PYCFILES) $(GDIR)/__init__.py $(GDIR)/__init__.pyc
 	-$(MAKE) -C ctypes || echo $(CURDIR)/ctypes >> $(ERRORLOG)
@@ -33,5 +34,10 @@ $(GDIR)/__init__.py: grass__init__.py | $(GDIR)
 $(DSTDIR)/%: % | $(DSTDIR)
 	$(INSTALL_DATA) $< $@
 
+setup.py: setup.py.tmp
+	sed \
+	-e 's#@LD_LIBRARY_PATH_VAR@#$(LD_LIBRARY_PATH_VAR)#' \
+	$< > $@
+
 #doxygen:
 DOXNAME = python

+ 2 - 18
lib/python/core.py

@@ -1,6 +1,6 @@
 """!@package grass.script.core
 
-@brief GRASS Python scripting module
+@brief GRASS Python scripting module (core functions)
 
 Core functions to be used in Python scripts.
 
@@ -29,7 +29,7 @@ import types
 import re
 import atexit
 import subprocess
-import tempfile as tmpfile
+import shutil
 
 # i18N
 import gettext
@@ -993,22 +993,6 @@ def _create_location_xy(location):
     
     return 0
 
-def init(gisbase, dbase, location, mapset):
-    os.environ['PATH'] += ':' + os.path.join(gisbase, 'bin') + ':' + \
-        os.path.join(gisbase, 'scripts')
-    os.environ['LD_LIBRARY_PATH'] = os.path.join(gisbase, 'lib')
-    
-    os.environ['GIS_LOCK'] = str(os.getpid())
-    
-    fd, gisrc = tmpfile.mkstemp()
-    os.environ['GISRC'] = gisrc
-    fd.write("GISDBASE: %s\n" % dbase)
-    fd.write("LOCATION_NAME: %s\n" % location)
-    fd.write("MAPSET: %s\n" % mapset)
-    fd.close()
-    
-    return gisrc
-
 # get debug_level
 if find_program('g.gisenv', ['--help']):
     debug_level = int(gisenv().get('DEBUG', 0))

+ 1 - 1
lib/python/db.py

@@ -1,6 +1,6 @@
 """!@package grass.script.db
 
-@brief GRASS Python scripting module
+@brief GRASS Python scripting module (database functions)
 
 Database related functions to be used in Python scripts.
 

+ 1 - 1
lib/python/raster.py

@@ -1,6 +1,6 @@
 """!@package grass.script.raster
 
-@brief GRASS Python scripting module
+@brief GRASS Python scripting module (raster functions)
 
 Raster related functions to be used in Python scripts.
 

+ 55 - 0
lib/python/setup.py.sed

@@ -0,0 +1,55 @@
+"""!@package grass.script.setup
+
+@brief GRASS Python scripting module (setup)
+
+Setup functions to be used in Python scripts.
+
+Usage:
+
+@code
+from grass.script import setup as grass
+
+grass.init()
+...
+@endcode
+
+(C) 2010 by the GRASS Development Team
+This program is free software under the GNU General Public
+License (>=v2). Read the file COPYING that comes with GRASS
+for details.
+
+@author Martin Landa <landa.martin gmail.com>
+"""
+
+import os
+import tempfile as tmpfile
+
+def init(gisbase, dbase, location, mapset):
+    """!Initialize system variables to run scripts without starting
+    GRASS explicitly.
+
+    User is resposible to delete gisrc file.
+
+    @param gisbase path to GRASS installation
+    @param dbase   path to GRASS database
+    @param location location name
+    @param mapset   mapset within given location
+    @return path to gisrc file
+    """
+    os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'bin') + \
+        os.pathsep + os.path.join(gisbase, 'scripts')
+    if not os.environ.has_key('LD_LIBRARY_PATH'):
+        os.environ['@LD_LIBRARY_PATH_VAR@'] = ''
+    os.environ['@LD_LIBRARY_PATH_VAR@'] += os.path.join(gisbase, 'lib')
+    
+    os.environ['GIS_LOCK'] = str(os.getpid())
+    
+    fd, gisrc = tmpfile.mkstemp()
+    os.environ['GISRC'] = gisrc
+    os.write(fd, "GISDBASE: %s\n" % dbase)
+    os.write(fd, "LOCATION_NAME: %s\n" % location)
+    os.write(fd, "MAPSET: %s\n" % mapset)
+    os.close(fd)
+    
+    return gisrc
+

+ 1 - 1
lib/python/vector.py

@@ -1,6 +1,6 @@
 """!@package grass.script.vector
 
-@brief GRASS Python scripting module
+@brief GRASS Python scripting module (vector functions)
 
 Vector related functions to be used in Python scripts.