root / snf-common / synnefo / util / version.py @ d9300506
History | View | Annotate | Download (2.3 kB)
1 |
import pkg_resources |
---|---|
2 |
import os |
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 |
version = vcs_version() |
30 |
if not version: |
31 |
version = __import__('synnefo.versions.%s' % modname, |
32 |
fromlist=['synnefo.versions']).__version__
|
33 |
return version
|
34 |
except Exception, e: |
35 |
return 'unknown' |
36 |
|
37 |
|
38 |
def vcs_info(): |
39 |
"""
|
40 |
Return current git HEAD commit information
|
41 |
"""
|
42 |
import subprocess |
43 |
callgit = lambda(cmd): subprocess.Popen(
|
44 |
['/bin/sh', '-c', cmd], |
45 |
stdout=subprocess.PIPE).communicate()[0].strip()
|
46 |
|
47 |
branch = callgit('git branch | grep -Ei "\* (.*)" | cut -f2 -d" "')
|
48 |
revid = callgit("git --no-pager log --max-count=1 | cut -f2 -d' ' | head -1")
|
49 |
revno = callgit('git --no-pager log --oneline | wc -l')
|
50 |
desc = callgit('git describe')
|
51 |
|
52 |
return branch, revid, revno, desc
|
53 |
|
54 |
|
55 |
def vcs_version(): |
56 |
"""
|
57 |
Package version based on `git describe`, compatible with setuptools
|
58 |
version format
|
59 |
"""
|
60 |
return "-".join(vcs_info()[3].lstrip('v').split("-")[:-1]) |
61 |
|
62 |
|
63 |
def update_version(module, name='version', root="."): |
64 |
"""
|
65 |
Helper util to generate/replace a version.py file containing version
|
66 |
information retrieved from vcs_version as a submodule of passed `module`
|
67 |
"""
|
68 |
|
69 |
# exit early if not in development environment
|
70 |
if not os.path.exists(os.path.join(root, '..', '.git')): |
71 |
return
|
72 |
|
73 |
paths = [root] + module.split(".") + ["%s.py" % name] |
74 |
module_filename = os.path.join(*paths) |
75 |
content = """
|
76 |
__version__ = "%(version)s"
|
77 |
__version_info__ = __version__.split(".")
|
78 |
""" % dict(version=vcs_version()) |
79 |
|
80 |
module_file = file(module_filename, "w+") |
81 |
module_file.write(content) |
82 |
module_file.close() |
83 |
|