From e1c9d94708fe4b13986fa61aa3fa308f3b6e8a70 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Tue, 29 Nov 2016 00:57:01 +0530 Subject: Allow many version conditions for pkg-config deps Sometimes we want to restrict the acceptable versions to a list of versions, or a smallest-version + largest-version, or both. For instance, GStreamer's opencv plugin is only compatible with 3.1.0 >= opencv >= 2.3.0 --- mesonbuild/mesonlib.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'mesonbuild/mesonlib.py') diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index 4d9cc69f4..4670685f3 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -146,21 +146,26 @@ def detect_vcs(source_dir): return vcs return None -def grab_leading_numbers(vstr): +def grab_leading_numbers(vstr, strict=False): result = [] for x in vstr.split('.'): try: result.append(int(x)) - except ValueError: + except ValueError as e: + if strict: + msg = 'Invalid version to compare against: {!r}; only ' \ + 'numeric digits separated by "." are allowed: ' + str(e) + raise MesonException(msg.format(vstr)) break return result numpart = re.compile('[0-9.]+') -def version_compare(vstr1, vstr2): +def version_compare(vstr1, vstr2, strict=False): match = numpart.match(vstr1.strip()) if match is None: - raise MesonException('Uncomparable version string %s.' % vstr1) + msg = 'Uncomparable version string {!r}.' + raise MesonException(msg.format(vstr1)) vstr1 = match.group(0) if vstr2.startswith('>='): cmpop = operator.ge @@ -185,10 +190,22 @@ def version_compare(vstr1, vstr2): vstr2 = vstr2[1:] else: cmpop = operator.eq - varr1 = grab_leading_numbers(vstr1) - varr2 = grab_leading_numbers(vstr2) + varr1 = grab_leading_numbers(vstr1, strict) + varr2 = grab_leading_numbers(vstr2, strict) return cmpop(varr1, varr2) +def version_compare_many(vstr1, conditions): + if not isinstance(conditions, (list, tuple)): + conditions = [conditions] + found = [] + not_found = [] + for req in conditions: + if not version_compare(vstr1, req, strict=True): + not_found.append(req) + else: + found.append(req) + return (not_found == [], not_found, found) + def default_libdir(): if is_debianlike(): try: -- cgit v1.2.3