File: _version.py

package info (click to toggle)
pycorrfit 1.1.7%2Bnopack-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,652 kB
  • sloc: python: 15,100; makefile: 58; sh: 13
file content (141 lines) | stat: -rw-r--r-- 4,918 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
"""Determine package version for git repositories from tags

Each time this file is imported it checks if the ".git" folder is
present and if so, obtains the version from the git history using
`git describe`. This information is then stored in the file
`_version_save.py` which is not versioned by git, but distributed
along e.g. on PyPI.
"""
from __future__ import print_function

# Put the entire script into a `True` statement and add the hint
# `pragma: no cover` to ignore code coverage here.
if True:  # pragma: no cover
    import imp
    import os
    from os.path import abspath, basename, dirname, join
    import subprocess
    import sys
    import time
    import traceback
    import warnings

    def git_describe():
        """
        Return a string describing the version returned by the
        command `git describe --tags HEAD`.
        If it is not possible to determine the correct version,
        then an empty string is returned.
        """
        # Make sure we are in a directory that belongs to the correct
        # repository.
        ourdir = dirname(abspath(__file__))

        def _minimal_ext_cmd(cmd):
            # construct minimal environment
            env = {}
            for k in ['SYSTEMROOT', 'PATH']:
                v = os.environ.get(k)
                if v is not None:
                    env[k] = v
            # LANGUAGE is used on win32
            env['LANGUAGE'] = 'C'
            env['LANG'] = 'C'
            env['LC_ALL'] = 'C'
            pop = subprocess.Popen(cmd,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   env=env)
            out = pop.communicate()[0]
            return out

        # change directory
        olddir = abspath(os.curdir)
        os.chdir(ourdir)

        try:
            out = _minimal_ext_cmd(['git', 'describe', '--tags', 'HEAD'])
            git_revision = out.strip().decode('ascii')
        except OSError:
            git_revision = ""

        # go back to original directory
        os.chdir(olddir)

        return git_revision

    def load_version(versionfile):
        """load version from version_save.py"""
        longversion = ""
        try:
            _version_save = imp.load_source("_version_save", versionfile)
            longversion = _version_save.longversion
        except BaseException:
            try:
                from ._version_save import longversion
            except BaseException:
                try:
                    from _version_save import longversion
                except BaseException:
                    pass

        return longversion

    def save_version(version, versionfile):
        """save version to version_save.py"""
        data = "#!/usr/bin/env python3\n" \
            + "# This file was created automatically\n" \
            + "longversion = '{VERSION}'\n"
        try:
            with open(versionfile, "w") as fd:
                fd.write(data.format(VERSION=version))
        except BaseException:
            msg = "Could not write package version to {}.".format(versionfile)
            warnings.warn(msg)

    hdir = dirname(abspath(__file__))
    if basename(__file__) == "conf.py" and "name" in locals():
        # This script is executed in conf.py from the docs directory
        versionfile = join(join(join(hdir, ".."),
                                name),  # noqa: F821
                           "_version_save.py")
    else:
        # This script is imported as a module
        versionfile = join(hdir, "_version_save.py")

    # Determine the accurate version
    longversion = ""

    # 1. git describe
    try:
        # Get the version using `git describe`
        longversion = git_describe()
    except BaseException:
        pass

    # 2. previously created version file
    if longversion == "":
        # Either this is this is not a git repository or we are in the
        # wrong git repository.
        # Get the version from the previously generated `_version_save.py`
        longversion = load_version(versionfile)

    # 3. last resort: date
    if longversion == "":
        print("Could not determine version. Reason:")
        print(traceback.format_exc())
        ctime = os.stat(__file__)[8]
        longversion = time.strftime("%Y.%m.%d-%H-%M-%S", time.gmtime(ctime))
        print("Using creation time as version: {}".format(longversion))

    if not hasattr(sys, 'frozen'):
        # Save the version to `_version_save.py` to allow distribution using
        # `python3 setup.py sdist`.
        # This is only done if the program is not frozen (with e.g.
        # pyinstaller),
        if longversion != load_version(versionfile):
            save_version(longversion, versionfile)

    # PEP 440-conform development version:
    version = ".post".join(longversion.split("-")[:2])