public inbox for isar-users@googlegroups.com
 help / color / mirror / Atom feed
From: claudius.heine.ext@siemens.com
To: isar-users@googlegroups.com
Cc: Harald Seiler <hws@denx.de>, Claudius Heine <ch@denx.de>
Subject: [PATCH v4 1/6] Remove all uses of subprocess.call(shell=True)
Date: Mon,  4 Mar 2019 14:07:56 +0100	[thread overview]
Message-ID: <20190304130801.20628-2-claudius.heine.ext@siemens.com> (raw)
In-Reply-To: <20190304130801.20628-1-claudius.heine.ext@siemens.com>

From: Harald Seiler <hws@denx.de>

Using shell-expansion in subprocess calls has a lot of nasty
side-effects that are hard to debug should one ever surface.
This commit changes all uses of subprocess to

A) not use shell=True by giving a list of args instead of
   a command-line.
B) ensure return-codes are handled appropriately.

Furthermore, in some places the subprocess usage was changed to
be more idiomatic.

Co-authored-by: Claudius Heine <ch@denx.de>
Signed-off-by: Harald Seiler <hws@denx.de>
---
 meta/classes/base.bbclass                  | 10 ++++++----
 meta/classes/image.bbclass                 |  5 +++--
 meta/classes/isar-bootstrap-helper.bbclass |  8 +++-----
 meta/classes/isar-events.bbclass           | 13 +++++++------
 meta/classes/patch.bbclass                 | 13 +++++++++----
 5 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/meta/classes/base.bbclass b/meta/classes/base.bbclass
index 4279a68..1f415af 100644
--- a/meta/classes/base.bbclass
+++ b/meta/classes/base.bbclass
@@ -171,15 +171,17 @@ addtask clean
 do_clean[nostamp] = "1"
 python do_clean() {
     import subprocess
+    import glob
 
     for f in (d.getVar('CLEANFUNCS', True) or '').split():
         bb.build.exec_func(f, d)
 
-    dir = d.expand("${WORKDIR}")
-    subprocess.call('sudo rm -rf ' + dir, shell=True)
+    workdir = d.expand("${WORKDIR}")
+    subprocess.check_call(["sudo", "rm", "-rf", workdir])
 
-    dir = "%s.*" % bb.data.expand(d.getVar('STAMP', False), d)
-    subprocess.call('sudo rm -rf ' + dir, shell=True)
+    stamppath = bb.data.expand(d.getVar('STAMP', False), d)
+    stampdirs = glob.glob(stamppath + ".*")
+    subprocess.check_call(["sudo", "rm", "-rf"] + stampdirs)
 }
 
 # Derived from OpenEmbedded Core: meta/classes/base.bbclass
diff --git a/meta/classes/image.bbclass b/meta/classes/image.bbclass
index 2514c88..34d6515 100644
--- a/meta/classes/image.bbclass
+++ b/meta/classes/image.bbclass
@@ -56,8 +56,9 @@ def get_rootfs_size(d):
     import subprocess
     rootfs_extra = int(d.getVar("ROOTFS_EXTRA", True))
 
-    output = subprocess.check_output(['sudo', 'du', '-s', '--block-size=1k',
-                                      d.getVar("IMAGE_ROOTFS", True)])
+    output = subprocess.check_output(
+        ["sudo", "du", "-s", "--block-size=1k", d.getVar("IMAGE_ROOTFS", True)]
+    )
     base_size = int(output.split()[0])
 
     return base_size + rootfs_extra * 1024
diff --git a/meta/classes/isar-bootstrap-helper.bbclass b/meta/classes/isar-bootstrap-helper.bbclass
index d780b85..18081a0 100644
--- a/meta/classes/isar-bootstrap-helper.bbclass
+++ b/meta/classes/isar-bootstrap-helper.bbclass
@@ -9,11 +9,9 @@ IMAGE_TRANSIENT_PACKAGES ??= ""
 
 def get_deb_host_arch():
     import subprocess
