@@ -371,6 +371,50 @@ def bump_build(self, token="build"):
371
371
build = cls ._increment_string (self ._build or (token or "build" ) + ".0" )
372
372
return cls (self ._major , self ._minor , self ._patch , self ._prerelease , build )
373
373
374
+ def compare (self , other ):
375
+ """
376
+ Compare self with other.
377
+
378
+ :param other: second version (can be string or a VersionInfo instance)
379
+ :return: The return value is negative if ver1 < ver2,
380
+ zero if ver1 == ver2 and strictly positive if ver1 > ver2
381
+ :rtype: int
382
+
383
+ >>> semver.VersionInfo.parse("1.0.0").compare("2.0.0")
384
+ -1
385
+ >>> semver.VersionInfo.parse("2.0.0").compare("1.0.0")
386
+ 1
387
+ >>> semver.VersionInfo.parse("2.0.0").compare("2.0.0")
388
+ 0
389
+ """
390
+ cls = type (self )
391
+ if isinstance (other , str ):
392
+ other = cls .parse (other )
393
+ elif not isinstance (other , cls ):
394
+ raise TypeError (
395
+ "Expected str or {} instance, but got {}" .format (
396
+ cls .__name__ , type (other )
397
+ )
398
+ )
399
+
400
+ v1 = self .to_tuple ()[:3 ]
401
+ v2 = other .to_tuple ()[:3 ]
402
+ x = cmp (v1 , v2 )
403
+ if x :
404
+ return x
405
+
406
+ rc1 , rc2 = self .prerelease , other .prerelease
407
+ rccmp = _nat_cmp (rc1 , rc2 )
408
+
409
+ if not rccmp :
410
+ return 0
411
+ if not rc1 :
412
+ return 1
413
+ elif not rc2 :
414
+ return - 1
415
+
416
+ return rccmp
417
+
374
418
@comparator
375
419
def __eq__ (self , other ):
376
420
return _compare_by_keys (self .to_dict (), _to_dict (other )) == 0
@@ -424,6 +468,53 @@ def finalize_version(self):
424
468
cls = type (self )
425
469
return cls (self .major , self .minor , self .patch )
426
470
471
+ def match (self , match_expr ):
472
+ """
473
+ Compare self to match a match expression.
474
+
475
+ :param str match_expr: operator and version; valid operators are
476
+ < smaller than
477
+ > greater than
478
+ >= greator or equal than
479
+ <= smaller or equal than
480
+ == equal
481
+ != not equal
482
+ :return: True if the expression matches the version, otherwise False
483
+ :rtype: bool
484
+
485
+ >>> semver.VersionInfo.parse("2.0.0").match(">=1.0.0")
486
+ True
487
+ >>> semver.VersionInfo.parse("1.0.0").match(">1.0.0")
488
+ False
489
+ """
490
+ prefix = match_expr [:2 ]
491
+ if prefix in (">=" , "<=" , "==" , "!=" ):
492
+ match_version = match_expr [2 :]
493
+ elif prefix and prefix [0 ] in (">" , "<" ):
494
+ prefix = prefix [0 ]
495
+ match_version = match_expr [1 :]
496
+ else :
497
+ raise ValueError (
498
+ "match_expr parameter should be in format <op><ver>, "
499
+ "where <op> is one of "
500
+ "['<', '>', '==', '<=', '>=', '!=']. "
501
+ "You provided: %r" % match_expr
502
+ )
503
+
504
+ possibilities_dict = {
505
+ ">" : (1 ,),
506
+ "<" : (- 1 ,),
507
+ "==" : (0 ,),
508
+ "!=" : (- 1 , 1 ),
509
+ ">=" : (0 , 1 ),
510
+ "<=" : (- 1 , 0 ),
511
+ }
512
+
513
+ possibilities = possibilities_dict [prefix ]
514
+ cmp_res = self .compare (match_version )
515
+
516
+ return cmp_res in possibilities
517
+
427
518
@staticmethod
428
519
def parse (version ):
429
520
"""
@@ -579,6 +670,7 @@ def _compare_by_keys(d1, d2):
579
670
return rccmp
580
671
581
672
673
+ @deprecated (version = "2.10.0" )
582
674
def compare (ver1 , ver2 ):
583
675
"""
584
676
Compare two versions strings.
@@ -596,13 +688,13 @@ def compare(ver1, ver2):
596
688
>>> semver.compare("2.0.0", "2.0.0")
597
689
0
598
690
"""
599
-
600
- v1 = VersionInfo .parse (ver1 ).to_dict ()
601
- v2 = VersionInfo .parse (ver2 ).to_dict ()
602
-
603
- return _compare_by_keys (v1 , v2 )
691
+ v1 = VersionInfo .parse (ver1 )
692
+ # v2 = VersionInfo.parse(ver2)
693
+ return v1 .compare (ver2 )
694
+ # return _compare_by_keys(v1, v2)
604
695
605
696
697
+ @deprecated (version = "2.10.0" )
606
698
def match (version , match_expr ):
607
699
"""
608
700
Compare two versions strings through a comparison.
@@ -623,33 +715,8 @@ def match(version, match_expr):
623
715
>>> semver.match("1.0.0", ">1.0.0")
624
716
False
625
717
"""
626
- prefix = match_expr [:2 ]
627
- if prefix in (">=" , "<=" , "==" , "!=" ):
628
- match_version = match_expr [2 :]
629
- elif prefix and prefix [0 ] in (">" , "<" ):
630
- prefix = prefix [0 ]
631
- match_version = match_expr [1 :]
632
- else :
633
- raise ValueError (
634
- "match_expr parameter should be in format <op><ver>, "
635
- "where <op> is one of "
636
- "['<', '>', '==', '<=', '>=', '!=']. "
637
- "You provided: %r" % match_expr
638
- )
639
-
640
- possibilities_dict = {
641
- ">" : (1 ,),
642
- "<" : (- 1 ,),
643
- "==" : (0 ,),
644
- "!=" : (- 1 , 1 ),
645
- ">=" : (0 , 1 ),
646
- "<=" : (- 1 , 0 ),
647
- }
648
-
649
- possibilities = possibilities_dict [prefix ]
650
- cmp_res = compare (version , match_version )
651
-
652
- return cmp_res in possibilities
718
+ ver = VersionInfo .parse (version )
719
+ return ver .match (match_expr )
653
720
654
721
655
722
def max_ver (ver1 , ver2 ):
@@ -664,9 +731,10 @@ def max_ver(ver1, ver2):
664
731
>>> semver.max_ver("1.0.0", "2.0.0")
665
732
'2.0.0'
666
733
"""
667
- cmp_res = compare (ver1 , ver2 )
668
- if cmp_res == 0 or cmp_res == 1 :
669
- return ver1
734
+ ver1 = VersionInfo .parse (ver1 )
735
+ cmp_res = ver1 .compare (ver2 )
736
+ if cmp_res >= 0 :
737
+ return str (ver1 )
670
738
else :
671
739
return ver2
672
740
@@ -683,9 +751,10 @@ def min_ver(ver1, ver2):
683
751
>>> semver.min_ver("1.0.0", "2.0.0")
684
752
'1.0.0'
685
753
"""
686
- cmp_res = compare (ver1 , ver2 )
687
- if cmp_res == 0 or cmp_res == - 1 :
688
- return ver1
754
+ ver1 = VersionInfo .parse (ver1 )
755
+ cmp_res = ver1 .compare (ver2 )
756
+ if cmp_res <= 0 :
757
+ return str (ver1 )
689
758
else :
690
759
return ver2
691
760
0 commit comments