utils.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  2. @package startup.utils
  3. @brief General 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. """
  9. import os
  10. def get_possible_database_path():
  11. """Finds a path to what is possibly a GRASS Database.
  12. Looks for directory named grassdata in the usual locations.
  13. Returns the path as a string or None if nothing was found, so the
  14. return value can be used to test if the directory was found.
  15. """
  16. home = os.path.expanduser('~')
  17. # try some common directories for grassdata
  18. # grassdata (lowercase) in home for Linux (first choice)
  19. # Documents and My Documents for Windows
  20. # potential translations (old Windows and some Linux)
  21. # but ~ and ~/Documents should cover most of the cases
  22. # ordered by preference and then likelihood
  23. candidates = [
  24. os.path.join(home, "grassdata"),
  25. os.path.join(home, "Documents", "grassdata"),
  26. os.path.join(home, "My Documents", "grassdata"),
  27. ]
  28. try:
  29. # here goes everything which has potential unicode issues
  30. candidates.append(os.path.join(home, _("Documents"), "grassdata"))
  31. candidates.append(os.path.join(home, _("My Documents"), "grassdata"))
  32. except UnicodeDecodeError:
  33. # just ignore the errors if it doesn't work
  34. pass
  35. path = None
  36. for candidate in candidates:
  37. if os.path.exists(candidate):
  38. path = candidate
  39. break # get the first match
  40. return path
  41. def get_lockfile_if_present(database, location, mapset):
  42. """Return path to lock if present, None otherwise
  43. Returns the path as a string or None if nothing was found, so the
  44. return value can be used to test if the lock is present.
  45. """
  46. lock_name = '.gislock'
  47. lockfile = os.path.join(database, location, mapset, lock_name)
  48. if os.path.isfile(lockfile):
  49. return lockfile
  50. else:
  51. return None