Skip to content

Commit

Permalink
Merge pull request #3711 from nilsvu/py_main_entry
Browse files Browse the repository at this point in the history
Add a main entry point to the Python package
  • Loading branch information
nilsdeppe authored Mar 6, 2022
2 parents 634089c + c941990 commit 8cd48a6
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 39 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/Tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ ${{ env.CACHE_KEY_SUFFIX }}"
-D SPECTRE_UNIT_TEST_TIMEOUT_FACTOR=${TEST_TIMEOUT_FACTOR:-'1'}
-D SPECTRE_INPUT_FILE_TEST_TIMEOUT_FACTOR=${TEST_TIMEOUT_FACTOR:-'1'}
-D SPECTRE_PYTHON_TEST_TIMEOUT_FACTOR=${TEST_TIMEOUT_FACTOR:-'1'}
-D CMAKE_INSTALL_PREFIX=/work/spectre_install
--warn-uninitialized
$GITHUB_WORKSPACE 2>&1 | tee CMakeOutput.txt 2>&1
- name: Check for CMake warnings
Expand Down Expand Up @@ -522,6 +523,17 @@ ${{ env.CACHE_KEY_SUFFIX }}"
# We get occasional random timeouts, repeat tests to see if
# it is a random timeout or systematic
ctest -j2 -LE unit --output-on-failure --repeat after-timeout:3
- name: Install
working-directory: /work/build
# Make sure the `install` target runs without error. We could add some
# basic smoke tests here to make sure the installation worked.
run: |
make install
- name: Print size of install directory
working-directory: /work/spectre_install
run: |
ls | xargs du -sh
du -sh .
- name: Test formaline tar can be built
if: matrix.build_type == 'Debug'
working-directory: /work/build
Expand Down Expand Up @@ -716,6 +728,7 @@ ${{ env.CACHE_KEY_SUFFIX }}"
-D USE_PCH=ON \
-D USE_CCACHE=ON \
-D SPECTRE_TEST_TIMEOUT_FACTOR=5 \
-D CMAKE_INSTALL_PREFIX=../install \
$GITHUB_WORKSPACE
- name: Build tests
working-directory: build
Expand All @@ -739,6 +752,15 @@ ${{ env.CACHE_KEY_SUFFIX }}"
working-directory: build
run: |
ctest -j3 --repeat after-timeout:3 --output-on-failure
- name: Install
working-directory: build
run: |
make install
- name: Print size of install directory
working-directory: install
run: |
ls | xargs du -sh
du -sh .
# Retain caches only on the 'develop' branch (see unit_tests job)
- name: Clear caches
if: github.ref != 'refs/heads/develop'
Expand Down
2 changes: 1 addition & 1 deletion cmake/SpectrePythonExecutable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# See LICENSE.txt for details.

PYTHONPATH="@CMAKE_BINARY_DIR@/bin/python:$PYTHONPATH" @Python_EXECUTABLE@ \
-m @PYTHON_MODULE_LOCATION@.@PYTHON_FILE_JUST_NAME_WE@ "$@"
-m @PYTHON_SCRIPT_LOCATION@ "$@"
44 changes: 33 additions & 11 deletions cmake/SpectreSetupPythonPackage.cmake
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

set(SPECTRE_PYTHON_INSTALL_LIBDIR
"lib/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages"
CACHE STRING "Location where the Python package is installed. Defaults to \
CMAKE_INSTALL_PREFIX/lib/pythonX.Y/site-packages/.")

spectre_define_test_timeout_factor_option(PYTHON "Python")

set(SPECTRE_PYTHON_PREFIX "${CMAKE_BINARY_DIR}/bin/python/spectre/")
Expand All @@ -23,10 +18,28 @@ if(NOT EXISTS "${SPECTRE_PYTHON_PREFIX}/__init__.py")
"__all__ = []")
endif()

# Write a file for installing the Python modules
# Create the root __main__.py entry point
configure_file(
"${CMAKE_SOURCE_DIR}/support/Python/__main__.py"
"${SPECTRE_PYTHON_PREFIX}/__main__.py"
)
# Also link the main entry point to bin/
set(PYTHON_SCRIPT_LOCATION "spectre")
configure_file(
"${CMAKE_SOURCE_DIR}/cmake/SpectrePythonExecutable.sh"
"${CMAKE_BINARY_DIR}/tmp/spectre")
file(COPY "${CMAKE_BINARY_DIR}/tmp/spectre"
DESTINATION "${CMAKE_BINARY_DIR}/bin"
FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ
GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

# Write configuration files for installing the Python modules
configure_file(
"${CMAKE_SOURCE_DIR}/src/PythonBindings/pyproject.toml"
"${SPECTRE_PYTHON_PREFIX_PARENT}/pyproject.toml")
configure_file(
"${CMAKE_SOURCE_DIR}/src/PythonBindings/setup.py"
"${SPECTRE_PYTHON_PREFIX_PARENT}/setup.py")
"${CMAKE_SOURCE_DIR}/src/PythonBindings/setup.cfg"
"${SPECTRE_PYTHON_PREFIX_PARENT}/setup.cfg")

