Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / util / version.py @ 762b93d7

History | View | Annotate | Download (3.6 kB)

1
import pkg_resources
2
import os
3
import pprint
4

    
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:
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 "unknown"
36
    except Exception:
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 =\
58
        callgit("git --no-pager log --max-count=1 | cut -f2 -d' ' | head -1")
59
    revno = callgit('git --no-pager log --oneline | wc -l')
60
    desc = callgit('git describe --tags')
61

    
62
    return branch, revid, revno, desc
63

    
64

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

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

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

    
86

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

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

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

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

    
112

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