-    host_arch = subprocess.Popen("dpkg --print-architecture",
-                                 shell=True,
-                                 env=os.environ,
-                                 stdout=subprocess.PIPE
-                                ).stdout.read().decode('utf-8').strip()
+    host_arch = subprocess.check_output(
+        ["dpkg", "--print-architecture"]
+    ).strip()
     return host_arch
 
 #Debian Distribution for SDK host
diff --git a/meta/classes/isar-events.bbclass b/meta/classes/isar-events.bbclass
index c4a6149..a178d05 100644
--- a/meta/classes/isar-events.bbclass
+++ b/meta/classes/isar-events.bbclass
@@ -16,13 +16,14 @@ python isar_handler() {
 
     basepath = tmpdir + '/work/'
 
-    with open(os.devnull, 'w') as devnull:
-        with open('/proc/mounts', 'rU') as f:
-            lines = f.readlines()
-        for line in lines:
+    with open('/proc/mounts') as f:
+        for line in f.readlines():
             if basepath in line:
-                subprocess.call('sudo umount -l ' + line.split()[1],
-                                stdout=devnull, stderr=devnull, shell=True)
+                subprocess.call(
+                    ["sudo", "umount", "-l", line.split()[1]],
+                    stdout=subprocess.DEVNULL,
+                    stderr=subprocess.DEVNULL,
+                )
 }
 
 isar_handler[eventmask] = "bb.runqueue.runQueueExitWait bb.event.BuildCompleted"
diff --git a/meta/classes/patch.bbclass b/meta/classes/patch.bbclass
index 0bc449f..c19542d 100644
--- a/meta/classes/patch.bbclass
+++ b/meta/classes/patch.bbclass
@@ -21,10 +21,15 @@ python do_patch() {
 
             striplevel = fetcher.ud[src_uri].parm.get("striplevel") or "1"
 
-            cmd = "patch --no-backup-if-mismatch -p " + striplevel + \
-                  " --directory " + src_dir + " --input " + workdir + basename
-            bb.note(cmd)
-            if subprocess.call(cmd, shell=True) != 0:
+            cmd = [
+                "patch",
+                "--no-backup-if-mismatch",
+                "-p", striplevel,
+                "--directory", src_dir,
+                "--input", workdir + basename,
+            ]
+            bb.note(" ".join(cmd))
+            if subprocess.call(cmd) != 0:
                 bb.fatal("patching failed")
         except bb.fetch2.BBFetchException as e:
             raise bb.build.FuncFailed(e)
-- 
2.20.1


  reply	other threads:[~2019-03-04 13:08 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-04 13:07 [PATCH v4 0/6] Python refactoring claudius.heine.ext
2019-03-04 13:07 ` claudius.heine.ext [this message]
2019-03-04 13:07 ` [PATCH v4 2/6] isar-bootstrap-helper: get_deb_host_arch: fix decoding claudius.heine.ext
2019-03-04 13:07 ` [PATCH v4 3/6] Use modern python formatting claudius.heine.ext
2019-03-04 13:07 ` [PATCH v4 4/6] image: Remove recursion in get_image_name claudius.heine.ext
2019-03-04 13:08 ` [PATCH v4 5/6] wic: Refactor fakeroot script claudius.heine.ext
2019-03-04 13:08 ` [PATCH v4 6/6] Fix python style claudius.heine.ext
2019-03-04 16:21 ` [PATCH v4 0/6] Python refactoring Claudius Heine
2019-03-11 10:10   ` Maxim Yu. Osipov
2019-03-11 12:22     ` Claudius Heine
2019-03-12 14:56 ` Claudius Heine
2019-03-12 15:26   ` Jan Kiszka
2019-03-12 15:34     ` Claudius Heine
2019-03-12 16:03       ` Jan Kiszka
2019-03-12 16:17         ` Claudius Heine
2019-03-12 22:36 ` Maxim Yu. Osipov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190304130801.20628-2-claudius.heine.ext@siemens.com \
    --to=claudius.heine.ext@siemens.com \
    --cc=ch@denx.de \
    --cc=hws@denx.de \
    --cc=isar-users@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox