Will be used in an additional place in a follow-on commit.

Signed-off-by: Matt Turner <matts...@gentoo.org>
---
 catalyst/base/stagebase.py | 10 +++++-----
 catalyst/support.py        |  9 +++++++++
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py
index 6f5aa23a..1ccc9f04 100644
--- a/catalyst/base/stagebase.py
+++ b/catalyst/base/stagebase.py
@@ -16,7 +16,8 @@ from DeComp.compress import CompressMap
 from catalyst import log
 from catalyst.defaults import (confdefaults, MOUNT_DEFAULTS, PORT_LOGDIR_CLEAN)
 from catalyst.support import (CatalystError, file_locate, normpath,
-                              cmd, read_makeconf, ismount, file_check)
+                              cmd, read_makeconf, ismount, file_check,
+                              sanitize_name)
 from catalyst.base.targetbase import TargetBase
 from catalyst.base.clearbase import ClearBase
 from catalyst.base.genbase import GenBase
@@ -1314,10 +1315,9 @@ class StageBase(TargetBase, ClearBase, GenBase):
                 for opt in self.settings[x]:
                     self.env['clst_' + opt.upper()] = "true"
                 continue
-            # Sanitize var names by doing "s|/-.|_|g"
-            varname = "clst_" + x.replace("/", "_")
-            varname = varname.replace("-", "_")
-            varname = varname.replace(".", "_")
+
+            varname = 'clst_' + sanitize_name(x)
+
             if isinstance(self.settings[x], str):
                 # Prefix to prevent namespace clashes
                 if "path" in x:
diff --git a/catalyst/support.py b/catalyst/support.py
index a6a6854a..f49315a5 100644
--- a/catalyst/support.py
+++ b/catalyst/support.py
@@ -256,3 +256,12 @@ def normpath(mypath):
     if TrailingSlash:
         newpath = newpath+'/'
     return newpath
+
+
+def sanitize_name(name: str) -> str:
+    """
+    Normalize name by replacing [.-/] with _, so it may be used as a
+    variable name in bash
+    """
+    table = str.maketrans(".-/", "___")
+    return name.translate(table)
-- 
2.26.2


Reply via email to