浏览代码

grass.script: Fix path resolving in init (#1979)

* Search for install path (GISBASE) now correctly returns the path if found.
* Runtime environment setup now accepts any Path-like objects.
* The extra checks before mapset path resolve function call now support tilde for home directory.
Vaclav Petras 3 年之前
父节点
当前提交
59731f80db
共有 1 个文件被更改,包括 10 次插入6 次删除
  1. 10 6
      python/grass/script/setup.py

+ 10 - 6
python/grass/script/setup.py

@@ -163,7 +163,7 @@ def get_install_path(path=None):
     if path and shutil.which(path):
     if path and shutil.which(path):
         # The path was provided by the user and it is an executable
         # The path was provided by the user and it is an executable
         # (on path or provided with full path), so raise exception on failure.
         # (on path or provided with full path), so raise exception on failure.
-        ask_executable(path)
+        return ask_executable(path)
 
 
     # Presumably directory was provided.
     # Presumably directory was provided.
     if path:
     if path:
@@ -179,7 +179,7 @@ def get_install_path(path=None):
     # at this point (to be re-evaluated).
     # at this point (to be re-evaluated).
     grass_bin = os.environ.get("GRASSBIN")
     grass_bin = os.environ.get("GRASSBIN")
     if grass_bin and shutil.which(grass_bin):
     if grass_bin and shutil.which(grass_bin):
-        ask_executable(grass_bin)
+        return ask_executable(grass_bin)
 
 
     # Derive the path from path to this file (Python module).
     # Derive the path from path to this file (Python module).
     # This is the standard way when there is no user-provided settings.
     # This is the standard way when there is no user-provided settings.
@@ -190,7 +190,7 @@ def get_install_path(path=None):
     bin_path = install_path / "bin"
     bin_path = install_path / "bin"
     lib_path = install_path / "lib"
     lib_path = install_path / "lib"
     if bin_path.is_dir() and lib_path.is_dir():
     if bin_path.is_dir() and lib_path.is_dir():
-        path = install_path
+        return install_path
 
 
     # As a last resort, try running grass command if it exists.
     # As a last resort, try running grass command if it exists.
     # This is less likely give the right result than the relative path on systems
     # This is less likely give the right result than the relative path on systems
@@ -198,7 +198,7 @@ def get_install_path(path=None):
     # However, it allows for non-standard installations with standard command.
     # However, it allows for non-standard installations with standard command.
     grass_bin = "grass"
     grass_bin = "grass"
     if grass_bin and shutil.which(grass_bin):
     if grass_bin and shutil.which(grass_bin):
-        ask_executable(grass_bin)
+        return ask_executable(grass_bin)
 
 
     return None
     return None
 
 
@@ -208,6 +208,8 @@ def setup_runtime_env(gisbase):
 
 
     Modifies the global environment (os.environ) so that GRASS modules can run.
     Modifies the global environment (os.environ) so that GRASS modules can run.
     """
     """
+    # Accept Path objects.
+    gisbase = os.fspath(gisbase)
     # Set GISBASE
     # Set GISBASE
     os.environ["GISBASE"] = gisbase
     os.environ["GISBASE"] = gisbase
     mswin = sys.platform.startswith("win")
     mswin = sys.platform.startswith("win")
@@ -304,11 +306,13 @@ def init(path, location=None, mapset=None, grass_path=None):
     from grass.grassdb.checks import get_mapset_invalid_reason, is_mapset_valid
     from grass.grassdb.checks import get_mapset_invalid_reason, is_mapset_valid
     from grass.grassdb.manage import resolve_mapset_path
     from grass.grassdb.manage import resolve_mapset_path
 
 
+    # Support ~ in the path for user home directory.
+    path = Path(path).expanduser()
     # A simple existence test. The directory, whatever it is, should exist.
     # A simple existence test. The directory, whatever it is, should exist.
-    if not Path(path).exists():
+    if not path.exists():
         raise ValueError(_("Path '{path}' does not exist").format(path=path))
         raise ValueError(_("Path '{path}' does not exist").format(path=path))
     # A specific message when it exists, but it is a file.
     # A specific message when it exists, but it is a file.
-    if Path(path).is_file():
+    if path.is_file():
         raise ValueError(
         raise ValueError(
             _("Path '{path}' is a file, but a directory is needed").format(path=path)
             _("Path '{path}' is a file, but a directory is needed").format(path=path)
         )
         )