From 1309fc59b3581c93c73c934e2d3864011ae1cecf Mon Sep 17 00:00:00 2001 From: lungd Date: Sun, 5 Nov 2017 16:10:54 +0100 Subject: [PATCH 1/4] Add execute_command_in_dir_with_realtime_output. Print output of command during execution. --- pyneuroml/pynml.py | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/pyneuroml/pynml.py b/pyneuroml/pynml.py index 45de117f..a1f907cc 100644 --- a/pyneuroml/pynml.py +++ b/pyneuroml/pynml.py @@ -29,6 +29,7 @@ import random import inspect import zipfile +import shlex DEFAULTS = {'v':False, 'default_java_max_memory':'400M', @@ -947,8 +948,22 @@ def run_jneuroml(pre_args, jar_path = get_path_to_jnml_jar() 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: + print_comment('*** Execution of jnml has failed! ***', True) + print_comment('*** Command: %s ***'%command, True) + #print_comment('Output: %s'%output, True) + if exit_on_fail: + sys.exit(-1) + else: + return False + + """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(command, exec_in_dir, verbose=verbose, @@ -967,7 +982,7 @@ def run_jneuroml(pre_args, if exit_on_fail: sys.exit(-1) else: - return False + return False""" return True @@ -985,6 +1000,25 @@ 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): + + 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 = 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, + p.wait() # wait for the subprocess to exit + + return True + + def execute_command_in_dir(command, directory, verbose=DEFAULTS['v'], prefix="Output: ", env=None): @@ -1225,3 +1259,4 @@ def main(args=None): if __name__ == "__main__": main() + From 769908650adce35f78924d799e8788124929eef3 Mon Sep 17 00:00:00 2001 From: lungd Date: Wed, 15 Nov 2017 14:25:19 +0100 Subject: [PATCH 2/4] Update --- pyneuroml/pynml.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/pyneuroml/pynml.py b/pyneuroml/pynml.py index a1f907cc..f19d15d3 100644 --- a/pyneuroml/pynml.py +++ b/pyneuroml/pynml.py @@ -30,6 +30,7 @@ import inspect import zipfile import shlex +import signal DEFAULTS = {'v':False, 'default_java_max_memory':'400M', @@ -949,21 +950,8 @@ def run_jneuroml(pre_args, 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: - print_comment('*** Execution of jnml has failed! ***', True) - print_comment('*** Command: %s ***'%command, True) - #print_comment('Output: %s'%output, True) - if exit_on_fail: - sys.exit(-1) - else: - return False - """try: + 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(command, exec_in_dir, verbose=verbose, @@ -974,6 +962,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) @@ -982,11 +973,12 @@ def run_jneuroml(pre_args, if exit_on_fail: sys.exit(-1) else: - return False""" + return False return True + def print_comment_v(text): print_comment(text, True) @@ -1003,6 +995,7 @@ def print_comment(text, print_it=DEFAULTS['v']): 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) @@ -1010,11 +1003,18 @@ def execute_command_in_dir_with_realtime_output(command, directory, verbose=DEFA if env is not None: print_comment("Extra env variables %s" % (env), verbose) - 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, - p.wait() # wait for the subprocess to exit + 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, + p.wait() # wait for the subprocess to exit + except KeyboardInterrupt as e: + if p: + p.kill() + #return True + raise e return True From 87beb519791f9c7b8d04e7360823c6fd3a82af93 Mon Sep 17 00:00:00 2001 From: lungd Date: Wed, 15 Nov 2017 16:03:26 +0100 Subject: [PATCH 3/4] Tries to fix python3.4 'print' issue --- pyneuroml/pynml.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyneuroml/pynml.py b/pyneuroml/pynml.py index f19d15d3..1bdcacf5 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 @@ -978,7 +979,6 @@ def run_jneuroml(pre_args, return True - def print_comment_v(text): print_comment(text, True) @@ -1008,7 +1008,7 @@ def execute_command_in_dir_with_realtime_output(command, directory, verbose=DEFA 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, + print(line, end="") p.wait() # wait for the subprocess to exit except KeyboardInterrupt as e: if p: From b9f384398652dfcc608377c44a452eea271bab26 Mon Sep 17 00:00:00 2001 From: lungd Date: Fri, 17 Nov 2017 11:58:08 +0100 Subject: [PATCH 4/4] Update --- pyneuroml/pynml.py | 54 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/pyneuroml/pynml.py b/pyneuroml/pynml.py index 1bdcacf5..711d6400 100644 --- a/pyneuroml/pynml.py +++ b/pyneuroml/pynml.py @@ -553,7 +553,8 @@ def run_lems_with_jneuroml_neuron(lems_file_name, only_generate_scripts = False, 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" \ @@ -579,8 +580,18 @@ def run_lems_with_jneuroml_neuron(lems_file_name, 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) - success = run_jneuroml("", + else: + success = run_jneuroml("", lems_file_name, post_args, max_memory = max_memory, @@ -979,6 +990,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)