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

PICARD-2354: DeprecationWarning: the imp module is deprecated in favour of importlib #2134

Closed
wants to merge 10 commits into from
31 changes: 18 additions & 13 deletions picard/pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@


from functools import partial
import imp
import importlib
import json
import os.path
Expand Down Expand Up @@ -123,10 +122,8 @@ def _plugin_name_from_path(path):

def load_manifest(archive_path):
archive = zipfile.ZipFile(archive_path)
manifest_data = None
with archive.open('MANIFEST.json') as f:
manifest_data = json.loads(str(f.read().decode()))
return manifest_data
return json.loads(str(f.read().decode()))


def zip_import(path):
Expand Down Expand Up @@ -232,7 +229,7 @@ def _get_plugin_index_by_name(self, name):
return (None, None)

def _load_plugin_from_directory(self, name, plugindir):
module_file = None
info = None
zipfilename = os.path.join(plugindir, name + '.zip')
(zip_importer, module_name, manifest_data) = zip_import(zipfilename)
if zip_importer:
Expand All @@ -243,15 +240,23 @@ def _load_plugin_from_directory(self, name, plugindir):
return None
module_pathname = zip_importer.get_filename(name)
else:
try:
info = imp.find_module(name, [plugindir])
module_file = info[0]
module_pathname = info[1]
except ImportError:
# instead of looking for a module in the whole directory, we have to provide an explicit path to it
# e.g. for Picard in /home/user/picard it'd be:
# importlib.util.spec_from_file_location("picard", "/home/user/picard/picard/__init__.py")
# alternatively we could use importlib.find_loader, which works pretty much the same but is unfortunately
# deprecated since 3.4
#
# comment to be deleted, just for the PR purposes
info = importlib.machinery.PathFinder().find_spec(name, [plugindir])
if not info.loader:
error = _("Failed loading plugin %r in %r")
self.plugin_error(name, error, name, [plugindir])
return None

module_pathname = info.origin
if module_pathname.endswith("__init__.py"):
module_pathname = os.path.dirname(module_pathname)

plugin = None
try:
existing_plugin, existing_plugin_index = self._get_plugin_index_by_name(name)
Expand All @@ -267,7 +272,9 @@ def _load_plugin_from_directory(self, name, plugindir):
if zip_importer:
plugin_module = zip_importer.load_module(full_module_name)
else:
plugin_module = imp.load_module(full_module_name, *info)
plugin_module = importlib.util.module_from_spec(info)
info.loader.exec_module(plugin_module)

plugin = PluginWrapper(plugin_module, plugindir,
file=module_pathname, manifest_data=manifest_data)
compatible_versions = _compatible_api_versions(plugin.api_versions)
Expand All @@ -293,8 +300,6 @@ def _load_plugin_from_directory(self, name, plugindir):
except BaseException:
error = _("Plugin %r")
self.plugin_error(name, error, name, log_func=log.exception)
if module_file is not None:
module_file.close()
return plugin

def _get_existing_paths(self, plugin_name, fileexts):
Expand Down