[elbe-devel] [PATCH 4/5] elbepack: shellhelper: drop automatic input encoding
Thomas Weißschuh
thomas.weissschuh at linutronix.de
Mon May 6 10:07:50 CEST 2024
Using a non-specific encoding through str.encode() without arguments is
problematic.
Instead punt the encoding choice to the caller.
This also aligns the API closer to the stdlib subprocess package.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
elbepack/egpg.py | 3 ++-
elbepack/finetuning.py | 11 ++++++-----
elbepack/rfs.py | 2 +-
elbepack/shellhelper.py | 9 ---------
4 files changed, 9 insertions(+), 16 deletions(-)
diff --git a/elbepack/egpg.py b/elbepack/egpg.py
index d4f951d90217..58940913cb4c 100644
--- a/elbepack/egpg.py
+++ b/elbepack/egpg.py
@@ -292,7 +292,8 @@ def unarmor_openpgp_keyring(armored):
Returns a binary string (empty for invalid keys).
"""
try:
- conv_cmd = get_command_out('/usr/bin/gpg --no-options --dearmor', input=armored)
+ conv_cmd = get_command_out('/usr/bin/gpg --no-options --dearmor',
+ input=armored.encode('ascii'))
except subprocess.CalledProcessError as e:
logging.error(e)
return b''
diff --git a/elbepack/finetuning.py b/elbepack/finetuning.py
index a6d30dcd0c5d..a10c89e55ea3 100644
--- a/elbepack/finetuning.py
+++ b/elbepack/finetuning.py
@@ -227,7 +227,7 @@ class AddUserAction(FinetuningAction):
if 'passwd_hashed' in att:
chroot(target.path, ['chpasswd', '--encrypted'],
- input=f"{self.node.et.text}:{att['passwd_hashed']}")
+ input=f"{self.node.et.text}:{att['passwd_hashed']}".encode('ascii'))
@FinetuningAction.register('addgroup')
@@ -324,18 +324,19 @@ class CmdAction(ImageFinetuningAction):
dev = f"{loop_dev}p{self.node.et.attrib['part']}"
if self.node.bool_attr('nomount'):
- do('/bin/sh', input=script,
+ do('/bin/sh', input=script.encode('ascii'),
env_add={'ELBE_DEV': dev},
log_cmd=script)
else:
with ImgMountFilesystem(mnt, dev) as fs:
- do('/bin/sh', input=script,
+ do('/bin/sh', input=script.encode('ascii'),
env_add={'ELBE_MNT': fs.path},
log_cmd=script)
def execute(self, _buildenv, target):
with target:
- chroot(target.path, '/bin/sh', input=self.node.et.text, log_cmd=self.node.et.text)
+ chroot(target.path, '/bin/sh', input=self.node.et.text.encode('ascii'),
+ log_cmd=self.node.et.text)
@FinetuningAction.register('buildenv_command')
@@ -343,7 +344,7 @@ class BuildenvCmdAction(FinetuningAction):
def execute(self, buildenv, _target):
with buildenv:
- chroot(buildenv.path, '/bin/sh', input=self.node.et.text)
+ chroot(buildenv.path, '/bin/sh', input=self.node.et.text.encode('ascii'))
@FinetuningAction.register('purge')
diff --git a/elbepack/rfs.py b/elbepack/rfs.py
index ee30bf917816..5fbf55cf37e5 100644
--- a/elbepack/rfs.py
+++ b/elbepack/rfs.py
@@ -310,7 +310,7 @@ class BuildEnv:
def seed_etc(self):
passwd = self.xml.text('target/passwd_hashed')
- chroot(self.rfs.path, ['chpasswd', '--encrypted'], input='root:' + passwd)
+ chroot(self.rfs.path, ['chpasswd', '--encrypted'], input=b'root:' + passwd.encode('ascii'))
hostname = self.xml.text('target/hostname')
fqdn = hostname
diff --git a/elbepack/shellhelper.py b/elbepack/shellhelper.py
index d0e0d7a11a04..e82a3b3dbaff 100644
--- a/elbepack/shellhelper.py
+++ b/elbepack/shellhelper.py
@@ -58,9 +58,6 @@ def do(cmd, /, *, check=True, input=None, env_add=None, log_cmd=None):
if env_add:
new_env.update(env_add)
- if isinstance(input, str):
- input = input.encode()
-
logging.info(log_cmd or _log_cmd(cmd), extra={'context': '[CMD] '})
with async_logging_ctx() as w:
@@ -121,9 +118,6 @@ def get_command_out(cmd, /, *, input=None, check=True, env_add=None):
>>> get_command_out("cat -", input=b"ELBE", env_add={"TRUE":"true"})
b'ELBE'
-
- >>> get_command_out("cat -", input="ELBE", env_add={"TRUE":"true"})
- b'ELBE'
"""
new_env = os.environ.copy()
@@ -131,9 +125,6 @@ def get_command_out(cmd, /, *, input=None, check=True, env_add=None):
if env_add:
new_env.update(env_add)
- if isinstance(input, str):
- input = input.encode()
-
logging.info(_log_cmd(cmd), extra={'context': '[CMD] '})
with async_logging_ctx() as w:
--
2.45.0
More information about the elbe-devel
mailing list