Skip to content

Commit

Permalink
MAINT: Added discovery via PKG-INFO file
Browse files Browse the repository at this point in the history
  • Loading branch information
pastewka committed Mar 22, 2024
1 parent 2b5804e commit e212128
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
This file lists all changes to the code

v0.1.7 (22Mar24)
v0.1.8 (22Mar24)
----------------

* MAINT: Added discovery via PKG-INFO file

* v0.1.7 (22Mar24)
----------------

* BUG: Use __name__ for package name
Expand Down
34 changes: 27 additions & 7 deletions DiscoverVersion/Discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ class CannotDiscoverVersion(Exception):
pass


def get_version_from_pkginfo():
"""
Discover version from PKG-INFO file.
"""
if not os.path.exists('PKG-INFO'):
raise CannotDiscoverVersion('PKG-INFO file does not exist.')

with open('PKG-INFO', 'r') as f:
for line in f:
if line.startswith('Version: '):
return line.split()[1]

raise CannotDiscoverVersion('Version not found in PKG-INFO.')


def get_version_from_git(package_name):
"""
Discover version from git repository.
Expand Down Expand Up @@ -64,7 +79,7 @@ def get_version_from_git(package_name):
return version


def get_version(package_name, use_git=True, use_importlib=True):
def get_version(package_name, use_git=True, use_importlib=True, use_pkginfo=True):
"""
Discover version of package `package_name`.
Expand All @@ -76,6 +91,8 @@ def get_version(package_name, use_git=True, use_importlib=True):
Try to discover version from git. (Default: true)
use_importlib : bool, optional
Try to discover version from importlib. (Default: true)
use_pkginfo : bool, optional
Try to discover version from PKG-INFO file. (Default: true)
Returns
-------
Expand All @@ -85,8 +102,6 @@ def get_version(package_name, use_git=True, use_importlib=True):
discovered_version = None
tried = ''

print('package_name:', package_name)

# If package_name is a submodule, we need to strip the submodule part
s = package_name.split('.')
package_name = s[0]
Expand All @@ -98,26 +113,31 @@ def get_version(package_name, use_git=True, use_importlib=True):
if discovered_version is None and use_git:
try:
discovered_version = get_version_from_git(package_name)
print('git:', discovered_version)
except CannotDiscoverVersion:
tried += ', git'
discovered_version = None

# importlib is present in Python >= 3.8
if discovered_version is None and use_importlib:
if discovered_version is None and package_name != 'flit_core' and use_importlib:
try:
from importlib.metadata import version

discovered_version = version(package_name)
print('importlib:', discovered_version)
except ImportError:
tried += ', importlib'
discovered_version = None

# pkg-info
if discovered_version is None and use_pkginfo:
try:
discovered_version = get_version_from_pkginfo()
except CannotDiscoverVersion:
tried += ', PKG-INFO'
discovered_version = None

# Nope. Out of options.

if discovered_version is None:
raise CannotDiscoverVersion(f'Tried: {tried[2:]}')

return discovered_version

2 changes: 1 addition & 1 deletion test/test_version_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from DiscoverVersion import __version__, get_version


_current_version = '0.1.7'
_current_version = '0.1.8'


def test_version_discovery():
Expand Down

0 comments on commit e212128

Please sign in to comment.