def get_package_version(application_name, app=None):  # noqa
    """
    # >>> get_package_version('django_sysinfo') == django_sysinfo.__version__
    # True
    # >>> with mock.patch('pkg_resources.get_distribution', None):
    # ...     get_package_version('django_sysinfo') == django_sysinfo.__version__
    # True

    :param application_name:
    :param app:
    :return:

    >>> from mock_import import mock_import
    >>> import django_sysinfo, mock
    >>> from mock_import import mock_import
    >>> with mock_import(spec={'VERSION'}):
    ...     import my_module
    ...     my_module.VERSION = '1.0'
    ...     assert get_package_version('my_module', my_module) == '1.0'


    >>> with mock_import(spec=['VERSION'],VERSION='1.0'):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) == '1.0'

    >>> with mock_import(spec=['__version__'], __version__='1.1'):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) == '1.1'

    >>> with mock_import(spec=['get_version'], get_version=lambda : '1.2'):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) == '1.2'

    >>> with mock_import(spec=['get_version'], get_version=mock.MagicMock(side_effect=Exception)):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) is None

    >>> with mock_import(spec=['version'], version='1.3'):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) == '1.3'

    >>> with mock_import(spec=['VERSION'], VERSION=[1,4]):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) == '1.4'

    >>> with mock_import(spec=['VERSION'], VERSION=None):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) is None

    >>> with mock_import(spec=[]):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) is None
    """

    parts = application_name.split('.')
    module_name = parts[0]
    try:
        return pkg_resources.get_distribution(module_name).version
    except Exception:
        pass
    # if app is None:
    #     app = __import__(module_name)
    if hasattr(app, 'get_version'):
        version = app.get_version
    elif hasattr(app, '__version__'):
        version = app.__version__
    elif hasattr(app, 'VERSION'):
        version = app.VERSION
    elif hasattr(app, 'version'):
        version = app.version
    else:
        version = None

    if callable(version):
        try:
            version = version()
        except Exception:
            return None

    if not isinstance(version, six.string_types + (list, tuple)):
        version = None

    if version is None:
        return None

    if isinstance(version, (list, tuple)):
        version = '.'.join(map(six.text_type, version))

    return six.text_type(version)
def get_package_version ( application_name , app = None ) : parts = application_name . split ( '.' ) module_name = parts [ 0 ] try : return pkg_resources . get_distribution ( module_name ) . version except Exception : pass if hasattr ( app , 'get_version' ) : version = app . get_version elif hasattr ( app , '__version__' ) : version = app . __version__ elif hasattr ( app , 'VERSION' ) : version = app . VERSION elif hasattr ( app , 'version' ) : version = app . version else : version = None if callable ( version ) : try : version = version ( ) except Exception : return None if not isinstance ( version , six . string_types + ( list , tuple ) ) : version = None if version is None : return None if isinstance ( version , ( list , tuple ) ) : version = '.' . join ( map ( six . text_type , version ) ) return six . text_type ( version )
# >>> get_package_version('django_sysinfo') == django_sysinfo.__version__
    # True
    # >>> with mock.patch('pkg_resources.get_distribution', None):
    # ...     get_package_version('django_sysinfo') == django_sysinfo.__version__
    # True

    :param application_name:
    :param app:
    :return:

    >>> from mock_import import mock_import
    >>> import django_sysinfo, mock
    >>> from mock_import import mock_import
    >>> with mock_import(spec={'VERSION'}):
    ...     import my_module
    ...     my_module.VERSION = '1.0'
    ...     assert get_package_version('my_module', my_module) == '1.0'


    >>> with mock_import(spec=['VERSION'],VERSION='1.0'):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) == '1.0'

    >>> with mock_import(spec=['__version__'], __version__='1.1'):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) == '1.1'

    >>> with mock_import(spec=['get_version'], get_version=lambda : '1.2'):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) == '1.2'

    >>> with mock_import(spec=['get_version'], get_version=mock.MagicMock(side_effect=Exception)):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) is None

    >>> with mock_import(spec=['version'], version='1.3'):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) == '1.3'

    >>> with mock_import(spec=['VERSION'], VERSION=[1,4]):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) == '1.4'

    >>> with mock_import(spec=['VERSION'], VERSION=None):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) is None

    >>> with mock_import(spec=[]):
    ...     import my_module
    ...     assert get_package_version('my_module', my_module) is None