Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace pkg_resources.parse_version with packaging.version.parse #693

Merged
merged 7 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools
python -m pip install PyYAML argparse empy rosdep vcstools catkin-pkg python-dateutil
python -m pip install PyYAML argparse empy rosdep vcstools catkin-pkg python-dateutil packaging
python -m pip install nose coverage pep8
- name: Run tests
run: |
Expand Down
4 changes: 2 additions & 2 deletions bloom/commands/git/import_upstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import tarfile
import tempfile

from pkg_resources import parse_version
from packaging.version import parse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being a bit of a python outsider, I've always disliked that a function's utility is sometimes obfuscated when the functions are imported outside their module.
Does using the alias-on-import feature make sense so that parse doesn't lose its context.
If using the alias-on-import feature is not seen as idiomatic then I could live with it as is, but I think it improves local legibility.

Suggested change
from packaging.version import parse
from packaging.version import parse as parse_version

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another alternative that is used in the other files is to import the entire version module which would afford the same context as the function would be invoked as version.parse().

Suggested change
from packaging.version import parse
from packaging import version

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say the function name should just be something more specific than parse, but I think your first suggestion is just fine for now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like I've avoided using from packaging import version, because the parameter for the version_check method was also version and there was a conflict.

But I can go with parse_version.


try:
from urlparse import urlparse
Expand Down Expand Up @@ -87,7 +87,7 @@ def version_check(version):
info(fmt("The latest upstream tag in the release repository is '@!{0}@|'."
.format(last_tag.replace('@', '@@'))))
# Ensure the new version is greater than the last tag
if parse_version(version) < parse_version(last_tag_version):
if parse(version) < parse(last_tag_version):
nuclearsandwich marked this conversation as resolved.
Show resolved Hide resolved
warning("""\
Version discrepancy:
The upstream version '{0}' isn't newer than upstream version '{1}'.
Expand Down
2 changes: 1 addition & 1 deletion bloom/commands/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import webbrowser
import yaml

from pkg_resources import parse_version
from packaging import version

# python2/3 compatibility
try:
Expand Down
4 changes: 2 additions & 2 deletions bloom/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from bloom.util import add_global_arguments
from bloom.util import handle_global_arguments

from pkg_resources import parse_version
from packaging import version
from threading import Lock

_updater_running = False
Expand Down Expand Up @@ -115,7 +115,7 @@ def fetch_update(user_bloom):
newest_version = pypi_result['info']['version']
current_version = bloom.__version__
if newest_version and bloom.__version__ != 'unset':
if parse_version(bloom.__version__) < parse_version(newest_version):
if version.parse(bloom.__version__) < version.parse(newest_version):
version_dict = {
'current': str(current_version),
'newest': str(newest_version)
Expand Down
4 changes: 2 additions & 2 deletions bloom/generators/debian/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
except ImportError:
from ConfigParser import SafeConfigParser
from dateutil import tz
from pkg_resources import parse_version
from packaging import version

from bloom.generators import BloomGenerator
from bloom.generators import GeneratorError
Expand Down Expand Up @@ -449,7 +449,7 @@ def generate_substitutions_from_package(
bad_changelog = True
# Make sure that the current version is the latest in the changelog
for changelog in changelogs:
if parse_version(package.version) < parse_version(changelog[0]):
if version.parse(package.version) < version.parse(changelog[0]):
error("")
error("There is at least one changelog entry, '{0}', which has a "
"newer version than the version of package '{1}' being released, '{2}'."
Expand Down
4 changes: 2 additions & 2 deletions bloom/generators/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@

try:
import catkin_pkg
from pkg_resources import parse_version
if parse_version(catkin_pkg.__version__) < parse_version('0.3.8'):
from packaging import version
if version.parse(catkin_pkg.__version__) < version.parse('0.3.8'):
nuclearsandwich marked this conversation as resolved.
Show resolved Hide resolved
warning("This version of bloom requires catkin_pkg version >= '0.3.8',"
" the used version of catkin_pkg is '{0}'".format(catkin_pkg.__version__))
from catkin_pkg import metapackage
Expand Down
7 changes: 5 additions & 2 deletions bloom/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@

import os
import functools
import re
import shutil
import subprocess
import tempfile

from subprocess import PIPE
from subprocess import CalledProcessError

from pkg_resources import parse_version
from packaging import version

from bloom.logging import debug
from bloom.logging import error
Expand Down Expand Up @@ -686,7 +687,9 @@ def get_last_tag_by_version(directory=None):
versions = []
for line in output.splitlines():
tags.append(line.strip())
versions.append(parse_version(line.strip()))
ver = re.match("[0-9]+.[0-9]+.[0-9]+", line)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ver = re.match("[0-9]+.[0-9]+.[0-9]+", line)
ver = re.match(r"[0-9]+\.[0-9]+\.[0-9]+", line)

Since dots are regex metacharacters they need to be escaped for an exact match. Or is the intent that any character could separate these digits?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely what was intended.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in cd20317

if ver:
versions.append(ver)
return tags[versions.index(max(versions))] if versions else ''


Expand Down
17 changes: 6 additions & 11 deletions bloom/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@
import datetime
import os
from platform import mac_ver
try:
from pkg_resources import parse_version
except OSError:
os.chdir(os.path.expanduser('~'))
from pkg_resources import parse_version
import re
import string
import sys
Expand All @@ -60,7 +55,7 @@

_emoji_check_mark = "✅ "
_emoji_cross_mark = "❌ "
_is_mac_lion_or_greater = parse_version(mac_ver()[0]) >= parse_version('10.7.0')
_is_mac = (mac_ver()[0] != '')
nuclearsandwich marked this conversation as resolved.
Show resolved Hide resolved


def ansi(key):
Expand Down Expand Up @@ -127,17 +122,17 @@ def disable_ANSI_colors():
_ansi[key] = ''


def is_mac_lion_or_greater():
global _is_mac_lion_or_greater
return _is_mac_lion_or_greater
def _is_mac():
global _is_mac
return _is_mac


def get_success_prefix():
return _emoji_check_mark if _is_mac_lion_or_greater else "@{gf}<== @|"
return _emoji_check_mark if _is_mac else "@{gf}<== @|"


def get_error_prefix():
return _emoji_cross_mark if _is_mac_lion_or_greater else "@{rf}@!<== @|"
return _emoji_cross_mark if _is_mac else "@{rf}@!<== @|"


# Default to ansi colors on
Expand Down
4 changes: 2 additions & 2 deletions bloom/rosdistro_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import sys
import traceback

from pkg_resources import parse_version
from packaging import version

# python2/3 compatibility
try:
Expand All @@ -58,7 +58,7 @@

try:
import rosdistro
if parse_version(rosdistro.__version__) < parse_version('0.7.0'):
if version.parse(rosdistro.__version__) < version.parse('0.7.0'):
error("rosdistro version 0.7.0 or greater is required, found '{0}' from '{1}'."
.format(rosdistro.__version__, os.path.dirname(rosdistro.__file__)),
exit=True)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'catkin_pkg >= 0.4.3',
'setuptools',
'empy',
'packaging',
'python-dateutil',
'PyYAML',
'rosdep >= 0.15.0',
Expand Down