|
| 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 | +# '13775a83962ae60744d691eb7f7fd1e96599e656' |
| 55 | + |
0 commit comments