|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +"""Calculates the current version number. |
| 4 | +
|
| 5 | +If possible, uses output of “git describe” modified to conform to the |
| 6 | +visioning scheme that setuptools uses (see PEP 386). Releases must be |
| 7 | +labelled with annotated tags (signed tags are annotated) of the following |
| 8 | +format: |
| 9 | +
|
| 10 | + v<num>(.<num>)+ [ {a|b|c|rc} <num> (.<num>)* ] |
| 11 | +
|
| 12 | +If “git describe” returns an error (likely because we're in an unpacked copy |
| 13 | +of a release tarball, rather than a git working copy), or returns a tag that |
| 14 | +does not match the above format, version is read from RELEASE-VERSION file. |
| 15 | +
|
| 16 | +To use this script, simply import it your setup.py file, and use the results |
| 17 | +of get_version() as your package version: |
| 18 | +
|
| 19 | + import version |
| 20 | + setup( |
| 21 | + version=version.get_version(), |
| 22 | + . |
| 23 | + . |
| 24 | + . |
| 25 | + ) |
| 26 | +
|
| 27 | +This will automatically update the RELEASE-VERSION file. The RELEASE-VERSION |
| 28 | +file should *not* be checked into git but it *should* be included in sdist |
| 29 | +tarballs (as should version.py file). To do this, run: |
| 30 | +
|
| 31 | + echo include RELEASE-VERSION version.py >>MANIFEST.in |
| 32 | + echo RELEASE-VERSION >>.gitignore |
| 33 | +
|
| 34 | +With that setup, a new release can be labelled by simply invoking: |
| 35 | +
|
| 36 | + git tag -s v1.0 |
| 37 | +""" |
| 38 | + |
| 39 | +__author__ = ("Douglas Creager <dcreager@dcreager.net>", "Michal Nazarewicz <mina86@mina86.com>") |
| 40 | +__license__ = "This file is placed into the public domain." |
| 41 | +__maintainer__ = "Michal Nazarewicz" |
| 42 | +__email__ = "mina86@mina86.com" |
| 43 | + |
| 44 | +__all__ = "get_version" |
| 45 | + |
| 46 | + |
| 47 | +import re |
| 48 | +import subprocess |
| 49 | +import sys |
| 50 | + |
| 51 | + |
| 52 | +RELEASE_VERSION_FILE = "RELEASE-VERSION" |
| 53 | + |
| 54 | +# http://www.python.org/dev/peps/pep-0386/ |
| 55 | +_PEP386_SHORT_VERSION_RE = r"\d+(?:\.\d+)+(?:(?:[abc]|rc)\d+(?:\.\d+)*)?" |
| 56 | +_PEP386_VERSION_RE = r"^%s(?:\.post\d+)?(?:\.dev\d+)?$" % (_PEP386_SHORT_VERSION_RE) |
| 57 | +_GIT_DESCRIPTION_RE = r"^v(?P<ver>%s)-(?P<commits>\d+)-g(?P<sha>[\da-f]+)$" % ( |
| 58 | + _PEP386_SHORT_VERSION_RE |
| 59 | +) |
| 60 | + |
| 61 | + |
| 62 | +def read_git_version(): |
| 63 | + try: |
| 64 | + proc = subprocess.Popen( |
| 65 | + ("git", "describe", "--long", "--tags", "--match", "v[0-9]*.*"), |
| 66 | + stdout=subprocess.PIPE, |
| 67 | + stderr=subprocess.PIPE, |
| 68 | + ) |
| 69 | + data, _ = proc.communicate() |
| 70 | + if proc.returncode: |
| 71 | + return None |
| 72 | + ver = data.decode().splitlines()[0].strip() |
| 73 | + except: |
| 74 | + return None |
| 75 | + |
| 76 | + if not ver: |
| 77 | + return None |
| 78 | + match = re.search(_GIT_DESCRIPTION_RE, ver) |
| 79 | + if not match: |
| 80 | + sys.stderr.write("version: git description (%s) is invalid, " "ignoring\n" % ver) |
| 81 | + return None |
| 82 | + |
| 83 | + commits = int(match.group("commits")) |
| 84 | + if not commits: |
| 85 | + return match.group("ver") |
| 86 | + return "%s.post%d.dev%d" % (match.group("ver"), commits, int(match.group("sha"), 16)) |
| 87 | + |
| 88 | + |
| 89 | +def read_release_version(): |
| 90 | + try: |
| 91 | + with open(RELEASE_VERSION_FILE) as infile: |
| 92 | + ver = infile.readline().strip() |
| 93 | + if not re.search(_PEP386_VERSION_RE, ver): |
| 94 | + sys.stderr.write( |
| 95 | + "version: release version (%s) is invalid, " "will use it anyway\n" % ver |
| 96 | + ) |
| 97 | + return ver |
| 98 | + except: |
| 99 | + return None |
| 100 | + |
| 101 | + |
| 102 | +def write_release_version(version): |
| 103 | + with open(RELEASE_VERSION_FILE, "w") as outfile: |
| 104 | + outfile.write("%s\n" % version) |
| 105 | + |
| 106 | + |
| 107 | +def get_version(): |
| 108 | + release_version = read_release_version() |
| 109 | + version = read_git_version() or release_version |
| 110 | + if not version: |
| 111 | + raise ValueError("Cannot find the version number") |
| 112 | + if version != release_version: |
| 113 | + write_release_version(version) |
| 114 | + return version |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + print(get_version()) |
0 commit comments