Skip to content

Commit

Permalink
Revert "Remove deprecated pkg_resources, replace with importlib"
Browse files Browse the repository at this point in the history
This doesn't work...

    Traceback (most recent call last):
      File "/usr/bin/qvm-sync-appmenus", line 33, in <module>
        sys.exit(load_entry_point('qubesdesktop==4.2.8', 'console_scripts', 'qvm-sync-appmenus')())
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/site-packages/qubesappmenus/receive.py", line 411, in main
        process_appmenus_templates(appmenusext, vm, new_appmenus)
      File "/usr/lib/python3.11/site-packages/qubesappmenus/receive.py", line 308, in process_appmenus_templates
        template_data = importlib.resources.files(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib64/python3.11/importlib/resources/_common.py", line 22, in files
        return from_package(get_package(package))
                            ^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib64/python3.11/importlib/resources/_common.py", line 55, in get_package
        raise TypeError(f'{package!r} is not a package')
    TypeError: 'qubesappmenus.receive' is not a package

This reverts commit 173fd24.
  • Loading branch information
marmarek committed May 17, 2024
1 parent 90a27a8 commit f1f0016
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 71 deletions.
21 changes: 11 additions & 10 deletions qubesappmenus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import logging

import itertools
import importlib.resources
import pkg_resources
import xdg.BaseDirectory

import qubesadmin
Expand Down Expand Up @@ -345,11 +345,11 @@ def _appmenus_create_onedir(self, vm, force=False, refresh_cache=True,
anything_changed = False
directory_changed = False
directory_file = self._directory_path(vm, dispvm=dispvm)
data = importlib.resources.files(__name__).joinpath(
self.directory_template_name(vm, dispvm)).read_text()
if self.write_desktop_file(
vm,
data,
pkg_resources.resource_string(
__name__,
self.directory_template_name(vm, dispvm)).decode(),
directory_file,
dispvm):
anything_changed = True
Expand Down Expand Up @@ -384,11 +384,12 @@ def _appmenus_create_onedir(self, vm, force=False, refresh_cache=True,
if not dispvm:
vm_settings_fname = os.path.join(
appmenus_dir, self.settings_name(vm))
data = importlib.resources.files(__name__).joinpath(
'qubes-vm-settings.desktop.template').read_text()
if self.write_desktop_file(
vm,
data,
pkg_resources.resource_string(
__name__,
'qubes-vm-settings.desktop.template'
).decode(),
vm_settings_fname):
changed_appmenus.append(vm_settings_fname)
target_appmenus.append(os.path.basename(vm_settings_fname))
Expand Down Expand Up @@ -597,9 +598,9 @@ def appmenus_init(self, vm, src=None):
with open(
os.path.join(own_templates_dir, 'qubes-start.desktop'),
'wb') as qubes_start_f:
data = importlib.resources.files(__name__).joinpath(
'qubes-start.desktop.template').read_bytes()
qubes_start_f.write(data)
qubes_start_f.write(pkg_resources.resource_string(
__name__,
'qubes-start.desktop.template'))

source_whitelist_filename = 'vm-' + AppmenusSubdirs.whitelist
if src and ('default-menu-items' in src.features or os.path.exists(
Expand Down
9 changes: 4 additions & 5 deletions qubesappmenus/receive.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import os
import sys
import shlex
import importlib.resources
import pkg_resources
import qubesimgconverter

import qubesadmin.exc
Expand Down Expand Up @@ -305,10 +305,9 @@ def process_appmenus_templates(appmenusext, vm, appmenus):
if not os.path.exists(qubes_start_fname):
with open(qubes_start_fname, 'wb') as qubes_start_f:
vm.log.info("Creating Start")
template_data = importlib.resources.files(
__name__).joinpath(
'qubes-start.desktop.template').read_bytes()
qubes_start_f.write(template_data)
qubes_start_f.write(pkg_resources.resource_string(
__name__,
'qubes-start.desktop.template'))

# Do not create reserved Start entry
appmenus.pop('qubes-start', None)
Expand Down
89 changes: 33 additions & 56 deletions qubesappmenus/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import unittest.mock

import logging
import importlib.resources
import pkg_resources
import qubesappmenus
import qubesappmenus.receive

Expand Down Expand Up @@ -247,19 +247,16 @@ def test_005_created_appvm(self):
self.ext.appmenus_init(appvm)
with open(os.path.join(self.ext.templates_dirs(tpl)[0],
'evince.desktop'), 'wb') as f:
f.write(importlib.resources.files(
anchor=__name__).joinpath(
'test-data/evince.desktop.template').read_bytes())
f.write(pkg_resources.resource_string(__name__,
'test-data/evince.desktop.template'))
self.ext.appmenus_create(appvm, refresh_cache=False)
self.ext.appicons_create(appvm)
evince_path = self._make_desktop_name(appvm, 'evince.desktop')
self.assertPathExists(evince_path)
with open(evince_path, 'rb') as f:
new_file_bytes = importlib.resources.files(
anchor=__name__).joinpath(
'test-data/evince.desktop').read_bytes()
self.assertEqual(
new_file_bytes.replace(b'%BASEDIR%',
pkg_resources.resource_string(__name__,
'test-data/evince.desktop').replace(b'%BASEDIR%',
qubesappmenus.basedir.encode()),
f.read()
)
Expand All @@ -282,34 +279,27 @@ def test_006_created_appvm_custom(self):
self.ext.appmenus_init(appvm)
with open(os.path.join(self.ext.templates_dirs(tpl)[0],
'evince.desktop'), 'wb') as f:
evince_data = importlib.resources.files(
anchor=__name__).joinpath(
'test-data/evince.desktop.template').read_bytes()
f.write(evince_data)
f.write(pkg_resources.resource_string(__name__,
'test-data/evince.desktop.template'))
self.ext.appmenus_create(appvm, refresh_cache=False)
self.ext.appicons_create(appvm)

with open(os.path.join(self.ext.templates_dirs(tpl)[0],
'xterm.desktop'), 'wb') as f:
xterm_data = importlib.resources.files(
anchor=__name__).joinpath(
'test-data/xterm.desktop.template').read_bytes()
f.write(xterm_data)
f.write(pkg_resources.resource_string(__name__,
'test-data/xterm.desktop.template'))
with open(os.path.join(self.ext.templates_dirs(tpl)[0],
'evince.desktop'), 'wb') as f:
evince_data = importlib.resources.files(
anchor=__name__).joinpath(
'test-data/evince.desktop.template').read_bytes()
f.write(evince_data.replace(b'Document Viewer', b'Random Viewer'))
f.write(pkg_resources.resource_string(__name__,
'test-data/evince.desktop.template').
replace(b'Document Viewer', b'Random Viewer'))
self.ext.appmenus_update(appvm)
evince_path = self._make_desktop_name(appvm, 'evince.desktop')
self.assertPathExists(evince_path)
with open(evince_path, 'rb') as f:
evince_data = importlib.resources.files(
anchor=__name__).joinpath(
'test-data/evince.desktop').read_bytes()
self.assertEqual(
evince_data
pkg_resources.resource_string(__name__,
'test-data/evince.desktop')
.replace(b'%BASEDIR%', qubesappmenus.basedir.encode())
.replace(b'Document Viewer', b'Random Viewer'),
f.read()
Expand All @@ -318,10 +308,9 @@ def test_006_created_appvm_custom(self):
xterm_path = self._make_desktop_name(appvm, 'xterm.desktop')
self.assertPathExists(xterm_path)
with open(xterm_path, 'rb') as f:
xterm_data = importlib.resources.files(
anchor=__name__).joinpath(
'test-data/xterm.desktop').read_bytes()
self.assertEqual(xterm_data
self.assertEqual(
pkg_resources.resource_string(__name__,
'test-data/xterm.desktop')
.replace(b'%BASEDIR%', qubesappmenus.basedir.encode()),
f.read()
)
Expand All @@ -346,10 +335,8 @@ def test_007_created_dispvm(self):
self.ext.appmenus_init(appvm)
with open(os.path.join(self.ext.templates_dirs(tpl)[0],
'evince.desktop'), 'wb') as f:
evince_data = importlib.resources.files(
anchor=__name__).joinpath(
'test-data/evince.desktop.template').read_bytes()
f.write(evince_data)
f.write(pkg_resources.resource_string(__name__,
'test-data/evince.desktop.template'))
self.ext.appmenus_create(appvm, refresh_cache=False)
self.ext.appicons_create(appvm)
appmenus_dir = self.ext.appmenus_dir(appvm)
Expand Down Expand Up @@ -399,8 +386,8 @@ class PopenMockup(object):
pass
self.assertEqual(service, 'qubes.GetAppmenus')
p = PopenMockup()
p.stdout = importlib.resources.files(
anchor=__name__).joinpath('test-data/appmenus.input').open(mode='rb')
p.stdout = pkg_resources.resource_stream(__name__,
'test-data/appmenus.input')
p.wait = lambda: None
p.returncode = 0
return p
Expand Down Expand Up @@ -522,10 +509,8 @@ def test_120_create_appvm(self, mock_subprocess):
old_path = os.path.join(self.ext.templates_dirs(tpl)[0],
'evince.desktop')
with open(old_path, 'wb') as f:
evince_data = importlib.resources.files(
anchor=__name__).joinpath(
'test-data/evince.desktop.template').read_bytes()
f.write(evince_data)
f.write(pkg_resources.resource_string(__name__,
'test-data/evince.desktop.template'))
appvm = TestVM('test-inst-app',
klass='AppVM',
template=tpl,
Expand All @@ -551,12 +536,10 @@ def test_120_create_appvm(self, mock_subprocess):
evince_path = self._make_desktop_name(appvm, 'evince.desktop')
self.assertPathExists(evince_path)
with open(evince_path, 'rb') as f:
evince_data = importlib.resources.files(
anchor=__name__).joinpath(
'test-data/evince.desktop').read_bytes()
self.assertEqual(
evince_data.replace(b'%BASEDIR%',
qubesappmenus.basedir.encode()),
pkg_resources.resource_string(__name__,
'test-data/evince.desktop').replace(b'%BASEDIR%',
qubesappmenus.basedir.encode()),
f.read()
)

Expand Down Expand Up @@ -600,10 +583,8 @@ def test_121_create_appvm_with_whitelist(self, mock_subprocess):
self.ext.appmenus_init(tpl)
with open(os.path.join(self.ext.templates_dirs(tpl)[0],
'evince.desktop'), 'wb') as f:
evince_data = importlib.resources.files(
anchor=__name__).joinpath(
'test-data/evince.desktop.template').read_bytes()
f.write(evince_data)
f.write(pkg_resources.resource_string(__name__,
'test-data/evince.desktop.template'))
with open(os.path.join(self.basedir,
tpl.name,
'vm-whitelisted-appmenus.list'), 'wb') as f:
Expand All @@ -620,12 +601,10 @@ def test_121_create_appvm_with_whitelist(self, mock_subprocess):
evince_path = self._make_desktop_name(appvm, 'evince.desktop')
self.assertPathExists(evince_path)
with open(evince_path, 'rb') as f:
evince_data = importlib.resources.files(
anchor=__name__).joinpath(
'test-data/evince.desktop').read_bytes()
self.assertEqual(
evince_data.replace(b'%BASEDIR%',
qubesappmenus.basedir.encode()),
pkg_resources.resource_string(__name__,
'test-data/evince.desktop').replace(b'%BASEDIR%',
qubesappmenus.basedir.encode()),
f.read()
)

Expand Down Expand Up @@ -687,11 +666,9 @@ class PopenMockup(object):
'evince.desktop')
self.assertPathExists(evince_path)
with open(evince_path, 'rb') as f:
evince_data = importlib.resources.files(
anchor=__name__).joinpath(
'test-data/evince.desktop.template').read_bytes()
self.assertEqual(
evince_data,
pkg_resources.resource_string(__name__,
'test-data/evince.desktop.template'),
f.read()
)
self.assertCountEqual(self.appvm.log.mock_calls, [
Expand Down

0 comments on commit f1f0016

Please sign in to comment.