Skip to content

Commit c66c8df

Browse files
authored
Merge pull request #115 from dihm/compiler_cleanup
Compiler cleanup
2 parents c1aa98b + 6083f02 commit c66c8df

File tree

1 file changed

+62
-32
lines changed

1 file changed

+62
-32
lines changed

runmanager/__init__.py

+62-32
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ def stop_trace(self):
118118

119119

120120
def new_globals_file(filename):
121+
"""Creates a new globals h5 file.
122+
123+
Creates a 'globals' group at the top level.
124+
If file does not exist, a new h5 file is created.
125+
"""
121126
with h5py.File(filename, 'w') as f:
122127
f.create_group('globals')
123128

@@ -132,7 +137,7 @@ def add_expansion_groups(filename):
132137
requires_expansion_group = []
133138
for groupname in f['globals']:
134139
group = f['globals'][groupname]
135-
if not 'expansion' in group:
140+
if 'expansion' not in group:
136141
requires_expansion_group.append(groupname)
137142
if requires_expansion_group:
138143
group_globalslists = [get_globalslist(filename, groupname) for groupname in requires_expansion_group]
@@ -507,7 +512,7 @@ def evaluate_globals(sequence_globals, raise_exceptions=True):
507512
for global_name in sequence_globals[group_name]:
508513
# Do not attempt to override exception objects already stored
509514
# as the result of multiply defined globals:
510-
if not global_name in results[group_name]:
515+
if global_name not in results[group_name]:
511516
results[group_name][global_name] = evaled_globals[global_name]
512517

