|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Author: Douglas Creager <dcreager@dcreager.net> |
| 4 | +# This file is placed into the public domain. |
| 5 | +# Adapted from Gist: https://gist.github.com/3067886 |
| 6 | + |
| 7 | +# Calculates the current version number. If possible, this is the |
| 8 | +# output of “git describe”, modified to conform to the versioning |
| 9 | +# scheme that setuptools uses. If “git describe” returns an error |
| 10 | +# (most likely because we're in an unpacked copy of a release tarball, |
| 11 | +# rather than in a git working copy), then we fall back on reading the |
| 12 | +# contents of the RELEASE-VERSION file. |
| 13 | +# |
| 14 | +# To use this script, simply import it your setup.py file, and use the |
| 15 | +# results of get_git_version() as your package version: |
| 16 | +# |
| 17 | +# from version import * |
| 18 | +# |
| 19 | +# setup( |
| 20 | +# version=get_git_version(), |
| 21 | +# . |
| 22 | +# . |
| 23 | +# . |
| 24 | +# ) |
| 25 | +# |
| 26 | +# This will automatically update the RELEASE-VERSION file, if |
| 27 | +# necessary. Note that the RELEASE-VERSION file should *not* be |
| 28 | +# checked into git; please add it to your top-level .gitignore file. |
| 29 | +# |
| 30 | +# You'll probably want to distribute the RELEASE-VERSION file in your |
| 31 | +# sdist tarballs; to do this, just create a MANIFEST.in file that |
| 32 | +# contains the following line: |
| 33 | +# |
| 34 | +# include RELEASE-VERSION |
| 35 | + |
| 36 | +__all__ = ("get_git_version") |
| 37 | + |
| 38 | +from subprocess import Popen, PIPE |
| 39 | +import os |
| 40 | + |
| 41 | +def call_git_describe(): |
| 42 | + try: |
| 43 | + p = Popen(['git', 'describe', '--tags', '--dirty', '--always'], |
| 44 | + stdout=PIPE, stderr=PIPE) |
| 45 | + p.stderr.close() |
| 46 | + line = p.stdout.readlines()[0] |
| 47 | + return line.strip() |
| 48 | + except: |
| 49 | + return None |
| 50 | + |
| 51 | + |
| 52 | +def read_release_version(): |
| 53 | + try: |
| 54 | + f = open(os.path.join(os.path.dirname(__file__), 'RELEASE-VERSION'), "r") |
| 55 | + try: |
| 56 | + version = f.readlines()[0] |
| 57 | + return version.strip() |
| 58 | + finally: |
| 59 | + f.close() |
| 60 | + except: |
| 61 | + return None |
| 62 | + |
| 63 | + |
| 64 | +def write_release_version(version): |
| 65 | + f = open(os.path.join(os.path.dirname(__file__), 'RELEASE-VERSION'), "w") |
| 66 | + f.write("%s\n" % version) |
| 67 | + f.close() |
| 68 | + |
| 69 | + |
| 70 | +def get_git_version(): |
| 71 | + # Read in the version that's currently in RELEASE-VERSION. |
| 72 | + release_version = read_release_version() |
| 73 | + |
| 74 | + # First try to get the current version using “git describe”. |
| 75 | + version = call_git_describe() |
| 76 | + |
| 77 | + # Take off the leading if present. |
| 78 | + if version is not None and version[0] == 'v': |
| 79 | + version = version[1:] |
| 80 | + |
| 81 | + #adapt to PEP 386 compatible versioning scheme |
| 82 | + version = pep386adapt(version) |
| 83 | + |
| 84 | + # If that doesn't work, fall back on the value that's in |
| 85 | + # RELEASE-VERSION. |
| 86 | + if version is None: |
| 87 | + version = release_version |
| 88 | + |
| 89 | + # If we still don't have anything, that's an error. |
| 90 | + if version is None: |
| 91 | + raise ValueError("Cannot find the version number!") |
| 92 | + |
| 93 | + # If the current version is different from what's in the |
| 94 | + # RELEASE-VERSION file, update the file to be current. |
| 95 | + if version != release_version: |
| 96 | + write_release_version(version) |
| 97 | + |
| 98 | + # Finally, return the current version. |
| 99 | + return version |
| 100 | + |
| 101 | + |
| 102 | +def pep386adapt(version): |
| 103 | + if version is not None and '-' in version: |
| 104 | + # adapt git-describe version to be in line with PEP 386 |
| 105 | + # Break PEP 386 a bit here and append the Git hash |
| 106 | + parts = version.split('-') |
| 107 | + if len(parts) > 2: |
| 108 | + version = '%s.post%s-%s' % ( |
| 109 | + parts[0], parts[1], |
| 110 | + '-'.join(parts[2:]) |
| 111 | + ) |
| 112 | + return version |
| 113 | + else: |
| 114 | + return version |
| 115 | + |
| 116 | +VERSION = get_git_version() |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + print get_git_version() |
| 120 | + |
0 commit comments