root / snf-common / synnefo / version.py @ cfcedbb1
History | View | Annotate | Download (1.4 kB)
1 |
import pkg_resources |
---|---|
2 |
|
3 |
|
4 |
def get_dist_from_module(modname): |
5 |
pkgroot = pkg_resources.get_provider(modname).egg_root |
6 |
return list(pkg_resources.find_distributions(pkgroot))[0] |
7 |
|
8 |
|
9 |
def get_dist(dist_name): |
10 |
return pkg_resources.get_distribution(dist_name)
|
11 |
|
12 |
|
13 |
def get_dist_version(dist_name): |
14 |
"""
|
15 |
Get the version for the specified distribution name
|
16 |
"""
|
17 |
try:
|
18 |
return get_dist(dist_name).version
|
19 |
except Exception, e: |
20 |
return 'unknown' |
21 |
|
22 |
|
23 |
def get_component_version(modname): |
24 |
"""
|
25 |
Return the version of a synnefo module/package based on its
|
26 |
corresponding distributed package version
|
27 |
"""
|
28 |
try:
|
29 |
return get_dist_from_module(modname).version
|
30 |
except Exception, e: |
31 |
return 'unknown' |
32 |
|
33 |
|
34 |
def vcs_info(): |
35 |
"""
|
36 |
Return current git HEAD commit information
|
37 |
"""
|
38 |
import subprocess |
39 |
callgit = lambda(cmd): subprocess.Popen(
|
40 |
['/bin/sh', '-c', cmd], |
41 |
stdout=subprocess.PIPE).communicate()[0].strip()
|
42 |
|
43 |
branch = callgit('git branch | grep -Ei "\* (.*)" | cut -f2 -d" "')
|
44 |
revid = callgit("git --no-pager log --max-count=1 | cut -f2 -d' ' | head -1")
|
45 |
revno = callgit('git --no-pager log --oneline | wc -l')
|
46 |
desc = callgit('git describe')
|
47 |
|
48 |
return branch, revid, revno, desc
|
49 |
|
50 |
|
51 |
def vcs_version(): |
52 |
"""
|
53 |
Package version based on `git describe`, compatible with setuptools
|
54 |
version format
|
55 |
"""
|
56 |
return vcs_info()[3].replace('v', '') |
57 |
|