Do a cmp-style comparison on two packages. Return -1 if pkg1 < pkg2, 0 if
    pkg1 == pkg2, and 1 if pkg1 > pkg2. Return None if there was a problem
    making the comparison.

    ignore_epoch : False
        Set to ``True`` to ignore the epoch when comparing versions

        .. versionadded:: 2015.8.10,2016.3.2

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.version_cmp '0.2.4-0ubuntu1' '0.2.4.1-0ubuntu1'

def version_cmp(pkg1, pkg2, ignore_epoch=False, **kwargs):
    '''
    Do a cmp-style comparison on two packages. Return -1 if pkg1 < pkg2, 0 if
    pkg1 == pkg2, and 1 if pkg1 > pkg2. Return None if there was a problem
    making the comparison.

    ignore_epoch : False
        Set to ``True`` to ignore the epoch when comparing versions

        .. versionadded:: 2015.8.10,2016.3.2

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.version_cmp '0.2.4-0ubuntu1' '0.2.4.1-0ubuntu1'
    '''
    normalize = lambda x: six.text_type(x).split(':', 1)[-1] \
                if ignore_epoch else six.text_type(x)
    # both apt_pkg.version_compare and _cmd_quote need string arguments.
    pkg1 = normalize(pkg1)
    pkg2 = normalize(pkg2)

    # if we have apt_pkg, this will be quickier this way
    # and also do not rely on shell.
    if HAS_APTPKG:
        try:
            # the apt_pkg module needs to be manually initialized
            apt_pkg.init_system()

            # if there is a difference in versions, apt_pkg.version_compare will
            # return an int representing the difference in minor versions, or
            # 1/-1 if the difference is smaller than minor versions. normalize
            # to -1, 0 or 1.
            try:
                ret = apt_pkg.version_compare(pkg1, pkg2)
            except TypeError:
                ret = apt_pkg.version_compare(six.text_type(pkg1), six.text_type(pkg2))
            return 1 if ret > 0 else -1 if ret < 0 else 0
        except Exception:
            # Try to use shell version in case of errors w/python bindings
            pass
    try:
        for oper, ret in (('lt', -1), ('eq', 0), ('gt', 1)):
            cmd = ['dpkg', '--compare-versions', pkg1, oper, pkg2]
            retcode = __salt__['cmd.retcode'](cmd,
                                              output_loglevel='trace',
                                              python_shell=False,
                                              ignore_retcode=True)
            if retcode == 0:
                return ret
    except Exception as exc:
        log.error(exc)
    return None