Skip to content

Commit

Permalink
fix bug with rendering of labels in templates (#650)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
vsoch authored Jun 7, 2023
1 parent 25e1da0 commit 2a2e30c
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and **Merged pull requests**. Critical items to know are:
The versions coincide with releases on pip. Only major versions will be released as tags on Github.

## [0.0.x](https://github.com/singularityhub/singularity-hpc/tree/main) (0.0.x)
- Labels with newlines need additional parsing (0.1.23)
- Do not write directly to output with shpc show (0.1.22)
- Podman template bug (0.1.21)
- Improvement to shpc help command output (0.1.2)
Expand Down
5 changes: 4 additions & 1 deletion docs/getting_started/user-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ as environment variables, as they will be subbed in by shpc before environment
variable replacement. A summary table of variables is included below, and then further discussed in detail.


.. list-table:: Title
.. list-table:: Settings
:widths: 25 65 10
:header-rows: 1

Expand Down Expand Up @@ -191,6 +191,9 @@ variable replacement. A summary table of variables is included below, and then f
* - updated_at
- a timestamp to keep track of when you last saved
- never
* - label_separator
- When parsing labels, replace newlines with this string
- ', '
* - default_version
- Should a default version be used?
- module_sys
Expand Down
9 changes: 9 additions & 0 deletions shpc/main/container/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ def container_dir(self, name):
return os.path.join(self.settings.module_base, name)
return os.path.join(self.settings.container_base, name)

def clean_labels(self, labels):
"""
Clean labels, meaning removing newlines and replacing with label separator
"""
updated_labels = {}
for key, value in labels.items():
updated_labels[key] = value.replace("\n", self.settings.label_separator)
return updated_labels

def guess_tag(self, module_name, allow_fail=False):
"""
If a user asks for a name without a tag, try to figure it out.
Expand Down
3 changes: 3 additions & 0 deletions shpc/main/container/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ def get_url(self):
return self.get("docker") or self.get("gh") or self.get("path")

def get(self, key, default=None):
"""
Get a value from the config.
"""
return self.entry._config.get(key, default)

def get_pull_type(self):
Expand Down
3 changes: 2 additions & 1 deletion shpc/main/container/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ def install(self, module_path, template, module, features=None):
"Container %s was not found. Was it pulled?" % module.container_path
)

labels = manifest[0].get("Labels", {})
labels = manifest[0].get("Labels") or {}
labels = self.clean_labels(labels)

# Option to create wrapper scripts for commands
aliases = module.config.get_aliases()
Expand Down
5 changes: 4 additions & 1 deletion shpc/main/container/singularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def install(self, module_path, template, module, features=None):
metadata = self.inspect(module.container_path)

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

# Labels with newlines need to be handled, replace with comma
labels = self.clean_labels(labels)

# Wrapper scripts can be global (for aliases) or container specific
wrapper_scripts = []
if self.settings.wrapper_scripts["enabled"] is True:
Expand Down
1 change: 1 addition & 0 deletions shpc/main/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
]
},
"enable_tty": {"type": "boolean"},
"label_separator": {"type": "string"},
"views_base": {"type": ["string", "null"]},
"default_view": {"type": ["string", "null"]},
"wrapper_scripts": wrapper_scripts,
Expand Down
3 changes: 3 additions & 0 deletions shpc/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ default_version: module_sys
# It's recommended to do this for faster loading
container_base: $root_dir/containers

# When parsing labels, replace newlines with this string
label_separator: ', '

# Default root directory to create views
views_base: $root_dir/views

Expand Down
2 changes: 1 addition & 1 deletion shpc/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__copyright__ = "Copyright 2021-2023, Vanessa Sochat"
__license__ = "MPL 2.0"

__version__ = "0.1.22"
__version__ = "0.1.23"
AUTHOR = "Vanessa Sochat"
EMAIL = "[email protected]"
NAME = "singularity-hpc"
Expand Down

0 comments on commit 2a2e30c

Please sign in to comment.