3636
3737from singularity .cli import Singularity
3838from singularity .reproduce import (
39- get_image_hash ,
39+ get_image_file_hash ,
4040 get_image_hashes ,
4141 get_memory_tar ,
4242 extract_content
@@ -75,11 +75,11 @@ def estimate_image_size(spec_file,sudopw=None,padding=None):
7575 bot .logger .debug ("Original image size calculated as %s" ,original_size )
7676 padded_size = original_size + padding
7777 bot .logger .debug ("Size with padding will be %s" ,padded_size )
78- shutil .rmtree (image_folder )
7978 return padded_size
8079
8180
82- def build_from_spec (spec_file = None ,build_dir = None ,size = None ,sudopw = None ,build_folder = False ,debug = False ):
81+ def build_from_spec (spec_file = None ,build_dir = None ,size = None ,sudopw = None ,
82+ build_folder = False ,debug = False ):
8383 '''build_from_spec will build a "spec" file in a "build_dir" and return the directory
8484 :param spec_file: the spec file, called "Singuarity"
8585 :param sudopw: the sudopw for Singularity, root should provide ''
@@ -120,14 +120,14 @@ def build_from_spec(spec_file=None,build_dir=None,size=None,sudopw=None,build_fo
120120 bot .logger .debug ("build_folder is true, creating %s" ,image_path )
121121 os .mkdir (image_path )
122122 else :
123- cli .create (image_path ,size = size )
123+ cli .create (image_path ,size = size , sudo = True )
124124
125125 result = cli .bootstrap (image_path = image_path ,spec_path = spec_path )
126126 print (result )
127127
128128 # If image, rename based on hash
129129 if build_folder == False :
130- version = get_image_hash (image_path )
130+ version = get_image_file_hash (image_path )
131131 final_path = "%s/%s" % (build_dir ,version )
132132 os .rename (image_path ,final_path )
133133 image_path = final_path
@@ -137,7 +137,8 @@ def build_from_spec(spec_file=None,build_dir=None,size=None,sudopw=None,build_fo
137137
138138
139139def package (image_path ,spec_path = None ,output_folder = None ,runscript = True ,
140- software = True ,remove_image = False ,verbose = False ,S = None ,sudopw = None ):
140+ software = True ,remove_image = False ,verbose = False ,S = None ,sudopw = None ,
141+ old_version = False ):
141142 '''package will take an image and generate a zip (including the image
142143 to a user specified output_folder.
143144 :param image_path: full path to singularity image file
@@ -154,7 +155,15 @@ def package(image_path,spec_path=None,output_folder=None,runscript=True,
154155 else :
155156 S = Singularity (debug = verbose ) # This command will ask the user for sudo
156157
157- file_obj ,tar = get_memory_tar (image_path )
158+ # Singularity < version 2.3
159+ if old_version == False :
160+ file_obj ,tar = get_memory_tar (image_path )
161+
162+ # Singularity 2.3 and up
163+ else :
164+ tmptar = S .export (image_path = image_path ,old_version = True )
165+ tar = tarfile .open (tmptar )
166+
158167 members = tar .getmembers ()
159168 image_name = os .path .basename (image_path )
160169 zip_name = "%s.zip" % (image_name .replace (" " ,"_" ))
@@ -171,12 +180,15 @@ def package(image_path,spec_path=None,output_folder=None,runscript=True,
171180 to_package ['Singularity' ] = singularity_spec
172181
173182 # Package the image with an sha1, identical standard, as VERSION
174- hashes = get_image_hashes (image_path )
175- to_package ["VERSION" ] = hashes ['REPLICATE' ]
176- to_package ["HASHES" ] = hashes
183+ if old_version == False :
184+ hashes = get_image_hashes (image_path )
185+ to_package ["HASHES" ] = hashes
186+ to_package ["VERSION" ] = hashes ['IDENTICAL' ]
187+ else :
188+ to_package ["VERSION" ] = get_image_file_hash (image_path )
189+
177190
178191 # Look for runscript
179-
180192 if runscript is True :
181193 try :
182194 to_package ["runscript" ] = extract_content (image_path ,'./singularity' ,cli = S )
@@ -194,7 +206,10 @@ def package(image_path,spec_path=None,output_folder=None,runscript=True,
194206 # Do zip up here - let's start with basic structures
195207 zipfile = zip_up (to_package ,zip_name = zip_name ,output_folder = output_folder )
196208 bot .logger .debug ("Package created at %s" % (zipfile ))
197- file_obj .close ()
209+
210+ if old_version == False :
211+ file_obj .close ()
212+
198213 # return package to user
199214 return zipfile
200215
0 commit comments