set(_JEMALLOC_MESSAGE "")
if(BUILD_PYTHON_BINDINGS AND "${JEMALLOC_LIB_TYPE}" STREQUAL SHARED)
Expand All @@ -47,10 +60,17 @@ configure_file(
"${CMAKE_BINARY_DIR}/tmp/LoadPython.sh"
"${CMAKE_BINARY_DIR}/bin/LoadPython.sh")

# Install the SpECTRE Python package to the user-specified location.
# Install the SpECTRE Python package to the CMAKE_INSTALL_PREFIX, using pip.
# This will install the package into the expected subdirectory, typically
# `lib/pythonX.Y/site-packages/`. It also creates symlinks to entry points
# specified in `setup.py`.
install(
DIRECTORY ${SPECTRE_PYTHON_PREFIX}
DESTINATION ${SPECTRE_PYTHON_INSTALL_LIBDIR}
CODE "execute_process(\
COMMAND ${Python_EXECUTABLE} -m pip install \
--no-deps --no-input --no-cache-dir --no-index --ignore-installed \
--disable-pip-version-check --no-build-isolation \
--prefix ${CMAKE_INSTALL_PREFIX} ${SPECTRE_PYTHON_PREFIX_PARENT} \
)"
)

add_custom_target(all-pybindings)
Expand Down Expand Up @@ -257,6 +277,8 @@ function(SPECTRE_PYTHON_ADD_MODULE MODULE_NAME)
# Write an executable in `${CMAKE_BINARY_DIR}/bin` that runs the Python
# script in the correct Python environment
if(${PYTHON_FILE} IN_LIST ARG_PYTHON_EXECUTABLES)
set(PYTHON_SCRIPT_LOCATION
"${PYTHON_MODULE_LOCATION}.${PYTHON_FILE_JUST_NAME_WE}")
configure_file(
"${CMAKE_SOURCE_DIR}/cmake/SpectrePythonExecutable.sh"
"${CMAKE_BINARY_DIR}/tmp/${PYTHON_FILE_JUST_NAME_WE}")
Expand Down
4 changes: 0 additions & 4 deletions docs/DevGuide/BuildSystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,6 @@ cmake -D FLAG1=OPT1 ... -D FLAGN=OPTN <SPECTRE_ROOT>
- Location where the `install` target copies executables, libraries, etc. Make
sure to set this variable before you `install`, or a default location such
as `/usr/local` is used.
- SPECTRE_PYTHON_INSTALL_LIBDIR
- Location where the `install` target copies the SpECTRE Python package.
Defaults to `CMAKE_INSTALL_PREFIX/lib/pythonX.Y/site-packages/`, which is a
location where Python packages are often expected.
- DEBUG_SYMBOLS
- Whether or not to use debug symbols (default is `ON`)
- Disabling debug symbols will reduce compile time and total size of the build
Expand Down
6 changes: 6 additions & 0 deletions src/PythonBindings/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
25 changes: 25 additions & 0 deletions src/PythonBindings/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

[metadata]
name = spectre
version = @SPECTRE_VERSION@
description = Python bindings for SpECTRE
author = SXS collaboration
url = @SPECTRE_HOMEPAGE@
license = MIT

[options]
packages = find:
install_requires =
h5py >= 3.0.0
numpy
scipy

[options.package_data]
# Install Python bindings libs alongside the Python code
* = *.so

[options.entry_points]
console_scripts =
spectre = spectre.__main__:main
23 changes: 0 additions & 23 deletions src/PythonBindings/setup.py

This file was deleted.

27 changes: 27 additions & 0 deletions support/Python/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

SPECTRE_VERSION = "@SPECTRE_VERSION@"


def main():
import argparse

class HelpFormatter(argparse.ArgumentDefaultsHelpFormatter,
argparse.RawDescriptionHelpFormatter):
pass

parser = argparse.ArgumentParser(
prog='spectre',
description="SpECTRE version: {}".format(SPECTRE_VERSION),
formatter_class=HelpFormatter)

# Version endpoint
parser.add_argument('--version', action='version', version=SPECTRE_VERSION)

parser.parse_args()
raise NotImplementedError("No subprograms are implemented yet.")


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

add_subdirectory(support)
add_subdirectory(Unit)
4 changes: 4 additions & 0 deletions tests/support/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

add_subdirectory(Python)
11 changes: 11 additions & 0 deletions tests/support/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

if (BUILD_PYTHON_BINDINGS)
add_test(NAME "support.Python.main"
COMMAND ${CMAKE_BINARY_DIR}/bin/spectre --version)
set_tests_properties(
"support.Python.main" PROPERTIES
PASS_REGULAR_EXPRESSION "${SPECTRE_VERSION}"
LABELS "python")
endif()

0 comments on commit 8cd48a6

Please sign in to comment.