diff --git a/pyneuroml/runners.py b/pyneuroml/runners.py index bfdcf894..1c51ee42 100644 --- a/pyneuroml/runners.py +++ b/pyneuroml/runners.py @@ -705,6 +705,7 @@ def run_jneuroml( report_jnml_output: bool = True, exit_on_fail: bool = False, return_string: bool = False, + output_prefix: str = " jNeuroML >> ", ) -> typing.Union[typing.Tuple[bool, str], bool]: """Run jnml with provided arguments. @@ -726,6 +727,8 @@ def run_jneuroml( :type exit_on_fail: bool :param return_string: toggle whether the output string should be returned :type return_string: bool + :param output_prefix: string to prefix the returned jNeuroML output with + :type output_prefix: str :returns: either a bool, or a Tuple (bool, str) depending on the value of return_string: True of jnml ran successfully, False if not; along with the @@ -756,7 +759,7 @@ def run_jneuroml( try: command = f'java -Xmx{max_memory} {pre_jar} -jar "{jar_path}" {pre_args} {target_file} {post_args}' retcode, output = execute_command_in_dir( - command, exec_in_dir, verbose=verbose, prefix=" jNeuroML >> " + command, exec_in_dir, verbose=verbose, prefix=output_prefix ) if retcode != 0: @@ -919,7 +922,7 @@ def execute_command_in_dir_with_realtime_output( raise e if not p.returncode == 0: - logger.critical( + logger.error( "*** Problem running command (return code: %s): [%s]" % (p.returncode, command) ) @@ -990,14 +993,14 @@ def execute_command_in_dir( return return_string.decode("utf-8") except subprocess.CalledProcessError as e: - logger.critical("*** Problem running last command: %s" % e) + logger.error("*** Problem running last command: %s" % e) print("####################################################################") print("%s%s" % (prefix, e.output.decode().replace("\n", "\n" + prefix))) print("####################################################################") return (e.returncode, e.output.decode()) except Exception as e: - logger.critical("*** Unknown problem running command: %s" % e) + logger.error("*** Unknown problem running command: %s" % e) return (-1, str(e)) diff --git a/pyneuroml/utils/jnmlwrapper.py b/pyneuroml/utils/jnmlwrapper.py new file mode 100644 index 00000000..aa013a06 --- /dev/null +++ b/pyneuroml/utils/jnmlwrapper.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +""" +Simple wrapper around jNeuroML to allow users to use jnml using the version +bundled in PyNeuroML + +File: pyneuroml/utils/jnmlwrapper.py + +Copyright 2025 NeuroML contributors +Author: Ankur Sinha +""" + +import logging +import os +import sys + +from ..runners import run_jneuroml + + +def __jnmlwrapper(): + """Wrapper around jNeuroML jar shipped with PyNeuroML. + + The following environment variables can be set: + + - JNML_MAX_MEMORY_LOCAL: set the maximum memory available to the Java + Virtual Machine (default: 400M) + + """ + max_memory = os.getenv("JNML_MAX_MEMORY_LOCAL", "400M") + logging.getLogger("pyneuroml.runners").setLevel(logging.CRITICAL) + + retstat, output = run_jneuroml( + pre_args=" ".join(sys.argv[1:]), + target_file="", + post_args="", + max_memory=max_memory, + report_jnml_output=False, + output_prefix="", + return_string=True, + exit_on_fail=False, + ) + + # if command ran successfully, print the output + # if it didn't, `run_jneuroml` will throw a critical error + if retstat is True: + print(output) + sys.exit(0) + else: + sys.exit(1) diff --git a/setup.cfg b/setup.cfg index 303cc311..57934ff4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -58,6 +58,7 @@ console_scripts = pynml-sonata = neuromllite.SonataReader:main pynml-xpp = pyneuroml.xppaut:main pynml-swc2nml = pyneuroml.swc.ExportNML:main + jnml = pyneuroml.utils.jnmlwrapper:__jnmlwrapper [options.package_data] * = diff --git a/test-ghactions.sh b/test-ghactions.sh index 11b9f47b..4db3520e 100755 --- a/test-ghactions.sh +++ b/test-ghactions.sh @@ -28,7 +28,7 @@ echo "## Testing all CLI tools" full_path=$(command -v pynml) bin_location=$(dirname $full_path) -for f in ${bin_location}/pynml* +for f in ${bin_location}/pynml* ${bin_location}/jnml do current_exec=$(basename $f) echo "-> Testing $current_exec runs"