Skip to content

Commit 42bc294

Browse files
committed

File tree

1 file changed

+53
-13
lines changed

1 file changed

+53
-13
lines changed

libtmux/common.py

+53-13
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
77
"""
88
import collections
9+
import functools
910
import logging
1011
import os
1112
import re
1213
import subprocess
1314
import sys
14-
from distutils.version import LooseVersion
15+
import typing
1516

1617
from . import exc
1718
from ._compat import MutableMapping, console_to_str, str_from_console
@@ -435,6 +436,45 @@ def _is_executable_file_or_link(exe):
435436
return None
436437

437438

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+
438478
def get_version():
439479
"""
440480
Return tmux version.
@@ -447,14 +487,14 @@ def get_version():
447487
448488
Returns
449489
-------
450-
:class:`distutils.version.LooseVersion`
490+
:class:`distutils.version.Version`
451491
tmux version according to :func:`libtmux.common.which`'s tmux
452492
"""
453493
proc = tmux_cmd("-V")
454494
if proc.stderr:
455495
if proc.stderr[0] == "tmux: unknown option -- V":
456496
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)
458498
raise exc.LibTmuxException(
459499
"libtmux supports tmux %s and greater. This system"
460500
" is running tmux 1.3 or earlier." % TMUX_MIN_VERSION
@@ -465,11 +505,11 @@ def get_version():
465505

466506
# Allow latest tmux HEAD
467507
if version == "master":
468-
return LooseVersion("%s-master" % TMUX_MAX_VERSION)
508+
return Version("%s-master" % TMUX_MAX_VERSION)
469509

470510
version = re.sub(r"[a-z-]", "", version)
471511

472-
return LooseVersion(version)
512+
return Version(version)
473513

474514

475515
def has_version(version):
@@ -486,7 +526,7 @@ def has_version(version):
486526
bool
487527
True if version matches
488528
"""
489-
return get_version() == LooseVersion(version)
529+
return get_version() == Version(version)
490530

491531

492532
def has_gt_version(min_version):
@@ -503,7 +543,7 @@ def has_gt_version(min_version):
503543
bool
504544
True if version above min_version
505545
"""
506-
return get_version() > LooseVersion(min_version)
546+
return get_version() > Version(min_version)
507547

508548

509549
def has_gte_version(min_version):
@@ -520,7 +560,7 @@ def has_gte_version(min_version):
520560
bool
521561
True if version above or equal to min_version
522562
"""
523-
return get_version() >= LooseVersion(min_version)
563+
return get_version() >= Version(min_version)
524564

525565

526566
def has_lte_version(max_version):
@@ -537,7 +577,7 @@ def has_lte_version(max_version):
537577
bool
538578
True if version below or equal to max_version
539579
"""
540-
return get_version() <= LooseVersion(max_version)
580+
return get_version() <= Version(max_version)
541581

542582

543583
def has_lt_version(max_version):
@@ -554,7 +594,7 @@ def has_lt_version(max_version):
554594
bool
555595
True if version below max_version
556596
"""
557-
return get_version() < LooseVersion(max_version)
597+
return get_version() < Version(max_version)
558598

559599

560600
def has_minimum_version(raises=True):
@@ -587,7 +627,7 @@ def has_minimum_version(raises=True):
587627
588628
.. _Issue 55: https://github.com/tmux-python/tmuxp/issues/55.
589629
"""
590-
if get_version() < LooseVersion(TMUX_MIN_VERSION):
630+
if get_version() < Version(TMUX_MIN_VERSION):
591631
if raises:
592632
raise exc.VersionTooLow(
593633
"libtmux only supports tmux %s and greater. This system"
@@ -670,9 +710,9 @@ def get_libtmux_version():
670710
671711
Returns
672712
-------
673-
distutils.version.LooseVersion
713+
distutils.version.Version
674714
libtmux version
675715
"""
676716
from libtmux.__about__ import __version__
677717

678-
return LooseVersion(__version__)
718+
return Version(__version__)

0 commit comments

Comments
 (0)