checks.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. """
  2. Checking objects in a GRASS GIS Spatial Database
  3. (C) 2020 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. .. sectionauthor:: Vaclav Petras <wenzeslaus gmail com>
  8. """
  9. import os
  10. from pathlib import Path
  11. def mapset_exists(database, location, mapset):
  12. """Returns True whether mapset path exists."""
  13. location_path = os.path.join(database, location)
  14. mapset_path = os.path.join(location_path, mapset)
  15. if os.path.exists(mapset_path):
  16. return True
  17. return False
  18. def location_exists(database, location):
  19. """Returns True whether location path exists."""
  20. location_path = os.path.join(database, location)
  21. if os.path.exists(location_path):
  22. return True
  23. return False
  24. # TODO: distinguish between valid for getting maps and usable as current
  25. # https://lists.osgeo.org/pipermail/grass-dev/2016-September/082317.html
  26. # interface created according to the current usage
  27. def is_mapset_valid(mapset_path):
  28. """Return True if GRASS Mapset is valid"""
  29. # WIND is created from DEFAULT_WIND by `g.region -d` and functions
  30. # or modules which create a new mapset. Most modules will fail if
  31. # WIND doesn't exist (assuming that neither GRASS_REGION nor
  32. # WIND_OVERRIDE environmental variables are set).
  33. return os.access(os.path.join(mapset_path, "WIND"), os.R_OK)
  34. def is_location_valid(database, location):
  35. """Return True if GRASS Location is valid
  36. :param database: Path to GRASS GIS database directory
  37. :param location: name of a Location
  38. """
  39. # DEFAULT_WIND file should not be required until you do something
  40. # that actually uses them. The check is just a heuristic; a directory
  41. # containing a PERMANENT/DEFAULT_WIND file is probably a GRASS
  42. # location, while a directory lacking it probably isn't.
  43. return os.access(
  44. os.path.join(database, location, "PERMANENT", "DEFAULT_WIND"), os.F_OK
  45. )
  46. def is_current_user_mapset_owner(mapset_path):
  47. """Returns True if mapset owner is the current user"""
  48. # Note that this does account for libgis built with SKIP_MAPSET_OWN_CHK
  49. # which disables the ownerships check, i.e., even if it was build with the
  50. # skip, it still needs the env variable.
  51. if os.environ.get("GRASS_SKIP_MAPSET_OWNER_CHECK", None):
  52. # Mapset just needs to be accessible for writing.
  53. return os.access(mapset_path, os.W_OK)
  54. # Mapset needs to be owned by user.
  55. stat_info = os.stat(mapset_path)
  56. mapset_uid = stat_info.st_uid
  57. return mapset_uid == os.getuid()
  58. def is_different_mapset_owner(mapset_path):
  59. """Returns True if mapset owner is different from the current user"""
  60. return not is_current_user_mapset_owner(mapset_path)
  61. def get_mapset_owner(mapset_path):
  62. """Returns mapset owner name or None if owner name unknown"""
  63. try:
  64. path = Path(mapset_path)
  65. return path.owner()
  66. except KeyError:
  67. return None
  68. def is_mapset_locked(mapset_path):
  69. """Check if the mapset is locked"""
  70. lock_name = ".gislock"
  71. lockfile = os.path.join(mapset_path, lock_name)
  72. return os.path.exists(lockfile)
  73. def get_lockfile_if_present(database, location, mapset):
  74. """Return path to lock if present, None otherwise
  75. Returns the path as a string or None if nothing was found, so the
  76. return value can be used to test if the lock is present.
  77. """
  78. lock_name = ".gislock"
  79. lockfile = os.path.join(database, location, mapset, lock_name)
  80. if os.path.isfile(lockfile):
  81. return lockfile
  82. return None
  83. def can_start_in_mapset(mapset_path, ignore_lock=False):
  84. """Check if a mapset from a gisrc file is usable for new session"""
  85. if not is_mapset_valid(mapset_path):
  86. return False
  87. if not is_current_user_mapset_owner(mapset_path):
  88. return False
  89. if not ignore_lock and is_mapset_locked(mapset_path):
  90. return False
  91. return True
  92. def dir_contains_location(path):
  93. """Return True if directory *path* contains a valid location"""
  94. if not os.path.isdir(path):
  95. return False
  96. for name in os.listdir(path):
  97. if os.path.isdir(os.path.join(path, name)):
  98. if is_location_valid(path, name):
  99. return True
  100. return False
  101. # basically checking location, possibly split into two functions
  102. # (mapset one can call location one)
  103. def get_mapset_invalid_reason(database, location, mapset, none_for_no_reason=False):
  104. """Returns a message describing what is wrong with the Mapset
  105. The goal is to provide the most suitable error message
  106. (rather than to do a quick check).
  107. :param database: Path to GRASS GIS database directory
  108. :param location: name of a Location
  109. :param mapset: name of a Mapset
  110. :returns: translated message
  111. """
  112. # Since we are trying to get the one most likely message, we need all
  113. # those return statements here.
  114. # pylint: disable=too-many-return-statements
  115. location_path = os.path.join(database, location)
  116. mapset_path = os.path.join(location_path, mapset)
  117. # first checking the location validity
  118. # perhaps a special set of checks with different messages mentioning mapset
  119. # will be needed instead of the same set of messages used for location
  120. location_msg = get_location_invalid_reason(
  121. database, location, none_for_no_reason=True
  122. )
  123. if location_msg:
  124. return location_msg
  125. # if location is valid, check mapset
  126. if mapset not in os.listdir(location_path):
  127. # TODO: remove the grass.py specific wording
  128. return _(
  129. "Mapset <{mapset}> doesn't exist in GRASS Location <{location}>"
  130. ).format(mapset=mapset, location=location)
  131. if not os.path.isdir(mapset_path):
  132. return _("<%s> is not a GRASS Mapset because it is not a directory") % mapset
  133. if not os.path.isfile(os.path.join(mapset_path, "WIND")):
  134. return (
  135. _(
  136. "<%s> is not a valid GRASS Mapset"
  137. " because it does not have a WIND file"
  138. )
  139. % mapset
  140. )
  141. # based on the is_mapset_valid() function
  142. if not os.access(os.path.join(mapset_path, "WIND"), os.R_OK):
  143. return (
  144. _(
  145. "<%s> is not a valid GRASS Mapset"
  146. " because its WIND file is not readable"
  147. )
  148. % mapset
  149. )
  150. # no reason for invalidity found (might be valid)
  151. if none_for_no_reason:
  152. return None
  153. return _(
  154. "Mapset <{mapset}> or Location <{location}> is invalid for an unknown reason"
  155. ).format(mapset=mapset, location=location)
  156. def get_location_invalid_reason(database, location, none_for_no_reason=False):
  157. """Returns a message describing what is wrong with the Location
  158. The goal is to provide the most suitable error message
  159. (rather than to do a quick check).
  160. By default, when no reason is found, a message about unknown reason is
  161. returned. This applies also to the case when this function is called on
  162. a valid location (e.g. as a part of larger investigation).
  163. ``none_for_no_reason=True`` allows the function to be used as part of other
  164. diagnostic. When this function fails to find reason for invalidity, other
  165. the caller can continue the investigation in their context.
  166. :param database: Path to GRASS GIS database directory
  167. :param location: name of a Location
  168. :param none_for_no_reason: When True, return None when reason is unknown
  169. :returns: translated message or None
  170. """
  171. location_path = os.path.join(database, location)
  172. permanent_path = os.path.join(location_path, "PERMANENT")
  173. # directory
  174. if not os.path.exists(location_path):
  175. return _("Location <%s> doesn't exist") % location_path
  176. # permament mapset
  177. if "PERMANENT" not in os.listdir(location_path):
  178. return (
  179. _(
  180. "<%s> is not a valid GRASS Location"
  181. " because PERMANENT Mapset is missing"
  182. )
  183. % location_path
  184. )
  185. if not os.path.isdir(permanent_path):
  186. return (
  187. _(
  188. "<%s> is not a valid GRASS Location"
  189. " because PERMANENT is not a directory"
  190. )
  191. % location_path
  192. )
  193. # partially based on the is_location_valid() function
  194. if not os.path.isfile(os.path.join(permanent_path, "DEFAULT_WIND")):
  195. return (
  196. _(
  197. "<%s> is not a valid GRASS Location"
  198. " because PERMANENT Mapset does not have a DEFAULT_WIND file"
  199. " (default computational region)"
  200. )
  201. % location_path
  202. )
  203. # no reason for invalidity found (might be valid)
  204. if none_for_no_reason:
  205. return None
  206. return _("Location <{location_path}> is invalid for an unknown reason").format(
  207. location_path=location_path
  208. )
  209. def get_location_invalid_suggestion(database, location):
  210. """Return suggestion what to do when specified location is not valid
  211. It gives suggestion when:
  212. * A mapset was specified instead of a location.
  213. * A GRASS database was specified instead of a location.
  214. """
  215. location_path = os.path.join(database, location)
  216. # a common error is to use mapset instead of location,
  217. # if that's the case, include that info into the message
  218. if is_mapset_valid(location_path):
  219. return _(
  220. "<{location}> looks like a mapset, not a location."
  221. " Did you mean just <{one_dir_up}>?"
  222. ).format(location=location, one_dir_up=database)
  223. # confusion about what is database and what is location
  224. if dir_contains_location(location_path):
  225. return _(
  226. "It looks like <{location}> contains locations."
  227. " Did you mean to specify one of them?"
  228. ).format(location=location)
  229. return None