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

Removed Python imp dependency #1460

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions framework/pym/play/cmdloader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import print_function
import imp
import importlib.util
import importlib.machinery
import os
import warnings
import traceback
Expand All @@ -22,8 +23,9 @@ def load_core(self):
for filename in os.listdir(self.path):
if filename != "__init__.py" and filename.endswith(".py"):
try:
name = filename.replace(".py", "")
mod = load_python_module(name, self.path)
module_name = filename.replace(".py", "")
module_path = os.path.join(self.path, filename)
mod = load_python_module(module_name, module_path)
self._load_cmd_from(mod)
except Exception as e:
print (e)
Expand All @@ -35,7 +37,8 @@ def load_play_module(self, modname):
if os.path.exists(commands):
try:
leafname = os.path.basename(modname).split('.')[0]
mod = imp.load_source(leafname, os.path.join(modname, "commands.py"))
# print(f"Loading commands for module \"{modname}\"")
mod = load_source(leafname, os.path.join(modname, "commands.py"))
self._load_cmd_from(mod)
except Exception as e:
print('~')
Expand All @@ -55,12 +58,26 @@ def _load_cmd_from(self, mod):
if 'MODULE' in dir(mod):
self.modules[mod.MODULE] = mod


def load_python_module(name, location):
mod_desc = imp.find_module(name, [location])
mod_file = mod_desc[0]
try:
return imp.load_module(name, mod_desc[0], mod_desc[1], mod_desc[2])
finally:
if mod_file != None and not mod_file.closed:
mod_file.close()
# print(f"Loading module \"{name}\" at location \"{location}\"")
spec = importlib.util.spec_from_file_location(name, location)
if spec is None:
raise ImportError(f"Could not find module {name} at {location}")

mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)

return mod


# Obtained from https://docs.python.org/dev/whatsnew/3.12.html#imp
def load_source(modname, filename):
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module
14 changes: 11 additions & 3 deletions framework/pym/play/commands/modulesrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import urllib.request, urllib.error, urllib.parse
import shutil
import string
import imp
import importlib.util
import time
import urllib.request, urllib.parse, urllib.error
import yaml
Expand Down Expand Up @@ -40,8 +40,16 @@

def load_module(name):
base = os.path.normpath(os.path.dirname(os.path.realpath(sys.argv[0])))
mod_desc = imp.find_module(name, [os.path.join(base, 'framework/pym')])
return imp.load_module(name, mod_desc[0], mod_desc[1], mod_desc[2])
module_path = os.path.join(base, 'framework/pym', name, '__init__.py')

spec = importlib.util.spec_from_file_location(name, module_path)
if spec is None:
raise ImportError(f"Could not find module \"{name}\" at \"{module_path}\"")

mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)

return mod

json = load_module('simplejson')

Expand Down
Loading