Skip to content

Commit 3860326

Browse files
add helper for distribution versions
1 parent 2aa0de9 commit 3860326

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

apipkg.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ def _py_abspath(path):
2424
return os.path.abspath(path)
2525

2626

27+
def distribution_version(name):
28+
"""try to get the version of the named distribution,
29+
returs None on failure"""
30+
from pkg_resources import get_distribution, DistributionNotFound
31+
try:
32+
dist = get_distribution(name)
33+
except DistributionNotFound:
34+
pass
35+
else:
36+
return dist.version
37+
38+
2739
def initpkg(pkgname, exportdefs, attr=dict()):
2840
""" initialize given package from the export definitions. """
2941
oldmod = sys.modules.get(pkgname)

test_apipkg.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,8 @@ def test_initpkg_without_old_module():
526526
dict(modules="sys:modules"))
527527
from initpkg_without_old_module import modules
528528
assert modules is sys.modules
529+
530+
531+
def test_get_distribution_version():
532+
assert apipkg.distribution_version('setuptools') is not None
533+
assert apipkg.distribution_version('email') is None

0 commit comments

Comments
 (0)