summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMonty Taylor <mordred@inaugust.com>2013-03-19 21:06:19 -0700
committerMonty Taylor <mordred@inaugust.com>2013-03-19 21:07:35 -0700
commitd8c8b7db3593184c98f2f8321070ad1c1ceb98ac (patch)
treea7a56fc8a4031f2fc2aee2864e812258bf89ecf6
parent806a5b915b5d87409cc21aae2c68661a11ef0b13 (diff)
downloadpbr-d8c8b7db3593184c98f2f8321070ad1c1ceb98ac.tar.gz
Added version code.
Change-Id: I7e7da2eb238dd6b80b778acec5a23945473f7a74
-rw-r--r--pbr/tests/moxstubout.py37
-rw-r--r--pbr/tests/test_version.py75
-rw-r--r--pbr/tests/utils.py3
-rw-r--r--pbr/version.py95
-rw-r--r--test-requirements.txt1
5 files changed, 211 insertions, 0 deletions
diff --git a/pbr/tests/moxstubout.py b/pbr/tests/moxstubout.py
new file mode 100644
index 0000000..f277fdd
--- /dev/null
+++ b/pbr/tests/moxstubout.py
@@ -0,0 +1,37 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# Copyright 2013 Hewlett-Packard Development Company, L.P.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import fixtures
+import mox
+import stubout
+
+
+class MoxStubout(fixtures.Fixture):
+ """Deal with code around mox and stubout as a fixture."""
+
+ def setUp(self):
+ super(MoxStubout, self).setUp()
+ # emulate some of the mox stuff, we can't use the metaclass
+ # because it screws with our generators
+ self.mox = mox.Mox()
+ self.stubs = stubout.StubOutForTesting()
+ self.addCleanup(self.mox.UnsetStubs)
+ self.addCleanup(self.stubs.UnsetAll)
+ self.addCleanup(self.stubs.SmartUnsetAll)
+ self.addCleanup(self.mox.VerifyAll)
diff --git a/pbr/tests/test_version.py b/pbr/tests/test_version.py
new file mode 100644
index 0000000..4a66b6b
--- /dev/null
+++ b/pbr/tests/test_version.py
@@ -0,0 +1,75 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 Red Hat, Inc.
+# Copyright 2012-2013 Hewlett-Packard Development Company, L.P.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import StringIO
+import sys
+
+from oslo.config import cfg
+
+from pbr.tests import utils
+from pbr import version
+
+
+class DeferredVersionTestCase(utils.BaseTestCase):
+
+ def setUp(self):
+ super(DeferredVersionTestCase, self).setUp()
+ self.conf = cfg.ConfigOpts()
+
+ def test_cached_version(self):
+ class MyVersionInfo(version.VersionInfo):
+ def _get_version_from_pkg_resources(self):
+ return "5.5.5.5"
+
+ deferred_string = MyVersionInfo("openstack").\
+ cached_version_string()
+ self.conf([], project="project", prog="prog", version=deferred_string)
+ self.assertEquals("5.5.5.5", str(self.conf.version))
+
+ def test_print_cached_version(self):
+ class MyVersionInfo(version.VersionInfo):
+ def _get_version_from_pkg_resources(self):
+ return "5.5.5.5"
+
+ deferred_string = MyVersionInfo("openstack")\
+ .cached_version_string()
+ self.stubs.Set(sys, 'stderr', StringIO.StringIO())
+ self.assertRaises(SystemExit,
+ self.conf, ['--version'],
+ project="project",
+ prog="prog",
+ version=deferred_string)
+ self.assertEquals("5.5.5.5", sys.stderr.getvalue().strip())
+
+ def test_print_cached_version_with_long_string(self):
+ my_version = "11111222223333344444555556666677777888889999900000"
+
+ class MyVersionInfo(version.VersionInfo):
+ def _get_version_from_pkg_resources(self):
+ return my_version
+
+ deferred_string = MyVersionInfo("openstack")\
+ .cached_version_string()
+
+ for i in range(50):
+ self.stubs.Set(sys, 'stderr', StringIO.StringIO())
+ self.assertRaises(SystemExit,
+ self.conf, ['--version'],
+ project="project",
+ prog="prog",
+ version=deferred_string)
+ self.assertEquals(my_version, sys.stderr.getvalue().strip())
diff --git a/pbr/tests/utils.py b/pbr/tests/utils.py
index 4c11554..453d3ad 100644
--- a/pbr/tests/utils.py
+++ b/pbr/tests/utils.py
@@ -21,6 +21,8 @@ import os
import fixtures
import testtools
+from pbr.tests import moxstubout
+
_TRUE = ('True', '1')
@@ -40,3 +42,4 @@ class BaseTestCase(testtools.TestCase):
self.useFixture(fixtures.NestedTempfile())
self.useFixture(fixtures.FakeLogger())
self.useFixture(fixtures.Timeout(30, True))
+ self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
diff --git a/pbr/version.py b/pbr/version.py
new file mode 100644
index 0000000..87a5ca9
--- /dev/null
+++ b/pbr/version.py
@@ -0,0 +1,95 @@
+
+# Copyright 2012 OpenStack Foundation
+# Copyright 2012-2013 Hewlett-Packard Development Company, L.P.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+"""
+Utilities for consuming the version from pkg_resources.
+"""
+
+import pkg_resources
+
+
+class VersionInfo(object):
+
+ def __init__(self, package):
+ """Object that understands versioning for a package
+ :param package: name of the python package, such as glance, or
+ python-glanceclient
+ """
+ self.package = package
+ self.release = None
+ self.version = None
+ self._cached_version = None
+
+ def __str__(self):
+ """Make the VersionInfo object behave like a string."""
+ return self.version_string()
+
+ def __repr__(self):
+ """Include the name."""
+ return "VersionInfo(%s:%s)" % (self.package, self.version_string())
+
+ def _get_version_from_pkg_resources(self):
+ """Get the version of the package from the pkg_resources record
+ associated with the package.
+ """
+ try:
+ requirement = pkg_resources.Requirement.parse(self.package)
+ provider = pkg_resources.get_provider(requirement)
+ return provider.version
+ except pkg_resources.DistributionNotFound:
+ # The most likely cause for this is running tests in a tree
+ # produced from a tarball where the package itself has not been
+ # installed into anything. Revert to setup-time logic.
+ from pbr import packaging
+ return packaging.get_version(self.package)
+
+ def release_string(self):
+ """Return the full version of the package including suffixes indicating
+ VCS status.
+ """
+ if self.release is None:
+ self.release = self._get_version_from_pkg_resources()
+
+ return self.release
+
+ def version_string(self):
+ """Return the short version minus any alpha/beta tags."""
+ if self.version is None:
+ parts = []
+ for part in self.release_string().split('.'):
+ if part[0].isdigit():
+ parts.append(part)
+ else:
+ break
+ self.version = ".".join(parts)
+
+ return self.version
+
+ # Compatibility functions
+ canonical_version_string = version_string
+ version_string_with_vcs = release_string
+
+ def cached_version_string(self, prefix=""):
+ """Generate an object which will expand in a string context to
+ the results of version_string(). We do this so that don't
+ call into pkg_resources every time we start up a program when
+ passing version information into the CONF constructor, but
+ rather only do the calculation when and if a version is requested
+ """
+ if not self._cached_version:
+ self._cached_version = "%s%s" % (prefix,
+ self.version_string())
+ return self._cached_version
diff --git a/test-requirements.txt b/test-requirements.txt
index 9b872d8..8c0100c 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -2,6 +2,7 @@ coverage>=3.6
discover
fixtures>=0.3.12
flake8
+mox
python-subunit
sphinx>=1.1.2
testrepository>=0.0.13