summaryrefslogtreecommitdiffstats
path: root/version.py
blob: 498ce512d9e246705c4d243650e7f8fe511308a7 (plain)
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
# -*- coding: utf-8 -*-
# To use this script, simply import it your setup.py file, and use the
# results of get_git_version() as your package version:
#
# from version import *
#
# setup(
#     version=get_git_version(),
#     .
#     .
#     .
# )

__all__ = ["get_git_version"]

import os
import re
from subprocess import Popen, PIPE

OWN_DIR = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))

def call_git_describe(abbrev=4):
    try:
        p = Popen(['git', 'describe', '--abbrev=%d' % abbrev,
                                      '--tags', '--dirty'],
                  cwd=OWN_DIR, stdout=PIPE, stderr=PIPE)
        p.stderr.close()
        line = p.stdout.readlines()[0]
        return line.strip()

    except:
        return None

def get_git_version(abbrev=4):
    version = call_git_describe(abbrev)
    if version is None:
        raise ValueError("Cannot find the version number!")

    return re.sub('^debian/', '', version)

if __name__ == "__main__":
    print get_git_version()