[elbe-devel] [PATCH 05/11] elbepack: migrate system_out to subprocess package

Thomas Weißschuh thomas.weissschuh at linutronix.de
Wed Mar 13 16:54:54 CET 2024


The subprocess APIs are more powerful, better documented and
standardized.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/commands/init.py         |  7 +++++--
 elbepack/commands/parselicence.py |  7 ++++---
 elbepack/shellhelper.py           | 26 --------------------------
 3 files changed, 9 insertions(+), 31 deletions(-)

diff --git a/elbepack/commands/init.py b/elbepack/commands/init.py
index 5a0c2ab1a6ed..d331f609449b 100644
--- a/elbepack/commands/init.py
+++ b/elbepack/commands/init.py
@@ -5,6 +5,7 @@
 import logging
 import os
 import shutil
+import subprocess
 import sys
 from optparse import OptionParser
 
@@ -13,7 +14,7 @@ from elbepack.debinstaller import NoKinitrdException, copy_kinitrd
 from elbepack.directories import elbe_dir, init_template_dir
 from elbepack.filesystem import Filesystem
 from elbepack.log import elbe_logging
-from elbepack.shellhelper import do, system, system_out
+from elbepack.shellhelper import do, system
 from elbepack.templates import get_initvm_preseed, write_template
 from elbepack.treeutils import etree
 from elbepack.validate import validate_xml
@@ -214,7 +215,9 @@ def run_command(argv):
             keys.append(key.et.text)
 
         if opt.cdrom:
-            keys.append(system_out(f'7z x -so "{opt.cdrom}" repo.pub'))
+            keys.append(subprocess.run([
+                '7z', 'x', '-so', opt.cdrom, 'repo.pub',
+            ], check=True, capture_output=True, encoding='utf-8').stdout)
 
         import_keyring = os.path.join(out_path, 'elbe-keyring')
 
diff --git a/elbepack/commands/parselicence.py b/elbepack/commands/parselicence.py
index ee22375f4b0c..deb85f46c0ec 100644
--- a/elbepack/commands/parselicence.py
+++ b/elbepack/commands/parselicence.py
@@ -4,12 +4,12 @@
 
 import io
 import os
+import subprocess
 import sys
 from datetime import datetime
 from optparse import OptionParser
 from tempfile import NamedTemporaryFile
 
-from elbepack.shellhelper import system_out
 from elbepack.treeutils import etree
 from elbepack.version import elbe_version
 
@@ -93,8 +93,9 @@ class license_dep5_to_spdx (dict):
 def scan_nomos(license_text):
     with NamedTemporaryFile() as f:
         f.write(license_text.encode('utf-8'))
-        nomos_out = system_out(
-            f'/usr/share/fossology/nomos/agent/nomos "{f.name}"')
+        nomos_out = subprocess.run([
+            '/usr/share/fossology/nomos/agent/nomos', f.name,
+        ], check=True, capture_output=True, encoding='utf-8').stdout
 
     expected_start = f'File {os.path.basename(f.name)} contains license(s) '
     if not nomos_out.startswith(expected_start):
diff --git a/elbepack/shellhelper.py b/elbepack/shellhelper.py
index 02d8fdae220d..483e6eacaebc 100644
--- a/elbepack/shellhelper.py
+++ b/elbepack/shellhelper.py
@@ -86,32 +86,6 @@ def command_out(cmd, stdin=None, output=PIPE, env_add=None):
     return p.returncode, out
 
 
-def system_out(cmd, stdin=None, allow_fail=False, env_add=None):
-    """system_out() - Wrapper around command_out().
-
-    On failure, raises an exception if allow_fail=False, on success,
-    returns the output of cmd.
-
-    --
-
-    >>> system_out("false") # doctest: +ELLIPSIS
-    Traceback (most recent call last):
-    ...
-    subprocess.CalledProcessError: ...
-
-    >>> system_out("false", allow_fail=True)
-    ''
-
-    """
-    code, out = command_out(cmd, stdin=stdin, env_add=env_add)
-
-    if code != 0:
-        if not allow_fail:
-            raise subprocess.CalledProcessError(code, cmd)
-
-    return out
-
-
 def command_out_stderr(cmd, stdin=None, env_add=None):
     """command_out_stderr() - Execute cmd in a shell.
 

-- 
2.44.0



More information about the elbe-devel mailing list