Skip to content

Commit

Permalink
Removed Python dependency imp
Browse files Browse the repository at this point in the history
  • Loading branch information
tazmaniax committed Dec 10, 2023
1 parent aa95484 commit 709a1cc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Set up python 3
uses: actions/setup-python@v3
with:
python-version: '3.11.x'
python-version: '3.x'
architecture: 'x64'
cache: 'pip'
cache-dependency-path: '**/requirements.txt'
Expand Down Expand Up @@ -63,7 +63,7 @@ jobs:
- name: Set up python 3
uses: actions/setup-python@v3
with:
python-version: '3.11.x'
python-version: '3.x'
architecture: 'x64'

- name: Set up JDK 17
Expand Down
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

0 comments on commit 709a1cc

Please sign in to comment.