|
| 1 | +# |
| 2 | +# Copyright 2024 Lars Pastewka |
| 3 | +# |
| 4 | +# ### MIT license |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in |
| 14 | +# all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +# SOFTWARE. |
| 23 | +# |
| 24 | + |
| 25 | +""" |
| 26 | +Meson-python metadata provider for DiscoverVersion. |
| 27 | +
|
| 28 | +This module provides version discovery integration with meson-python build |
| 29 | +backend. To use it, add the following to your pyproject.toml: |
| 30 | +
|
| 31 | + [tool.meson-python.metadata] |
| 32 | + version.provider = "DiscoverVersion.meson_python" |
| 33 | +
|
| 34 | +""" |
| 35 | + |
| 36 | +from pathlib import Path |
| 37 | +from typing import Any, Mapping |
| 38 | + |
| 39 | +from .discovery import ( |
| 40 | + CannotDiscoverVersion, |
| 41 | + get_version_from_env, |
| 42 | + get_version_from_git, |
| 43 | + get_version_from_pkginfo, |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +class MesonPythonMetadataProvider: |
| 48 | + """ |
| 49 | + Metadata provider for meson-python. |
| 50 | +
|
| 51 | + This class follows the meson-python metadata provider interface. |
| 52 | + """ |
| 53 | + |
| 54 | + def __call__(self, source_dir: Path) -> Mapping[str, Any]: |
| 55 | + """ |
| 56 | + Return package metadata including version. |
| 57 | +
|
| 58 | + Parameters |
| 59 | + ---------- |
| 60 | + source_dir : Path |
| 61 | + Path to the source directory. |
| 62 | +
|
| 63 | + Returns |
| 64 | + ------- |
| 65 | + dict |
| 66 | + Dictionary with 'version' key. |
| 67 | + """ |
| 68 | + version = None |
| 69 | + |
| 70 | + # Check environment variable override first (highest priority) |
| 71 | + version = get_version_from_env() |
| 72 | + |
| 73 | + # Try PKG-INFO (for sdist builds) |
| 74 | + if version is None: |
| 75 | + try: |
| 76 | + import os |
| 77 | + old_cwd = os.getcwd() |
| 78 | + os.chdir(source_dir) |
| 79 | + try: |
| 80 | + version = get_version_from_pkginfo() |
| 81 | + finally: |
| 82 | + os.chdir(old_cwd) |
| 83 | + except CannotDiscoverVersion: |
| 84 | + pass |
| 85 | + |
| 86 | + # Try git |
| 87 | + if version is None: |
| 88 | + try: |
| 89 | + version = get_version_from_git(source_dir) |
| 90 | + except CannotDiscoverVersion: |
| 91 | + pass |
| 92 | + |
| 93 | + if version is None: |
| 94 | + raise CannotDiscoverVersion( |
| 95 | + "Could not discover version from environment, PKG-INFO, or git" |
| 96 | + ) |
| 97 | + |
| 98 | + return {"version": version} |
| 99 | + |
| 100 | + |
| 101 | +# Module-level callable for meson-python |
| 102 | +__call__ = MesonPythonMetadataProvider() |
0 commit comments