data.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. """Provides functions for the main GRASS GIS executable
  2. (C) 2020 by Vaclav Petras and the GRASS Development Team
  3. This program is free software under the GNU General Public
  4. License (>=v2). Read the file COPYING that comes with GRASS
  5. for details.
  6. .. sectionauthor:: Vaclav Petras <wenzeslaus gmail com>
  7. .. sectionauthor:: Linda Kladivova <l.kladivova seznam cz>
  8. This is not a stable part of the API. Use at your own risk.
  9. """
  10. import os
  11. import tempfile
  12. import getpass
  13. import sys
  14. from shutil import copytree, ignore_patterns
  15. from grass.grassdb.checks import is_location_valid
  16. def get_possible_database_path():
  17. """Looks for directory 'grassdata' (case-insensitive) in standard
  18. locations to detect existing GRASS Database.
  19. Returns the path as a string or None if nothing was found.
  20. """
  21. home = os.path.expanduser("~")
  22. # try some common directories for grassdata
  23. candidates = [
  24. home,
  25. os.path.join(home, "Documents"),
  26. ]
  27. # find possible database path
  28. for candidate in candidates:
  29. if os.path.exists(candidate):
  30. for subdir in next(os.walk(candidate))[1]:
  31. if "grassdata" in subdir.lower():
  32. return os.path.join(candidate, subdir)
  33. return None
  34. def create_database_directory():
  35. """Creates the standard GRASS GIS directory.
  36. Creates database directory named grassdata in the standard location
  37. according to the platform.
  38. Returns the new path as a string or None if nothing was found or created.
  39. """
  40. home = os.path.expanduser("~")
  41. # Determine the standard path according to the platform
  42. if sys.platform == "win32":
  43. path = os.path.join(home, "Documents", "grassdata")
  44. else:
  45. path = os.path.join(home, "grassdata")
  46. # Create "grassdata" directory
  47. try:
  48. os.mkdir(path)
  49. return path
  50. except OSError:
  51. pass
  52. # Create a temporary "grassdata" directory if GRASS is running
  53. # in some special environment and the standard directories
  54. # cannot be created which might be the case in some "try out GRASS"
  55. # use cases.
  56. path = os.path.join(tempfile.gettempdir(), "grassdata_{}".format(getpass.getuser()))
  57. # The created tmp is not cleaned by GRASS, so we are relying on
  58. # the system to do it at some point. The positive outcome is that
  59. # another GRASS instance will find the data created by the first
  60. # one which is desired in the "try out GRASS" use case we are
  61. # aiming towards."
  62. if os.path.exists(path):
  63. return path
  64. try:
  65. os.mkdir(path)
  66. return path
  67. except OSError:
  68. pass
  69. return None
  70. def _get_startup_location_in_distribution():
  71. """Check for startup location directory in distribution.
  72. Returns startup location if found or None if nothing was found.
  73. """
  74. gisbase = os.getenv("GISBASE")
  75. startup_location = os.path.join(gisbase, "demolocation")
  76. # Find out if startup location exists
  77. if os.path.exists(startup_location):
  78. return startup_location
  79. return None
  80. def _copy_startup_location(startup_location, location_in_grassdb):
  81. """Copy the simple startup_location with some data to GRASS database.
  82. Returns True if successfully copied or False
  83. when an error was encountered.
  84. """
  85. # Copy source startup location into GRASS database
  86. try:
  87. copytree(
  88. startup_location,
  89. location_in_grassdb,
  90. ignore=ignore_patterns("*.tmpl", "Makefile*"),
  91. )
  92. return True
  93. except (IOError, OSError):
  94. pass
  95. return False
  96. def create_startup_location_in_grassdb(grassdatabase, startup_location_name):
  97. """Create a new startup location in the given GRASS database.
  98. Returns True if a new startup location successfully created
  99. in the given GRASS database.
  100. Returns False if there is no location to copy in the installation
  101. or copying failed.
  102. """
  103. # Find out if startup location exists
  104. startup_location = _get_startup_location_in_distribution()
  105. if not startup_location:
  106. return False
  107. # Copy the simple startup_location with some data to GRASS database
  108. location_in_grassdb = os.path.join(grassdatabase, startup_location_name)
  109. if _copy_startup_location(startup_location, location_in_grassdb):
  110. return True
  111. return False
  112. def ensure_demolocation():
  113. """Ensure that demolocation exists
  114. Creates both database directory and location if needed.
  115. Returns the db, location name, and preferred mapset of the demolocation.
  116. """
  117. grassdb = get_possible_database_path()
  118. # If nothing found, try to create GRASS directory and copy startup loc
  119. if grassdb is None:
  120. grassdb = create_database_directory()
  121. location = "world_latlong_wgs84"
  122. if not is_location_valid(grassdb, location):
  123. create_startup_location_in_grassdb(grassdb, location)
  124. return (grassdb, location, "PERMANENT")