From 23b4cf8b6413dc19ef8fb5af099b01e76b11ec2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Gait=C3=A1n?= Date: Tue, 16 Apr 2024 14:53:34 -0300 Subject: [PATCH 1/4] packaging.version as a CLI tool --- src/packaging/version.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/packaging/version.py b/src/packaging/version.py index 46bc2613..18168221 100644 --- a/src/packaging/version.py +++ b/src/packaging/version.py @@ -561,3 +561,43 @@ def _cmpkey( ) return epoch, _release, _pre, _post, _dev, _local + + +if __name__ == "__main__": + import argparse + import sys + + operations = { + "lt": lambda v1, v2: v1 < v2, + "le": lambda v1, v2: v1 <= v2, + "eq": lambda v1, v2: v1 == v2, + "ne": lambda v1, v2: v1 != v2, + "ge": lambda v1, v2: v1 >= v2, + "gt": lambda v1, v2: v1 > v2, + "lt-nl": lambda v1, v2: (v1 < v2) if v1 and v2 else True, + "le-nl": lambda v1, v2: (v1 <= v2) if v1 and v2 else True, + "ge-nl": lambda v1, v2: (v1 >= v2) if v1 and v2 else False, + "gt-nl": lambda v1, v2: (v1 > v2) if v1 and v2 else False, + "<": lambda v1, v2: v1 < v2, + "<<": lambda v1, v2: v1 < v2, + "<=": lambda v1, v2: v1 <= v2, + "=": lambda v1, v2: v1 == v2, + ">=": lambda v1, v2: v1 >= v2, + ">>": lambda v1, v2: v1 > v2, + ">": lambda v1, v2: v1 > v2, + } + + parser = argparse.ArgumentParser(description="Compare two semantic versions.") + parser.add_argument("version1", type=Version, help="First version to compare") + parser.add_argument( + "operator", + type=str, + choices=operations.keys(), + help="Comparison operator", + ) + parser.add_argument("version2", type=Version, help="Second version to compare") + + args = parser.parse_args() + result = operations[args.operator](args.version1, args.version2) + + sys.exit(0 if result else 1) From 190088e86f51fcf7e339da9a7b42ea6effa2531d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Gait=C3=A1n?= Date: Mon, 9 Sep 2024 17:08:44 -0300 Subject: [PATCH 2/4] Update src/packaging/version.py Co-authored-by: Brett Cannon --- src/packaging/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/packaging/version.py b/src/packaging/version.py index 18168221..b4a171e1 100644 --- a/src/packaging/version.py +++ b/src/packaging/version.py @@ -600,4 +600,4 @@ def _cmpkey( args = parser.parse_args() result = operations[args.operator](args.version1, args.version2) - sys.exit(0 if result else 1) + sys.exit(not result) From 39caeeabcd53cc5441a4487fe02cd6d1f94fe381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Gait=C3=A1n?= Date: Mon, 9 Sep 2024 18:53:53 -0300 Subject: [PATCH 3/4] remove -nl operators --- src/packaging/version.py | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/packaging/version.py b/src/packaging/version.py index b4a171e1..d175fc00 100644 --- a/src/packaging/version.py +++ b/src/packaging/version.py @@ -562,31 +562,28 @@ def _cmpkey( return epoch, _release, _pre, _post, _dev, _local - if __name__ == "__main__": import argparse import sys + import operator operations = { - "lt": lambda v1, v2: v1 < v2, - "le": lambda v1, v2: v1 <= v2, - "eq": lambda v1, v2: v1 == v2, - "ne": lambda v1, v2: v1 != v2, - "ge": lambda v1, v2: v1 >= v2, - "gt": lambda v1, v2: v1 > v2, - "lt-nl": lambda v1, v2: (v1 < v2) if v1 and v2 else True, - "le-nl": lambda v1, v2: (v1 <= v2) if v1 and v2 else True, - "ge-nl": lambda v1, v2: (v1 >= v2) if v1 and v2 else False, - "gt-nl": lambda v1, v2: (v1 > v2) if v1 and v2 else False, - "<": lambda v1, v2: v1 < v2, - "<<": lambda v1, v2: v1 < v2, - "<=": lambda v1, v2: v1 <= v2, - "=": lambda v1, v2: v1 == v2, - ">=": lambda v1, v2: v1 >= v2, - ">>": lambda v1, v2: v1 > v2, - ">": lambda v1, v2: v1 > v2, + "lt": operator.lt, + "le": operator.le, + "eq": operator.eq, + "ne": operator.ne, + "ge": operator.ge, + "gt": operator.gt, + "<": operator.lt, + "<<": operator.lt, + "<=": operator.le, + "=": operator.eq, + ">=": operator.ge, + ">>": operator.gt, + ">": operator.gt, } + # Argument parsing parser = argparse.ArgumentParser(description="Compare two semantic versions.") parser.add_argument("version1", type=Version, help="First version to compare") parser.add_argument( @@ -596,8 +593,8 @@ def _cmpkey( help="Comparison operator", ) parser.add_argument("version2", type=Version, help="Second version to compare") - args = parser.parse_args() - result = operations[args.operator](args.version1, args.version2) + result = operations[args.operator](args.version1, args.version2) sys.exit(not result) + From 58b2cf4c7e7f7d54c0328c2e0fd2116e5d534541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Gait=C3=A1n?= Date: Mon, 9 Sep 2024 19:02:35 -0300 Subject: [PATCH 4/4] remove not named ops --- src/packaging/version.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/packaging/version.py b/src/packaging/version.py index d175fc00..b208868d 100644 --- a/src/packaging/version.py +++ b/src/packaging/version.py @@ -574,13 +574,6 @@ def _cmpkey( "ne": operator.ne, "ge": operator.ge, "gt": operator.gt, - "<": operator.lt, - "<<": operator.lt, - "<=": operator.le, - "=": operator.eq, - ">=": operator.ge, - ">>": operator.gt, - ">": operator.gt, } # Argument parsing