summaryrefslogtreecommitdiffstats
path: root/meta/lib
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib')
-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):
25 d.setVar("PACKAGES", "foo foo-data foo-locale-en-gb") 25 d.setVar("PACKAGES", "foo foo-data foo-locale-en-gb")
26 pkgs = oe.utils.packages_filter_out_system(d) 26 pkgs = oe.utils.packages_filter_out_system(d)
27 self.assertEqual(pkgs, ["foo-data"]) 27 self.assertEqual(pkgs, ["foo-data"])
28
29
30class TestTrimVersion(unittest.TestCase):
31 def test_version_exception(self):
32 with self.assertRaises(TypeError):
33 trim_version(None, 2)
34 with self.assertRaises(TypeError):
35 trim_version((1, 2, 3), 2)
36
37 def test_num_exception(self):
38 with self.assertRaises(ValueError):
39 trim_version("1.2.3", 0)
40 with self.assertRaises(ValueError):
41 trim_version("1.2.3", -1)
42
43 def test_valid(self):
44 self.assertEqual(trim_version("1.2.3", 1), "1")
45 self.assertEqual(trim_version("1.2.3", 2), "1.2")
46 self.assertEqual(trim_version("1.2.3", 3), "1.2.3")
47 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):
135 135
136def getstatusoutput(cmd): 136def getstatusoutput(cmd):
137 return cmdstatus.getstatusoutput(cmd) 137 return cmdstatus.getstatusoutput(cmd)
138
139
140def trim_version(version, num_parts=2):
141 """
142 Return just the first <num_parts> of <version>, split by periods. For
143 example, trim_version("1.2.3", 2) will return "1.2".
144 """
145 if type(version) is not str:
146 raise TypeError("Version should be a string")
147 if num_parts < 1:
148 raise ValueError("Cannot split to parts < 1")
149
150 parts = version.split(".")
151 trimmed = ".".join(parts[:num_parts])
152 return trimmed