Skip to content

Commit 2a2e30c

Browse files
authored
fix bug with rendering of labels in templates (#650)
* fix bug with rendering of labels in templates This was done last night (and the PR should have been opened then). This will fix a bug that labels often have newlines, which breaks the templates! Signed-off-by: vsoch <[email protected]>
1 parent 25e1da0 commit 2a2e30c

File tree

9 files changed

+28
-4
lines changed

9 files changed

+28
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and **Merged pull requests**. Critical items to know are:
1414
The versions coincide with releases on pip. Only major versions will be released as tags on Github.
1515

1616
## [0.0.x](https://github.com/singularityhub/singularity-hpc/tree/main) (0.0.x)
17+
- Labels with newlines need additional parsing (0.1.23)
1718
- Do not write directly to output with shpc show (0.1.22)
1819
- Podman template bug (0.1.21)
1920
- Improvement to shpc help command output (0.1.2)

docs/getting_started/user-guide.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ as environment variables, as they will be subbed in by shpc before environment
157157
variable replacement. A summary table of variables is included below, and then further discussed in detail.
158158

159159

160-
.. list-table:: Title
160+
.. list-table:: Settings
161161
:widths: 25 65 10
162162
:header-rows: 1
163163

@@ -191,6 +191,9 @@ variable replacement. A summary table of variables is included below, and then f
191191
* - updated_at
192192
- a timestamp to keep track of when you last saved
193193
- never
194+
* - label_separator
195+
- When parsing labels, replace newlines with this string
196+
- ', '
194197
* - default_version
195198
- Should a default version be used?
196199
- module_sys

shpc/main/container/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ def container_dir(self, name):
102102
return os.path.join(self.settings.module_base, name)
103103
return os.path.join(self.settings.container_base, name)
104104

105+
def clean_labels(self, labels):
106+
"""
107+
Clean labels, meaning removing newlines and replacing with label separator
108+
"""
109+
updated_labels = {}
110+
for key, value in labels.items():
111+
updated_labels[key] = value.replace("\n", self.settings.label_separator)
112+
return updated_labels
113+
105114
def guess_tag(self, module_name, allow_fail=False):
106115
"""
107116
If a user asks for a name without a tag, try to figure it out.

shpc/main/container/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ def get_url(self):
254254
return self.get("docker") or self.get("gh") or self.get("path")
255255

256256
def get(self, key, default=None):
257+
"""
258+
Get a value from the config.
259+
"""
257260
return self.entry._config.get(key, default)
258261

259262
def get_pull_type(self):

shpc/main/container/docker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ def install(self, module_path, template, module, features=None):
207207
"Container %s was not found. Was it pulled?" % module.container_path
208208
)
209209

210-
labels = manifest[0].get("Labels", {})
210+
labels = manifest[0].get("Labels") or {}
211+
labels = self.clean_labels(labels)
211212

212213
# Option to create wrapper scripts for commands
213214
aliases = module.config.get_aliases()

shpc/main/container/singularity.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def install(self, module_path, template, module, features=None):
191191
metadata = self.inspect(module.container_path)
192192

193193
# Add labels, and deffile
194-
labels = metadata.get("attributes", {}).get("labels")
194+
labels = metadata.get("attributes", {}).get("labels") or {}
195195
deffile = (
196196
metadata.get("attributes", {}).get("deffile", "").replace("\n", "\\n")
197197
)
@@ -203,6 +203,9 @@ def install(self, module_path, template, module, features=None):
203203
# Option to create wrapper scripts for commands
204204
aliases = module.config.get_aliases()
205205

206+
# Labels with newlines need to be handled, replace with comma
207+
labels = self.clean_labels(labels)
208+
206209
# Wrapper scripts can be global (for aliases) or container specific
207210
wrapper_scripts = []
208211
if self.settings.wrapper_scripts["enabled"] is True:

shpc/main/schemas.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
]
158158
},
159159
"enable_tty": {"type": "boolean"},
160+
"label_separator": {"type": "string"},
160161
"views_base": {"type": ["string", "null"]},
161162
"default_view": {"type": ["string", "null"]},
162163
"wrapper_scripts": wrapper_scripts,

shpc/settings.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ default_version: module_sys
4242
# It's recommended to do this for faster loading
4343
container_base: $root_dir/containers
4444

45+
# When parsing labels, replace newlines with this string
46+
label_separator: ', '
47+
4548
# Default root directory to create views
4649
views_base: $root_dir/views
4750

shpc/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__copyright__ = "Copyright 2021-2023, Vanessa Sochat"
33
__license__ = "MPL 2.0"
44

5-
__version__ = "0.1.22"
5+
__version__ = "0.1.23"
66
AUTHOR = "Vanessa Sochat"
77
88
NAME = "singularity-hpc"

0 commit comments

Comments
 (0)