setup.py.sed 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """!@package grass.script.setup
  2. @brief GRASS Python scripting module (setup)
  3. Setup functions to be used in Python scripts.
  4. Usage:
  5. @code
  6. from grass.script import setup as grass
  7. grass.init()
  8. ...
  9. @endcode
  10. (C) 2010 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Martin Landa <landa.martin gmail.com>
  15. """
  16. import os
  17. import tempfile as tmpfile
  18. def init(gisbase, dbase, location, mapset):
  19. """!Initialize system variables to run scripts without starting
  20. GRASS explicitly.
  21. User is resposible to delete gisrc file.
  22. @param gisbase path to GRASS installation
  23. @param dbase path to GRASS database
  24. @param location location name
  25. @param mapset mapset within given location
  26. @return path to gisrc file
  27. """
  28. os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'bin') + \
  29. os.pathsep + os.path.join(gisbase, 'scripts')
  30. if not os.environ.has_key('LD_LIBRARY_PATH'):
  31. os.environ['@LD_LIBRARY_PATH_VAR@'] = ''
  32. os.environ['@LD_LIBRARY_PATH_VAR@'] += os.path.join(gisbase, 'lib')
  33. os.environ['GIS_LOCK'] = str(os.getpid())
  34. fd, gisrc = tmpfile.mkstemp()
  35. os.environ['GISRC'] = gisrc
  36. os.write(fd, "GISDBASE: %s\n" % dbase)
  37. os.write(fd, "LOCATION_NAME: %s\n" % location)
  38. os.write(fd, "MAPSET: %s\n" % mapset)
  39. os.close(fd)
  40. return gisrc