Gentoo Archives: gentoo-catalyst

From: Matt Turner <mattst88@g.o>
To: gentoo-catalyst@l.g.o
Cc: Matt Turner <mattst88@g.o>
Subject: [gentoo-catalyst] [PATCH 04/37] catalyst: Add and use sanitize_name() function
Date: Wed, 21 Oct 2020 00:24:06
Message-Id: 20201021002344.378131-4-mattst88@gentoo.org
In Reply to: [gentoo-catalyst] [PATCH 01/37] catalyst: Use early return to unindent code by Matt Turner
1 Will be used in an additional place in a follow-on commit.
2
3 Signed-off-by: Matt Turner <mattst88@g.o>
4 ---
5 catalyst/base/stagebase.py | 10 +++++-----
6 catalyst/support.py | 9 +++++++++
7 2 files changed, 14 insertions(+), 5 deletions(-)
8
9 diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py
10 index 6f5aa23a..1ccc9f04 100644
11 --- a/catalyst/base/stagebase.py
12 +++ b/catalyst/base/stagebase.py
13 @@ -16,7 +16,8 @@ from DeComp.compress import CompressMap
14 from catalyst import log
15 from catalyst.defaults import (confdefaults, MOUNT_DEFAULTS, PORT_LOGDIR_CLEAN)
16 from catalyst.support import (CatalystError, file_locate, normpath,
17 - cmd, read_makeconf, ismount, file_check)
18 + cmd, read_makeconf, ismount, file_check,
19 + sanitize_name)
20 from catalyst.base.targetbase import TargetBase
21 from catalyst.base.clearbase import ClearBase
22 from catalyst.base.genbase import GenBase
23 @@ -1314,10 +1315,9 @@ class StageBase(TargetBase, ClearBase, GenBase):
24 for opt in self.settings[x]:
25 self.env['clst_' + opt.upper()] = "true"
26 continue
27 - # Sanitize var names by doing "s|/-.|_|g"
28 - varname = "clst_" + x.replace("/", "_")
29 - varname = varname.replace("-", "_")
30 - varname = varname.replace(".", "_")
31 +
32 + varname = 'clst_' + sanitize_name(x)
33 +
34 if isinstance(self.settings[x], str):
35 # Prefix to prevent namespace clashes
36 if "path" in x:
37 diff --git a/catalyst/support.py b/catalyst/support.py
38 index a6a6854a..f49315a5 100644
39 --- a/catalyst/support.py
40 +++ b/catalyst/support.py
41 @@ -256,3 +256,12 @@ def normpath(mypath):
42 if TrailingSlash:
43 newpath = newpath+'/'
44 return newpath
45 +
46 +
47 +def sanitize_name(name: str) -> str:
48 + """
49 + Normalize name by replacing [.-/] with _, so it may be used as a
50 + variable name in bash
51 + """
52 + table = str.maketrans(".-/", "___")
53 + return name.translate(table)
54 --
55 2.26.2