utils.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. """Looks for directory 'grassdata' (case-insensitive) in standard
  16. locations to detect existing GRASS Database.
  17. Returns the path as a string or None if nothing was found.
  18. """
  19. home = os.path.expanduser('~')
  20. # try some common directories for grassdata
  21. candidates = [
  22. home,
  23. os.path.join(home, "Documents"),
  24. ]
  25. # find possible database path
  26. for candidate in candidates:
  27. if os.path.exists(candidate):
  28. for subdir in next(os.walk(candidate))[1]:
  29. if 'grassdata' in subdir.lower():
  30. return os.path.join(candidate,subdir)
  31. return None
  32. def get_lockfile_if_present(database, location, mapset):
  33. """Return path to lock if present, None otherwise
  34. Returns the path as a string or None if nothing was found, so the
  35. return value can be used to test if the lock is present.
  36. """
  37. lock_name = '.gislock'
  38. lockfile = os.path.join(database, location, mapset, lock_name)
  39. if os.path.isfile(lockfile):
  40. return lockfile
  41. else:
  42. return None
  43. def create_mapset(database, location, mapset):
  44. """Creates a mapset in a specified location"""
  45. location_path = os.path.join(database, location)
  46. mapset_path = os.path.join(location_path, mapset)
  47. # create an empty directory
  48. os.mkdir(mapset_path)
  49. # copy DEFAULT_WIND file and its permissions from PERMANENT
  50. # to WIND in the new mapset
  51. region_path1 = os.path.join(location_path, 'PERMANENT', 'DEFAULT_WIND')
  52. region_path2 = os.path.join(location_path, mapset, 'WIND')
  53. shutil.copy(region_path1, region_path2)
  54. # set permissions to u+rw,go+r (disabled; why?)
  55. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  56. def delete_mapset(database, location, mapset):
  57. """Deletes a specified mapset"""
  58. if mapset == 'PERMANENT':
  59. # TODO: translatable or not?
  60. raise ValueError("Mapset PERMANENT cannot be deleted"
  61. " (whole location can be)")
  62. shutil.rmtree(os.path.join(database, location, mapset))
  63. def delete_location(database, location):
  64. """Deletes a specified location"""
  65. shutil.rmtree(os.path.join(database, location))
  66. def rename_mapset(database, location, old_name, new_name):
  67. """Rename mapset from *old_name* to *new_name*"""
  68. location_path = os.path.join(database, location)
  69. os.rename(os.path.join(location_path, old_name),
  70. os.path.join(location_path, new_name))
  71. def rename_location(database, old_name, new_name):
  72. """Rename location from *old_name* to *new_name*"""
  73. os.rename(os.path.join(database, old_name),
  74. os.path.join(database, new_name))