Skip to content

Commit

Permalink
make version.py more complicated
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Sep 20, 2023
1 parent 640ca9e commit 642241e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ jobs:
- name: Tag Version
id: set_tag
run: |
export VER=$(python -c "print(eval(next(L.split('=')[1] for L in open('pyproject.toml') if 'version' in L)))")
export VER=$(python trimesh/version.py)
echo "::set-output name=tag_name::${VER}"
- name: Create Release
id: create_release
Expand Down
68 changes: 60 additions & 8 deletions trimesh/version.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,65 @@
# get the version trimesh was installed with from metadata
try:
# Python >= 3.8
"""
# version.py
Get the current version from package metadata or pyproject.toml
if everything else fails.
"""


def _get_version():
"""
Try all our methods to get the version.
"""
for method in [_importlib, _pkgresources, _pyproject]:
try:
return method()
except BaseException:
pass
return None


def _importlib() -> str:
"""
Get the version string using package metadata on Python >= 3.8
"""

from importlib.metadata import version
__version__ = version('trimesh')
except BaseException:
# Python < 3.8

return version("trimesh")


def _pkgresources() -> str:
"""
Get the version string using package metadata on Python < 3.8
"""
from pkg_resources import get_distribution
__version__ = get_distribution('trimesh').version

if __name__ == '__main__':
return get_distribution("trimesh").version


def _pyproject() -> str:
"""
Get the version string from the pyproject.toml file.
"""
import json
import os

# use a path relative to this file
pyproject = os.path.abspath(
os.path.join(
os.path.dirname(os.path.abspath(os.path.expanduser(__file__))),
"..",
"pyproject.toml",
)
)
with open(pyproject) as f:
# json.loads cleans up the string and removes the quotes
return next(json.loads(L.split("=")[1]) for L in f if "version" in L)


# try all our tricks
__version__ = _get_version()

if __name__ == "__main__":
# print version if run directly i.e. in a CI script
print(__version__)

0 comments on commit 642241e

Please sign in to comment.