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

Fix MExt #49

Merged
merged 3 commits into from
Apr 12, 2021
Merged
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
28 changes: 15 additions & 13 deletions idwarp/MExt.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import tempfile
import imp
from importlib.util import find_spec
from pathlib import Path
import os
import shutil
import sys


def _tmp_pkg(dir):
def _tmp_pkg(tempDir):
"""
Create a temporary package.

Returns (name, path)
"""
while True:
path = tempfile.mkdtemp(dir=dir)
path = tempfile.mkdtemp(dir=tempDir)
name = os.path.basename(path)
try:
imp.find_module(name)
# if name is found, delete and try again
os.rmdir(path)
except: # noqa: E722
spec = find_spec(name)
# None means the name was not found
if spec is None:
break
init = open(os.path.join(path, "__init__.py"), "w")
init.close()
# if name is found, delete and try again
os.rmdir(path)
# this creates an init file so that python recognizes this as a package
Path(os.path.join(path, "__init__.py")).touch()
return name, path


@@ -30,12 +31,13 @@ class MExt(object):
Load a unique copy of a module that can be treated as a "class instance".
"""

def __init__(self, name, path=None, debug=False):
def __init__(self, libName, packageName, debug=False):
tmpdir = tempfile.gettempdir()
self.name = name
self.name = libName
self.debug = debug
# first find the "real" module on the "real" syspath
srcfile, srcpath, srcdesc = imp.find_module(name, path)
spec = find_spec(packageName)
srcpath = os.path.join(spec.submodule_search_locations[0], f"{libName}.so")
# now create a temp directory for the bogus package
self._pkgname, self._pkgdir = _tmp_pkg(tmpdir)
# copy the original module to the new package
4 changes: 2 additions & 2 deletions idwarp/MultiUnstructuredMesh.py
Original file line number Diff line number Diff line change
@@ -101,8 +101,8 @@ def __init__(self, CGNSFile, optionsDict, comm=None, dtype="d", debug=False):
try:
self.warp
except AttributeError:
curDir = os.path.dirname(os.path.realpath(__file__))
self.warp = MExt("idwarp", [curDir], debug=debug)._module
curDir = os.path.basename(os.path.dirname(os.path.realpath(__file__)))
self.warp = MExt("idwarp", curDir, debug=debug)._module

# Store communicator
self.comm = comm
4 changes: 2 additions & 2 deletions idwarp/MultiUnstructuredMesh_C.py
Original file line number Diff line number Diff line change
@@ -28,6 +28,6 @@ def __init__(self, *args, **kwargs):
if "debug" in kwargs:
debug = kwargs["debug"]

curDir = os.path.dirname(os.path.realpath(__file__))
self.warp = MExt.MExt("idwarp_cs", [curDir], debug=debug)._module
curDir = os.path.basename(os.path.dirname(os.path.realpath(__file__)))
self.warp = MExt.MExt("idwarp_cs", curDir, debug=debug)._module
MultiUSMesh.__init__(self, dtype="D", *args, **kwargs)
4 changes: 2 additions & 2 deletions idwarp/UnstructuredMesh.py
Original file line number Diff line number Diff line change
@@ -116,8 +116,8 @@ def __init__(self, options=None, comm=None, debug=False):
try:
self.warp
except AttributeError:
curDir = os.path.dirname(os.path.realpath(__file__))
self.warp = MExt("idwarp", [curDir], debug=debug)._module
curDir = os.path.basename(os.path.dirname(os.path.realpath(__file__)))
self.warp = MExt("idwarp", curDir, debug=debug)._module

# Initialize PETSc if not done so
self.warp.initpetsc(self.comm.py2f())
4 changes: 2 additions & 2 deletions idwarp/UnstructuredMesh_C.py
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ def __init__(self, *args, **kwargs):
if "debug" in kwargs:
debug = kwargs["debug"]

curDir = os.path.dirname(os.path.realpath(__file__))
self.warp = MExt.MExt("idwarp_cs", [curDir], debug=debug)._module
curDir = os.path.basename(os.path.dirname(os.path.realpath(__file__)))
self.warp = MExt.MExt("idwarp_cs", curDir, debug=debug)._module
USMesh.__init__(self, *args, **kwargs)
self.dtype = "D"