@@ -126,14 +126,17 @@ def execute(self,image_path,command,writable=False,contain=False):
126126 if writable == True :
127127 sudo = True
128128
129- cmd = cmd + [image_path ,command ]
129+ if not isinstance (list ,command ):
130+ command = command .split (' ' )
131+
132+ cmd = cmd + [image_path ] + command
130133
131134 # Run the command
132135 return self .run_command (cmd ,sudo = sudo )
133136
134137
135138
136- def export (self ,image_path ,pipe = False ,output_file = None ,command = None , export_format = "tar" ):
139+ def export (self ,image_path ,pipe = False ,output_file = None ,export_format = "tar" ):
137140 '''export will export an image, sudo must be used.
138141 :param image_path: full path to image
139142 :param pipe: export to pipe and not file (default, False)
@@ -184,8 +187,10 @@ def importcmd(self,image_path,input_source,import_type=None,command=None):
184187 sudo = True
185188 if import_type == "tar" :
186189 cmd = ['singularity' ,'import' ,'--file' ,input_source ]
187- if command != None :
188- cmd = cmd + ["--command" ,command ]
190+ if command is not None :
191+ if not isinstance (list ,command ):
192+ command = command .split (' ' )
193+ cmd = cmd + ["--command" ] + command
189194 cmd .append (image_path )
190195 return self .run_command (cmd ,sudo = sudo )
191196 else :
@@ -199,7 +204,7 @@ def pull(self,image_path):
199204 '''pull will pull a singularity hub image
200205 :param image_path: full path to image
201206 '''
202- if not image_path .startswit ('shub://' ):
207+ if not image_path .startswith ('shub://' ):
203208 bot .logger .error ("pull is only valid for the shub://uri, %s is invalid." ,image_name )
204209 sys .exit (1 )
205210
@@ -211,44 +216,57 @@ def pull(self,image_path):
211216
212217
213218
214- def run (self ,image_path ,command ,writable = False ,contain = False ):
215- '''run will run a command inside the container, probably not intended for within python
219+ def run (self ,image_path ,args = None ,writable = False ,contain = False ):
220+ '''run will run the container, with or withour arguments (which
221+ should be provided in a list)
216222 :param image_path: full path to singularity image
217- :param command: command to send to container
223+ :param args: args to include with the run
218224 '''
219225 sudo = False
220226 cmd = ["singularity" ,"run" ]
221227 cmd = self .add_flags (cmd ,writable = writable ,contain = contain )
228+ cmd = cmd + [image_path ]
222229
223230 # Conditions for needing sudo
224231 if writable == True :
225232 sudo = True
226-
227- cmd = cmd + [image_path ,command ]
228-
229- # Run the command
230- return self .run_command (cmd ,sudo = sudo )
231-
232-
233- def start (self ,image_path ,writable = False ,contain = False ):
234- '''start will start a container
233+
234+ if args is not None :
235+ if not isinstance (list ,args ):
236+ args = command .split (' ' )
237+ cmd = cmd + args
238+
239+ result = self .run_command (cmd ,sudo = sudo )
240+ if isinstance (result ,bytes ):
241+ result = result .decode ('utf-8' )
242+ result = result .strip ('\n ' )
243+ try :
244+ result = json .loads (result )
245+ except :
246+ pass
247+ return result
248+
249+
250+ def get_labels (self ,image_path ):
251+ '''get_labels will return all labels defined in the image
235252 '''
236- sudo = False
237- cmd = ['singularity' ,'start' ]
238- cmd = self .add_flags (cmd ,writable = writable ,contain = contain )
239- if writable == True :
240- sudo = True
241-
242- cmd .append (image_path )
243- return self .run_command (cmd ,sudo = sudo )
244-
245-
246- def stop (self ,image_path ):
247- '''stop will stop a container
253+ cmd = ['singularity' ,'exec' ,image_path ,'cat' ,'/.singularity/labels.json' ]
254+ labels = self .run_command (cmd )
255+ return json .loads (labels .decode ('utf-8' ))
256+
257+
258+ def get_args (self ,image_path ):
259+ '''get_args will return the subset of labels intended to be arguments
260+ (in format SINGULARITY_RUNSCRIPT_ARG_*
248261 '''
249- cmd = ['singularity' ,'stop' ,image_path ]
250- return self .run_command (cmd )
251-
262+ labels = self .get_labels (image_path )
263+ args = dict ()
264+ for label ,values in labels .items ():
265+ if re .search ("^SINGULARITY_RUNSCRIPT_ARG" ,label ):
266+ vartype = label .split ('_' )[- 1 ].lower ()
267+ if vartype in ["str" ,"float" ,"int" ,"bool" ]:
268+ args [vartype ] = values .split (',' )
269+ return args
252270
253271
254272 def add_flags (self ,cmd ,writable ,contain ):
@@ -267,9 +285,11 @@ def add_flags(self,cmd,writable,contain):
267285 return cmd
268286
269287
270- ######################################################################################################
288+
289+
290+ #################################################################################
271291# HELPER FUNCTIONS
272- ######################################################################################################
292+ #################################################################################
273293
274294def get_image (image ,return_existed = False ,sudopw = None ,size = None ,debug = False ):
275295 '''get_image will return the file, if it exists, or if it's docker or
0 commit comments