Skip to content

Commit

Permalink
Remove use of pkg_resources from osrf_pycommon. (#66)
Browse files Browse the repository at this point in the history
* Remove use of pkg_resources from osrf_pycommon.

Replace it with the use of the more modern importlib*
libraries.  There are a couple of reasons to do this:

1.  pkg_resources is quite slow to import; on my machine,
just firing up the python interpreter takes ~35ms, while
firing up the python interpreter and importing pkg_resources
takes ~175ms.  Firing up the python interpreter and importing
importlib_metadata takes ~70ms.  Removing 100ms per invocation
of the command-line both makes it speedier for users, and
will speed up our tests (which call out to the command-line
quite a lot).

2.  pkg_resources is somewhat deprecated and being replaced
by importlib.  https://importlib-metadata.readthedocs.io/en/latest/using.html
describes some of it

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* depend on importlib-metadata everywhere

Signed-off-by: William Woodall <william@osrfoundation.org>

* Fixes from review.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* restrict suites to only include ones that have python3-importlib-metadata

Signed-off-by: William Woodall <william@osrfoundation.org>

* add note to README about release changes

Signed-off-by: William Woodall <william@osrfoundation.org>

Co-authored-by: William Woodall <william@osrfoundation.org>
  • Loading branch information
clalancette and wjwwood committed Feb 1, 2022
1 parent d5fb7c9 commit 6eaa13c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ python:
- "3.7"
- "3.8"
install:
- pip install coverage nose flake8 mock
- pip install coverage nose flake8 mock importlib-metadata
- pip install git+https://github.com/PyCQA/flake8-import-order.git
script:
- PYTHONASYNCIODEBUG=1 python setup.py nosetests -s --with-coverage --cover-inclusive
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ osrf_pycommon

Commonly needed Python modules, used by Python software developed at OSRF.

Branches
========

If you are releasing (using ``stdeb`` or on the ROS buildfarm) for any Ubuntu < ``focal``, or for any OS that doesn't have a key for ``python3-importlib-metadata``, then you need to use the ``1.0.x`` branch, or the latest ``1.<something>`` branch, because starting with ``2.0.0``, that dependency will be required.

If you are using Python 2, then you should use the ``python2`` branch.
9 changes: 6 additions & 3 deletions osrf_pycommon/cli_utils/verb_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import sys
import inspect

import pkg_resources
try:
import importlib.metadata as importlib_metadata
except ModuleNotFoundError:
import importlib_metadata


def call_prepare_arguments(func, parser, sysargs=None):
Expand Down Expand Up @@ -149,7 +152,7 @@ def list_verbs(group):
:rtype: list of str
"""
verbs = []
for entry_point in pkg_resources.iter_entry_points(group=group):
for entry_point in importlib_metadata.entry_points().get(group, []):
verbs.append(entry_point.name)
return verbs

Expand All @@ -162,7 +165,7 @@ def load_verb_description(verb_name, group):
:returns: verb description
:rtype: dict
"""
for entry_point in pkg_resources.iter_entry_points(group=group):
for entry_point in importlib_metadata.entry_points().get(group, []):
if entry_point.name == verb_name:
return entry_point.load()

Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<license>Apache License 2.0</license>

<exec_depend>python3-importlib-metadata</exec_depend>
<exec_depend>python3-mock</exec_depend>

<export>
Expand Down
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import sys

from setuptools import find_packages
from setuptools import setup


install_requires = [
'setuptools',
]
if sys.version_info < (3, 8):
install_requires.append('importlib-metadata')
package_excludes = ['tests*', 'docs*']
packages = find_packages(exclude=package_excludes)

Expand Down
6 changes: 3 additions & 3 deletions stdeb.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[DEFAULT]
Depends3: python3-setuptools
Depends3: python3-setuptools, python3-importlib-metadata
Conflicts3: python-osrf-pycommon
Suite3: bionic cosmic disco eoan focal jammy buster bullseye
X-Python3-Version: >= 3.5
Suite3: focal jammy buster bullseye
X-Python3-Version: >= 3.8
No-Python2:

0 comments on commit 6eaa13c

Please sign in to comment.