utils.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. This file should not use (import) anything from GUI code (wx or wxGUI).
  9. This can potentially be part of the Python library (i.e. it needs to
  10. solve the errors etc. in a general manner).
  11. """
  12. import os
  13. import shutil
  14. import tempfile
  15. import getpass
  16. import sys
  17. def get_possible_database_path():
  18. """Looks for directory 'grassdata' (case-insensitive) in standard
  19. locations to detect existing GRASS Database.
  20. Returns the path as a string or None if nothing was found.
  21. """
  22. home = os.path.expanduser('~')
  23. # try some common directories for grassdata
  24. candidates = [
  25. home,
  26. os.path.join(home, "Documents"),
  27. ]
  28. # find possible database path
  29. for candidate in candidates:
  30. if os.path.exists(candidate):
  31. for subdir in next(os.walk(candidate))[1]:
  32. if 'grassdata' in subdir.lower():
  33. return os.path.join(candidate,subdir)
  34. return None
  35. def create_database_directory():
  36. """Creates the standard GRASS GIS directory.
  37. Creates database directory named grassdata in the standard location
  38. according to the platform.
  39. Returns the new path as a string or None if nothing was found or created.
  40. """
  41. home = os.path.expanduser('~')
  42. # Determine the standard path according to the platform
  43. if sys.platform == 'win32':
  44. path = os.path.join(home, "Documents", "grassdata")
  45. else:
  46. path = os.path.join(home, "grassdata")
  47. # Create "grassdata" directory
  48. try:
  49. os.mkdir(path)
  50. return path
  51. except OSError:
  52. pass
  53. # Create a temporary "grassdata" directory if GRASS is running
  54. # in some special environment and the standard directories
  55. # cannot be created which might be the case in some "try out GRASS"
  56. # use cases.
  57. path = os.path.join(
  58. tempfile.gettempdir(),
  59. "grassdata_{}".format(getpass.getuser())
  60. )
  61. # The created tmp is not cleaned by GRASS, so we are relying on
  62. # the system to do it at some point. The positive outcome is that
  63. # another GRASS instance will find the data created by the first
  64. # one which is desired in the "try out GRASS" use case we are
  65. # aiming towards."
  66. if os.path.exists(path):
  67. return path
  68. try:
  69. os.mkdir(path)
  70. return path
  71. except OSError:
  72. pass
  73. return None
  74. def get_lockfile_if_present(database, location, mapset):
  75. """Return path to lock if present, None otherwise
  76. Returns the path as a string or None if nothing was found, so the
  77. return value can be used to test if the lock is present.
  78. """
  79. lock_name = '.gislock'
  80. lockfile = os.path.join(database, location, mapset, lock_name)
  81. if os.path.isfile(lockfile):
  82. return lockfile
  83. else:
  84. return None
  85. def create_mapset(database, location, mapset):
  86. """Creates a mapset in a specified location"""
  87. location_path = os.path.join(database, location)
  88. mapset_path = os.path.join(location_path, mapset)
  89. # create an empty directory
  90. os.mkdir(mapset_path)
  91. # copy DEFAULT_WIND file and its permissions from PERMANENT
  92. # to WIND in the new mapset
  93. region_path1 = os.path.join(location_path, 'PERMANENT', 'DEFAULT_WIND')
  94. region_path2 = os.path.join(location_path, mapset, 'WIND')
  95. shutil.copy(region_path1, region_path2)
  96. # set permissions to u+rw,go+r (disabled; why?)
  97. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  98. def delete_mapset(database, location, mapset):
  99. """Deletes a specified mapset"""
  100. if mapset == 'PERMANENT':
  101. # TODO: translatable or not?
  102. raise ValueError("Mapset PERMANENT cannot be deleted"
  103. " (whole location can be)")
  104. shutil.rmtree(os.path.join(database, location, mapset))
  105. def delete_location(database, location):
  106. """Deletes a specified location"""
  107. shutil.rmtree(os.path.join(database, location))
  108. def rename_mapset(database, location, old_name, new_name):
  109. """Rename mapset from *old_name* to *new_name*"""
  110. location_path = os.path.join(database, location)
  111. os.rename(os.path.join(location_path, old_name),
  112. os.path.join(location_path, new_name))
  113. def rename_location(database, old_name, new_name):
  114. """Rename location from *old_name* to *new_name*"""
  115. os.rename(os.path.join(database, old_name),
  116. os.path.join(database, new_name))
  117. def get_default_mapset_name():
  118. """Returns default name for mapset."""
  119. try:
  120. defaultName = getpass.getuser()
  121. defaultName.encode('ascii')
  122. except UnicodeEncodeError:
  123. # raise error if not ascii (not valid mapset name)
  124. defaultName = 'user'
  125. return defaultName