setup.py.sed 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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-2012 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 sys
  18. import tempfile as tmpfile
  19. def init(gisbase, dbase = '', location = 'demolocation', mapset = 'PERMANENT'):
  20. """!Initialize system variables to run scripts without starting
  21. GRASS explicitly.
  22. User is resposible to delete gisrc file.
  23. @param gisbase path to GRASS installation
  24. @param dbase path to GRASS database (default: '')
  25. @param location location name (default: 'demolocation')
  26. @param mapset mapset within given location (default: 'PERMANENT')
  27. @return path to gisrc file
  28. """
  29. # define PATH
  30. os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'bin')
  31. os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'scripts')
  32. if sys.platform.startswith('win'): # added for winGRASS
  33. os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'extralib')
  34. # define LD_LIBRARY_PATH
  35. if '@LD_LIBRARY_PATH_VAR@' not in os.environ:
  36. os.environ['@LD_LIBRARY_PATH_VAR@'] = ''
  37. os.environ['@LD_LIBRARY_PATH_VAR@'] += os.pathsep + os.path.join(gisbase, 'lib')
  38. os.environ['GIS_LOCK'] = str(os.getpid())
  39. # Set PYTHONPATH to find GRASS Python modules
  40. path = os.getenv('PYTHONPATH')
  41. dir = os.path.join(gisbase, 'etc', 'python')
  42. if path:
  43. path = dir + os.pathsep + path
  44. else:
  45. path = dir
  46. os.environ['PYTHONPATH'] = path
  47. if not dbase:
  48. dbase = gisbase
  49. fd, gisrc = tmpfile.mkstemp()
  50. os.environ['GISRC'] = gisrc
  51. os.write(fd, "GISDBASE: %s\n" % dbase)
  52. os.write(fd, "LOCATION_NAME: %s\n" % location)
  53. os.write(fd, "MAPSET: %s\n" % mapset)
  54. os.close(fd)
  55. return gisrc