513518
return results, global_hierarchy, expansions
@@ -810,33 +815,30 @@ def make_run_file_from_globals_files(labscript_file, globals_files, output_path,
810815
make_single_run_file(output_path, sequence_globals, shots[0], sequence_attrs, 1, 1)
811816

812817

813-
def compile_labscript(labscript_file, run_file):
814-
"""Compiles labscript_file with the run file, returning
815-
the processes return code, stdout and stderr."""
816-
proc = subprocess.Popen([sys.executable, labscript_file, run_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
817-
stdout, stderr = proc.communicate()
818-
return proc.returncode, stdout, stderr
819-
820-
821-
def compile_labscript_with_globals_files(labscript_file, globals_files, output_path):
822-
"""Creates a run file output_path, using all the globals from
823-
globals_files. Compiles labscript_file with the run file, returning
824-
the processes return code, stdout and stderr."""
825-
make_run_file_from_globals_files(labscript_file, globals_files, output_path)
826-
returncode, stdout, stderr = compile_labscript(labscript_file, output_path)
827-
return returncode, stdout, stderr
828-
829-
830-
def compile_labscript_async(labscript_file, run_file, stream_port, done_callback):
831-
"""Compiles labscript_file with run_file. This function is designed to be called in
832-
a thread. The stdout and stderr from the compilation will be shovelled into
818+
def compile_labscript_async(labscript_file, run_file,
819+
stream_port=None, done_callback=None):
820+
"""Compiles labscript_file with run_file.
821+
822+
This function is designed to be called in a thread.
823+
The stdout and stderr from the compilation will be shovelled into
833824
stream_port via zmq push as it spews forth, and when compilation is complete,
834825
done_callback will be called with a boolean argument indicating success. Note that
835826
the zmq communication will be encrypted, or not, according to security settings in
836827
labconfig. If you want to receive the data on a zmq socket, do so using a PULL
837828
socket created from a labscript_utils.ls_zprocess.Context, or using a
838829
labscript_utils.ls_zprocess.ZMQServer. These subclasses will also be configured
839830
with the appropriate security settings and will be able to receive the messages.
831+
832+
Args:
833+
labscript_file (str): Path to labscript file to be compiled
834+
run_file (str): Path to h5 file where compilation output is stored.
835+
This file must already exist with proper globals initialization.
836+
See :func:`new_globals_file` for details.
837+
stream_port (zmq.socket, optional): ZMQ socket to push stdout and stderr.
838+
If None, defaults to calling process stdout/stderr. Default is None.
839+
done_callback (function, optional): Callback function run when compilation finishes.
840+
Takes a single boolean argument marking compilation success or failure.
841+
If None, callback is skipped. Default is None.
840842
"""
841843
compiler_path = os.path.join(os.path.dirname(__file__), 'batch_compiler.py')
842844
to_child, from_child, child = process_tree.subprocess(
@@ -849,22 +851,37 @@ def compile_labscript_async(labscript_file, run_file, stream_port, done_callback
849851
success = data
850852
to_child.put(['quit', None])
851853
child.communicate()
852-
done_callback(success)
854+
if done_callback is not None:
855+
done_callback(success)
853856
break
854857
else:
855858
raise RuntimeError((signal, data))
856859

857860

858-
def compile_multishot_async(labscript_file, run_files, stream_port, done_callback):
859-
"""Compiles labscript_file with run_files. This function is designed to be called in
860-
a thread. The stdout and stderr from the compilation will be shovelled into
861+
def compile_multishot_async(labscript_file, run_files,
862+
stream_port=None, done_callback=None):
863+
"""Compiles labscript_file with multiple run_files (ie globals).
864+
865+
This function is designed to be called in a thread.
866+
The stdout and stderr from the compilation will be shovelled into
861867
stream_port via zmq push as it spews forth, and when each compilation is complete,
862868
done_callback will be called with a boolean argument indicating success. Compilation
863869
will stop after the first failure. If you want to receive the data on a zmq socket,
864870
do so using a PULL socket created from a labscript_utils.ls_zprocess.Context, or
865871
using a labscript_utils.ls_zprocess.ZMQServer. These subclasses will also be
866872
configured with the appropriate security settings and will be able to receive the
867-
messages."""
873+
messages.
874+
875+
Args:
876+
labscript_file (str): Path to labscript file to be compiled
877+
run_files (list of str): Paths to h5 file where compilation output is stored.
878+
These files must already exist with proper globals initialization.
879+
stream_port (zmq.socket, optional): ZMQ socket to push stdout and stderr.
880+
If None, defaults to calling process stdout/stderr. Default is None.
881+
done_callback (function, optional): Callback function run when compilation finishes.
882+
Takes a single boolean argument marking compilation success or failure.
883+
If None, callback is skipped. Default is None.
884+
"""
868885
compiler_path = os.path.join(os.path.dirname(__file__), 'batch_compiler.py')
869886
to_child, from_child, child = process_tree.subprocess(
870887
compiler_path, output_redirection_port=stream_port
@@ -876,7 +893,8 @@ def compile_multishot_async(labscript_file, run_files, stream_port, done_callbac
876893
signal, data = from_child.get()
877894
if signal == 'done':
878895
success = data
879-
done_callback(data)
896+
if done_callback is not None:
897+
done_callback(data)
880898
break
881899
if not success:
882900
break
@@ -890,16 +908,28 @@ def compile_multishot_async(labscript_file, run_files, stream_port, done_callbac
890908
child.communicate()
891909

892910

893-
def compile_labscript_with_globals_files_async(labscript_file, globals_files, output_path, stream_port, done_callback):
894-
"""Same as compile_labscript_with_globals_files, except it launches a thread to do
895-
the work and does not return anything. Instead, stderr and stdout will be put to
911+
def compile_labscript_with_globals_files_async(labscript_file, globals_files, output_path,
912+
stream_port, done_callback):
913+
"""Compiles labscript_file with multiple globals files into a directory.
914+
915+
Instead, stderr and stdout will be put to
896916
stream_port via zmq push in the multipart message format ['stdout','hello, world\n']
897917
etc. When compilation is finished, the function done_callback will be called a
898918
boolean argument indicating success or failure. If you want to receive the data on
899919
a zmq socket, do so using a PULL socket created from a
900920
labscript_utils.ls_zprocess.Context, or using a
901921
labscript_utils.ls_zprocess.ZMQServer. These subclasses will also be configured with
902-
the appropriate security settings and will be able to receive the messages."""
922+
the appropriate security settings and will be able to receive the messages.
923+
924+
Args:
925+
labscript_file (str): Path to labscript file to be compiled
926+
globals_files (list of str): Paths to h5 file where globals values to be used are stored.
927+
See :func:`make_run_file_from_globals_files` for details.
928+
output_path (str): Folder to save compiled h5 files to.
929+
stream_port (zmq.socket): ZMQ socket to push stdout and stderr.
930+
done_callback (function): Callback function run when compilation finishes.
931+
Takes a single boolean argument marking compilation success or failure.
932+
"""
903933
try:
904934
make_run_file_from_globals_files(labscript_file, globals_files, output_path)
905935
thread = threading.Thread(

0 commit comments

Comments
 (0)