Skip to content

Commit

Permalink
Remove deprecated imp.load_source() re #9832
Browse files Browse the repository at this point in the history
The `imp` module was deprecated in Python 3.4
and removed in Python 3.12.
  • Loading branch information
jacobtylerwalls committed Oct 27, 2023
1 parent a1b6fb4 commit bc41499
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 26 deletions.
1 change: 0 additions & 1 deletion arches/install/arches-project
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import argparse
import codecs
import django
import errno
import imp
import os
import sys
import subprocess
Expand Down
16 changes: 6 additions & 10 deletions arches/management/commands/datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"])
Expand Down
8 changes: 3 additions & 5 deletions arches/management/commands/fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""

import os
import sys
import uuid
from arches.management.commands import utils
from arches.app.models import models
Expand Down Expand Up @@ -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"])
Expand Down
9 changes: 6 additions & 3 deletions arches/management/commands/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
15 changes: 8 additions & 7 deletions arches/management/commands/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

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


Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions arches/management/commands/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import importlib.util
import os
import sys
import codecs


Expand Down Expand Up @@ -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)

0 comments on commit bc41499

Please sign in to comment.