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

#1457 python imp module #1465

Merged
merged 6 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
14 changes: 7 additions & 7 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ jobs:
name: Check / Tests -> JDK-${{ matrix.jdk }}/${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
# we don't know what commit the last tag was it's safer to get entire repo so previousStableVersion resolves
fetch-depth: 0

- name: Set up python 3
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: '3.x'
architecture: 'x64'
Expand All @@ -39,7 +39,7 @@ jobs:
- run: pip install -r python/requirements.txt

- name: Set up JDK ${{ matrix.jdk }}
uses: actions/setup-java@v2
uses: actions/setup-java@v3
with:
java-version: ${{ matrix.jdk }}
distribution: 'adopt'
Expand All @@ -55,19 +55,19 @@ jobs:
name: BUILD ${{ github.sha }}
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
# we don't know what commit the last tag was it's safer to get entire repo so previousStableVersion resolves
fetch-depth: 0

- name: Set up python 3
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: '3.x'
architecture: 'x64'

- name: Set up JDK 17
uses: actions/setup-java@v2
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'adopt'
Expand All @@ -77,7 +77,7 @@ jobs:
run: ant artifact

- name: ziping artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: play-${{ github.sha }}
if-no-files-found: error
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
4 changes: 2 additions & 2 deletions samples-and-tests/i-am-a-developer/test_jvm_version_flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def testWithFlag(self, mock):
play_app.java_cmd([])

step('Assert getJavaVersion was not called')
self.assert_(not mock.called)
mock.assert_(not mock.called)

@mock.patch('play.application.getJavaVersion', return_value='')
def testWithoutFlag(self, mock):
Expand All @@ -43,7 +43,7 @@ def testWithoutFlag(self, mock):
play_app.java_cmd([])

step('Assert getJavaVersion was called once')
self.assert_(mock.called)
mock.assert_(mock.called)


if __name__ == '__main__':
Expand Down
Loading
Loading