setup.py.sed 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 '@LD_LIBRARY_PATH_VAR@' not in os.environ:
  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. # Set PYTHONPATH to find GRASS Python modules
  35. path = os.getenv('PYTHONPATH')
  36. dir = os.path.join(gisbase, 'etc', 'python')
  37. if path:
  38. path = dir + os.pathsep + path
  39. else:
  40. path = dir
  41. os.environ['PYTHONPATH'] = path
  42. if not dbase:
  43. dbase = gisbase
  44. fd, gisrc = tmpfile.mkstemp()
  45. os.environ['GISRC'] = gisrc
  46. os.write(fd, "GISDBASE: %s\n" % dbase)
  47. os.write(fd, "LOCATION_NAME: %s\n" % location)
  48. os.write(fd, "MAPSET: %s\n" % mapset)
  49. os.close(fd)
  50. return gisrc