Skip to content

Commit 46d62b4

Browse files
authored
grass.script: Remove build vars from setup.py (#6391)
Remove build vars from setup and use the existing dependency on grass.app.runtime to get the information. Additionally, move some of the parametrization to grass.app.runtime because now, both the functions executing the setup steps and the variable are there, so there is no reason to supply these from the outside. Config dir does not require version, but I kept a general function there which is used in g.extension and in GUI because these use the runtime-obtained version as opposed to the one from grass.app.runtime (difference between running system and setup of the env). I removed the build variables from the documentation which makes it a little simpler to edit, although less explicit assuming that the version usage there was actually correct. The example needs more unrelated edits, so I'm keeping it as is. This removes the working Autotools variable substitution and the not working CMake variable substitution. The env parameter is now a mandatory keyword argument, so adds env= accordingly.
1 parent f4df7b4 commit 46d62b4

File tree

7 files changed

+39
-49
lines changed

7 files changed

+39
-49
lines changed

gui/wxpython/core/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from grass.script import core as grass
3030
from grass.script import task as gtask
31-
from grass.app.runtime import get_grass_config_dir
31+
from grass.app.runtime import get_grass_config_dir_for_version
3232

3333
from core.gcmd import RunCommand
3434
from core.debug import Debug
@@ -798,7 +798,9 @@ def GetFormats(writableOnly=False):
798798
def GetSettingsPath():
799799
"""Get full path to the settings directory"""
800800
version_major, version_minor, _ = grass.version()["version"].split(".")
801-
return get_grass_config_dir(version_major, version_minor, os.environ)
801+
return get_grass_config_dir_for_version(
802+
version_major, version_minor, env=os.environ
803+
)
802804

803805

804806
def StoreEnvVariable(key, value=None, envFile=None):

lib/init/grass.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@
8585
CMD_NAME = None
8686
GRASS_VERSION = None
8787
GRASS_VERSION_MAJOR = None
88-
GRASS_VERSION_MINOR = None
89-
LD_LIBRARY_PATH_VAR = None
9088
CONFIG_PROJSHARE = None
9189
GRASS_VERSION_GIT = None
9290

@@ -380,9 +378,7 @@ def create_grass_config_dir() -> str:
380378
from grass.app.runtime import get_grass_config_dir
381379

382380
try:
383-
directory = get_grass_config_dir(
384-
GRASS_VERSION_MAJOR, GRASS_VERSION_MINOR, os.environ
385-
)
381+
directory = get_grass_config_dir(env=os.environ)
386382
except (RuntimeError, NotADirectoryError) as e:
387383
fatal(f"{e}")
388384

@@ -2123,8 +2119,6 @@ def main() -> None:
21232119
CMD_NAME, \
21242120
GRASS_VERSION, \
21252121
GRASS_VERSION_MAJOR, \
2126-
GRASS_VERSION_MINOR, \
2127-
LD_LIBRARY_PATH_VAR, \
21282122
GRASS_VERSION_GIT, \
21292123
GISBASE, \
21302124
CONFIG_PROJSHARE
@@ -2133,8 +2127,6 @@ def main() -> None:
21332127
CMD_NAME = runtime_paths.grass_exe_name
21342128
GRASS_VERSION = runtime_paths.version
21352129
GRASS_VERSION_MAJOR = runtime_paths.version_major
2136-
GRASS_VERSION_MINOR = runtime_paths.version_minor
2137-
LD_LIBRARY_PATH_VAR = runtime_paths.ld_library_path_var
21382130
GRASS_VERSION_GIT = runtime_paths.grass_version_git
21392131
GISBASE = runtime_paths.gisbase
21402132
CONFIG_PROJSHARE = runtime_paths.config_projshare
@@ -2215,7 +2207,6 @@ def main() -> None:
22152207
set_paths(
22162208
install_path=GISBASE,
22172209
grass_config_dir=grass_config_dir,
2218-
ld_library_path_variable_name=LD_LIBRARY_PATH_VAR,
22192210
)
22202211
# Set GRASS_PAGER, GRASS_PYTHON, GRASS_GNUPLOT, GRASS_PROJSHARE
22212212
set_defaults(config_projshare_path=CONFIG_PROJSHARE)

python/grass/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ endforeach()
3232
copy_python_files_in_subdir(app ${PYDIR_GRASS} EXCLUDE resource_paths.py)
3333

3434
configure_file(__init__.py ${OUTDIR}/${PYDIR_GRASS}/ COPYONLY)
35-
configure_file(script/setup.py ${OUTDIR}/${PYDIR_GRASS}/script/setup.py
36-
COPYONLY)
37-
3835

3936
file(TO_NATIVE_PATH ${CMAKE_BINARY_DIR}/bin BINARY_DIR)
4037
set(CONFIG_PROJSHARE)

python/grass/app/runtime.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,23 @@ def __get_dir(self, env_var):
8686
return res
8787

8888

89-
def get_grass_config_dir(major_version, minor_version, env):
90-
"""Get configuration directory
89+
def get_grass_config_dir(*, env):
90+
"""Get configuration directory path
9191
92-
Determines path of GRASS user configuration directory.
92+
Determines path of GRASS user configuration directory for the current platform
93+
using the build-time version information.
94+
"""
95+
paths = RuntimePaths(env=env)
96+
return get_grass_config_dir_for_version(
97+
paths.version_major, paths.version_minor, env=env
98+
)
99+
100+
101+
def get_grass_config_dir_for_version(major_version, minor_version, *, env):
102+
"""Get configuration directory path for specific version
103+
104+
Determines path of GRASS user configuration directory for the current platform
105+
and for the provided version.
93106
"""
94107
if env.get("GRASS_CONFIG_DIR"):
95108
# use GRASS_CONFIG_DIR environmental variable is defined
@@ -172,19 +185,18 @@ def set_executable_paths(install_path, grass_config_dir, env):
172185
env["PATH"] = os.pathsep.join(paths)
173186

174187

175-
def set_paths(install_path, grass_config_dir, ld_library_path_variable_name):
188+
def set_paths(install_path, grass_config_dir):
176189
"""Set variables with executable paths, library paths, and other paths"""
177190
set_executable_paths(
178191
install_path=install_path, grass_config_dir=grass_config_dir, env=os.environ
179192
)
180-
# Set LD_LIBRARY_PATH (etc) to find GRASS shared libraries
181-
# this works for subprocesses but won't affect the current process
182-
if ld_library_path_variable_name:
183-
set_dynamic_library_path(
184-
variable_name=ld_library_path_variable_name,
185-
install_path=install_path,
186-
env=os.environ,
187-
)
193+
# Set LD_LIBRARY_PATH (etc) to find GRASS shared libraries.
194+
# This works for subprocesses, but won't affect the current process.
195+
set_dynamic_library_path(
196+
variable_name=res_paths.LD_LIBRARY_PATH_VAR,
197+
install_path=install_path,
198+
env=os.environ,
199+
)
188200
set_python_path_variable(install_path=install_path, env=os.environ)
189201

190202
# retrieving second time, but now it is always set

python/grass/script/Makefile

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,3 @@ $(DSTDIR):
1717

1818
$(DSTDIR)/%: % | $(DSTDIR)
1919
$(INSTALL_DATA) $< $@
20-
21-
EXTRA_CLEAN_FILES = setup.py.tmp
22-
23-
$(DSTDIR)/setup.py: setup.py.tmp | $(DSTDIR)
24-
$(INSTALL_DATA) $< $@
25-
26-
setup.py.tmp: setup.py
27-
sed \
28-
-e 's#@LD_LIBRARY_PATH_VAR@#$(LD_LIBRARY_PATH_VAR)#' \
29-
-e 's#@GRASS_VERSION_MAJOR@#$(GRASS_VERSION_MAJOR)#' \
30-
-e 's#@GRASS_VERSION_MINOR@#$(GRASS_VERSION_MINOR)#' \
31-
$< > $@

python/grass/script/setup.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
# executable = r'C:\Program Files (x86)\GRASS <version>\grass.bat'
3434
# this can be skipped if GRASS executable is added to PATH
3535
elif sys.platform == "darwin":
36-
# Mac OS X
37-
version = "@GRASS_VERSION_MAJOR@.@GRASS_VERSION_MINOR@"
38-
executable = f"/Applications/GRASS-{version}.app/Contents/Resources/bin/grass"
36+
# macOS
37+
executable = f"/Applications/GRASS-<version>.app/Contents/Resources/bin/grass"
3938
4039
# query GRASS itself for its Python package path
4140
grass_cmd = [executable, "--config", "python_path"]
@@ -92,9 +91,6 @@
9291
WINDOWS = sys.platform.startswith("win")
9392
MACOS = sys.platform.startswith("darwin")
9493

95-
VERSION_MAJOR = "@GRASS_VERSION_MAJOR@"
96-
VERSION_MINOR = "@GRASS_VERSION_MINOR@"
97-
9894

9995
def write_gisrc(dbase, location, mapset):
10096
"""Write the ``gisrc`` file and return its path."""
@@ -222,17 +218,18 @@ def setup_runtime_env(gisbase=None, *, env=None):
222218
set_executable_paths,
223219
set_path_to_python_executable,
224220
set_python_path_variable,
221+
RuntimePaths,
225222
)
226223

227224
# Set GISBASE
228225
env["GISBASE"] = gisbase
229226
set_executable_paths(
230227
install_path=gisbase,
231-
grass_config_dir=get_grass_config_dir(VERSION_MAJOR, VERSION_MINOR, env=env),
228+
grass_config_dir=get_grass_config_dir(env=env),
232229
env=env,
233230
)
234231
set_dynamic_library_path(
235-
variable_name="@LD_LIBRARY_PATH_VAR@", install_path=gisbase, env=env
232+
variable_name=RuntimePaths().ld_library_path_var, install_path=gisbase, env=env
236233
)
237234
set_python_path_variable(install_path=gisbase, env=env)
238235
set_path_to_python_executable(env=env)

scripts/g.extension/g.extension.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,10 +2475,13 @@ def resolve_install_prefix(path, to_system):
24752475
path = os.environ["GISBASE"]
24762476
if path == "$GRASS_ADDON_BASE":
24772477
if not os.getenv("GRASS_ADDON_BASE"):
2478-
from grass.app.runtime import get_grass_config_dir
2478+
from grass.app.runtime import get_grass_config_dir_for_version
24792479

24802480
path = os.path.join(
2481-
get_grass_config_dir(VERSION[0], VERSION[1], os.environ), "addons"
2481+
get_grass_config_dir_for_version(
2482+
VERSION[0], VERSION[1], env=os.environ
2483+
),
2484+
"addons",
24822485
)
24832486
gs.warning(
24842487
_("GRASS_ADDON_BASE is not defined, installing to {}").format(path)

0 commit comments

Comments
 (0)