21
21
from spython .utils import stream_command
22
22
import os
23
23
import re
24
+ import shutil
24
25
import sys
26
+ import tempfile
25
27
26
28
def pull (self ,
27
29
image = None ,
@@ -30,6 +32,8 @@ def pull(self,
30
32
ext = "simg" ,
31
33
force = False ,
32
34
capture = False ,
35
+ name_by_commit = False ,
36
+ name_by_hash = False ,
33
37
stream = False ):
34
38
35
39
'''pull will pull a singularity hub or Docker image
@@ -68,20 +72,55 @@ def pull(self,
68
72
self .setenv ('SINGULARITY_PULLFOLDER' , pull_folder )
69
73
70
74
# 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' )
73
82
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 ]
75
89
76
90
if force is True :
77
91
cmd = cmd + ["--force" ]
78
92
79
93
cmd .append (image )
80
94
bot .info (' ' .join (cmd ))
81
95
96
+ # If name is still None, make empty string
97
+ if name is None :
98
+ name = ''
99
+
82
100
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 )
84
108
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
85
124
else :
86
125
return final_image , stream_command (cmd , sudo = False )
87
126
0 commit comments