|
1 | | -# get the version trimesh was installed with from metadata |
2 | | -try: |
3 | | - # Python >= 3.8 |
| 1 | +""" |
| 2 | +# version.py |
| 3 | +
|
| 4 | +Get the current version from package metadata or pyproject.toml |
| 5 | +if everything else fails. |
| 6 | +""" |
| 7 | + |
| 8 | + |
| 9 | +def _get_version(): |
| 10 | + """ |
| 11 | + Try all our methods to get the version. |
| 12 | + """ |
| 13 | + for method in [_importlib, _pkgresources, _pyproject]: |
| 14 | + try: |
| 15 | + return method() |
| 16 | + except BaseException: |
| 17 | + pass |
| 18 | + return None |
| 19 | + |
| 20 | + |
| 21 | +def _importlib() -> str: |
| 22 | + """ |
| 23 | + Get the version string using package metadata on Python >= 3.8 |
| 24 | + """ |
| 25 | + |
4 | 26 | from importlib.metadata import version |
5 | | - __version__ = version('trimesh') |
6 | | -except BaseException: |
7 | | - # Python < 3.8 |
| 27 | + |
| 28 | + return version("trimesh") |
| 29 | + |
| 30 | + |
| 31 | +def _pkgresources() -> str: |
| 32 | + """ |
| 33 | + Get the version string using package metadata on Python < 3.8 |
| 34 | + """ |
8 | 35 | from pkg_resources import get_distribution |
9 | | - __version__ = get_distribution('trimesh').version |
10 | 36 |
|
11 | | -if __name__ == '__main__': |
| 37 | + return get_distribution("trimesh").version |
| 38 | + |
| 39 | + |
| 40 | +def _pyproject() -> str: |
| 41 | + """ |
| 42 | + Get the version string from the pyproject.toml file. |
| 43 | + """ |
| 44 | + import json |
| 45 | + import os |
| 46 | + |
| 47 | + # use a path relative to this file |
| 48 | + pyproject = os.path.abspath( |
| 49 | + os.path.join( |
| 50 | + os.path.dirname(os.path.abspath(os.path.expanduser(__file__))), |
| 51 | + "..", |
| 52 | + "pyproject.toml", |
| 53 | + ) |
| 54 | + ) |
| 55 | + with open(pyproject) as f: |
| 56 | + # json.loads cleans up the string and removes the quotes |
| 57 | + return next(json.loads(L.split("=")[1]) for L in f if "version" in L) |
| 58 | + |
| 59 | + |
| 60 | +# try all our tricks |
| 61 | +__version__ = _get_version() |
| 62 | + |
| 63 | +if __name__ == "__main__": |
12 | 64 | # print version if run directly i.e. in a CI script |
13 | 65 | print(__version__) |
0 commit comments