All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ross Burton <ross.burton@intel.com>
To: openembedded-core@lists.openembedded.org
Subject: [PATCH 03/14] utils: add trim_version() function
Date: Thu, 30 May 2013 14:52:37 +0100	[thread overview]
Message-ID: <ae21fe8c8e38ed0b23b3a50aa5e404f652196a6d.1369921759.git.ross.burton@intel.com> (raw)
In-Reply-To: <cover.1369921759.git.ross.burton@intel.com>
In-Reply-To: <cover.1369921759.git.ross.burton@intel.com>

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>
---
 meta/lib/oe/tests/test_utils.py |   20 ++++++++++++++++++++
 meta/lib/oe/utils.py            |   15 +++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/meta/lib/oe/tests/test_utils.py b/meta/lib/oe/tests/test_utils.py
index 8bb36f2..5d9ac52 100644
--- a/meta/lib/oe/tests/test_utils.py
+++ b/meta/lib/oe/tests/test_utils.py
@@ -29,3 +29,23 @@ class TestPackagesFilterOutSystem(unittest.TestCase):
         d.setVar("PACKAGES", "foo foo-data foo-locale-en-gb")
         pkgs = 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 0a2092b..82987e8 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
-- 
1.7.10.4



  parent reply	other threads:[~2013-05-30 13:56 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-30 13:52 [PATCH 00/14] GTK+ 3.8 and Wayland integration Ross Burton
2013-05-30 13:52 ` [PATCH 01/14] test-utils: handle import bb failing and skip the test Ross Burton
2013-05-30 13:52 ` [PATCH 02/14] test_utils: import functions directly for conciseness Ross Burton
2013-05-30 13:52 ` Ross Burton [this message]
2013-05-30 13:52 ` [PATCH 04/14] gdk-pixbuf: upgrade to 2.28.1 Ross Burton
2013-05-30 14:02   ` Martin Jansa
2013-05-30 14:05     ` Burton, Ross
2013-05-30 13:52 ` [PATCH 05/14] atk: upgrade to 2.8 Ross Burton
2013-05-30 13:52 ` [PATCH 06/14] at-spi2: add -core and -atk, for GTK+ 3.8 Ross Burton
2013-05-30 13:52 ` [PATCH 07/14] gtk+3: update to 3.8.2 Ross Burton
2013-05-30 13:52 ` [PATCH 08/14] gtk+3: explicitly disable introspection Ross Burton
2013-05-30 13:52 ` [PATCH 09/14] gtk+3: respect x11 and wayland DISTRO_FEATURES Ross Burton
2013-05-30 13:52 ` [PATCH 10/14] gtk+3: add dependencies for gtk+3-demo Ross Burton
2013-05-30 13:52 ` [PATCH 11/14] gtk+: remove spurious libgcrypt dependency Ross Burton
2013-05-30 13:52 ` [PATCH 12/14] gtk+3: register GSetting schemas Ross Burton
2013-05-30 13:52 ` [PATCH 13/14] gtk+3: split into .bb/.inc Ross Burton
2013-05-30 13:52 ` [PATCH 14/14] gtk+3: clean up libtool link creation to avoid errors in configure log Ross Burton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ae21fe8c8e38ed0b23b3a50aa5e404f652196a6d.1369921759.git.ross.burton@intel.com \
    --to=ross.burton@intel.com \
    --cc=openembedded-core@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.