setup.py.sed 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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-2011 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 = 'demolocation', mapset = 'PERMANENT'):
  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 (default: '')
  24. @param location location name (default: 'demolocation')
  25. @param mapset mapset within given location (default: 'PERMANENT')
  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_VAR@'):
  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. if not dbase:
  35. dbase = gisbase
  36. fd, gisrc = tmpfile.mkstemp()
  37. os.environ['GISRC'] = gisrc
  38. os.write(fd, "GISDBASE: %s\n" % dbase)
  39. os.write(fd, "LOCATION_NAME: %s\n" % location)
  40. os.write(fd, "MAPSET: %s\n" % mapset)
  41. os.close(fd)
  42. return gisrc