-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
61 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__) |