1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
9 from hashlib import md5 as md5
13 from config import config
16 return re.sub("([\"\\\\$`])", r"\\\1", cmd)
18 def command(cmd, user = None, nostdin=""):
20 user = config.builder_user
22 nostdin = "exec < /dev/null; "
23 return "%s sudo chroot %s su - %s -c \"export LC_ALL=C; %s %s\"" \
24 % (config.sudo_chroot_wrapper, config.chroot, user, nostdin, quote(cmd))
27 return "%s sudo chroot %s /bin/sh -c \"export LC_ALL=C; exec < /dev/null; %s\"" \
28 % (config.sudo_chroot_wrapper, config.chroot, quote(cmd))
30 def popen(cmd, user = "builder", mode = "r"):
32 p = subprocess.Popen(command(cmd, user), shell=True, stdout=subprocess.PIPE, close_fds=True)
35 p = subprocess.Popen(command(cmd, user), shell=True, stdin=subprocess.PIPE, close_fds=True)
39 def run(cmd, user = "builder", logfile = None, logstdout = None):
40 c = command(cmd, user, nostdin=True)
43 c = "%s 2>&1 | /usr/bin/tee -a %s" % (c, logfile)
45 c = "%s >> %s 2>&1" % (c, logfile)
56 def cp(file, outfile, user="builder", rm=False):
58 m.update(util.to_bytes(str(random.sample(range(100000), 500))))
59 digest = m.hexdigest()
61 marker_start = "--- FILE BEGIN DIGEST %s ---" % digest
62 marker_end = "--- FILE END DIGEST %s ---" % digest
64 f = open(outfile, 'wb')
65 cmd = "echo \"%s\"; cat %s; echo \"%s\"" % (marker_start, file, marker_end)
67 cmd += "; rm %s" % file
68 c = command(cmd, user)
73 if not marker and l.strip() == marker_start:
76 me = l.find(marker_end)
79 f.write(util.to_bytes(l))
83 f.write(util.to_bytes(l))