diff --git a/pyneuroml/pynml.py b/pyneuroml/pynml.py index acc713b5..2bb9b1ee 100644 --- a/pyneuroml/pynml.py +++ b/pyneuroml/pynml.py @@ -10,6 +10,7 @@ """ from __future__ import absolute_import +from __future__ import print_function import os import sys import subprocess @@ -29,6 +30,8 @@ import random import inspect import zipfile +import shlex +import signal DEFAULTS = {'v':False, 'default_java_max_memory':'400M', @@ -551,7 +554,8 @@ def run_lems_with_jneuroml_neuron(lems_file_name, compile_mods = True, verbose = DEFAULTS['v'], exit_on_fail = True, - cleanup=False): + cleanup=False, + realtime_output=False): #jnml_runs_neuron=True): #jnml_runs_neuron=False is Work in progress!!! print_comment("Loading LEMS file: %s and running with jNeuroML_NEURON" \ @@ -578,9 +582,21 @@ def run_lems_with_jneuroml_neuron(lems_file_name, if not path+":" in os.environ['PYTHONPATH']: os.environ['PYTHONPATH'] = '%s:%s'%(path,os.environ['PYTHONPATH']) + print_comment('PYTHONPATH for NEURON: %s'%os.environ['PYTHONPATH'], verbose) + + if realtime_output: + success = run_jneuroml_with_realtime_output("", + lems_file_name, + post_args, + max_memory = max_memory, + exec_in_dir = exec_in_dir, + verbose = verbose, + exit_on_fail = exit_on_fail) + #print_comment('PYTHONPATH for NEURON: %s'%os.environ['PYTHONPATH'], verbose) - - success = run_jneuroml("", + + else: + success = run_jneuroml("", lems_file_name, post_args, max_memory = max_memory, @@ -959,6 +975,7 @@ def run_jneuroml(pre_args, jar_path = get_path_to_jnml_jar() output = '' + try: command = 'java -Xmx%s %s -jar "%s" %s "%s" %s' % \ @@ -971,6 +988,9 @@ def run_jneuroml(pre_args, sys.exit(-1) else: return False + + #except KeyboardInterrupt as e: + # raise e except: print_comment('*** Execution of jnml has failed! ***', True) @@ -984,6 +1004,45 @@ def run_jneuroml(pre_args, return True + +# TODO: Refactorinng +def run_jneuroml_with_realtime_output(pre_args, + target_file, + post_args, + max_memory = DEFAULTS['default_java_max_memory'], + exec_in_dir = ".", + verbose = DEFAULTS['v'], + exit_on_fail = True): + + # NOTE: Only tested with Linux + + if 'nogui' in post_args and not os.name == 'nt': + pre_jar = " -Djava.awt.headless=true" + else: + pre_jar = "" + + jar_path = get_path_to_jnml_jar() + + command = '' + output = '' + + try: + command = 'java -Xmx%s %s -jar "%s" %s "%s" %s' % \ + (max_memory, pre_jar, jar_path, pre_args, target_file, post_args) + output = execute_command_in_dir_with_realtime_output(command, exec_in_dir, verbose=verbose, + prefix = ' jNeuroML >> ') + except KeyboardInterrupt as e: + raise e + except: + print_comment('*** Execution of jnml has failed! ***', True) + print_comment('*** Command: %s ***'%command, True) + if exit_on_fail: + sys.exit(-1) + else: + return False + + return True + def print_comment_v(text): print_comment(text, True) @@ -997,6 +1056,33 @@ def print_comment(text, print_it=DEFAULTS['v']): print("%s%s"%(prefix, text.replace("\n", "\n"+prefix))) +def execute_command_in_dir_with_realtime_output(command, directory, verbose=DEFAULTS['v'], + prefix="Output: ", env=None): + + # NOTE: Only tested with Linux + if os.name == 'nt': + directory = os.path.normpath(directory) + + print_comment("Executing: (%s) in directory: %s" % (command, directory), verbose) + if env is not None: + print_comment("Extra env variables %s" % (env), verbose) + + p = None + try: + p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, bufsize=1, cwd=directory, env=env) + with p.stdout: + for line in iter(p.stdout.readline, b''): + print(line, end="") + p.wait() # wait for the subprocess to exit + except KeyboardInterrupt as e: + if p: + p.kill() + #return True + raise e + + return True + + def execute_command_in_dir(command, directory, verbose=DEFAULTS['v'], prefix="Output: ", env=None): @@ -1244,3 +1330,4 @@ def main(args=None): if __name__ == "__main__": main() +