Skip to content

Commit 642241e

Browse files
committed
make version.py more complicated
1 parent 640ca9e commit 642241e

2 files changed

Lines changed: 61 additions & 9 deletions

File tree

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ jobs:
132132
- name: Tag Version
133133
id: set_tag
134134
run: |
135-
export VER=$(python -c "print(eval(next(L.split('=')[1] for L in open('pyproject.toml') if 'version' in L)))")
135+
export VER=$(python trimesh/version.py)
136136
echo "::set-output name=tag_name::${VER}"
137137
- name: Create Release
138138
id: create_release

trimesh/version.py

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,65 @@
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+
426
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+
"""
835
from pkg_resources import get_distribution
9-
__version__ = get_distribution('trimesh').version
1036

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__":
1264
# print version if run directly i.e. in a CI script
1365
print(__version__)

0 commit comments

Comments
 (0)