utils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. def get_possible_database_path():
  15. """Finds a path to what is possibly a GRASS Database.
  16. Looks for directory named grassdata in the usual locations.
  17. Returns the path as a string or None if nothing was found, so the
  18. return value can be used to test if the directory was found.
  19. """
  20. home = os.path.expanduser('~')
  21. # try some common directories for grassdata
  22. # grassdata (lowercase) in home for Linux (first choice)
  23. # Documents and My Documents for Windows
  24. # potential translations (old Windows and some Linux)
  25. # but ~ and ~/Documents should cover most of the cases
  26. # ordered by preference and then likelihood
  27. candidates = [
  28. os.path.join(home, "grassdata"),
  29. os.path.join(home, "Documents", "grassdata"),
  30. os.path.join(home, "My Documents", "grassdata"),
  31. ]
  32. try:
  33. # here goes everything which has potential unicode issues
  34. candidates.append(os.path.join(home, _("Documents"), "grassdata"))
  35. candidates.append(os.path.join(home, _("My Documents"), "grassdata"))
  36. except UnicodeDecodeError:
  37. # just ignore the errors if it doesn't work
  38. pass
  39. path = None
  40. for candidate in candidates:
  41. if os.path.exists(candidate):
  42. path = candidate
  43. break # get the first match
  44. return path
  45. def get_lockfile_if_present(database, location, mapset):
  46. """Return path to lock if present, None otherwise
  47. Returns the path as a string or None if nothing was found, so the
  48. return value can be used to test if the lock is present.
  49. """
  50. lock_name = '.gislock'
  51. lockfile = os.path.join(database, location, mapset, lock_name)
  52. if os.path.isfile(lockfile):
  53. return lockfile
  54. else:
  55. return None
  56. def create_mapset(database, location, mapset):
  57. """Creates a mapset in a specified location"""
  58. location_path = os.path.join(database, location)
  59. mapset_path = os.path.join(location_path, mapset)
  60. # create an empty directory
  61. os.mkdir(mapset_path)
  62. # copy WIND file and its permissions from PERMANENT
  63. # this uses PERMANENT's current region instead of default (?)
  64. region_file = 'WIND'
  65. region_path = os.path.join(location_path, 'PERMANENT', region_file)
  66. shutil.copy(region_path, mapset_path)
  67. # set permissions to u+rw,go+r (disabled)
  68. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  69. def delete_mapset(database, location, mapset):
  70. """Deletes a specified mapset"""
  71. if mapset == 'PERMANENT':
  72. # TODO: translatable or not?
  73. raise ValueError("Mapset PERMANENT cannot be deleted"
  74. " (whole location can be)")
  75. shutil.rmtree(os.path.join(database, location, mapset))
  76. def delete_location(database, location):
  77. """Deletes a specified location"""
  78. shutil.rmtree(os.path.join(database, location))
  79. # TODO: similar to (but not the same as) read_gisrc function in grass.py
  80. def read_gisrc(self):
  81. """Read variables from a current GISRC file
  82. Returns a dictionary representation of the file content.
  83. """
  84. grassrc = {}
  85. gisrc = os.getenv("GISRC")
  86. if gisrc and os.path.isfile(gisrc):
  87. try:
  88. rc = open(gisrc, "r")
  89. for line in rc.readlines():
  90. try:
  91. key, val = line.split(":", 1)
  92. except ValueError as e:
  93. sys.stderr.write(
  94. _('Invalid line in GISRC file (%s):%s\n' % (e, line)))
  95. grassrc[key.strip()] = DecodeString(val.strip())
  96. finally:
  97. rc.close()
  98. return grassrc