1616import os
1717
1818
19- def build_from_spec (spec = None ,name = None ,build_dir = None ,size = None ,sudopw = None ,output_folder = None ):
19+ def build_from_spec (spec = None ,build_dir = None ,size = None ,sudopw = None ,
20+ output_folder = None ,build_folder = False ):
2021 '''build_from_spec will build a "spec" file in a "build_dir" and return the directory
2122 :param spec: the spec file, called "Singuarity"
22- :parma name: the name to call the image, will go under a collection for some library/name
2323 :param build_dir: the directory to build in. If not defined, will use tmpdir.
2424 :param size: the size of the image
2525 :param output_folder: where to output the image package
26+ :param build_folder: "build" the image into a folder instead. Default False
2627 '''
2728 if spec == None :
2829 spec = "Singularity"
2930 if build_dir == None :
3031 build_dir = tempfile .mkdtemp ()
3132 # Copy the spec to a temporary directory
3233 spec_path = "%s/%s" % (build_dir ,spec )
33- shutil .copyfile (spec ,spec_path )
34+ if not os .path .exists (spec_path ):
35+ shutil .copyfile (spec ,spec_path )
3436 # If name isn't provided, call it Singularity
35- if name == None :
36- name = "Singularity"
37- image_path = "%s/%s.img" % (build_dir ,name )
37+ image_path = "%s/image" % (build_dir )
3838 # Run create image and bootstrap with Singularity command line tool.
3939 if sudopw != None :
4040 cli = Singularity (sudopw = sudopw )
4141 else :
4242 cli = Singularity () # This command will ask the user for sudo
4343 print ("\n Creating and boostrapping image..." )
44- cli .create (image_path ,size = size )
44+ # Does the user want to "build" into a folder or image?
45+ if build_folder == True :
46+ os .mkdir (image_path )
47+ else :
48+ cli .create (image_path ,size = size )
4549 result = cli .bootstrap (image_path = image_path ,spec_path = spec_path )
4650 print (result )
47- # Finally, package the image.
48- print ("\n Packing image..." )
49- zipfile = package (image_path = image_path ,
50- name = name ,
51- output_folder = output_folder ,
52- spec_path = spec_path ,
53- verbose = True ,
54- S = cli )
55- return zipfile
51+ # Rename the image
52+ version = get_image_hash (image_path )
53+ final_path = "%s/%s" % (build_dir ,version )
54+ os .rename (image_path ,final_path )
55+ return final_path
5656
5757
58- def package (image_path ,name = None , spec_path = None ,output_folder = None ,runscript = True ,
59- software = True ,remove_image = False ,verbose = False ,S = None ):
58+ def package (image_path ,spec_path = None ,output_folder = None ,runscript = True ,
59+ software = True ,remove_image = False ,verbose = False ,S = None , sudopw = None ):
6060 '''package will take an image and generate a zip (including the image
6161 to a user specified output_folder.
6262 :param image_path: full path to singularity image file
63- :param name: the name for the image to be included with the collection
6463 :param runscript: if True, will extract runscript to include in package as runscript
6564 :param software: if True, will extract files.txt and folders.txt to package
6665 :param remove_image: if True, will not include original image in package (default,False)
6766 :param verbose: be verbose when using singularity --export (default,False)
6867 :param S: the Singularity object (optional) will be created if not required.
6968 '''
69+ # Run create image and bootstrap with Singularity command line tool.
7070 if S == None :
71- S = Singularity (verbose = verbose )
71+ if sudopw != None :
72+ S = Singularity (sudopw = sudopw ,verbose = verbose )
73+ else :
74+ S = Singularity (verbose = verbose ) # This command will ask the user for sudo
7275 tmptar = S .export (image_path = image_path ,pipe = False )
7376 tar = tarfile .open (tmptar )
7477 members = tar .getmembers ()
7578 image_name = os .path .basename (image_path )
7679 zip_name = "%s.zip" % (image_name .replace (" " ,"_" ))
77-
7880 # Include the image in the package?
7981 if remove_image :
8082 to_package = dict ()
8183 else :
8284 to_package = {"files" :[image_path ]}
83- if name != None :
84- image_name = name
85- to_package ['NAME' ] = format_container_name (image_name )
86-
8785 # If the specfile is provided, it should also be packaged
8886 if spec_path != None :
8987 singularity_spec = "" .join (read_file (spec_path ))
9088 to_package ['Singularity' ] = singularity_spec
91-
9289 # Package the image with an md5 sum as VERSION
9390 version = get_image_hash (image_path )
9491 to_package ["VERSION" ] = version
95-
9692 # Look for runscript
9793 if runscript == True :
9894 try :
@@ -101,21 +97,17 @@ def package(image_path,name=None,spec_path=None,output_folder=None,runscript=Tru
10197 runscript = runscript_file .read ()
10298 to_package ["runscript" ] = runscript
10399 print ("Found runscript!" )
104-
105100 except KeyError :
106101 print ("No runscript found in image!" )
107-
108102 if software == True :
109103 print ("Adding software list to package!" )
110104 files = [x .path for x in members if x .isfile ()]
111105 folders = [x .path for x in members if x .isdir ()]
112106 to_package ["files.txt" ] = files
113107 to_package ["folders.txt" ] = folders
114-
115108 # Do zip up here - let's start with basic structures
116109 zipfile = zip_up (to_package ,zip_name = zip_name ,output_folder = output_folder )
117110 print ("Package created at %s" % (zipfile ))
118-
119111 # return package to user
120112 return zipfile
121113
0 commit comments