Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Gracefully handle errors getting package versions (fixes GH-143)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Aug 22, 2012
1 parent e15b0e3 commit 5c49f87
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions raven/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

import hashlib
import hmac
import logging
try:
import pkg_resources
except ImportError:
pkg_resources = None
import sys

logger = logging.getLogger('raven.errors')


def varmap(func, var, context=None, name=None):
"""
Expand Down Expand Up @@ -41,6 +44,32 @@ def varmap(func, var, context=None, name=None):
_VERSION_CACHE = {}


def get_version_from_app(module_name, app):
if hasattr(app, 'get_version'):
get_version = app.get_version
if callable(get_version):
version = get_version()
else:
version = get_version
elif hasattr(app, 'VERSION'):
version = app.VERSION
elif hasattr(app, '__version__'):
version = app.__version__
elif pkg_resources:
# pull version from pkg_resources if distro exists
try:
version = pkg_resources.get_distribution(module_name).version
except pkg_resources.DistributionNotFound:
return None
else:
return None

if isinstance(version, (list, tuple)):
version = '.'.join(str(o) for o in version)

return version


def get_versions(module_list=None):
if not module_list:
return {}
Expand All @@ -57,28 +86,18 @@ def get_versions(module_list=None):
__import__(module_name)
except ImportError:
continue
app = sys.modules[module_name]
if hasattr(app, 'get_version'):
get_version = app.get_version
if callable(get_version):
version = get_version()
else:
version = get_version
elif hasattr(app, 'VERSION'):
version = app.VERSION
elif hasattr(app, '__version__'):
version = app.__version__
elif pkg_resources:
# pull version from pkg_resources if distro exists
try:
version = pkg_resources.get_distribution(module_name).version
except pkg_resources.DistributionNotFound:
version = None
else:

try:
app = sys.modules[module_name]
except KeyError:
continue

try:
version = get_version_from_app(module_name, app)
except Exception, e:
logger.exception(e)
version = None

if isinstance(version, (list, tuple)):
version = '.'.join(str(o) for o in version)
_VERSION_CACHE[module_name] = version
else:
version = _VERSION_CACHE[module_name]
Expand Down

0 comments on commit 5c49f87

Please sign in to comment.