Skip to content

Commit

Permalink
Remove deprecated pkg_resources, replace with importlib
Browse files Browse the repository at this point in the history
Requires Python>=3.8

references QubesOS/qubes-issues#9195
  • Loading branch information
marmarta committed May 6, 2024
1 parent fc80889 commit 84d084c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
22 changes: 12 additions & 10 deletions qubes/api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import libvirt
import lxml.etree
import pkg_resources
import importlib.metadata
import yaml

import qubes.api
Expand Down Expand Up @@ -103,7 +103,7 @@ async def vmclass_list(self):
self.enforce(self.dest.name == 'dom0')

entrypoints = self.fire_event_for_filter(
pkg_resources.iter_entry_points(qubes.vm.VM_ENTRY_POINT))
importlib.metadata.entry_points(group=qubes.vm.VM_ENTRY_POINT))

return ''.join('{}\n'.format(ep.name)
for ep in entrypoints)
Expand Down Expand Up @@ -1034,14 +1034,16 @@ async def vm_feature_set(self, untrusted_payload):
self.app.save()

@qubes.api.method('admin.vm.Create.{endpoint}', endpoints=(ep.name
for ep in pkg_resources.iter_entry_points(qubes.vm.VM_ENTRY_POINT)),
for ep in importlib.metadata.entry_points(
group=qubes.vm.VM_ENTRY_POINT)),
scope='global', write=True)
def vm_create(self, endpoint, untrusted_payload=None):
return self._vm_create(endpoint, allow_pool=False,
untrusted_payload=untrusted_payload)

@qubes.api.method('admin.vm.CreateInPool.{endpoint}', endpoints=(ep.name
for ep in pkg_resources.iter_entry_points(qubes.vm.VM_ENTRY_POINT)),
for ep in importlib.metadata.entry_points(
group=qubes.vm.VM_ENTRY_POINT)),
scope='global', write=True)
def vm_create_in_pool(self, endpoint, untrusted_payload=None):
return self._vm_create(endpoint, allow_pool=True,
Expand Down Expand Up @@ -1184,12 +1186,12 @@ async def deviceclass_list(self):
self.enforce(self.dest.name == 'dom0')

entrypoints = self.fire_event_for_filter(
pkg_resources.iter_entry_points('qubes.devices'))
importlib.metadata.entry_points(group='qubes.devices'))

return ''.join('{}\n'.format(ep.name) for ep in entrypoints)

@qubes.api.method('admin.vm.device.{endpoint}.Available', endpoints=(ep.name
for ep in pkg_resources.iter_entry_points('qubes.devices')),
for ep in importlib.metadata.entry_points(group='qubes.devices')),
no_payload=True,
scope='local', read=True)
async def vm_device_available(self, endpoint):
Expand Down Expand Up @@ -1223,7 +1225,7 @@ async def vm_device_available(self, endpoint):
for ident in sorted(dev_info))

