Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / util / version.py @ 473e2b08

History | View | Annotate | Download (3.6 kB)

1
import pkg_resources
2
import os
3
import pprint
4
import re
5

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

    
10

    
11
def get_dist(dist_name):
12
    return pkg_resources.get_distribution(dist_name)
13

    
14

    
15
def get_dist_version(dist_name):
16
    """
17
    Get the version for the specified distribution name
18
    """
19
    try:
20
        return get_dist(dist_name).version
21
    except Exception, e:
22
        return 'unknown'
23

    
24

    
25
def get_component_version(modname):
26
    """
27
    Return the version of a synnefo module/package based on its
28
    corresponding distributed package version
29
    """
30
    try:
31
        try:
32
            return __import__('synnefo.versions.%s' % modname,
33
                    fromlist=['synnefo.versions']).__version__
34
        except ImportError:
35
            return  vcs_version()
36
    except Exception, e:
37
        return 'unknown'
38

    
39

    
40
def vcs_info():
41
    """
42
    Return current git HEAD commit information.
43

44
    Returns a tuple containing
45
        - branch name
46
        - commit id
47
        - commit index
48
        - git describe output
49
    """
50
    import subprocess
51
    callgit = lambda(cmd): subprocess.Popen(
52
            ['/bin/sh', '-c', cmd],
53
            stdout=subprocess.PIPE,
54
            stderr=subprocess.PIPE).communicate()[0].strip()
55

    
56
    branch = callgit('git branch | grep -Ei "\* (.*)" | cut -f2 -d" "')
57
    revid = callgit("git --no-pager log --max-count=1 | cut -f2 -d' ' | head -1")
58
    revno = callgit('git --no-pager log --oneline | wc -l')
59
    desc = callgit('git describe --tags')
60

    
61
    return branch, revid, revno, desc
62

    
63

    
64
def get_version_from_describe(describe):
65
    """
66
    Package version based on `git describe` output. Compatible with setuptools
67
    version format
68

69
    >>> get_version_from_describe("v0.8.0")
70
    '0.8.0'
71
    >>> get_version_from_describe("debian/v0.8.0")
72
    '0.8.0'
73
    >>> get_version_from_describe("0.8.0")
74
    '0.8.0'
75
    >>> get_version_from_describe("v0.8.0-34-g8f9a1bf")
76
    '0.8.0-34-g8f9a1bf'
77
    >>> get_version_from_describe("debian/v0.8.0-34-g8f9a1bf")
78
    '0.8.0-34-g8f9a1bf'
79
    """
80

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

    
85

    
86
def update_version_old(module, name='version', root="."):
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`
90
    """
91

    
92
    # exit early if not in development environment
93
    if not os.path.exists(os.path.join(root, '..', '.git')) and \
94
        not os.path.exists(os.path.join(root, '.git')):
95
        return
96

    
97
    paths = [root] + module.split(".") + ["%s.py" % name]
98
    module_filename = os.path.join(*paths)
99
    content = """
100
__version__ = "%(version)s"
101
__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()))
105

    
106
    module_file = file(module_filename, "w+")
107
    module_file.write(content)
108
    module_file.close()
109

    
110

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