2
# Copyright 2012 OpenStack LLC
3
# Copyright 2012-2013 Hewlett-Packard Development Company, L.P.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
18
Utilities for consuming the version from pkg_resources.
24
class VersionInfo(object):
26
def __init__(self, package):
27
"""Object that understands versioning for a package
28
:param package: name of the python package, such as glance, or
31
self.package = package
34
self._cached_version = None
36
def _get_version_from_pkg_resources(self):
37
"""Get the version of the package from the pkg_resources record
38
associated with the package."""
40
requirement = pkg_resources.Requirement.parse(self.package)
41
provider = pkg_resources.get_provider(requirement)
42
return provider.version
43
except pkg_resources.DistributionNotFound:
44
# The most likely cause for this is running tests in a tree with
45
# produced from a tarball where the package itself has not been
46
# installed into anything. Check for a PKG-INFO file.
47
from cinder.openstack.common import setup
48
return setup.get_version_from_pkg_info(self.package)
50
def release_string(self):
51
"""Return the full version of the package including suffixes indicating
54
if self.release is None:
55
self.release = self._get_version_from_pkg_resources()
59
def version_string(self):
60
"""Return the short version minus any alpha/beta tags."""
61
if self.version is None:
63
for part in self.release_string().split('.'):
68
self.version = ".".join(parts)
72
# Compatibility functions
73
canonical_version_string = version_string
74
version_string_with_vcs = release_string
76
def cached_version_string(self, prefix=""):
77
"""Generate an object which will expand in a string context to
78
the results of version_string(). We do this so that don't
79
call into pkg_resources every time we start up a program when
80
passing version information into the CONF constructor, but
81
rather only do the calculation when and if a version is requested
83
if not self._cached_version:
84
self._cached_version = "%s%s" % (prefix,
85
self.version_string())
86
return self._cached_version