1313 api_post
1414)
1515
16+ import os
17+ import sys
18+ from singularity .logman import bot
19+
1620api_base = "https://singularity-hub.org/api"
1721
1822def get_template (container_name ,get_type = None ):
@@ -36,6 +40,56 @@ def get_template(container_name,get_type=None):
3640 if image ['repo_tag' ] is not None and get_type is not "collection" :
3741 url = "%s:%s" % (url ,image ['repo_tag' ])
3842
39- result = api_get (url )
43+ result = api_get (url )
4044
4145 return result
46+
47+
48+ def download_image (manifest ,download_folder = None ,extract = True ,name = None ):
49+ '''download_image will download a singularity image from singularity
50+ hub to a download_folder, named based on the image version (commit id)
51+ :param manifest: the manifest obtained with get_manifest
52+ :param download_folder: the folder to download to, if None, will be pwd
53+ :param extract: if True, will extract image to .img and return that.
54+ :param name: if defined, use custom set image name instead of default
55+ '''
56+ if name is not None :
57+ image_file = name
58+ else :
59+ image_file = get_image_name (manifest )
60+
61+ print ("Found image %s:%s" % (manifest ['name' ],manifest ['branch' ]))
62+ print ("Downloading image... %s" % (image_file ))
63+
64+ if download_folder != None :
65+ image_file = "%s/%s" % (download_folder ,image_file )
66+ url = manifest ['image' ]
67+ image_file = api_get (url ,stream_to = image_file )
68+ if extract == True :
69+ print ("Decompressing %s" % image_file )
70+ os .system ('gzip -d -f %s' % (image_file ))
71+ image_file = image_file .replace ('.gz' ,'' )
72+ return image_file
73+
74+
75+ # Various Helpers ---------------------------------------------------------------------------------
76+ def get_image_name (manifest ,extension = 'img.gz' ,use_hash = False ):
77+ '''get_image_name will return the image name for a manifest
78+ :param manifest: the image manifest with 'image' as key with download link
79+ :param use_hash: use the image hash instead of name
80+ '''
81+ if not use_hash :
82+ image_name = "%s-%s.%s" % (manifest ['name' ].replace ('/' ,'-' ),
83+ manifest ['branch' ].replace ('/' ,'-' ),
84+ extension )
85+ else :
86+ image_url = os .path .basename (unquote (manifest ['image' ]))
87+ image_name = re .findall (".+[.]%s" % (extension ),image_url )
88+ if len (image_name ) > 0 :
89+ image_name = image_name [0 ]
90+ else :
91+ bot .logger .error ("Singularity Hub Image not found with expected extension %s, exiting." ,extension )
92+ sys .exit (1 )
93+
94+ bot .logger .info ("Singularity Hub Image: %s" , image_name )
95+ return image_name
0 commit comments