Skip to content

Commit

Permalink
Merge pull request #10195 from rouault/python_launcher_scripts
Browse files Browse the repository at this point in the history
Add Python launcher scripts (without .py extension) for dev environment
  • Loading branch information
rouault committed Jun 12, 2024
2 parents 2b94d7a + 8f29136 commit 6bd9d23
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ubuntu_24.04/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ set -e
LD_LIBRARY_PATH="/opt/instantclient_19_9:/opt/instantclient_19_9/lib:${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH

# Test development launcher script
gdal_edit --version

export PYTEST="python3 -m pytest -vv -p no:sugar --color=no"

# Run C++ tests
Expand Down
33 changes: 33 additions & 0 deletions doc/source/development/dev_environment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,36 @@ From a Conda enabled console
cd c:\dev\GDAL
cd _build.vs2019
ctest -V --build-config Release


.. _setting_dev_environment_variables:

Setting development environment variables
-----------------------------------------

Once GDAL has been built, a number of environment variables must be set to be
able to execute C++ or Python utilities of the build directory, or run tests.

This can be done by sourcing the following from the build directory:

.. code-block:: bash
. ../scripts/setdevenv.sh
(with adjustments to the above path if the build directory is not a subdirectory of the GDAL source root).

For Windows, a similar ``scripts/setdevenv.bat`` script exists (it currently assumes a Release build).

To verify that environment variables have been set correctly, you can check the version of a GDAL binary:

.. code-block:: bash
gdalinfo --version
# GDAL 3.7.0dev-5327c149f5-dirty, released 2018/99/99 (debug build)
and the Python bindings:

.. code-block:: bash
python3 -c 'from osgeo import gdal; print(gdal.__version__)'
# 3.7.0dev-5327c149f5-dirty
22 changes: 1 addition & 21 deletions doc/source/development/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,7 @@ Running a subset of tests using ``pytest``

The test subsets exposed by ``ctest`` are still rather large and some may take several minutes to run.
If a higher level of specificity is needed, ``pytest`` can be called directly to run groups of tests or individual tests.
Before running ``pytest``, it is important to set environment variables so that the development build of GDAL is tested,
rather than a system version. This can be done by sourcing the following from the build directory:

.. code-block:: bash
. ../scripts/setdevenv.sh
(with adjustments to the above path if the build directory is not a subdirectory of the GDAL source root).
To verify that environment variables were set correctly, you can check the version of a GDAL binary:

.. code-block:: bash
gdalinfo --version
# GDAL 3.7.0dev-5327c149f5-dirty, released 2018/99/99 (debug build)
and the Python bindings:

.. code-block:: bash
python3 -c 'from osgeo import gdal; print(gdal.__version__)'
# 3.7.0dev-5327c149f5-dirty
Before running ``pytest``, it is important to set :ref:`development environment variables <setting_dev_environment_variables>` so that the development build of GDAL is tested, rather than a system version.

Tests can then be run by calling ``pytest``, for example on an individual file.
On Linux and MacOS builds, the tests are symlinked into the build directory, so this
Expand Down
10 changes: 10 additions & 0 deletions scripts/setdevenv.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@echo off

REM This is a very primitive script which must be called from a build
REM directory and set the environment for a Release build

set PATH=%CD%\swig\python\bin;%CD%\apps\Release;%CD%\Release;%PATH%
set GDAL_DATA=%CD%\data
set PYTHONPATH=%CD%\swig\python
set GDAL_DRIVER_PATH=%CD%\gdalplugins\Release
set USE_PATH_FOR_GDAL_PYTHON=yes
4 changes: 3 additions & 1 deletion scripts/setdevenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ CUR_DIR=$PWD

echo "Setting environment for a CMake build from ${CUR_DIR}..."

GDAL_PYTHONPATH="$CUR_DIR/swig/python"

if [[ ! ${PATH} =~ $CUR_DIR/apps ]]; then
export PATH="$CUR_DIR/apps:$PATH"
export PATH="$CUR_DIR/perftests:$PATH"
export PATH="$GDAL_PYTHONPATH/bin:$PATH"
export PATH="$GDAL_ROOT/swig/python/gdal-utils/scripts:$PATH"
echo "Setting PATH=$PATH"
fi
Expand All @@ -59,7 +62,6 @@ if [[ ! "${GDAL_DATA:-}" =~ $CUR_DIR/data ]]; then
echo "Setting GDAL_DATA=$GDAL_DATA"
fi

GDAL_PYTHONPATH="$CUR_DIR/swig/python"
if [[ ! "${PYTHONPATH:-}" =~ $GDAL_PYTHONPATH ]]; then
export PYTHONPATH="$GDAL_PYTHONPATH:${PYTHONPATH:-}"
echo "Setting PYTHONPATH=$PYTHONPATH"
Expand Down
31 changes: 31 additions & 0 deletions swig/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,37 @@ if (Python_Interpreter_FOUND)
endif()
endif()

# Install launcher scripts in bin/ (for development)
file(GLOB SCRIPTS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/gdal-utils/scripts" "${CMAKE_CURRENT_SOURCE_DIR}/gdal-utils/scripts/*.py")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tmp_bin")
foreach(filename IN LISTS SCRIPTS)
get_filename_component(filename_without_ext "${filename}" NAME_WLE)
if (UNIX)
set(lancher_filename "${filename_without_ext}")
else()
set(lancher_filename "${filename_without_ext}.bat")
endif()
set(tmp_launcher_filename "${CMAKE_CURRENT_BINARY_DIR}/tmp_bin/${lancher_filename}")
if (UNIX)
file(WRITE "${tmp_launcher_filename}"
"#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import sys
from osgeo_utils.${filename_without_ext} import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\\.pyw|\\.exe)?\$', '', sys.argv[0])
sys.exit(main())
")
else()
file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/gdal-utils/scripts/${filename}" NATIVE_FILENAME)
file(WRITE "${tmp_launcher_filename}" "@python \"${NATIVE_FILENAME}\" %*")
endif()
file(COPY "${tmp_launcher_filename}"
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/bin"
FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
endforeach()

add_custom_target(python_binding ALL DEPENDS ${PY_SO_LIST} ${PY_SO_LIST_WITH_RPATH} ${GDAL_PYTHON_PYSOURCES} ${GDAL_LIB_TARGET_NAME})

# Generate wheel
Expand Down

0 comments on commit 6bd9d23

Please sign in to comment.