Skip to content

Commit 173fd24

Browse files
committed
Remove deprecated pkg_resources, replace with importlib
references QubesOS/qubes-issues#9195
1 parent 87a28b3 commit 173fd24

File tree

3 files changed

+71
-48
lines changed

3 files changed

+71
-48
lines changed

qubesappmenus/__init__.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import logging
3131

3232
import itertools
33-
import pkg_resources
33+
import importlib.resources
3434
import xdg.BaseDirectory
3535

3636
import qubesadmin
@@ -345,11 +345,11 @@ def _appmenus_create_onedir(self, vm, force=False, refresh_cache=True,
345345
anything_changed = False
346346
directory_changed = False
347347
directory_file = self._directory_path(vm, dispvm=dispvm)
348+
data = importlib.resources.files(__name__).joinpath(
349+
self.directory_template_name(vm, dispvm)).read_text()
348350
if self.write_desktop_file(
349351
vm,
350-
pkg_resources.resource_string(
351-
__name__,
352-
self.directory_template_name(vm, dispvm)).decode(),
352+
data,
353353
directory_file,
354354
dispvm):
355355
anything_changed = True
@@ -384,12 +384,11 @@ def _appmenus_create_onedir(self, vm, force=False, refresh_cache=True,
384384
if not dispvm:
385385
vm_settings_fname = os.path.join(
386386
appmenus_dir, self.settings_name(vm))
387+
data = importlib.resources.files(__name__).joinpath(
388+
'qubes-vm-settings.desktop.template').read_text()
387389
if self.write_desktop_file(
388390
vm,
389-
pkg_resources.resource_string(
390-
__name__,
391-
'qubes-vm-settings.desktop.template'
392-
).decode(),
391+
data,
393392
vm_settings_fname):
394393
changed_appmenus.append(vm_settings_fname)
395394
target_appmenus.append(os.path.basename(vm_settings_fname))
@@ -598,9 +597,9 @@ def appmenus_init(self, vm, src=None):
598597
with open(
599598
os.path.join(own_templates_dir, 'qubes-start.desktop'),
600599
'wb') as qubes_start_f:
601-
qubes_start_f.write(pkg_resources.resource_string(
602-
__name__,
603-
'qubes-start.desktop.template'))
600+
data = importlib.resources.files(__name__).joinpath(
601+
'qubes-start.desktop.template').read_bytes()
602+
qubes_start_f.write(data)
604603

605604
source_whitelist_filename = 'vm-' + AppmenusSubdirs.whitelist
606605
if src and ('default-menu-items' in src.features or os.path.exists(

qubesappmenus/receive.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import os
2828
import sys
2929
import shlex
30-
import pkg_resources
30+
import importlib.resources
3131
import qubesimgconverter
3232

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

312313
# Do not create reserved Start entry
313314
appmenus.pop('qubes-start', None)

qubesappmenus/tests.py

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import unittest.mock
3232

3333
import logging
34-
import pkg_resources
34+
import importlib.resources
3535
import qubesappmenus
3636
import qubesappmenus.receive
3737

@@ -247,16 +247,19 @@ def test_005_created_appvm(self):
247247
self.ext.appmenus_init(appvm)
248248
with open(os.path.join(self.ext.templates_dirs(tpl)[0],
249249
'evince.desktop'), 'wb') as f:
250-
f.write(pkg_resources.resource_string(__name__,
251-
'test-data/evince.desktop.template'))
250+
f.write(importlib.resources.files(
251+
anchor=__name__).joinpath(
252+
'test-data/evince.desktop.template').read_bytes())
252253
self.ext.appmenus_create(appvm, refresh_cache=False)
253254
self.ext.appicons_create(appvm)
254255
evince_path = self._make_desktop_name(appvm, 'evince.desktop')
255256
self.assertPathExists(evince_path)
256257
with open(evince_path, 'rb') as f:
258+
new_file_bytes = importlib.resources.files(
259+
anchor=__name__).joinpath(
260+
'test-data/evince.desktop').read_bytes()
257261
self.assertEqual(
258-
pkg_resources.resource_string(__name__,
259-
'test-data/evince.desktop').replace(b'%BASEDIR%',
262+
new_file_bytes.replace(b'%BASEDIR%',
260263
qubesappmenus.basedir.encode()),
261264
f.read()
262265
)
@@ -279,27 +282,34 @@ def test_006_created_appvm_custom(self):
279282
self.ext.appmenus_init(appvm)
280283
with open(os.path.join(self.ext.templates_dirs(tpl)[0],
281284
'evince.desktop'), 'wb') as f:
282-
f.write(pkg_resources.resource_string(__name__,
283-
'test-data/evince.desktop.template'))
285+
evince_data = importlib.resources.files(
286+
anchor=__name__).joinpath(
287+
'test-data/evince.desktop.template').read_bytes()
288+
f.write(evince_data)
284289
self.ext.appmenus_create(appvm, refresh_cache=False)
285290
self.ext.appicons_create(appvm)
286291

287292
with open(os.path.join(self.ext.templates_dirs(tpl)[0],
288293
'xterm.desktop'), 'wb') as f:
289-
f.write(pkg_resources.resource_string(__name__,
290-
'test-data/xterm.desktop.template'))
294+
xterm_data = importlib.resources.files(
295+
anchor=__name__).joinpath(
296+
'test-data/xterm.desktop.template').read_bytes()
297+
f.write(xterm_data)
291298
with open(os.path.join(self.ext.templates_dirs(tpl)[0],
292299
'evince.desktop'), 'wb') as f:
293-
f.write(pkg_resources.resource_string(__name__,
294-
'test-data/evince.desktop.template').
295-
replace(b'Document Viewer', b'Random Viewer'))
300+
evince_data = importlib.resources.files(
301+
anchor=__name__).joinpath(
302+
'test-data/evince.desktop.template').read_bytes()
303+
f.write(evince_data.replace(b'Document Viewer', b'Random Viewer'))
296304
self.ext.appmenus_update(appvm)
297305
evince_path = self._make_desktop_name(appvm, 'evince.desktop')
298306
self.assertPathExists(evince_path)
299307
with open(evince_path, 'rb') as f:
308+
evince_data = importlib.resources.files(
309+
anchor=__name__).joinpath(
310+
'test-data/evince.desktop').read_bytes()
300311
self.assertEqual(
301-
pkg_resources.resource_string(__name__,
302-
'test-data/evince.desktop')
312+
evince_data
303313
.replace(b'%BASEDIR%', qubesappmenus.basedir.encode())
304314
.replace(b'Document Viewer', b'Random Viewer'),
305315
f.read()
@@ -308,9 +318,10 @@ def test_006_created_appvm_custom(self):
308318
xterm_path = self._make_desktop_name(appvm, 'xterm.desktop')
309319
self.assertPathExists(xterm_path)
310320
with open(xterm_path, 'rb') as f:
311-
self.assertEqual(
312-
pkg_resources.resource_string(__name__,
313-
'test-data/xterm.desktop')
321+
xterm_data = importlib.resources.files(
322+
anchor=__name__).joinpath(
323+
'test-data/xterm.desktop').read_bytes()
324+
self.assertEqual(xterm_data
314325
.replace(b'%BASEDIR%', qubesappmenus.basedir.encode()),
315326
f.read()
316327
)
@@ -335,8 +346,10 @@ def test_007_created_dispvm(self):
335346
self.ext.appmenus_init(appvm)
336347
with open(os.path.join(self.ext.templates_dirs(tpl)[0],
337348
'evince.desktop'), 'wb') as f:
338-
f.write(pkg_resources.resource_string(__name__,
339-
'test-data/evince.desktop.template'))
349+
evince_data = importlib.resources.files(
350+
anchor=__name__).joinpath(
351+
'test-data/evince.desktop.template').read_bytes()
352+
f.write(evince_data)
340353
self.ext.appmenus_create(appvm, refresh_cache=False)
341354
self.ext.appicons_create(appvm)
342355
appmenus_dir = self.ext.appmenus_dir(appvm)
@@ -386,8 +399,8 @@ class PopenMockup(object):
386399
pass
387400
self.assertEqual(service, 'qubes.GetAppmenus')
388401
p = PopenMockup()
389-
p.stdout = pkg_resources.resource_stream(__name__,
390-
'test-data/appmenus.input')
402+
p.stdout = importlib.resources.files(
403+
anchor=__name__).joinpath('test-data/appmenus.input').open(mode='rb')
391404
p.wait = lambda: None
392405
p.returncode = 0
393406
return p
@@ -509,8 +522,10 @@ def test_120_create_appvm(self, mock_subprocess):
509522
old_path = os.path.join(self.ext.templates_dirs(tpl)[0],
510523
'evince.desktop')
511524
with open(old_path, 'wb') as f:
512-
f.write(pkg_resources.resource_string(__name__,
513-
'test-data/evince.desktop.template'))
525+
evince_data = importlib.resources.files(
526+
anchor=__name__).joinpath(
527+
'test-data/evince.desktop.template').read_bytes()
528+
f.write(evince_data)
514529
appvm = TestVM('test-inst-app',
515530
klass='AppVM',
516531
template=tpl,
@@ -536,10 +551,12 @@ def test_120_create_appvm(self, mock_subprocess):
536551
evince_path = self._make_desktop_name(appvm, 'evince.desktop')
537552
self.assertPathExists(evince_path)
538553
with open(evince_path, 'rb') as f:
554+
evince_data = importlib.resources.files(
555+
anchor=__name__).joinpath(
556+
'test-data/evince.desktop').read_bytes()
539557
self.assertEqual(
540-
pkg_resources.resource_string(__name__,
541-
'test-data/evince.desktop').replace(b'%BASEDIR%',
542-
qubesappmenus.basedir.encode()),
558+
evince_data.replace(b'%BASEDIR%',
559+
qubesappmenus.basedir.encode()),
543560
f.read()
544561
)
545562

@@ -583,8 +600,10 @@ def test_121_create_appvm_with_whitelist(self, mock_subprocess):
583600
self.ext.appmenus_init(tpl)
584601
with open(os.path.join(self.ext.templates_dirs(tpl)[0],
585602
'evince.desktop'), 'wb') as f:
586-
f.write(pkg_resources.resource_string(__name__,
587-
'test-data/evince.desktop.template'))
603+
evince_data = importlib.resources.files(
604+
anchor=__name__).joinpath(
605+
'test-data/evince.desktop.template').read_bytes()
606+
f.write(evince_data)
588607
with open(os.path.join(self.basedir,
589608
tpl.name,
590609
'vm-whitelisted-appmenus.list'), 'wb') as f:
@@ -601,10 +620,12 @@ def test_121_create_appvm_with_whitelist(self, mock_subprocess):
601620
evince_path = self._make_desktop_name(appvm, 'evince.desktop')
602621
self.assertPathExists(evince_path)
603622
with open(evince_path, 'rb') as f:
623+
evince_data = importlib.resources.files(
624+
anchor=__name__).joinpath(
625+
'test-data/evince.desktop').read_bytes()
604626
self.assertEqual(
605-
pkg_resources.resource_string(__name__,
606-
'test-data/evince.desktop').replace(b'%BASEDIR%',
607-
qubesappmenus.basedir.encode()),
627+
evince_data.replace(b'%BASEDIR%',
628+
qubesappmenus.basedir.encode()),
608629
f.read()
609630
)
610631

@@ -666,9 +687,11 @@ class PopenMockup(object):
666687
'evince.desktop')
667688
self.assertPathExists(evince_path)
668689
with open(evince_path, 'rb') as f:
690+
evince_data = importlib.resources.files(
691+
anchor=__name__).joinpath(
692+
'test-data/evince.desktop.template').read_bytes()
669693
self.assertEqual(
670-
pkg_resources.resource_string(__name__,
671-
'test-data/evince.desktop.template'),
694+
evince_data,
672695
f.read()
673696
)
674697
self.assertCountEqual(self.appvm.log.mock_calls, [

0 commit comments

Comments
 (0)