utils.py 4.3 KB

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