@qubes.api.method('admin.vm.device.{endpoint}.List', endpoints=(ep.name
for ep in pkg_resources.iter_entry_points('qubes.devices')),
for ep in importlib.metadata.entry_points(group='qubes.devices')),
no_payload=True,
scope='local', read=True)
async def vm_device_list(self, endpoint):
Expand Down Expand Up @@ -1259,7 +1261,7 @@ async def vm_device_list(self, endpoint):
# persistent=True) and volatile state of running VM (with persistent=False).
# For this reason, write=True + execute=True
@qubes.api.method('admin.vm.device.{endpoint}.Attach', endpoints=(ep.name
for ep in pkg_resources.iter_entry_points('qubes.devices')),
for ep in importlib.metadata.entry_points(group='qubes.devices')),
scope='local', write=True, execute=True)
async def vm_device_attach(self, endpoint, untrusted_payload):
devclass = endpoint
Expand Down Expand Up @@ -1303,7 +1305,7 @@ async def vm_device_attach(self, endpoint, untrusted_payload):
# persistent=True) and volatile state of running VM (with persistent=False).
# For this reason, write=True + execute=True
@qubes.api.method('admin.vm.device.{endpoint}.Detach', endpoints=(ep.name
for ep in pkg_resources.iter_entry_points('qubes.devices')),
for ep in importlib.metadata.entry_points(group='qubes.devices')),
no_payload=True,
scope='local', write=True, execute=True)
async def vm_device_detach(self, endpoint):
Expand All @@ -1329,7 +1331,7 @@ async def vm_device_detach(self, endpoint):
# For this reason, write=True + execute=True
@qubes.api.method('admin.vm.device.{endpoint}.Set.persistent',
endpoints=(ep.name
for ep in pkg_resources.iter_entry_points('qubes.devices')),
for ep in importlib.metadata.entry_points(group='qubes.devices')),
scope='local', write=True, execute=True)
async def vm_device_set_persistent(self, endpoint, untrusted_payload):
devclass = endpoint
Expand Down
4 changes: 2 additions & 2 deletions qubes/ext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
particular customer.
'''

import pkg_resources
import importlib.metadata
import qubes.events


Expand Down Expand Up @@ -56,7 +56,7 @@ def __new__(cls):

def get_extensions():
return set(ext.load()()
for ext in pkg_resources.iter_entry_points('qubes.ext'))
for ext in importlib.metadata.entry_points(group='qubes.ext'))


def handler(*events, **kwargs):
Expand Down
5 changes: 3 additions & 2 deletions qubes/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import asyncio
import lxml.etree
import pkg_resources
import importlib.metadata
import qubes
import qubes.exc
import qubes.utils
Expand Down Expand Up @@ -989,7 +989,8 @@ def _sanitize_config(config):
def pool_drivers():
""" Return a list of EntryPoints names """
return [ep.name
for ep in pkg_resources.iter_entry_points(STORAGE_ENTRY_POINT)]
for ep in importlib.metadata.entry_points(
group=STORAGE_ENTRY_POINT)]


def driver_parameters(name):
Expand Down
12 changes: 6 additions & 6 deletions qubes/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

import gc
import lxml.etree
import pkg_resources
import importlib.metadata

import qubes
import qubes.api
Expand Down Expand Up @@ -352,7 +352,7 @@ def __exit__(self, exc_type, exc_value, tb):


class substitute_entry_points(object):
"""Monkey-patch pkg_resources to substitute one group in iter_entry_points
"""Monkey-patch importlib.metadata to substitute one group in entry_points
This is for testing plugins, like device classes.
Expand All @@ -374,15 +374,15 @@ def __init__(self, group, tempgroup):
def _iter_entry_points(self, group, *args, **kwargs):
if group == self.group:
group = self.tempgroup
return self._orig_iter_entry_points(group, *args, **kwargs)
return self._orig_iter_entry_points(group=group, *args, **kwargs)

def __enter__(self):
self._orig_iter_entry_points = pkg_resources.iter_entry_points
pkg_resources.iter_entry_points = self._iter_entry_points
self._orig_iter_entry_points = importlib.metadata.entry_points
importlib.metadata.entry_points = self._iter_entry_points
return self

def __exit__(self, exc_type, exc_value, tb):
pkg_resources.iter_entry_points = self._orig_iter_entry_points
importlib.metadata.entry_points = self._orig_iter_entry_points
self._orig_iter_entry_points = None


Expand Down
12 changes: 6 additions & 6 deletions qubes/tests/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import subprocess
import sys

import pkg_resources
import importlib.metadata

import qubes.tests
import qubes.vm.appvm
Expand Down Expand Up @@ -217,13 +217,13 @@ def load_tests(loader, tests, pattern):
if 'QUBES_TEST_EXTRA_EXCLUDE' in os.environ:
exclude_list = os.environ['QUBES_TEST_EXTRA_EXCLUDE'].split()

for entry in pkg_resources.iter_entry_points('qubes.tests.extra'):
for entry in importlib.metadata.entry_points(group='qubes.tests.extra'):
if include_list is not None and entry.name not in include_list:
continue
if entry.name in exclude_list:
continue
try:
for test_case in entry.resolve()():
for test_case in entry.load()():
tests.addTests(loader.loadTestsFromNames([
'{}.{}'.format(test_case.__module__, test_case.__name__)]))
except Exception as err: # pylint: disable=broad-except
Expand All @@ -234,14 +234,14 @@ def runTest(self, err=err):
{entry.name: runTest})
tests.addTest(ExtraLoadFailure(entry.name))

for entry in pkg_resources.iter_entry_points(
'qubes.tests.extra.for_template'):
for entry in importlib.metadata.entry_points(
name='qubes.tests.extra.for_template'):
if include_list is not None and entry.name not in include_list:
continue
if entry.name in exclude_list:
continue
try:
for test_case in entry.resolve()():
for test_case in entry.load()():
tests.addTests(loader.loadTestsFromNames(
qubes.tests.create_testcases_for_templates(
test_case.__name__, test_case,
Expand Down
1 change: 0 additions & 1 deletion qubes/tests/integ/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import collections

import pkg_resources
import shutil

import sys
Expand Down
4 changes: 2 additions & 2 deletions qubes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import tempfile
from contextlib import contextmanager, suppress

import pkg_resources
import importlib.metadata

import docutils
import docutils.core
Expand Down Expand Up @@ -143,7 +143,7 @@ def urandom(size):


def get_entry_point_one(group, name):
epoints = tuple(pkg_resources.iter_entry_points(group, name))
epoints = tuple(importlib.metadata.entry_points(group=group, name=name))
if not epoints:
raise KeyError(name)
if len(epoints) > 1:
Expand Down

0 comments on commit 84d084c

Please sign in to comment.