From: Marcin Krol <hawk@tld-linux.org>
Date: Sun, 2 May 2021 18:49:12 +0000 (+0200)
Subject: - use subprocess.Popen instead of os.popen which is now text only
X-Git-Url: https://git.tld-linux.org/?a=commitdiff_plain;h=1b7332b1eeedc313bbef96e41ec44925d86cc96d;p=tld-builder.git

- use subprocess.Popen instead of os.popen which is now text only
---

diff --git a/TLD_Builder/chroot.py b/TLD_Builder/chroot.py
index de24598..3e427b9 100644
--- a/TLD_Builder/chroot.py
+++ b/TLD_Builder/chroot.py
@@ -6,11 +6,6 @@ import random
 import util
 import subprocess
 
-try:
-    from hashlib import md5 as md5
-except ImportError:
-    from md5 import md5
-
 from config import config
 
 def quote(cmd):
@@ -55,36 +50,16 @@ def run(cmd, user = "builder", logfile = None, logstdout = None):
         return r
 
 def cp(file, outfile, user="builder", rm=False):
-    m = md5()
-    m.update(util.to_bytes(str(random.sample(range(100000), 500))))
-    digest = m.hexdigest()
-
-    marker_start = "--- FILE BEGIN DIGEST %s ---" % digest
-    marker_end = "--- FILE END DIGEST %s ---" % digest
-
-    f = open(outfile, 'wb')
-    cmd = "echo \"%s\"; cat %s; echo \"%s\"" % (marker_start, file, marker_end)
+    f_out = open(outfile, 'wb')
+    cmd = "cat %s" % file
     if rm:
         cmd += "; rm %s" % file
-    c = command(cmd, user)
-    p = os.popen(c)
-    # get file contents
-    marker = False
-    for l in p:
-        if not marker and l.strip() == marker_start:
-            marker = True
-            continue
-        me = l.find(marker_end)
-        if me != -1:
-            l = l[:me]
-            f.write(util.to_bytes(l))
-            marker = False
-            break
-        if marker:
-            f.write(util.to_bytes(l))
-    rp = p.close()
-    rf = f.close()
-    if rp == None:
+    p = subprocess.Popen(command(cmd, user), shell=True, stdout=subprocess.PIPE, close_fds=True)
+    f_in = p.stdout
+    shutil.copyfileobj(f_in, f_out)
+    f_out.close()
+    r = f_in.close()
+    if r == None:
         return 0
     else:
-        return rp
+        return r