2121from spython .utils import stream_command
2222import os
2323import re
24+ import shutil
2425import sys
26+ import tempfile
2527
2628def pull (self ,
2729 image = None ,
@@ -30,6 +32,8 @@ def pull(self,
3032 ext = "simg" ,
3133 force = False ,
3234 capture = False ,
35+ name_by_commit = False ,
36+ name_by_hash = False ,
3337 stream = False ):
3438
3539 '''pull will pull a singularity hub or Docker image
@@ -68,20 +72,55 @@ def pull(self,
6872 self .setenv ('SINGULARITY_PULLFOLDER' , pull_folder )
6973
7074 # If we still don't have a custom name, base off of image uri.
71- if name is None :
72- name = self ._get_filename (image , ext )
75+ # Determine how to tell client to name the image, preference to hash
76+
77+ if name_by_hash is True :
78+ cmd .append ('--hash' )
79+
80+ elif name_by_commit is True :
81+ cmd .append ('--commit' )
7382
74- cmd = cmd + ["--name" , name ]
83+ elif name is None :
84+ name = self ._get_filename (image , ext )
85+
86+ # Only add name if we aren't naming by hash or commit
87+ if not name_by_commit and not name_by_hash :
88+ cmd = cmd + ["--name" , name ]
7589
7690 if force is True :
7791 cmd = cmd + ["--force" ]
7892
7993 cmd .append (image )
8094 bot .info (' ' .join (cmd ))
8195
96+ # If name is still None, make empty string
97+ if name is None :
98+ name = ''
99+
82100 final_image = os .path .join (pull_folder , name )
83- if stream is False :
101+
102+ # Option 1: For hash or commit, need return value to get final_image
103+ if name_by_commit or name_by_hash :
104+
105+ # Set pull to temporary location
106+ tmp_folder = tempfile .mkdtemp ()
107+ self .setenv ('SINGULARITY_PULLFOLDER' , tmp_folder )
84108 self ._run_command (cmd , capture = capture )
109+
110+ try :
111+ tmp_image = os .path .join (tmp_folder , os .listdir (tmp_folder )[0 ])
112+ final_image = os .path .join (pull_folder , os .path .basename (tmp_image ))
113+ shutil .move (tmp_image , final_image )
114+ shutil .rmtree (tmp_folder )
115+
116+ except :
117+ bot .error ('Issue pulling image with commit or hash, try without?' )
118+
119+ # Option 2: Streaming we just run to show user
120+ elif stream is False :
121+ self ._run_command (cmd , capture = capture )
122+
123+ # Option 3: A custom name we can predict (not commit/hash) and can also show
85124 else :
86125 return final_image , stream_command (cmd , sudo = False )
87126
0 commit comments