From fe149eebb6b91e3360273d9e7a39f0ccdc0d202a Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 27 Oct 2023 11:07:55 -0400 Subject: [PATCH] Remove deprecated `imp.load_source()` re #9832 The `imp` module was deprecated in Python 3.4 and removed in Python 3.12. --- arches/install/arches-project | 1 - arches/management/commands/datatype.py | 16 ++++++---------- arches/management/commands/fn.py | 8 +++----- arches/management/commands/packages.py | 9 ++++++--- arches/management/commands/search.py | 15 ++++++++------- arches/management/commands/utils.py | 12 ++++++++++++ 6 files changed, 35 insertions(+), 26 deletions(-) diff --git a/arches/install/arches-project b/arches/install/arches-project index f3d9e76c172..8756b3b7f59 100644 --- a/arches/install/arches-project +++ b/arches/install/arches-project @@ -5,7 +5,6 @@ import argparse import codecs import django import errno -import imp import os import sys import subprocess diff --git a/arches/management/commands/datatype.py b/arches/management/commands/datatype.py index 38ace63de99..b7844e1c43b 100644 --- a/arches/management/commands/datatype.py +++ b/arches/management/commands/datatype.py @@ -19,6 +19,8 @@ """This module contains commands for building Arches.""" import os +import sys + from arches.management.commands import utils from arches.app.models import models from django.core.management.base import BaseCommand, CommandError @@ -55,11 +57,8 @@ def register(self, source): Inserts a datatype into the arches db """ - - import imp - - dt_source = imp.load_source("", source) - details = dt_source.details + utils.load_source("dt_source", source) + details = sys.modules["dt_source"].details self.validate_details(details) dt = models.DDataType( @@ -103,11 +102,8 @@ def update(self, source): Updates an existing datatype in the arches db """ - - import imp - - dt_source = imp.load_source("", source) - details = dt_source.details + utils.load_source("dt_source", source) + details = sys.modules["dt_source"].details self.validate_details(details) instance = models.DDataType.objects.get(datatype=details["datatype"]) diff --git a/arches/management/commands/fn.py b/arches/management/commands/fn.py index 1c00cbada1e..6fa5148b55e 100644 --- a/arches/management/commands/fn.py +++ b/arches/management/commands/fn.py @@ -17,6 +17,7 @@ """ import os +import sys import uuid from arches.management.commands import utils from arches.app.models import models @@ -58,11 +59,8 @@ def register(self, source): Inserts a function into the arches db """ - - import imp - - fn_config = imp.load_source("", source) - details = fn_config.details + utils.load_source("fn_config", source) + details = sys.modules["fn_config"].details try: uuid.UUID(details["functionid"]) diff --git a/arches/management/commands/packages.py b/arches/management/commands/packages.py index 0abb2313d1c..1cfd9130300 100644 --- a/arches/management/commands/packages.py +++ b/arches/management/commands/packages.py @@ -7,7 +7,6 @@ import sys import urllib.request, urllib.parse, urllib.error import os -import imp import logging from arches.setup import unzip_file from arches.management.commands import utils @@ -817,10 +816,14 @@ def load_indexes(package_dir): root = settings.APP_ROOT if settings.APP_ROOT is not None else os.path.join(settings.ROOT_DIR, "app") dest_dir = os.path.join(root, "search_indexes") + if index_files: + module_name = "package_settings" + file_path = os.path.join(settings.APP_ROOT, "package_settings.py") + utils.load_from_source(module_name, file_path) + for index_file in index_files: shutil.copy(index_file, dest_dir) - package_settings = imp.load_source("", os.path.join(settings.APP_ROOT, "package_settings.py")) - for index in package_settings.ELASTICSEARCH_CUSTOM_INDEXES: + for index in sys.modules[module_name].ELASTICSEARCH_CUSTOM_INDEXES: es_index = import_class_from_string(index["module"])(index["name"]) es_index.prepare_index() diff --git a/arches/management/commands/search.py b/arches/management/commands/search.py index 0281a9fe26f..c30955fb0ec 100644 --- a/arches/management/commands/search.py +++ b/arches/management/commands/search.py @@ -16,10 +16,11 @@ along with this program. If not, see . """ -import os +import sys import uuid -import imp + from arches.app.models import models +from arches.management.commands import utils from django.core.management.base import BaseCommand, CommandError @@ -57,10 +58,10 @@ def register(self, source): """ - dt_source = imp.load_source("", source) + utils.load_source("sc_source", source) - if getattr(dt_source, "details", None): - details = dt_source.details + if getattr(sys.modules, "sc_source", None): + details = sys.modules["sc_source"].details try: uuid.UUID(details["searchcomponentid"]) @@ -89,8 +90,8 @@ def update(self, source): """ - dt_source = imp.load_source("", source) - name = dt_source.details["componentname"] + utils.load_source("sc_source", source) + name = sys.modules["sc_source"].details["componentname"] self.unregister(name) self.register(source) diff --git a/arches/management/commands/utils.py b/arches/management/commands/utils.py index a348ba6bba3..bca64a2f81b 100644 --- a/arches/management/commands/utils.py +++ b/arches/management/commands/utils.py @@ -1,4 +1,6 @@ +import importlib.util import os +import sys import codecs @@ -60,3 +62,13 @@ def get_valid_path(path): def print_message(message): border = "*" * 80 print("{1}\n{0}\n{1}".format(message, border)) + + +def load_from_source(module_name, file_path): + """Replacement for deprecated imp.load_source(). Recipe from + https://docs.python.org/3.12/library/importlib.html#importlib.abc.Loader.exec_module""" + + spec = importlib.util.spec_from_file_location(module_name, file_path) + module = importlib.util.module_from_spec(spec) + sys.modules[module_name] = module + spec.loader.exec_module(module)