1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#!/usr/bin/env python
"""
A generic wrapper to access nmodl binaries from a python installation
Please create a softlink with the binary name to be called.
"""
import os
import sys
import stat
from importlib.metadata import metadata, PackageNotFoundError
from importlib.resources import files
from find_libpython import find_libpython
def _config_exe(exe_name):
"""Sets the environment to run the real executable (returned)"""
NMODL_PREFIX = files("nmodl")
NMODL_HOME = NMODL_PREFIX / ".data"
NMODL_BIN = NMODL_HOME / "bin"
# add pywrapper path to environment
if sys.platform == "darwin":
os.environ["NMODL_WRAPLIB"] = os.path.join(NMODL_LIB, "libpywrapper.dylib")
else:
os.environ["NMODL_WRAPLIB"] = os.path.join(NMODL_LIB, "libpywrapper.so")
# add libpython*.so path to environment
os.environ["NMODL_PYLIB"] = find_libpython()
# add nmodl home to environment (i.e. necessary for nrnunits.lib)
os.environ["NMODLHOME"] = str(NMODL_HOME)
# set PYTHONPATH for embedded python to properly find the nmodl module
os.environ["PYTHONPATH"] = (
str(NMODL_PREFIX.parent) + ":" + os.environ.get("PYTHONPATH", "")
)
return os.path.join(NMODL_BIN, exe_name)
if __name__ == "__main__":
"""Set the pointed file as executable"""
exe = _config_exe(os.path.basename(sys.argv[0]))
st = os.stat(exe)
os.chmod(exe, st.st_mode | stat.S_IEXEC)
os.execv(exe, sys.argv)
|