Coverage for src / eo_history / container.py: 33%
26 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-12 02:10 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-12 02:10 +0000
1"""Utility routines for working with singularity containers.
3Some tools to work with singularity containers.
5"""
7import json
8from pathlib import Path
11def is_singularity(): # noqa: ANN201
12 """
13 Determine if we are running in a singularity container or not.
14 """
15 is_singularity = True if Path("/.singularity.d/").exists() else False
16 return is_singularity
19def is_docker(): # noqa: ANN201
20 """
21 Determine if we are running in a docker instance.
22 """
23 with open("/proc/1/cgroup", "rt") as ifh:
24 return "docker" in ifh.read()
27def is_container(): # noqa: ANN201
28 """True if either singularity or docker
30 Returns:
31 boolean -- true if in a container, false otherwise
32 """ # noqa: D415
33 return is_singularity() or is_docker()
36# ww get metadata by looking for a build info
37# we get from the def file otherwise they might
38# not belong to the image
39def get_container_build_info(
40 metadatafile="/etc/rsc_build_info.json",
41): # noqa: ANN001, ANN201
42 """
43 Get the labels from the json string.
45 Fetch the labels from a json string (by default held
46 in the current container) and store as a dictionary.
47 """
48 with open(metadatafile) as f:
49 labels = json.load(f)
50 return labels
53def get_build_singularity_labels(
54 deffile="/.singularity.d/Singularity",
55): # noqa: ANN001, ANN201
56 """
57 Get the labels from the current container that were defined at build time.
59 If we are running in a singularity container, fetch the labels from the build recipe
60 and store as a dictionary.
62 This is for backwards compatiibility. All recent containers should have
63 an /etc/rsc_build_info.json file.
64 """
65 labels = {}
66 with open(deffile) as f:
67 for line in f:
68 if line.startswith("%labels"):
69 for line in f:
70 if line.startswith("%"):
71 break
72 # check to see if any values in it (could be empty)
73 if not line.isspace():
74 key, value = line.split(None, 1)
75 labels[key] = value
76 return {"buildinfo": labels}