Skip to contentSkip to conversation

icecc.bbclass: replace os.popen with subprocess.check_output

PATCH Superseded Martin Jansa 3 participants 4 messages Jun 14, 2016 – Oct 5, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 icecc.bbclass: replace os.popen with subprocess.check_output

* otherwise there is a lot of warnings about missing close on file descriptor

---
 meta/classes/icecc.bbclass | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
meta/classes/icecc.bbclass +10 -4
index e1c06c4..cb44007 100644
@@ -47,7 +47,8 @@ def get_cross_kernel_cc(bb,d):
 
     # evaluate the expression by the shell if necessary
     if '`' in kernel_cc or '$(' in kernel_cc:
-        kernel_cc = os.popen("echo %s" % kernel_cc).read()[:-1]
+        import subprocess
+        kernel_cc = subprocess.check_output("echo %s" % kernel_cc, shell=True)
 
     kernel_cc = d.expand(kernel_cc)
     kernel_cc = kernel_cc.replace('ccache', '').strip()
@@ -220,9 +221,14 @@ def icecc_get_and_check_tool(bb, d, tool):
     # PATH or icecc-create-env script will silently create an invalid
     # compiler environment package.
     t = icecc_get_tool(bb, d, tool)
-    if t and os.popen("readlink -f %s" % t).read()[:-1] == get_icecc(d):
-        bb.error("%s is a symlink to %s in PATH and this prevents icecc from working" % (t, get_icecc(d)))
-        return ""
+    if t:
+        import subprocess
+        link_path = subprocess.check_output("readlink -f %s" % t, shell=True)
+        if link_path == get_icecc(d):
+            bb.error("%s is a symlink to %s in PATH and this prevents icecc from working" % (t, get_icecc(d)))
+            return ""
+        else:
+            return t
     else:
         return t
 
Signature · 1 line
2.9.0
#1 lore raw
Quoted 12 lines · Martin Jansa

* otherwise there is a lot of warnings about missing close on file descriptor

---
  meta/classes/icecc.bbclass | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
meta/classes/icecc.bbclass +0 -0
index e1c06c4..cb44007 100644

meta/classes/icecc.bbclass def get_cross_kernel_cc(bb,d): · line 50

Quoted 5 lines
meta/classes/icecc.bbclass +1 -1
@@ -47,7 +47,8 @@ def get_cross_kernel_cc(bb,d):
 
      # evaluate the expression by the shell if necessary
      if '`' in kernel_cc or '$(' in kernel_cc:
-        kernel_cc = os.popen("echo %s" % kernel_cc).read()[:-1]
+        import subprocess

There are two imports like this, would it make sense to place just once on top?

Also, now that we switch to subprocess.check_output, I wonder if we need to convert it to string (the output of this method) before proceeding.

Quoted 22 lines
meta/classes/icecc.bbclass +9 -3
+        kernel_cc = subprocess.check_output("echo %s" % kernel_cc, shell=True)
 
      kernel_cc = d.expand(kernel_cc)
      kernel_cc = kernel_cc.replace('ccache', '').strip()
@@ -220,9 +221,14 @@ def icecc_get_and_check_tool(bb, d, tool):
      # PATH or icecc-create-env script will silently create an invalid
      # compiler environment package.
      t = icecc_get_tool(bb, d, tool)
-    if t and os.popen("readlink -f %s" % t).read()[:-1] == get_icecc(d):
-        bb.error("%s is a symlink to %s in PATH and this prevents icecc from working" % (t, get_icecc(d)))
-        return ""
+    if t:
+        import subprocess
+        link_path = subprocess.check_output("readlink -f %s" % t, shell=True)
+        if link_path == get_icecc(d):
+            bb.error("%s is a symlink to %s in PATH and this prevents icecc from working" % (t, get_icecc(d)))
+            return ""
+        else:
+            return t
      else:
          return t
 
#2 lore raw
Quoted 3 lines · Leonardo Sandoval

Also, now that we switch to subprocess.check_output, I wonder if we need to convert it to string (the output of this method) before proceeding.

Yeah, check_output under py3 returns byte arrays, so you either need to convert to string or pass universal_newlines=True.

Ross

#3 lore raw

PATCH v2 1/1 icecc.bbclass: replace os.popen with subprocess.check_output

In reply to Martin Jansa

* otherwise there is a lot of warnings about missing close on file descriptor

---
 meta/classes/icecc.bbclass | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
meta/classes/icecc.bbclass +10 -4
index e1c06c4..a837894 100644
@@ -47,7 +47,8 @@ def get_cross_kernel_cc(bb,d):
 
     # evaluate the expression by the shell if necessary
     if '`' in kernel_cc or '$(' in kernel_cc:
-        kernel_cc = os.popen("echo %s" % kernel_cc).read()[:-1]
+        import subprocess
+        kernel_cc = subprocess.check_output("echo %s" % kernel_cc, shell=True).decode("utf-8")[:-1]
 
     kernel_cc = d.expand(kernel_cc)
     kernel_cc = kernel_cc.replace('ccache', '').strip()
@@ -220,9 +221,14 @@ def icecc_get_and_check_tool(bb, d, tool):
     # PATH or icecc-create-env script will silently create an invalid
     # compiler environment package.
     t = icecc_get_tool(bb, d, tool)
-    if t and os.popen("readlink -f %s" % t).read()[:-1] == get_icecc(d):
-        bb.error("%s is a symlink to %s in PATH and this prevents icecc from working" % (t, get_icecc(d)))
-        return ""
+    if t:
+        import subprocess
+        link_path = subprocess.check_output("readlink -f %s" % t, shell=True).decode("utf-8")[:-1]
+        if link_path == get_icecc(d):
+            bb.error("%s is a symlink to %s in PATH and this prevents icecc from working" % (t, get_icecc(d)))
+            return ""
+        else:
+            return t
     else:
         return t
 
Signature · 1 line
2.10.1