Skip to content

Commit 9cca278

Browse files
authored
Merge pull request #57 from vsoch/add/client
Add/client
2 parents 1377f6b + d534b95 commit 9cca278

25 files changed

+2800
-122
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
include README.md
22
include LICENSE
3+
recursive-include singularity/hub *
34
recursive-include singularity/templates *
45
recursive-include singularity/static *
56
recursive-include singularity/build *
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from glob import glob
2+
3+
from singularity.reproduce import (
4+
assess_replication,
5+
assess_differences
6+
)
7+
8+
image_files=glob('*.img')
9+
10+
# ASSESS REPLICATION #######################################
11+
# returns booleans for if the hashes for levels are the same
12+
13+
assess_replication(image_files[0],image_files[1])
14+
15+
{'BASE': False,
16+
'ENVIRONMENT': False,
17+
'IDENTICAL': False,
18+
'LABELS': True,
19+
'RECIPE': False,
20+
'REPLICATE': False,
21+
'RUNSCRIPT': False}
22+
23+
assess_replication(image_files[0],image_files[0])
24+
25+
{'BASE': True,
26+
'ENVIRONMENT': True,
27+
'IDENTICAL': True,
28+
'LABELS': True,
29+
'RECIPE': True,
30+
'REPLICATE': True,
31+
'RUNSCRIPT': True}
32+
33+
# ASSESS DIFFERENCES #######################################
34+
# returns dictionary with
35+
36+
report = assess_differences(image_files[0],image_files[1])
37+
38+
report.keys()
39+
# dict_keys(['different', 'missing', 'same'])
40+
41+
# These files are equivalent between the images
42+
print(len(report['same']))
43+
5663
44+
45+
# These files are present in both, but different
46+
print(report['different'])
47+
['./etc/hosts',
48+
'./.exec',
49+
'./environment',
50+
'./etc/mtab',
51+
'./etc/resolv.conf',
52+
'./.run',
53+
'./.shell',
54+
'./singularity']
55+
56+
# These files are found in the first image, but not the second
57+
print(report['missing'])
58+
['./var/lib/apt/lists/.wh.archive.ubuntu.com_ubuntu_dists_xenial-updates_main_i18n_Translation-en',
59+
'./bin/gunzip']
60+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from glob import glob
2+
3+
from singularity.reproduce import (
4+
get_image_hash,
5+
get_levels,
6+
get_content_hashes,
7+
get_image_file_hash
8+
)
9+
10+
image_files = glob("*.img")
11+
image_path = image_files[0]
12+
13+
########################################################
14+
# Get Image Hash (Single Container --> Single Value)
15+
########################################################
16+
17+
# This function will return a hash specific to a level of
18+
# reproducibility, meaning a subset of files within the
19+
# image. You can see the levels for selection:
20+
21+
levels = get_levels()
22+
23+
levels.keys()
24+
# dict_keys(['IDENTICAL', 'BASE', 'REPLICATE', 'RECIPE', 'RUNSCRIPT', 'ENVIRONMENT', 'LABELS'])
25+
26+
# or specify a different version:
27+
28+
levels = get_levels(version=2.3)
29+
30+
# We can, then generate an image hash, and by default the level "REPLICATION" will be used:
31+
32+
get_image_hash(image_path)
33+
#'bf8e242931e25ae9496015868ab2e8cc8d156ffd'
34+
35+
# But we can also specify a level that we want:
36+
get_image_hash(image_path,level="IDENTICAL")
37+
#'3f4dcf64b58d314ac9ef0c02f641cd2109a07d64'
38+
39+
########################################################
40+
# Get Image Content Digest (One Container --> Multiple)
41+
########################################################
42+
43+
44+
# We might want to get a dictionary of content hashes for all files
45+
# of one container at one level!
46+
digest = get_content_hashes(image_path)
47+
digest['./usr/bin/pinky']
48+
# 'ee2b438c278011bdac1a3a927e2d37519a8ed9c7'
49+
50+
# We can also get a hash of the entire image file, this is done on the
51+
# binary file and not contents inside.
52+
53+
file_hash = get_image_file_hash(image_path)
54+
# e'13775a83962ae60744d691eb7f7fd1e96599e656'

examples/run_singularity/singularity_client.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,43 @@
44

55
from singularity.cli import Singularity
66

7-
# The default will ask for your sudo password, and then not ask again to
8-
# run commands. It is not stored anywhere, however you should not save / pickle
9-
# the object as it will expose your password.
7+
# Create a client
108
S = Singularity()
119

1210
# Get general help:
1311
S.help()
1412

1513
# These are the defaults, which can be specified
16-
S = Singularity(sudo=True,verbose=False)
17-
18-
# Let's define a path to an image
19-
# wget http://www.vbmis.com/bmi/project/singularity/package_image/ubuntu:latest-2016-04-06.img
20-
image_path = 'ubuntu:latest-2016-04-06.img'
21-
22-
# Run singularity --exec
23-
S.execute(image_path=image_path,command='ls')
24-
# $'docker2singularity.sh\nget_docker_container_id.sh\nget_docker_meta.py\nmakeBases.py\nsingularity\nubuntu:latest-2016-04-06.img\n'
25-
# These are the defaults, which can be specified
14+
S = Singularity(sudo=False,sudopw=None,debug=False)
15+
16+
# Create an image
17+
image = S.create('myimage.img')
18+
19+
# Import into it
20+
S.importcmd(image,'docker://ubuntu:latest')
21+
22+
# Execute command to container
23+
result = S.execute(image,command='cat /singularity')
24+
print(result)
25+
'''
26+
#!/bin/sh
27+
28+
if test -x /bin/bash; then
29+
exec /bin/bash "$@"
30+
elif test -x /bin/sh; then
31+
exec /bin/sh "$@"
32+
else
33+
echo "ERROR: No valid shell within container"
34+
exit 255
35+
fi
36+
'''
2637

2738
# For any function you can get the docs:
2839
S.help(command="exec")
2940

30-
# or return as string
31-
help = S.help(command="exec",stdout=False)
32-
33-
# export an image, default export_type="tar" , pipe=False , output_file = None will produce file in tmp
34-
tmptar = S.export(image_path=image_path)
35-
36-
# create an empty image
37-
S.create(image_path='test.img')
41+
# export an image as a byte array
42+
byte_array = S.export(image,pipe=True)
3843

39-
# import a docker image
40-
S.importcmd(image_path,input_source='docker://ubuntu:latest')
44+
# Get an in memory tar
45+
from singularity.reproduce import get_memory_tar
46+
tar = get_memory_tar(image)

0 commit comments

Comments
 (0)