Skip to content

Commit

Permalink
Python: get version from CMake and export it as __version__
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Dec 13, 2023
1 parent 21c857a commit 8339910
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 23 deletions.
37 changes: 19 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

include(VersionSource)
find_package(GEOS REQUIRED)
if (GEOS_VERSION_MAJOR LESS 3 OR GEOS_VERSION_MINOR LESS 5)
message(FATAL_ERROR "GEOS version 3.5 or later is required.")
endif()

message(STATUS "Source version: " ${EXACTEXTRACT_VERSION_SOURCE})
set(SRC_GENERATED ${CMAKE_CURRENT_BINARY_DIR}/generated)
configure_file(src/version.h.in ${SRC_GENERATED}/version.h)

# Define coverage build type
set(CMAKE_CXX_FLAGS_COVERAGE "-fprofile-arcs -ftest-coverage")

# Make sure we know our build type
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified")
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}")
endif()

#Configure some options the various components this module can build
option(BUILD_CLI "Build the exactextract cli binary" ON) #requires gdal, cli11
Expand Down Expand Up @@ -85,7 +101,7 @@ if(BUILD_CLI)
subdivide
PRIVATE
${GDAL_INCLUDE_DIR}
${CMAKE_BINARY_DIR}/generated
${SRC_GENERATED}
)

target_link_libraries(
Expand All @@ -98,7 +114,7 @@ if(BUILD_CLI)
target_include_directories(
${BIN_NAME}
PRIVATE
${CMAKE_BINARY_DIR}/generated
${SRC_GENERATED}
${CMAKE_SOURCE_DIR}/src
)

Expand Down Expand Up @@ -178,21 +194,6 @@ if(BUILD_TEST)

endif() #BUILD_TEST

message(STATUS "Source version: " ${EXACTEXTRACT_VERSION_SOURCE})
configure_file(src/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/generated/version.h)

if (GEOS_VERSION_MAJOR LESS 3 OR GEOS_VERSION_MINOR LESS 5)
message(FATAL_ERROR "GEOS version 3.5 or later is required.")
endif()

# Define coverage build type
set(CMAKE_CXX_FLAGS_COVERAGE "-fprofile-arcs -ftest-coverage")

# Make sure we know our build type
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified")
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}")
endif()

set(PROJECT_SOURCES
src/measures.cpp
Expand Down Expand Up @@ -249,7 +250,7 @@ set(PROJECT_SOURCES
src/weighted_quantiles.h
src/weighted_quantiles.cpp
src/variance.h
${CMAKE_CURRENT_BINARY_DIR}/generated/version.h
${SRC_GENERATED}/version.h
)

add_library(${LIB_NAME} ${PROJECT_SOURCES})
Expand Down
2 changes: 2 additions & 0 deletions cmake/VersionSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ if(EXISTS ${CMAKE_SOURCE_DIR}/.git/HEAD)
endif()
endif()

message("exactextract version: ${EXACTEXTRACT_VERSION_SOURCE}")

unset(dirty)
unset(head)
8 changes: 5 additions & 3 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ src/pybindings/processor_bindings.h
src/pybindings/raster_source_bindings.cpp
src/pybindings/raster_source_bindings.h
src/pybindings/writer_bindings.cpp
src/pybindings/writer_bindings.h)
src/pybindings/writer_bindings.h
)

pybind11_add_module(_exactextract MODULE ${PYBIND_SOURCES})
target_include_directories(_exactextract PRIVATE
${CMAKE_SOURCE_DIR}/src
${SRC_GENERATED}
${GEOS_INCLUDE_DIR})
set_property(TARGET ${LIB_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
target_link_libraries(_exactextract PRIVATE ${LIB_NAME} ${GEOS_LIBRARY})
set_property(TARGET ${LIB_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
target_link_libraries(_exactextract PRIVATE ${LIB_NAME} ${GEOS_LIBRARY})

add_test(NAME "pybindings"
COMMAND ${CMAKE_COMMAND} -E env
Expand Down
1 change: 0 additions & 1 deletion python/project.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ url = https://github.com/isciences/exactextract
title = exactextract Documentation
description = Fast and accurate raster zonal statistics
long_description = exactextract provides a fast and accurate algorithm for summarizing values in the portion of a raster dataset that is covered by a polygon, often referred to as zonal statistics. Unlike other zonal statistics implementations, it takes into account raster cells that are partially covered by the polygon.
version = 0.2.0
tag =
11 changes: 10 additions & 1 deletion python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ def move_output(self, ext):
self.copy_file(str(source_path), str(dest_path))


def find_version():
version_script = os.path.abspath(
os.path.join(os.path.dirname(__file__), os.pardir, 'cmake', 'VersionSource.cmake'))
version = subprocess.run(['cmake', '-P', version_script],
capture_output=True,
text=True).stderr.split(':')[1].strip()
print(f"Version: {version}")
return version

# The information here can also be placed in setup.cfg - better separation of
# logic and declaration, and simpler if you include description/version in a file.

Expand All @@ -92,8 +101,8 @@ def move_output(self, ext):
url = config['base']['url']
title = config['base']['title']
description = config['base']['description']
version = config['base']['version']
tag = config['base']['tag']
version = find_version()

setup(
name=project,
Expand Down
1 change: 1 addition & 0 deletions python/src/exactextract/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Python bindings for exactextract """
from _exactextract import __version__

from .exact_extract import exact_extract
from .feature import Feature, GDALFeature, JSONFeature
Expand Down
3 changes: 3 additions & 0 deletions python/src/pybindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
#include "operation_bindings.h"
#include "processor_bindings.h"
#include "raster_source_bindings.h"
#include "version.h"
#include "writer_bindings.h"

namespace py = pybind11;

namespace exactextract {
PYBIND11_MODULE(_exactextract, m)
{
m.attr("__version__") = version();

bind_feature(m);
bind_feature_source(m);
bind_raster_source(m);
Expand Down
6 changes: 6 additions & 0 deletions python/tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import exactextract
import re

def test_version():

assert re.match('[0-9]+[.][0-9]+[.][0-9]+', exactextract.__version__)

0 comments on commit 8339910

Please sign in to comment.