1 33
import sys
2 33
from packaging.version import Version
3 33
from . import _version
4

5 33
__version__ = _version.get_versions()['version']
6

7

8 33
def _cmp(a, b):
9
    """ Implementation of ``cmp`` for Python 3
10
    """
11 33
    return (a > b) - (a < b)
12

13

14 33
def cmp_pkg_version(version_str, pkg_version_str=__version__):
15
    """ Compare ``version_str`` to current package version
16

17
    This comparator follows `PEP-440`_ conventions for determining version
18
    ordering.
19

20
    To be valid, a version must have a numerical major version. It may be
21
    optionally followed by a dot and a numerical minor version, which may,
22
    in turn, optionally be followed by a dot and a numerical micro version,
23
    and / or by an "extra" string.
24
    The extra string may further contain a "+". Any value to the left of a "+"
25
    labels the version as pre-release, while values to the right indicate a
26
    post-release relative to the values to the left. That is,
27
    ``1.2.0+1`` is post-release for ``1.2.0``, while ``1.2.0rc1+1`` is
28
    post-release for ``1.2.0rc1`` and pre-release for ``1.2.0``.
29

30
    Parameters
31
    ----------
32
    version_str : str
33
        Version string to compare to current package version
34
    pkg_version_str : str, optional
35
        Version of our package.  Optional, set fom ``__version__`` by default.
36

37
    Returns
38
    -------
39
    version_cmp : int
40
        1 if `version_str` is a later version than `pkg_version_str`, 0 if
41
        same, -1 if earlier.
42

43
    Examples
44
    --------
45
    >>> cmp_pkg_version('1.2.1', '1.2.0')
46
    1
47
    >>> cmp_pkg_version('1.2.0dev', '1.2.0')
48
    -1
49
    >>> cmp_pkg_version('1.2.0dev', '1.2.0rc1')
50
    -1
51
    >>> cmp_pkg_version('1.2.0rc1', '1.2.0')
52
    -1
53
    >>> cmp_pkg_version('1.2.0rc1+1', '1.2.0rc1')
54
    1
55
    >>> cmp_pkg_version('1.2.0rc1+1', '1.2.0')
56
    -1
57
    >>> cmp_pkg_version('1.2.0.post1', '1.2.0')
58
    1
59

60
    .. _`PEP-440`: https://www.python.org/dev/peps/pep-0440/
61
    """
62 33
    return _cmp(Version(version_str), Version(pkg_version_str))
63

64

65 33
def pkg_commit_hash(pkg_path=None):
66
    """ Get short form of commit hash
67

68
    Versioneer placed a ``_version.py`` file in the package directory. This file
69
    gets updated on installation or ``git archive``.
70
    We inspect the contents of ``_version`` to detect whether we are in a
71
    repository, an archive of the repository, or an installed package.
72

73
    If detection fails, we return a not-found placeholder tuple
74

75
    Parameters
76
    ----------
77
    pkg_path : str
78
       directory containing package
79

80
    Returns
81
    -------
82
    hash_from : str
83
       Where we got the hash from - description
84
    hash_str : str
85
       short form of hash
86
    """
87 33
    versions = _version.get_versions()
88 33
    hash_str = versions['full-revisionid'][:7]
89 33
    if hasattr(_version, 'version_json'):
90 33
        hash_from = 'installation'
91 33
    elif not _version.get_keywords()['full'].startswith('$Format:'):
92 0
        hash_from = 'archive substitution'
93 33
    elif versions['version'] == '0+unknown':
94 0
        hash_from, hash_str = '(none found)', '<not found>'
95
    else:
96 0
        hash_from = 'repository'
97 33
    return hash_from, hash_str
98

99

100 33
def get_pkg_info(pkg_path):
101
    """ Return dict describing the context of this package
102

103
    Parameters
104
    ----------
105
    pkg_path : str
106
       path containing __init__.py for package
107

108
    Returns
109
    -------
110
    context : dict
111
       with named parameters of interest
112
    """
113 33
    src, hsh = pkg_commit_hash()
114 33
    import numpy
115 33
    return dict(
116
        pkg_path=pkg_path,
117
        commit_source=src,
118
        commit_hash=hsh,
119
        sys_version=sys.version,
120
        sys_executable=sys.executable,
121
        sys_platform=sys.platform,
122
        np_version=numpy.__version__)

Read our documentation on viewing source code .

Loading