1
0
mirror of https://git.yoctoproject.org/poky synced 2026-05-30 00:20:08 +00:00

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.

(From OE-Core rev: 17a12e3c62807a7d60fcbf0aa4fd9cf4a739a204)

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>
This commit is contained in:
Ross Burton
2013-05-23 18:45:01 +00:00
committed by Richard Purdie
parent f5d0f6becc
commit fcfba0ddd9
2 changed files with 35 additions and 0 deletions
+15
View File
@@ -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