version.py 3.58 KB
Newer Older
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
1
2
import pkg_resources
import os
3
import pprint
4
import re
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
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

def get_dist_from_module(modname):
    pkgroot = pkg_resources.get_provider(modname).egg_root
    return list(pkg_resources.find_distributions(pkgroot))[0]


def get_dist(dist_name):
    return pkg_resources.get_distribution(dist_name)


def get_dist_version(dist_name):
    """
    Get the version for the specified distribution name
    """
    try:
        return get_dist(dist_name).version
    except Exception, e:
        return 'unknown'


def get_component_version(modname):
    """
    Return the version of a synnefo module/package based on its
    corresponding distributed package version
    """
    try:
31
32
        try:
            return __import__('synnefo.versions.%s' % modname,
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
33
                    fromlist=['synnefo.versions']).__version__
34
35
        except ImportError:
            return  vcs_version()
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
36
37
38
39
40
41
    except Exception, e:
        return 'unknown'


def vcs_info():
    """
42
43
44
45
46
47
48
    Return current git HEAD commit information.

    Returns a tuple containing
        - branch name
        - commit id
        - commit index
        - git describe output
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
49
50
51
52
    """
    import subprocess
    callgit = lambda(cmd): subprocess.Popen(
            ['/bin/sh', '-c', cmd],
53
54
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE).communicate()[0].strip()
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
55
56
57
58

    branch = callgit('git branch | grep -Ei "\* (.*)" | cut -f2 -d" "')
    revid = callgit("git --no-pager log --max-count=1 | cut -f2 -d' ' | head -1")
    revno = callgit('git --no-pager log --oneline | wc -l')
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
59
    desc = callgit('git describe --tags')
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
60
61
62
63

    return branch, revid, revno, desc


64
def get_version_from_describe(describe):
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
65
    """
66
    Package version based on `git describe` output. Compatible with setuptools
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
67
    version format
68
69
70
71
72
73
74
75
76
77
78

    >>> get_version_from_describe("v0.8.0")
    '0.8.0'
    >>> get_version_from_describe("debian/v0.8.0")
    '0.8.0'
    >>> get_version_from_describe("0.8.0")
    '0.8.0'
    >>> get_version_from_describe("v0.8.0-34-g8f9a1bf")
    '0.8.0-34-g8f9a1bf'
    >>> get_version_from_describe("debian/v0.8.0-34-g8f9a1bf")
    '0.8.0-34-g8f9a1bf'
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
79
80
    """

81
82
83
    version = describe.split("/")[-1].lstrip('v')
    version = version.lstrip('v')
    return version
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
84

85
86

def update_version_old(module, name='version', root="."):
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
87
88
    """
    Helper util to generate/replace a version.py file containing version
89
    information retrieved from get_version_from_describe as a submodule of passed `module`
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
90
91
92
    """

    # exit early if not in development environment
93
94
    if not os.path.exists(os.path.join(root, '..', '.git')) and \
        not os.path.exists(os.path.join(root, '.git')):
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
95
96
97
98
99
100
101
        return

    paths = [root] + module.split(".") + ["%s.py" % name]
    module_filename = os.path.join(*paths)
    content = """
__version__ = "%(version)s"
__version_info__ = __version__.split(".")
102
__version_vcs_info__ = %(vcs_info)s
103
    """ % dict(version=get_version_from_describe(vcs_info()[3]),
104
            vcs_info=pprint.PrettyPrinter().pformat(vcs_info()))
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
105
106
107
108
109

    module_file = file(module_filename, "w+")
    module_file.write(content)
    module_file.close()

110
111
112
113
114
115
116
117
118

def update_version(module, name='version', root='.'):
    try:
        from devflow import versioning
        return versioning.update_version(module, name, root)
    except ImportError:
        import sys
        paths = [root] + module.split(".") + ["%s.py" % name]
        module_filename = os.path.join(*paths)
Christos Stavrakakis's avatar
Christos Stavrakakis committed
119
        sys.stdout.write("WARNING: Cannot update version because `devflow` is"
120
121
                         " not installed. Please make sure to manually"
                         " update version file: '%s'\n" % module_filename)