6
6
7
7
"""
8
8
import collections
9
+ import functools
9
10
import logging
10
11
import os
11
12
import re
12
13
import subprocess
13
14
import sys
14
- from distutils . version import LooseVersion
15
+ import typing
15
16
16
17
from . import exc
17
18
from ._compat import MutableMapping , console_to_str , str_from_console
@@ -435,6 +436,45 @@ def _is_executable_file_or_link(exe):
435
436
return None
436
437
437
438
439
+ version_component_re = re .compile (r"(\d+|[a-z]+|\.)" )
440
+
441
+
442
+ @functools .total_ordering
443
+ class _Version (typing .NamedTuple ):
444
+ """based on https://github.com/asottile/flake8-typing-imports/blob/923a533/flake8_typing_imports.py#L32-L42
445
+
446
+ License MIT
447
+ """
448
+
449
+ major : int = 0
450
+ minor : int = 0
451
+ patch : int = 0
452
+
453
+ @classmethod
454
+ def parse (cls , s : str ) -> "_Version" :
455
+ return cls (* (int (p ) for p in s .split ("." )))
456
+
457
+ def __lt__ (self , other ):
458
+ if isinstance (other , str ):
459
+ other = Version (other )
460
+ return tuple (self ) < tuple (other )
461
+
462
+ def __eq__ (self , other ):
463
+ if isinstance (other , str ):
464
+ other = Version (other )
465
+ return self == other
466
+
467
+
468
+ def Version (version : typing .Union [str , tuple ]) -> _Version :
469
+ if isinstance (version , str ):
470
+ return _Version (version )
471
+ elif isinstance (version , (tuple , list )):
472
+ return _Version (* version )
473
+ else :
474
+ raise NotImplementedError (f"No way to parse { version } , type { type (version )} " )
475
+ return _Version (version )
476
+
477
+
438
478
def get_version ():
439
479
"""
440
480
Return tmux version.
@@ -447,14 +487,14 @@ def get_version():
447
487
448
488
Returns
449
489
-------
450
- :class:`distutils.version.LooseVersion `
490
+ :class:`distutils.version.Version `
451
491
tmux version according to :func:`libtmux.common.which`'s tmux
452
492
"""
453
493
proc = tmux_cmd ("-V" )
454
494
if proc .stderr :
455
495
if proc .stderr [0 ] == "tmux: unknown option -- V" :
456
496
if sys .platform .startswith ("openbsd" ): # openbsd has no tmux -V
457
- return LooseVersion ("%s-openbsd" % TMUX_MAX_VERSION )
497
+ return Version ("%s-openbsd" % TMUX_MAX_VERSION )
458
498
raise exc .LibTmuxException (
459
499
"libtmux supports tmux %s and greater. This system"
460
500
" is running tmux 1.3 or earlier." % TMUX_MIN_VERSION
@@ -465,11 +505,11 @@ def get_version():
465
505
466
506
# Allow latest tmux HEAD
467
507
if version == "master" :
468
- return LooseVersion ("%s-master" % TMUX_MAX_VERSION )
508
+ return Version ("%s-master" % TMUX_MAX_VERSION )
469
509
470
510
version = re .sub (r"[a-z-]" , "" , version )
471
511
472
- return LooseVersion (version )
512
+ return Version (version )
473
513
474
514
475
515
def has_version (version ):
@@ -486,7 +526,7 @@ def has_version(version):
486
526
bool
487
527
True if version matches
488
528
"""
489
- return get_version () == LooseVersion (version )
529
+ return get_version () == Version (version )
490
530
491
531
492
532
def has_gt_version (min_version ):
@@ -503,7 +543,7 @@ def has_gt_version(min_version):
503
543
bool
504
544
True if version above min_version
505
545
"""
506
- return get_version () > LooseVersion (min_version )
546
+ return get_version () > Version (min_version )
507
547
508
548
509
549
def has_gte_version (min_version ):
@@ -520,7 +560,7 @@ def has_gte_version(min_version):
520
560
bool
521
561
True if version above or equal to min_version
522
562
"""
523
- return get_version () >= LooseVersion (min_version )
563
+ return get_version () >= Version (min_version )
524
564
525
565
526
566
def has_lte_version (max_version ):
@@ -537,7 +577,7 @@ def has_lte_version(max_version):
537
577
bool
538
578
True if version below or equal to max_version
539
579
"""
540
- return get_version () <= LooseVersion (max_version )
580
+ return get_version () <= Version (max_version )
541
581
542
582
543
583
def has_lt_version (max_version ):
@@ -554,7 +594,7 @@ def has_lt_version(max_version):
554
594
bool
555
595
True if version below max_version
556
596
"""
557
- return get_version () < LooseVersion (max_version )
597
+ return get_version () < Version (max_version )
558
598
559
599
560
600
def has_minimum_version (raises = True ):
@@ -587,7 +627,7 @@ def has_minimum_version(raises=True):
587
627
588
628
.. _Issue 55: https://github.com/tmux-python/tmuxp/issues/55.
589
629
"""
590
- if get_version () < LooseVersion (TMUX_MIN_VERSION ):
630
+ if get_version () < Version (TMUX_MIN_VERSION ):
591
631
if raises :
592
632
raise exc .VersionTooLow (
593
633
"libtmux only supports tmux %s and greater. This system"
@@ -670,9 +710,9 @@ def get_libtmux_version():
670
710
671
711
Returns
672
712
-------
673
- distutils.version.LooseVersion
713
+ distutils.version.Version
674
714
libtmux version
675
715
"""
676
716
from libtmux .__about__ import __version__
677
717
678
- return LooseVersion (__version__ )
718
+ return Version (__version__ )
0 commit comments