scikit-build-core icon indicating copy to clipboard operation
scikit-build-core copied to clipboard

Executing Python commands from pyproject.toml

Open ahnaf-tahmid-chowdhury opened this issue 3 months ago • 5 comments

Description

I'm working on a project that maintains its version in CMakeLists.txt. The version string follows the format x.x.x or x.x.x-pre. To extract this version number and ensure its validity, I've added some configuration to pyproject.toml and created a Python script for validation.

The configuration in pyproject.toml looks like this:

# Get version from CMakeLists
[tool.scikit-build.metadata.version]
provider = "scikit_build_core.metadata.regex"
input = "CMakeLists.txt"
regex = 'SET\(PACKAGE_VERSION "(?P<value>[0-9.]+(-[a-z]+)?)"\)'

# Add the version to the package
[[tool.scikit-build.generate]]
path = "pymoab/_version.py"
template = '''
version = "${version}"
'''

And here's the Python script for validation (validate_version.py):

import re
import sys

def validate_version(version):
    pattern = re.compile(r'^[0-9]+\.[0-9]+\.[0-9]+(-[a-z]+)?$')
    if not pattern.match(version):
        raise ValueError(f"Invalid version string: {version}")

if __name__ == "__main__":
    validate_version(sys.argv[1])

Question

I'm unsure how to call the validate_version.py script from pyproject.toml. While I know I could use setup.py or add a command in CMakeLists.txt to run the Python script, I'm wondering if there's a way to execute Python commands directly from pyproject.toml.

ahnaf-tahmid-chowdhury avatar Apr 04 '24 19:04 ahnaf-tahmid-chowdhury