Skip to content

Commit 5932f0a

Browse files
Include docker image sha in hash (openproblems-bio#505)
* include docker image sha in hash * bugfixes
1 parent ab3a7eb commit 5932f0a

1 file changed

Lines changed: 22 additions & 14 deletions

File tree

openproblems/api/hash.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from ..tools.utils import check_version
21
from . import utils
32

43
import hashlib
@@ -31,6 +30,21 @@ def git_hash(file):
3130
return p.stdout.decode()
3231

3332

33+
def docker_hash(image_name):
34+
"""Get the docker image hash associated with an image."""
35+
subprocess.run(["docker", "pull", "-q", image_name])
36+
p = subprocess.run(
37+
["docker", "inspect", "--format='{{index .RepoDigests 0}}'", image_name],
38+
stdout=subprocess.PIPE,
39+
stderr=subprocess.PIPE,
40+
cwd=os.path.dirname(__file__),
41+
)
42+
if p.returncode != 0:
43+
raise RuntimeError(p.stderr.decode())
44+
else:
45+
return p.stdout.decode()
46+
47+
3448
def get_context(obj, context=None):
3549
"""Return a dictionary describing the context of an object.
3650
@@ -51,25 +65,19 @@ def get_context(obj, context=None):
5165
if isinstance(obj, _MODULE):
5266
module_name = obj.__name__
5367
else:
54-
if isinstance(obj, scprep.run.RFunction):
55-
try:
68+
if isinstance(obj, scprep.run.RFunction) and hasattr(obj, "__r_file__"):
69+
if obj.__r_file__ not in context:
5670
context[obj.__r_file__] = git_hash(obj.__r_file__)
57-
except AttributeError:
58-
pass
71+
if hasattr(obj, "metadata") and "image" in obj.metadata:
72+
image_name = f"singlecellopenproblems/{obj.metadata['image']}"
73+
if image_name not in context:
74+
context[image_name] = docker_hash(image_name)
5975
try:
6076
module_name = get_module(obj)
6177
except AttributeError:
6278
# doesn't belong to a module
6379
return context
64-
if module_name in context:
65-
# already done
66-
pass
67-
elif not module_name.startswith("openproblems"):
68-
# use version number of external libs
69-
version = check_version(module_name.split(".")[0])
70-
if version != "ModuleNotFound":
71-
context[module_name] = version
72-
else:
80+
if module_name.startswith("openproblems") and module_name not in context:
7381
module = importlib.import_module(module_name)
7482
context[module_name] = git_hash(module.__file__)
7583
for member_name in dir(module):

0 commit comments

Comments
 (0)