Use packaging.version instead of pkg_resources.parse_version by default

packaging.version is the new standard and should be preferred now. It
reduces startup time significantly too as pkg_resources is expensive to
import.

parse_version is only kept for compatibility here, perhaps consider
removing it in the future.
This commit is contained in:
Felix Yan 2021-05-06 08:44:54 +08:00
parent d7c553ae94
commit b34a43cec7
No known key found for this signature in database
GPG key ID: 786C63F330D7CB92
6 changed files with 35 additions and 28 deletions

View file

@ -203,9 +203,9 @@ exclude_regex
sort_version_key sort_version_key
Sort the version string using this key function. Choose between Sort the version string using this key function. Choose between
``parse_version`` and ``vercmp``. Default value is ``parse_version``. ``packaging``, ``parse_version`` and ``vercmp``. Default value is ``packaging``.
``parse_version`` use ``pkg_resources.parse_version``. ``vercmp`` use ``packaging`` use ``packaging.version``, ``parse_version`` use ``pkg_resources.parse_version``.
``pyalpm.vercmp``. ``vercmp`` use ``pyalpm.vercmp``.
ignored ignored
Version strings that are explicitly ignored, separated by whitespace. This Version strings that are explicitly ignored, separated by whitespace. This
@ -368,7 +368,7 @@ branch
use_max_tag use_max_tag
Set this to ``true`` to check for the max tag on Gitea. Will return the biggest one Set this to ``true`` to check for the max tag on Gitea. Will return the biggest one
sorted by ``pkg_resources.parse_version``. Will return the tag name instead of date. sorted by ``packaging.version``. Will return the tag name instead of date.
host host
Hostname for self-hosted Gitea instance. Hostname for self-hosted Gitea instance.
@ -400,7 +400,7 @@ branch
use_max_tag use_max_tag
Set this to ``true`` to check for the max tag on BitBucket. Will return the biggest one Set this to ``true`` to check for the max tag on BitBucket. Will return the biggest one
sorted by ``pkg_resources.parse_version``. Will return the tag name instead of date. sorted by ``packaging.version``. Will return the tag name instead of date.
max_page max_page
How many pages do we search for the max tag? Default is 3. This works when How many pages do we search for the max tag? Default is 3. This works when
@ -425,7 +425,7 @@ branch
use_max_tag use_max_tag
Set this to ``true`` to check for the max tag on GitLab. Will return the biggest one Set this to ``true`` to check for the max tag on GitLab. Will return the biggest one
sorted by ``pkg_resources.parse_version``. Will return the tag name instead of date. sorted by ``packaging.version``. Will return the tag name instead of date.
host host
Hostname for self-hosted GitLab instance. Hostname for self-hosted GitLab instance.

View file

@ -305,8 +305,8 @@ def apply_list_options(
if not versions: if not versions:
return None return None
sort_version_key = sort_version_keys[ sort_version_key = sort_version_keys(
conf.get("sort_version_key", "parse_version")] conf.get("sort_version_key", "packaging"))
versions.sort(key=sort_version_key) versions.sort(key=sort_version_key)
return versions[-1] return versions[-1]

View file

@ -2,21 +2,22 @@
# Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al.
''' '''
Sort versions using pkg_resource.parse_version or pyalpm.vercmp Sort versions using packaging.version, pkg_resource.parse_version, or pyalpm.vercmp
''' '''
__all__ = ["sort_version_keys"] __all__ = ["sort_version_keys"]
from functools import cmp_to_key from functools import cmp_to_key
from pkg_resources import parse_version def sort_version_keys(method: str):
try: if method == "packaging":
import pyalpm from packaging import version
vercmp = cmp_to_key(pyalpm.vercmp) return version.parse
vercmp_available = True elif method == "parse_version":
except ImportError: from pkg_resources import parse_version
def vercmp(k): return parse_version
raise NotImplementedError("Using vercmp but pyalpm can not be imported!") elif method == "vercmp":
vercmp_available = False import pyalpm
return cmp_to_key(pyalpm.vercmp)
sort_version_keys = {"parse_version": parse_version, "vercmp": vercmp} else:
raise NotImplementedError(f"Unsupported method {method} specified!")

View file

@ -72,9 +72,9 @@ def cmp() -> None:
parser.add_argument('-q', '--quiet', action='store_true', parser.add_argument('-q', '--quiet', action='store_true',
help="Quiet mode, output only the names.") help="Quiet mode, output only the names.")
parser.add_argument('-s', '--sort', parser.add_argument('-s', '--sort',
choices=('parse_version', 'vercmp'), default='parse_version', choices=('packaging', 'parse_version', 'vercmp'), default='packaging',
help='Version compare method to backwards the arrow ' help='Version compare method to backwards the arrow '
'(default: parse_version)') '(default: packaging)')
parser.add_argument('-n', '--newer', action='store_true', parser.add_argument('-n', '--newer', action='store_true',
help='Shows only the newer ones according to --sort.') help='Shows only the newer ones according to --sort.')
args = parser.parse_args() args = parser.parse_args()
@ -106,7 +106,7 @@ def cmp() -> None:
arrow = "->" arrow = "->"
if args.sort != "none" and oldver is not None and newver is not None: if args.sort != "none" and oldver is not None and newver is not None:
from .sortversion import sort_version_keys from .sortversion import sort_version_keys
version = sort_version_keys[args.sort] version = sort_version_keys(args.sort)
if version(oldver) > version(newver): if version(oldver) > version(newver):
arrow = f'{c.red}<-{c.normal}' arrow = f'{c.red}<-{c.normal}'
if args.newer: if args.newer:

View file

@ -1,8 +1,6 @@
# MIT licensed # MIT licensed
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
from nvchecker.api import sort_version_keys
# doc: https://confluence.atlassian.com/display/BITBUCKET/commits+or+commit+Resource # doc: https://confluence.atlassian.com/display/BITBUCKET/commits+or+commit+Resource
BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s' BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s'
BITBUCKET_MAX_TAG = 'https://bitbucket.org/api/2.0/repositories/%s/refs/tags' BITBUCKET_MAX_TAG = 'https://bitbucket.org/api/2.0/repositories/%s/refs/tags'

View file

@ -1,12 +1,20 @@
import pytest import pytest
from nvchecker.sortversion import parse_version, vercmp, vercmp_available from nvchecker.sortversion import sort_version_keys
def test_parse_version(): def test_parse_version():
parse_version = sort_version_keys("parse_version")
assert parse_version("v6.0") < parse_version("6.1") assert parse_version("v6.0") < parse_version("6.1")
assert parse_version("v6.0") > parse_version("v6.1-stable") assert parse_version("v6.0") > parse_version("v6.1-stable")
@pytest.mark.skipif(not vercmp_available, def test_packaging():
reason="needs pyalpm") packaging_version = sort_version_keys("packaging")
assert packaging_version("v6.0") < packaging_version("6.1")
assert packaging_version("v6.0") > packaging_version("v6.1-stable")
def test_vercmp(): def test_vercmp():
try:
vercmp = sort_version_keys("vercmp")
except ImportError:
pytest.skip("needs pyalpm")
assert vercmp("v6.0") < vercmp("v6.1-stable") assert vercmp("v6.0") < vercmp("v6.1-stable")