[elbe-devel] [PATCH 10/11] elbepack: migrate os.system to subprocess package

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


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

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 docs/conf.py                          |  4 +--
 elbepack/commands/toolchainextract.py | 48 ++++++++++++++++-----------------
 elbepack/debpkg.py                    | 50 +++++++++++++++++------------------
 3 files changed, 50 insertions(+), 52 deletions(-)

diff --git a/docs/conf.py b/docs/conf.py
index 5c31a547258d..ff614105e338 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -18,12 +18,12 @@
 # add these directories to sys.path here. If the directory is relative to the
 # documentation root, use os.path.abspath to make it absolute, like shown here.
 #
-import os
+import subprocess
 import sys
 sys.path.insert(0, os.path.abspath('..'))
 
 
-os.system("make")
+subprocess.run(['make'], check=True)
 
 # -- General configuration ------------------------------------------------
 
diff --git a/elbepack/commands/toolchainextract.py b/elbepack/commands/toolchainextract.py
index 3b0461aefa18..be113a65ce23 100644
--- a/elbepack/commands/toolchainextract.py
+++ b/elbepack/commands/toolchainextract.py
@@ -4,8 +4,8 @@
 
 import os
 import sys
+import tempfile
 from optparse import OptionParser
-from tempfile import mkdtemp
 
 from elbepack.debpkg import build_binary_deb
 from elbepack.log import elbe_logging
@@ -46,36 +46,34 @@ def run_command(argv):
         opt.path,
         defaults['arch'])
 
-    tmpdir = mkdtemp()
+    with tempfile.TemporaryDirectory() as tmpdir:
+        for lib in toolchain.pkg_libs:
+            files = toolchain.get_files_for_pkg(lib)
 
-    for lib in toolchain.pkg_libs:
-        files = toolchain.get_files_for_pkg(lib)
+            pkglibpath = os.path.join('usr/lib', defaults['triplet'])
+            fmap = [(f, pkglibpath) for f in files]
 
-        pkglibpath = os.path.join('usr/lib', defaults['triplet'])
-        fmap = [(f, pkglibpath) for f in files]
+            build_binary_deb(
+                lib,
+                defaults['arch'],
+                defaults['toolchainver'],
+                lib +
+                ' extracted from toolchain',
+                fmap,
+                toolchain.pkg_deps[lib],
+                tmpdir)
 
-        build_binary_deb(
-            lib,
-            defaults['arch'],
-            defaults['toolchainver'],
-            lib +
-            ' extracted from toolchain',
-            fmap,
-            toolchain.pkg_deps[lib],
-            tmpdir)
+        pkgs = os.listdir(tmpdir)
 
-    pkgs = os.listdir(tmpdir)
+        with elbe_logging({'streams': sys.stdout}):
 
-    with elbe_logging({'streams': sys.stdout}):
+            repo = ToolchainRepo(defaults['arch'],
+                                 opt.codename,
+                                 opt.output)
 
-        repo = ToolchainRepo(defaults['arch'],
-                             opt.codename,
-                             opt.output)
+            for p in pkgs:
+                repo.includedeb(os.path.join(tmpdir, p))
 
-        for p in pkgs:
-            repo.includedeb(os.path.join(tmpdir, p))
-
-        repo.finalize()
-        os.system(f'rm -r "{tmpdir}"')
+            repo.finalize()
 
     return 0
diff --git a/elbepack/debpkg.py b/elbepack/debpkg.py
index 89019caff326..6fc7fc85acd7 100644
--- a/elbepack/debpkg.py
+++ b/elbepack/debpkg.py
@@ -3,8 +3,10 @@
 # SPDX-FileCopyrightText: 2014, 2017 Linutronix GmbH
 
 import os
+import shutil
 import string
-from tempfile import mkdtemp
+import subprocess
+import tempfile
 
 control_template_string = """Package: ${name}
 Version: ${version}
@@ -46,32 +48,30 @@ def build_binary_deb(
         deps,
         target_dir):
 
-    tmpdir = mkdtemp()
-    pkgfname = f'{name}_{version}_{arch}'
-    pkgdir = os.path.join(tmpdir, pkgfname)
+    with tempfile.TemporaryDirectory() as tmpdir:
+        pkgfname = f'{name}_{version}_{arch}'
+        pkgdir = os.path.join(tmpdir, pkgfname)
 
-    os.system(f'mkdir -p "{os.path.join(pkgdir, "DEBIAN")}"')
-    write_file(
-        os.path.join(
-            pkgdir,
-            'DEBIAN',
-            'control'),
-        0o644,
-        gen_controlfile(
-            name,
-            version,
-            arch,
-            description,
-            deps))
+        os.makedirs(os.path.join(pkgdir, 'DEBIAN'), exist_ok=True)
+        write_file(
+            os.path.join(
+                pkgdir,
+                'DEBIAN',
+                'control'),
+            0o644,
+            gen_controlfile(
+                name,
+                version,
+                arch,
+                description,
+                deps))
 
-    for (fname, instpath) in files:
-        full_instpath = os.path.join(pkgdir, instpath)
-        os.system(f'mkdir -p "{full_instpath}"')
-        os.system(f'cp -a "{fname}" "{full_instpath}"')
+        for (fname, instpath) in files:
+            full_instpath = os.path.join(pkgdir, instpath)
+            os.makedirs(full_instpath, exist_ok=True)
+            shutil.copyfile(fname, full_instpath)
 
-    os.system(f'dpkg-deb --build "{pkgdir}"')
-    os.system(
-        f'cp -v "{os.path.join(tmpdir, pkgfname + ".deb")}" "{target_dir}"')
-    os.system(f'rm -r "{tmpdir}"')
+        subprocess.run(['dpkg-deb', '--build', pkgdir], check=True)
+        shutil.copyfile(os.path.join(tmpdir, pkgfname + '.deb'), target_dir)
 
     return pkgfname + '.deb'

-- 
2.44.0



More information about the elbe-devel mailing list