55from pathlib import Path
66from subprocess import check_output
77from labscript_profile import LABSCRIPT_SUITE_PROFILE , default_labconfig_path
8+ import argparse
89
910_here = os .path .dirname (os .path .abspath (__file__ ))
1011DEFAULT_PROFILE_CONTENTS = os .path .join (_here , 'default_profile' )
@@ -21,7 +22,15 @@ def make_shared_secret(directory):
2122 raise RuntimeError ("Could not parse output of zprocess.makesecret" )
2223
2324
24- def make_labconfig_file ():
25+ def make_labconfig_file (apparatus_name = None ):
26+ """Create labconfig file from template
27+
28+ Parameters
29+ ----------
30+ apparatus_name: str, optional
31+ Overrides the default apparatus name with the provided one if not None
32+ """
33+
2534 source_path = os .path .join (LABSCRIPT_SUITE_PROFILE , 'labconfig' , 'example.ini' )
2635 target_path = default_labconfig_path ()
2736 if os .path .exists (target_path ):
@@ -47,16 +56,88 @@ def make_labconfig_file():
4756 '%(labscript_suite)s' , shared_secret .relative_to (LABSCRIPT_SUITE_PROFILE )
4857 )
4958 config .set ('security' , 'shared_secret' , str (shared_secret_entry ))
59+ if apparatus_name is not None :
60+ print (f'\t Setting apparatus name to \' { apparatus_name } \' ' )
61+ config .set ('DEFAULT' , 'apparatus_name' , apparatus_name )
5062
5163 with open (target_path , 'w' ) as f :
5264 config .write (f )
5365
66+ def compile_connection_table ():
67+ """Compile the connection table defined in the labconfig file
68+
69+ The output is placed in the location defined by the labconfig file.
70+ """
71+
72+ try :
73+ import runmanager
74+ except ImportError :
75+ # if runmanager doesn't import, skip compilation
76+ return
77+
78+ config = configparser .ConfigParser (defaults = {'labscript_suite' : str (LABSCRIPT_SUITE_PROFILE )})
79+ config .read (default_labconfig_path ())
80+
81+ # The path to the user's connection_table.py script
82+ script_path = os .path .expandvars (config ['paths' ]['connection_table_py' ])
83+ # path to the connection_table.h5 destination
84+ output_h5_path = os .path .expandvars (config ['paths' ]['connection_table_h5' ])
85+ # create output directory, if needed
86+ Path (output_h5_path ).parent .mkdir (parents = True , exist_ok = True )
87+ # compile the h5 file
88+ runmanager .new_globals_file (output_h5_path )
89+
90+ def dummy_callback (success ):
91+ pass
92+
93+ runmanager .compile_labscript_async (labscript_file = script_path ,
94+ run_file = output_h5_path ,
95+ stream_port = None ,
96+ done_callback = dummy_callback )
97+ print (f'\t Output written to { output_h5_path } ' )
98+
99+ def create_profile_cli ():
100+ """Function that defines the labscript-profile-create command
101+
102+ Parses CLI arguments and calls :func:`~.create_profile`.
103+ """
104+
105+ # capture CMD arguments
106+ parser = argparse .ArgumentParser (prog = 'labscript-profile-create' ,
107+ description = 'Initialises a default labscript profile'
108+ )
109+
110+ parser .add_argument ('-n' , '--apparatus_name' ,
111+ type = str ,
112+ help = 'Sets the apparatus_name in the labconfig file. Defaults to example_apparatus' ,
113+ )
114+ parser .add_argument ('-c' , '--compile' ,
115+ action = 'store_true' ,
116+ help = 'Enables compilation of the default example connection table' ,
117+ default = False )
118+
119+ args = parser .parse_args ()
120+
121+ create_profile (args .apparatus_name , args .compile )
122+
123+ def create_profile (apparatus_name = None , compile_table = False ):
124+ """Function that creates a labscript config profile from the default config
125+
126+ Parameters
127+ ----------
128+ appratus_name: str, optional
129+ apparatus_name to define in the config.
130+ If None, defaults to example_apparatus (set in default config file)
131+ compile_table: bool, optional
132+ Whether to compile to example connection table defined by the default config file
133+ Default is False.
134+ """
54135
55- def create_profile ():
56136 src = Path (DEFAULT_PROFILE_CONTENTS )
57137 dest = Path (LABSCRIPT_SUITE_PROFILE )
138+ print (f'Creating labscript profile at { LABSCRIPT_SUITE_PROFILE } ' )
58139 # Profile directory may exist already, but we will error if it contains any of the
59- # files or directories we want to copy into it:
140+ # sub- directories we want to copy into it:
60141 os .makedirs (dest , exist_ok = True )
61142 # Preferable to raise errors if anything exists before copying anything, rather than
62143 # do a partial copy before hitting an error:
@@ -71,4 +152,16 @@ def create_profile():
71152 else :
72153 shutil .copy2 (src_file , dest_file )
73154
74- make_labconfig_file ()
155+ print ('Writing labconfig file' )
156+ make_labconfig_file (apparatus_name )
157+
158+ # rename apparatus directories
159+ if apparatus_name is not None :
160+ print ('\t Renaming apparatus directories' )
161+ for path in dest .glob ('**/example_apparatus/' ):
162+ new_path = Path (str (path ).replace ('example_apparatus' , apparatus_name ))
163+ path .rename (new_path )
164+
165+ if compile_table :
166+ print ('Compiling the example connection table' )
167+ compile_connection_table ()
0 commit comments