Skip to content

Singularity Hub Client

Vanessa Sochat edited this page May 15, 2017 · 2 revisions

Singularity Hub Client

The main Singularity Hub client is accessible by way of Singularity Python. We can use the client to obtain manifests, and download images with the Singularity Hub API.

from singularity.hub.client import Client
shub = Client() 

Collection Information

container_name = 'vsoch/singularity-hello-world'
collection = shub.get_collection(container_name)

INFO:shub_builder:User: vsoch
INFO:shub_builder:Repo Name: singularity-hello-world
INFO:shub_builder:Repo Tag: latest
DEBUG:shub_builder:GET https://singularity-hub.org/api/collection/vsoch/singularity-hello-world:latest
DEBUG:shub_builder:Headers found: Content-Type
DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): singularity-hub.org
DEBUG:requests.packages.urllib3.connectionpool:https://singularity-hub.org:443 "GET /api/collection/vsoch/singularity-hello-world:latest HTTP/1.1" 200 None

The containers associated with the collection are here:

container_ids = collection['container_set']

and here is a simple loop to show downloading them to a temporary directory, and obtaining the metrics for each

import tempfile
storage = tempfile.mkdtemp()

for container_id in container_ids:
   manifest = shub.get_container(container_id)
   image = shub.pull_container(manifest,
                               download_folder=storage,
                               name="%s.img.gz" %(manifest['version']))       

   metrics = shub.load_metrics(manifest)

Containers

Single Container

To get a single container, you do that by way of a manifest:

from singularity.hub.client import Client
shub = Client()    # Singularity Hub Client
collection_name = 'vsoch/singularity-hello-world'

If you don't know your container ID, you can look at the container's url on Singularity Hub, or you can get a list of all ids via the collection:

collection = shub.get_collection(container_name)
container_ids = collection['container_set']

For example, here is a container id that we are interested in, and then we download it's manifest:

container_id = container_ids.pop()
manifest = shub.get_container(container_id)

Now we pull the image from the manifest:

# Default will download to present working directory, 
image = shub.pull_container(manifest)

# You can also set the download_folder or name, eg:
image = shub.pull_container(manifest,
                            download_folder='/tmp',
                            name='container.img.gz')

All Containers

If you want to get ALL containers in Singularity Hub, you can do that too.

containers = shub.get_containers()

and then you can download them similarity:

for container_name,container in containers.items():
    for branch, manifest in container.items():        
        name = manifest['name'].replace('/','-')
        uncompressed = "%s-%s.img" %(name,branch)
           image = shub.pull_container(manifest,
                                       download_folder=hub,
                                       name="%s-%s.img.gz" %(name,branch))       
Clone this wiki locally