setup.py.sed 2.1 KB

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