summaryrefslogtreecommitdiff
path: root/meta
diff options
context:
space:
mode:
authorRoss Burton <ross.burton@intel.com>2013-05-23 18:45:01 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2013-05-30 20:51:27 +0100
commit17a12e3c62807a7d60fcbf0aa4fd9cf4a739a204 (patch)
tree4a3b0cf40e14cdc119a36c271dc31135d608fe86 /meta
parent1aa828d05ae1614689542c6a9ce6425a088bdc7d (diff)
downloadopenembedded-core-17a12e3c62807a7d60fcbf0aa4fd9cf4a739a204.tar.gz
openembedded-core-17a12e3c62807a7d60fcbf0aa4fd9cf4a739a204.tar.bz2
openembedded-core-17a12e3c62807a7d60fcbf0aa4fd9cf4a739a204.zip
utils: add trim_version() function
Add a helper function that returns just the first <num_parts> of <version>, split by periods. For example, trim_version("1.2.3", 2) will return "1.2". This should help reduce the spread of numerous copies of this idea across classes and recipes. Signed-off-by: Ross Burton <ross.burton@intel.com> Signed-off-by: Saul Wold <sgw@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta')
-rw-r--r--meta/lib/oe/tests/test_utils.py20
-rw-r--r--meta/lib/oe/utils.py15
2 files changed, 35 insertions, 0 deletions
diff --git a/meta/lib/oe/tests/test_utils.py b/meta/lib/oe/tests/test_utils.py
index 466c47eb9c..78b1361161 100644
--- a/meta/lib/oe/tests/test_utils.py
+++ b/meta/lib/oe/tests/test_utils.py
@@ -25,3 +25,23 @@ class TestPackagesFilterOutSystem(unittest.TestCase):
d.setVar("PACKAGES", "foo foo-data foo-locale-en-gb")
pkgs = oe.utils.packages_filter_out_system(d)
self.assertEqual(pkgs, ["foo-data"])
+
+
+class TestTrimVersion(unittest.TestCase):
+ def test_version_exception(self):
+ with self.assertRaises(TypeError):
+ trim_version(None, 2)
+ with self.assertRaises(TypeError):
+ trim_version((1, 2, 3), 2)
+
+ def test_num_exception(self):
+ with self.assertRaises(ValueError):
+ trim_version("1.2.3", 0)
+ with self.assertRaises(ValueError):
+ trim_version("1.2.3", -1)
+
+ def test_valid(self):
+ self.assertEqual(trim_version("1.2.3", 1), "1")
+ self.assertEqual(trim_version("1.2.3", 2), "1.2")
+ self.assertEqual(trim_version("1.2.3", 3), "1.2.3")
+ self.assertEqual(trim_version("1.2.3", 4), "1.2.3")
diff --git a/meta/lib/oe/utils.py b/meta/lib/oe/utils.py
index 0a2092b24b..82987e80d0 100644
--- a/meta/lib/oe/utils.py
+++ b/meta/lib/oe/utils.py
@@ -135,3 +135,18 @@ def packages_filter_out_system(d):
def getstatusoutput(cmd):
return cmdstatus.getstatusoutput(cmd)
+
+
+def trim_version(version, num_parts=2):
+ """
+ Return just the first <num_parts> of <version>, split by periods. For
+ example, trim_version("1.2.3", 2) will return "1.2".
+ """
+ if type(version) is not str:
+ raise TypeError("Version should be a string")
+ if num_parts < 1:
+ raise ValueError("Cannot split to parts < 1")
+
+ parts = version.split(".")
+ trimmed = ".".join(parts[:num_parts])
+ return trimmed